1.8.8 Gui Help

Discussion in 'Spigot Plugin Development' started by UtilsByteMedia, Feb 29, 2020.

  1. Hello I am trying to get the listener for my gui in my command class too work, so when I click an item it just doesn't do anything, I need help so it does something, but I can't get it too work, any help? By what I mean is like when I click the item in the gui it doesn't do anything it doesn't do what I set it too do. The onClick doesn't work.

    Here is my code.

    Code (Java):
    package best.ashton.testdev.Grant;

    import best.ashton.api.Utils.FinalUtil.CC;
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;

    import java.util.Arrays;

    public class CommandGrant implements CommandExecutor {
        String INVENTORY_TITLE = CC.Color("&6Grant");
        int INVENTORY_SIZE = 9*6;

        public boolean onCommand(final CommandSender sender, final Command cmd, final String label, final String[] args) {
            if (sender instanceof Player) {
                final Player p = (Player) sender;
                if (p.hasPermission("test.admin")) {
                    if (args.length == 0) {
                        p.sendMessage(CC.Color(CC.Color("&cUsage: /grant (player)")));
                    } else if (args.length == 1) {
                        Player GrantTarget = Bukkit.getPlayer(args[0]);
                        new CommandGrant(GrantTarget);
                    }
                } else {
                    p.sendMessage(CC.color("&cYou are not allowed to do this!"));
                }
            }
            return false;
        }

        Inventory inv = Bukkit.createInventory(null, INVENTORY_SIZE, INVENTORY_TITLE);

        public CommandGrant(Player GrantTarget) {
            if (GrantTarget == null) {
                return;
            }

            ItemStack Rank = new ItemStack(Material.DIAMOND, 1);
            ItemMeta RankMeta = Rank.getItemMeta();
            RankMeta.setDisplayName("§bRank");
            RankMeta.setLore(Arrays.asList("" + GrantTarget.getDisplayName() + " was here." + " "+ ""));
            Rank.setItemMeta(RankMeta);
            inv.setItem(11, Rank);

            ItemStack Glass = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 7);
            ItemMeta GlassMeta = Glass.getItemMeta();
            GlassMeta.setDisplayName(" ");
            Glass.setItemMeta(GlassMeta);

            for (int i = 36; i < 45; i++) {
                inv.setItem(i, Glass);
            }

            /**for(int i = 0; i < 9; i++) {
             inv.setItem(12 + i, outline);
             } */


            GrantTarget.openInventory(inv);
        }

        @EventHandler
        public void onClick(InventoryClickEvent e) {
            if(!e.getInventory().getName().equalsIgnoreCase(INVENTORY_TITLE)) {
                return;
            }

            Player GrantTarget = (Player) e.getWhoClicked();
            e.setCancelled(true);

            if(e.getCurrentItem() == null || e.getCurrentItem().getType() == null || !e.getCurrentItem().hasItemMeta()) {
                GrantTarget.getOpenInventory().close();
            }

            if(e.getSlot() == 10) {
                Bukkit.dispatchCommand(GrantTarget, "warp Build");
            }
        }

    }
    //p.getWorld().playSound(p.getLocation(), Sound.NOTE_PLING, 10, 1);
     
  2. Why are you creating a class for a simple method? Also its a pretty common inventory name,try making it private as well if ur not gonna use it elsewhere

    Also were at a point where we should avoid using the title as identifier do if(e.getinventory.equals(yourInv variable)
     
  3. What do you mean?
     
  4. Di
    Didn't help at all.
     
  5. Also go with some debugs,especially at the if(!e.getInventory().getName().equalsIgnoreCase(INVENTORY_TITLE)) {
    return;
    }
    part,close the inventory and see if it does it everytime,if it does then your passing a wrong inventory
     
  6. You have to implement Listener in the class that you have posted and put this in your onEnable():

    Code (Java):
    getServer().getPluginManager().registerEvents(new YourEventClass(), this);
     
    • Like Like x 1
    • Agree Agree x 1
  7. Code (Text):
     public CommandGrant(Player GrantTarget) {
            if (GrantTarget == null) {
                return;
            }

            ItemStack Rank = new ItemStack(Material.DIAMOND, 1);
            ItemMeta RankMeta = Rank.getItemMeta();
            RankMeta.setDisplayName("§bRank");
            RankMeta.setLore(Arrays.asList("" + GrantTarget.getDisplayName() + " was here." + " "+ ""));
            Rank.setItemMeta(RankMeta);
            inv.setItem(11, Rank);

            ItemStack Glass = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 7);
            ItemMeta GlassMeta = Glass.getItemMeta();
            GlassMeta.setDisplayName(" ");
            Glass.setItemMeta(GlassMeta);

            for (int i = 36; i < 45; i++) {
                inv.setItem(i, Glass);
            }

            /**for(int i = 0; i < 9; i++) {
             inv.setItem(12 + i, outline);
             } */

            GrantTarget.openInventory(inv);
        }
    This is what i meant about the method. Why do you use a constructor for a simple method
     
  8. Unless I am being blind here, why wouldn't he use a constructor?

    Also, please learn Java naming conventions...

    Code (Text):
      String INVENTORY_TITLE = CC.Color("&6Grant");
        int INVENTORY_SIZE = 9*6;
    Are these supposed to be constants...?
     
  9. Idk Was just testing some thing a bit ago.
     
  10. Have you solved the click event?
     
    • Like Like x 1
  11. Yes,were getting distracted one problem might be that ur setting the item to slot 11 and ur clicking slot 10(wich should be empty)
     
  12. Did you read my post?
    His problem is that the click event doesn't work because he hasn't done the things that you should do when you use events
     
    • Like Like x 1
    • Agree Agree x 1
  13. You didn’t implement Listener
     
    • Agree Agree x 1