Solved GUI not working

Discussion in 'Spigot Plugin Development' started by Wilsoon, Mar 24, 2020.

Thread Status:
Not open for further replies.
  1. Ok, so I get that right now I literally seem like I'm flooding the entire forum, but just bear with me.

    I have a GUI I created that I use a method from another separate class to open the GUI.
    My issue is that my InventoryClick Events aren't working. I've tried many ways to do the equalling from doing equal with MATERIAL to equal with Display name, yet none of them worked. I'm starting to think this may not be an issue with the event class itself, just that it doesn't seem to be recognising the event. I tried sending a test message and it didn't even manage to reach that point. I've checked that it is being registered, I'm using something like this to register:
    Code (Java):
    pm = Bukkit.getServer().getPluginManager()

    pm.registerEvents(new InventoryEvents(), this);
    pm.registerEvents(anotherEventClassWithPlayerChatEvent, this);
     
    InventoryEvents:
    Code (Java):

    package events;

    import org.bukkit.Bukkit;
    import org.bukkit.GameMode;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import net.md_5.bungee.api.ChatColor;

    public class InventoryEvents implements Listener {
        private static Player p = null;
        public static Player getPlayer() { return p; }
     
        @EventHandler
        public void onInvClick(InventoryClickEvent e) {
            Player p = (Player) e.getWhoClicked();
            if(e.getCurrentItem() == null || e.getCurrentItem().getType().equals(Material.AIR) || e.getCurrentItem().getType().equals(Material.STAINED_GLASS_PANE)) {
                p.closeInventory();
            }
            p.sendMessage(ChatColor.GREEN + "Test Message 123"); //<---- Did not run
            if(e.getSlot() == 0 && e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.AQUA + "" + ChatColor.BOLD + "Set to Creative Mode")) {
                p.closeInventory();
                p.setGameMode(GameMode.CREATIVE);
                p.sendMessage(ChatColor.GOLD + "Your gamemode has been updated to creative mode.");
            }
         
            if(e.getSlot() == 1 && e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.AQUA + "" + ChatColor.BOLD + "Set to Survival Mode")) {
                p.closeInventory();
                p.setGameMode(GameMode.SURVIVAL);
                p.sendMessage(ChatColor.GOLD + "Your gamemode has been updated to survival mode.");
            }
         
            if(e.getSlot() == 2 && e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.AQUA + "" + ChatColor.BOLD + "Set to Spectator Mode")) {
                p.closeInventory();
                p.setGameMode(GameMode.SPECTATOR);
                p.sendMessage(ChatColor.GOLD + "Your gamemode has been updated to spectator mode.");
            }
         
            if(e.getSlot() == 3 && e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GOLD + "" + ChatColor.BOLD + "Give Yourself An Item")) {
                p.closeInventory();
                p.sendMessage(ChatColor.GOLD + "What item do you need? (Type it in chat as a chat message), you may also type a value if you need a quantity other than 64.");
                p.sendMessage(ChatColor.GOLD + "Example: diamond 5");
                InventoryEvents.p = (Player) e.getWhoClicked();
                GiveCommandCaptureChatEvents.capturing = true;
            }
         
            if(e.getSlot() == 4 && e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.RED + "" + ChatColor.BOLD + "Heal yourself")) {
                p.closeInventory();
                p.setHealth(20.0D);
                p.sendMessage(ChatColor.GOLD + "You have been healed.");
            }
         
            if(e.getSlot() == 5 && e.getCurrentItem().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.RED + "" + ChatColor.BOLD + "Feed yourself")) {
                p.closeInventory();
                p.setFoodLevel(20);
                p.sendMessage(ChatColor.GOLD + "You are no longer hungry.");
            }
         
            if(e.getSlot() == 16) {
                p.closeInventory();
                Bukkit.dispatchCommand(p, "stop");
            }
            if(e.getSlot() == 17) {
                p.closeInventory();
                Bukkit.dispatchCommand(p, "restart");
            }
        }
    }


    Here's the separate class:
    Code (Java):

    package com.gmail.calorious;

    import org.bukkit.Bukkit;
    import org.bukkit.DyeColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import net.md_5.bungee.api.ChatColor;

    public class GUI_Interface {
        private static ChatColor B = ChatColor.BOLD;
        @SuppressWarnings("deprecation")
        public static void openMainGUI(Player p) {
            Inventory maingui = Bukkit.getServer().createInventory(null, 18, ChatColor.GREEN + "" + B + "Menu");
            ItemStack item1 = new ItemStack(Material.DIAMOND_BLOCK); // Creative Mode Item
            ItemStack item2 = new ItemStack(Material.CARROT_ITEM); // Survival Mode Item
            ItemStack item3 = new ItemStack(Material.COMMAND); // Give self Item
            ItemStack item4 = new ItemStack(Material.GOLDEN_APPLE); // Heal Self
            ItemStack item5 = new ItemStack(Material.COOKED_BEEF); // Feed Self
            ItemStack item6 = new ItemStack(Material.POTION, 1); // Spectator
            ItemStack item7 = new ItemStack(Material.WOOL, 1, DyeColor.RED.getData());
            ItemStack item8 = new ItemStack(Material.WOOL, 1, DyeColor.LIGHT_BLUE.getData());
            /*
            * Get Meta
            */

            ItemMeta item1meta = item1.getItemMeta();
            ItemMeta item2meta = item2.getItemMeta();
            ItemMeta item3meta = item3.getItemMeta();
            ItemMeta item4meta = item4.getItemMeta();
            ItemMeta item5meta = item5.getItemMeta();
            ItemMeta item6meta = item6.getItemMeta();
            ItemMeta item7meta = item7.getItemMeta();
            ItemMeta item8meta = item8.getItemMeta();
            /*
            * Modify
            */

            item1meta.setDisplayName(ChatColor.AQUA + "" + B + "Set to Creative Mode");
            item2meta.setDisplayName(ChatColor.AQUA + "" + B + "Set to Survival Mode");
            item3meta.setDisplayName(ChatColor.GOLD + "" + B + "Give Yourself An Item");
            item4meta.setDisplayName(ChatColor.RED + "" + B + "Heal yourself");
            item5meta.setDisplayName(ChatColor.RED + "" + B + "Feed yourself");
            item6meta.setDisplayName(ChatColor.AQUA + "" + B + "Set to Spectator Mode");
            item7meta.setDisplayName(ChatColor.DARK_RED + "" + B + "Stop Server");
            item8meta.setDisplayName(ChatColor.GREEN + "" + B + "Restart Server");
            /*
            * Set new meta
            */

            item1.setItemMeta(item1meta);
            item2.setItemMeta(item2meta);
            item3.setItemMeta(item3meta);
            item4.setItemMeta(item4meta);
            item5.setItemMeta(item5meta);
            item6.setItemMeta(item6meta);
            item7.setItemMeta(item7meta);
            item8.setItemMeta(item8meta);
            /*
            * Add to GUI
            */

            maingui.setItem(0, item1);
            maingui.setItem(1, item2);
            maingui.setItem(2, item6);
            maingui.setItem(3, item3);
            maingui.setItem(4, item4);
            maingui.setItem(5, item5);
            maingui.setItem(9, new ItemStack(Material.STAINED_GLASS_PANE, 1, DyeColor.GRAY.getData()));
            maingui.setItem(10, new ItemStack(Material.STAINED_GLASS_PANE, 1, DyeColor.GRAY.getData()));
            maingui.setItem(11, new ItemStack(Material.STAINED_GLASS_PANE, 1, DyeColor.GRAY.getData()));
            maingui.setItem(12, new ItemStack(Material.STAINED_GLASS_PANE, 1, DyeColor.GRAY.getData()));
            maingui.setItem(13, new ItemStack(Material.STAINED_GLASS_PANE, 1, DyeColor.GRAY.getData()));
            maingui.setItem(14, new ItemStack(Material.STAINED_GLASS_PANE, 1, DyeColor.GRAY.getData()));
            maingui.setItem(15, new ItemStack(Material.STAINED_GLASS_PANE, 1, DyeColor.GRAY.getData()));
            maingui.setItem(16, item7);
            maingui.setItem(17, item8);
            p.openInventory(maingui);
        }
    }
     
  2. did you put an 'if' statement for the Menu GUI to check if it's really the Menu GUI you're clicking with?
     
  3. Tried it before, that was one of the things I tried before I turned to the forums, it still didn't work.


    No errors, just that clicking the GUI just allowed me to take things out of it like a normal chest instead of closing like I wrote it to.
     
  4. Oh, try to change the 'equalsIgnoreCase' to 'contain' and just put the name of the item with it, no ChatColor statement. Second, try to remove the getSlot because of how other items don't have the same names as other items too. Third, keep the 'if' statement for the checking of GUI to secure if it's really the GUI you're clicking with.
     
  5. Still didn't seem to work, no errors on enabling and no errors when taking out the items.

    New code: InventoryEvents
    Code (Java):

    package events;

    import org.bukkit.Bukkit;
    import org.bukkit.GameMode;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import net.md_5.bungee.api.ChatColor;

    public class InventoryEvents implements Listener {
        private static Player p = null;
        public static Player getPlayer() { return p; }
       
        @EventHandler
        public void onInvClick(InventoryClickEvent e) {
            Player p = (Player) e.getWhoClicked();
            if(e.getInventory().getTitle().contains("Menu")) {
            if(e.getCurrentItem() == null || e.getCurrentItem().getType().equals(Material.AIR) || e.getCurrentItem().getType().equals(Material.STAINED_GLASS_PANE)) {
                p.closeInventory();
            }
            if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Set to Creative Mode")) {
                p.closeInventory();
                p.setGameMode(GameMode.CREATIVE);
                p.sendMessage(ChatColor.GOLD + "Your gamemode has been updated to creative mode.");
            }
           
            if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Set to Survival Mode")) {
                p.closeInventory();
                p.setGameMode(GameMode.SURVIVAL);
                p.sendMessage(ChatColor.GOLD + "Your gamemode has been updated to survival mode.");
            }
           
            if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Set to Spectator Mode")) {
                p.closeInventory();
                p.setGameMode(GameMode.SPECTATOR);
                p.sendMessage(ChatColor.GOLD + "Your gamemode has been updated to spectator mode.");
            }
           
            if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Give Yourself An Item")) {
                p.closeInventory();
                p.sendMessage(ChatColor.GOLD + "What item do you need? (Type it in chat as a chat message), you may also type a value if you need a quantity other than 64.");
                p.sendMessage(ChatColor.GOLD + "Example: diamond 5");
                InventoryEvents.p = (Player) e.getWhoClicked();
                GiveCommandCaptureChatEvents.capturing = true;
            }
           
            if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Heal yourself")) {
                p.closeInventory();
                p.setHealth(20.0D);
                p.sendMessage(ChatColor.GOLD + "You have been healed.");
            }
           
            if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Feed yourself")) {
                p.closeInventory();
                p.setFoodLevel(20);
                p.sendMessage(ChatColor.GOLD + "You are no longer hungry.");
            }
           
            if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Stop Server")) {
                p.closeInventory();
                Bukkit.dispatchCommand(p, "stop");
            }
            if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Restart Server")) {
                p.closeInventory();
                Bukkit.dispatchCommand(p, "restart");
            }
            }
        }
    }
     
  6. okay, try replacing your 'if' statement with this:

    Code (Text):
    if(!ChatColor.stripColor(e.getInventory().getName()).equalsIgnoreCase("Menu"))
                return;
    Add an event.setCancelled(true); just in the bottom of the code above.
     
    #7 joshuacc, Mar 24, 2020
    Last edited: Mar 24, 2020
  7. UPDATE:
    Just used this code and it worked
    Code (Text):
      @EventHandler
        public void onInvClick(InventoryClickEvent e) {
            Player p = (Player) e.getWhoClicked();
            e.setCancelled(true);
            if(!ChatColor.stripColor(e.getInventory().getName()).equalsIgnoreCase("Menu"))
                return;
            if(e.getCurrentItem() == null || e.getCurrentItem().getType().equals(Material.AIR) || e.getCurrentItem().getType().equals(Material.STAINED_GLASS_PANE)) {
                p.closeInventory();
                e.setCancelled(true);
                return;
            }
            if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Set to Creative Mode")) {
                p.closeInventory();
                p.setGameMode(GameMode.CREATIVE);
                p.sendMessage(ChatColor.GOLD + "Your gamemode has been updated to creative mode.");
            }
         
            if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Set to Survival Mode")) {
                p.closeInventory();
                p.setGameMode(GameMode.SURVIVAL);
                p.sendMessage(ChatColor.GOLD + "Your gamemode has been updated to survival mode.");
            }
         
            if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Set to Spectator Mode")) {
                p.closeInventory();
                p.setGameMode(GameMode.SPECTATOR);
                p.sendMessage(ChatColor.GOLD + "Your gamemode has been updated to spectator mode.");
            }
         
            if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Give Yourself An Item")) {
                p.closeInventory();
                p.sendMessage(ChatColor.GOLD + "What item do you need? (Type it in chat as a chat message), you may also type a value if you need a quantity other than 64.");
                p.sendMessage(ChatColor.GOLD + "Example: diamond 5");
     
            }
         
            if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Heal yourself")) {
                p.closeInventory();
                p.setHealth(20.0D);
                p.sendMessage(ChatColor.GOLD + "You have been healed.");
            }
         
            if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Feed yourself")) {
                p.closeInventory();
                p.setFoodLevel(20);
                p.sendMessage(ChatColor.GOLD + "You are no longer hungry.");
            }
         
            if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Stop Server")) {
                p.closeInventory();
                Bukkit.dispatchCommand(p, "stop");
            }
            if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Restart Server")) {
                p.closeInventory();
                Bukkit.dispatchCommand(p, "restart");
            }
            }
    }
    Try using that for now to see if the clicking works first and if it does, you can modify it if it's all working.
     
  8. Nope. Still the same thing
    None of the items do anything
    It looks like this:
    upload_2020-3-25_17-42-23.png
     
  9. Is the event actually firing ? If you print something on the first line of the method does it get printed ?
     
  10. I just realised it isn't even firing. Tried it with a try catch Exception too, didn't show anything. Welp. Guess I really have to send the code.

    InventoryEvents:
    Code (Java):

    package events;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.GameMode;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;

    public class InventoryEvents implements Listener {
        private static Player p = null;
        public static Player getPlayer() { return p; }
         
         @EventHandler
         public void onInvClick(InventoryClickEvent e) {
            Player p = (Player) e.getWhoClicked();
            p.sendMessage(ChatColor.GREEN + "Event fired!"); // <---- Test Message, Failed.
            if(!ChatColor.stripColor(e.getInventory().getName()).equalsIgnoreCase("Menu"))
                return;
            if(e.getCurrentItem() == null || e.getCurrentItem().getType().equals(Material.AIR) || e.getCurrentItem().getType().equals(Material.STAINED_GLASS_PANE)) {
                p.closeInventory();
                e.setCancelled(true);
                return;
            }
            if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Set to Creative Mode")) {
                p.closeInventory();
                p.setGameMode(GameMode.CREATIVE);
                p.sendMessage(ChatColor.GOLD + "Your gamemode has been updated to creative mode.");
            }
         
            if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Set to Survival Mode")) {
                p.closeInventory();
                p.setGameMode(GameMode.SURVIVAL);
                p.sendMessage(ChatColor.GOLD + "Your gamemode has been updated to survival mode.");
            }
         
            if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Set to Spectator Mode")) {
                p.closeInventory();
                p.setGameMode(GameMode.SPECTATOR);
                p.sendMessage(ChatColor.GOLD + "Your gamemode has been updated to spectator mode.");
            }
         
            if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Give Yourself An Item")) {
                p.closeInventory();
                p.sendMessage(ChatColor.GOLD + "What item do you need? (Type it in chat as a chat message), you may also type a value if you need a quantity other than 64.");
                p.sendMessage(ChatColor.GOLD + "Example: diamond 5");
                InventoryEvents.p = (Player) e.getWhoClicked();
            }
         
            if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Heal yourself")) {
                p.closeInventory();
                p.setHealth(20.0D);
                p.sendMessage(ChatColor.GOLD + "You have been healed.");
            }
         
            if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Feed yourself")) {
                p.closeInventory();
                p.setFoodLevel(20);
                p.sendMessage(ChatColor.GOLD + "You are no longer hungry.");
            }
         
            if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Stop Server")) {
                p.closeInventory();
                Bukkit.dispatchCommand(p, "stop");
            }
            if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Restart Server")) {
                p.closeInventory();
                Bukkit.dispatchCommand(p, "restart");
            }
            }
    }
    Main class:
    Code (Java):

    package com.gmail.calorious;

    import org.bukkit.Bukkit;
    import commands.*;
    import org.bukkit.plugin.java.JavaPlugin;
    import events.*;
    public class FundamentalGUI extends JavaPlugin {
        private static java.util.logging.Logger log;
        private static org.bukkit.plugin.PluginManager pm;
       
        @Override
        public void onEnable() {
            log = Bukkit.getLogger();
            pm = getServer().getPluginManager();
            try {
            getCommand("gui").setExecutor(new OpenGUICommand());
            pm.registerEvents(new InventoryEvents(), this);
            pm.registerEvents(new GiveCommandCaptureChatEvents(), this);
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
       
        @Override
        public void onDisable() {}
    }

    OpenGUICommand:
    [code=Java]
    package commands;

    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    public class OpenGUICommand implements CommandExecutor {
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player p = (Player) sender;
            com.gmail.calorious.GUI_Interface.openMainGUI(p);
            return true;
        }
    }
    GUI_Interface:
    Code (Java):

        package com.gmail.calorious;
       
        import org.bukkit.Bukkit;
        import org.bukkit.DyeColor;
        import org.bukkit.Material;
        import org.bukkit.entity.Player;
        import org.bukkit.inventory.Inventory;
        import org.bukkit.inventory.ItemStack;
        import org.bukkit.inventory.meta.ItemMeta;
        import net.md_5.bungee.api.ChatColor;
       
        public class GUI_Interface {
            public static ChatColor B = ChatColor.BOLD;
            @SuppressWarnings("deprecation")
            public static void openMainGUI(Player p) {
                Inventory maingui = Bukkit.getServer().createInventory(null, 18, ChatColor.GREEN + "" + B + "Menu");
                ItemStack item1 = new ItemStack(Material.DIAMOND_BLOCK); // Creative Mode Item
                ItemStack item2 = new ItemStack(Material.CARROT_ITEM); // Survival Mode Item
                ItemStack item3 = new ItemStack(Material.COMMAND); // Give self Item
                ItemStack item4 = new ItemStack(Material.GOLDEN_APPLE); // Heal Self
                ItemStack item5 = new ItemStack(Material.COOKED_BEEF); // Feed Self
                ItemStack item6 = new ItemStack(Material.POTION, 1); // Spectator
                ItemStack item7 = new ItemStack(Material.WOOL, 1, DyeColor.RED.getData());
                ItemStack item8 = new ItemStack(Material.WOOL, 1, DyeColor.LIGHT_BLUE.getData());
                /*
                * Get Meta
                */

                ItemMeta item1meta = item1.getItemMeta();
                ItemMeta item2meta = item2.getItemMeta();
                ItemMeta item3meta = item3.getItemMeta();
                ItemMeta item4meta = item4.getItemMeta();
                ItemMeta item5meta = item5.getItemMeta();
                ItemMeta item6meta = item6.getItemMeta();
                ItemMeta item7meta = item7.getItemMeta();
                ItemMeta item8meta = item8.getItemMeta();
                /*
                * Modify
                */

                item1meta.setDisplayName(ChatColor.AQUA + "" + B + "Set to Creative Mode");
                item2meta.setDisplayName(ChatColor.AQUA + "" + B + "Set to Survival Mode");
                item3meta.setDisplayName(ChatColor.GOLD + "" + B + "Give Yourself An Item");
                item4meta.setDisplayName(ChatColor.RED + "" + B + "Heal yourself");
                item5meta.setDisplayName(ChatColor.RED + "" + B + "Feed yourself");
                item6meta.setDisplayName(ChatColor.AQUA + "" + B + "Set to Spectator Mode");
                item7meta.setDisplayName(ChatColor.DARK_RED + "" + B + "Stop Server");
                item8meta.setDisplayName(ChatColor.GREEN + "" + B + "Restart Server");
                /*
                * Set new meta
                */

                item1.setItemMeta(item1meta);
                item2.setItemMeta(item2meta);
                item3.setItemMeta(item3meta);
                item4.setItemMeta(item4meta);
                item5.setItemMeta(item5meta);
                item6.setItemMeta(item6meta);
                item7.setItemMeta(item7meta);
                item8.setItemMeta(item8meta);
                /*
                * Add to GUI
                */

                maingui.setItem(0, item1);
                maingui.setItem(1, item2);
                maingui.setItem(2, item6);
                maingui.setItem(3, item3);
                maingui.setItem(4, item4);
                maingui.setItem(5, item5);
                maingui.setItem(9, new ItemStack(Material.STAINED_GLASS_PANE, 1, DyeColor.GRAY.getData()));
                maingui.setItem(10, new ItemStack(Material.STAINED_GLASS_PANE, 1, DyeColor.GRAY.getData()));
                maingui.setItem(11, new ItemStack(Material.STAINED_GLASS_PANE, 1, DyeColor.GRAY.getData()));
                maingui.setItem(12, new ItemStack(Material.STAINED_GLASS_PANE, 1, DyeColor.GRAY.getData()));
                maingui.setItem(13, new ItemStack(Material.STAINED_GLASS_PANE, 1, DyeColor.GRAY.getData()));
                maingui.setItem(14, new ItemStack(Material.STAINED_GLASS_PANE, 1, DyeColor.GRAY.getData()));
                maingui.setItem(15, new ItemStack(Material.STAINED_GLASS_PANE, 1, DyeColor.GRAY.getData()));
                maingui.setItem(16, item7);
                maingui.setItem(17, item8);
                p.openInventory(maingui);
            }
        }
     
  11. drives_a_ford

    Moderator

    First of all, I suggest you read the javadocs of InventoryClickEvent. I'll quote it here for you:
    If you didn't catch it, you shouldn't use the HumanEntity#closeInventory from within this event. Yet your event is full of those.

    Also you should make sure the interaction in question are actually within your GUI before running any of the other logic.

    EDIT:
    Why is there a try-catch around setting the listeners?
    If there's an error, you'll need to fix it instead of catching it.

    EDIT2:
    Also this is clear static abuse:
    Code (Java):
        private static Player p = null;
        public static Player getPlayer() { return p; }
     
     
  12. Is your other event firing?
     
  13. Yeah, I was attempting to catch errors with this so that I can take a look. Removed.

    Oops, I never read the JavaDocs of InventoryClickEvent. Thanks. New code, yet it still doesn't work:
    Code (Java):

    package events;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.GameMode;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;

    import com.gmail.calorious.FundamentalGUI;

    public class InventoryEvents implements Listener {
        private static Player p = null;
        public static Player getPlayer() { return p; }
         
         @EventHandler
         public void onInvClick(InventoryClickEvent e) {
            Player p = (Player) e.getWhoClicked();
            p.sendMessage(ChatColor.GREEN + "Event fired!"); // <---- Test Message, Failed.
            if(!ChatColor.stripColor(e.getInventory().getName()).equalsIgnoreCase("Menu"))
                return;
            if(e.getCurrentItem() == null || e.getCurrentItem().getType().equals(Material.AIR) || e.getCurrentItem().getType().equals(Material.STAINED_GLASS_PANE)) {
                Bukkit.getScheduler().runTask(FundamentalGUI.getInstance(), new Runnable() {
                    public void run() {
                        p.closeInventory();
                    }
                });
                e.setCancelled(true);
                return;
            }
            if(ChatColor.stripColor(e.getInventory().getName()).equalsIgnoreCase("Menu")) {
            if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Set to Creative Mode")) {
                Bukkit.getScheduler().runTask(FundamentalGUI.getInstance(), new Runnable() {
                    public void run() {
                        p.closeInventory();
                    }
                });
                p.setGameMode(GameMode.CREATIVE);
                p.sendMessage(ChatColor.GOLD + "Your gamemode has been updated to creative mode.");
            }
         
            if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Set to Survival Mode")) {
                Bukkit.getScheduler().runTask(FundamentalGUI.getInstance(), new Runnable() {
                    public void run() {
                        p.closeInventory();
                    }
                });
                p.setGameMode(GameMode.SURVIVAL);
                p.sendMessage(ChatColor.GOLD + "Your gamemode has been updated to survival mode.");
            }
         
            if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Set to Spectator Mode")) {
                Bukkit.getScheduler().runTask(FundamentalGUI.getInstance(), new Runnable() {
                    public void run() {
                        p.closeInventory();
                    }
                });
                p.setGameMode(GameMode.SPECTATOR);
                p.sendMessage(ChatColor.GOLD + "Your gamemode has been updated to spectator mode.");
            }
         
            if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Give Yourself An Item")) {
                Bukkit.getScheduler().runTask(FundamentalGUI.getInstance(), new Runnable() {
                    public void run() {
                        p.closeInventory();
                    }
                });
                p.sendMessage(ChatColor.GOLD + "What item do you need? (Type it in chat as a chat message), you may also type a value if you need a quantity other than 64.");
                p.sendMessage(ChatColor.GOLD + "Example: diamond 5");
                InventoryEvents.p = (Player) e.getWhoClicked();
            }
         
            if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Heal yourself")) {
                Bukkit.getScheduler().runTask(FundamentalGUI.getInstance(), new Runnable() {
                    public void run() {
                        p.closeInventory();
                    }
                });
                p.setHealth(20.0D);
                p.sendMessage(ChatColor.GOLD + "You have been healed.");
            }
         
            if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Feed yourself")) {
                Bukkit.getScheduler().runTask(FundamentalGUI.getInstance(), new Runnable() {
                    public void run() {
                        p.closeInventory();
                    }
                });
                p.setFoodLevel(20);
                p.sendMessage(ChatColor.GOLD + "You are no longer hungry.");
            }
         
            if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Stop Server")) {
                Bukkit.getScheduler().runTask(FundamentalGUI.getInstance(), new Runnable() {
                    public void run() {
                        p.closeInventory();
                    }
                });
                Bukkit.dispatchCommand(p, "stop");
            }
            if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Restart Server")) {
                Bukkit.getScheduler().runTask(FundamentalGUI.getInstance(), new Runnable() {
                    public void run() {
                        p.closeInventory();
                    }
                });
                Bukkit.dispatchCommand(p, "restart");
            }
            }
         }
     
  14. Can't really fire the other event without firing this one first.
    That event is set as a chain to this event I have.
     
  15. Then I honestly have no idea, also what's the point of declaring an instance of PluginManager as static? It's used only in your main class, and if you really needed it you should pass the instance of your main class through a constructor, and retrieve it from there.
     
    • Like Like x 1
  16. drives_a_ford

    Moderator

    Are you testing this in survival mode?
    Because if you're doing so in creative mode, then the InventoryCreativeEvent will be called instead.
     
    • Agree Agree x 1
    • Useful Useful x 1
    • Informative Informative x 1
  17. Fixed it, kinda lazy to constantly do getServer().getPluginManager().


    Tested it in both creative and survival. Same thing.


    I know this, but I'm using this for like a private server. So I wouldn't really need to care about these things, thanks anyway.
     
  18. drives_a_ford

    Moderator

    If your event handler method isn't being called then the instance has not been registered as a listener or has been unresgistered after initially being registered.

    You can add a debug message before and after your registration of the listener to make sure that part of your code gets run on server startup.

    You can also use InventoryClickEvent#getHandlerList and then HandlerList#getRegisteredListeners to get a list of registered listeners (of your plugin) listening to this event.
    You can then iterate over them and see if any of the RegisteredListener#getListeners are an instance of your InventoryEvents class.
     
Thread Status:
Not open for further replies.