GUI Question

Discussion in 'Spigot Plugin Development' started by NewKaD, Jun 17, 2015.

  1. Hey guys! I'm actually making a GUI plugin but I don't know how to make players run command by clicking the item, please help :D

    Examples: I want the item on GUI making them running "/spawn" (I know how to teleport them to spawn but I really want that thanks !
    Code (Java):

     @EventHandler
     public void onInventoryClick(InventoryClickEvent e) {
     if (!e.getInventory().getName().equalsIgnoreCase(GUI.getName())) return;
     if (e.getCurrentItem().getItemMeta() == null) return;
     if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Spawn")){
     e.setCancelled(true);
     e.getWhoClicked().teleport(e.getWhoClicked().getWorld().getSpawnLocation());
     e.getWhoClicked().closeInventory();
     }
     }
     
     
  2. Is there not sth. like Player#useCommand()
     
  3. PHP:
    Bukkit.getServer().dispatchCommand(player, "spawn");
     
  4. Code (Java):

    @EventHandler
      public void onInventoryClick(InventoryClickEvent e) {
      if (!e.getInventory().getName().equalsIgnoreCase(GUI.getName())) return;
      if (e.getCurrentItem().getItemMeta() == null) return;
      if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Spawn")){
      e.setCancelled(true);
      Bukkit.getServer().dispatchCommand(player, "spawn");
      e.getWhoClicked().closeInventory();
      }
      }
    Like that?
     
  5. Define the player object
     
  6. Don't understand what you want to say
     
  7. PHP:
     Player player = (Player) e.getWhoClicked();
     
  8. Code (Java):

    @EventHandler
    Player player = (Player) e.getWhoClicked();
      public void onInventoryClick(InventoryClickEvent e) {
      if (!e.getInventory().getName().equalsIgnoreCase(GUI.getName())) return;
      if (e.getCurrentItem().getItemMeta() == null) return;
      if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Spawn")){
      e.setCancelled(true);
      Bukkit.getServer().dispatchCommand(player, "spawn");
      e.getWhoClicked().closeInventory();
      }
      }
    That's good right ?
     
  9. You guys know that Player#performCommand exists, right?
     
  10. Im pretty sure .dispatchCommand would return the console as sender, and not the player. Check out the chat() method, so you can do the following which is a little more friendly with other plugins.

    playerwhoclicked.chat("/spawn");
     
  11. Nope DarkSeraphim and I'll try CAmadeusA ;)
     
  12. Actually it does exist, check the javadoc https://hub.spigotmc.org/javadocs/spigot/
    Also any of the following work
    PHP:
    player.perfromCommand("spawn");
    player.chat("/spawn");
    Bukkit.getServer().dispatchCommand(player, "spawn");
     
    • Useful Useful x 2
  13. It doesn't return any CommandSender, it returns void. And the first parameter, of type CommandSender, can be anything (console, player, command block, remote command).
     
    • Informative Informative x 1
    • Useful Useful x 1