Need help with using items to do commands in a menu!

Discussion in 'Plugin Development' started by TechAttax, Jul 15, 2014.

Thread Status:
Not open for further replies.
  1. Offline


    Hello, I was making a GUI for my plugin but got stuck adding a command to my GUI that a player can click!

    Here is my code:
    1. package me.tomatogeek;
    3. import java.util.Arrays;
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.DyeColor;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.inventory.InventoryClickEvent;
    11. import org.bukkit.inventory.Inventory;
    12. import org.bukkit.inventory.ItemStack;
    13. import org.bukkit.inventory.meta.ItemMeta;
    14. import org.bukkit.material.Wool;
    16. public class Menu implements Listener{
    18. private Inventory inv;
    19. private ItemStack l, lm;
    21. public Menu() {
    22. inv = Bukkit.createInventory(null, 9, "FastLeave!");
    24. l = createItem(DyeColor.RED, "Leave!");
    25. lm = createItem(DyeColor.BLUE, "View Leave Message!");
    27. inv.setItem(2, l);
    28. inv.setItem(8, lm);
    29. }
    31. private ItemStack createItem(DyeColor dc, String name) {
    32. ItemStack i = new Wool(dc).toItemStack(1);
    33. ItemMeta im = i.getItemMeta();
    34. im.setDisplayName(name);
    35. im.setLore(Arrays.asList("Do the fast leave commnd:" + name.toLowerCase()));
    36. i.setItemMeta(im);
    37. return i;
    38. }
    40. public void show(Player p) {
    41. p.openInventory(inv);
    42. }
    44. @EventHandler
    45. public void onInventoryClick(InventoryClickEvent e) {
    46. if (!(e.getInventory().equals(inv))) return;
    47. if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Leave!")); {
    48. e.getWhoClicked().HERE IS COMMAND!
    49. }
    50. }
    51. }
  2. Offline


    Try using "Bukkit.getServer().dispatchCommand(e.getWhoClicked(), "<fast leave command here>");" rather than "e.getWhoClicked().HERE IS COMMAND!". I have been experienced with using Menus with Bukkit. I prefer just creating an inventory that opens when I interact with anything using a certain item.

    Also note that if you haven't fixed the tick event upon a laggy player performing the command or interaction twice in one server tick, a player may take an item from the Menu and leave it like that for others to discover.

    There are ways to fix this but I go with the easier one. I create the inventory whenever it is needed so that it re-creates itself when the event is performed again. There is also the option of adding a player to an ArrayList or HashMap with a statement inside the event saying that you can not open it while you remain in those groups. Using the Bukkit Scheduler will allow you to do this.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Jun 9, 2016
  3. Offline


  4. Offline


    More detail please\

    1. @EventHandler
    2. public void onInventoryClick(InventoryClickEvent e) {
    3. if (!(e.getInventory().equals(inv))) return;
    4. if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Leave!")); {
    5. e.getWhoClicked().HERE IS COMMAND! <--- What is that?
    6. }
    7. }
    8. }

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Jun 9, 2016
  5. Offline


    I need your stacktrace.

    I'm going to assume its a NPE and that its because you didn't add a null check for getWhoClicked & if the item isn't null.

  6. Offline


    TechAttax Please refer to my 2 previous replies and I have started up a conversation with you if you need anymore help.
  7. Offline


    That is where I need help.
Thread Status:
Not open for further replies.

Share This Page