1.8.8 event not firing in only one class

Discussion in 'Spigot Plugin Development' started by ZoiBox, Mar 9, 2020.

  1. Hello everyone,

    I was just wondering if anyone here could help me as I have been trying to fix this issue for days.It is really simple however for some reason the "inventory click event" fires in every GUI apart from my mob arena one. Please find the code below;

    Code (Java):
    package me.zoibox.minigameselector.GUI;

    import me.zoibox.minigameselector.MinigameSelector;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    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 org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;

    public class Handler implements Listener {

        private MobArena mobArena = new MobArena();

        private MinigameSelector plugin;

        public Handler(MinigameSelector plugin){

        plugin = plugin;
         }

        @EventHandler
        public void onClick(InventoryClickEvent event){



            Player player = (Player) event.getWhoClicked();


            ItemStack MobArena = new ItemStack(Material.MONSTER_EGG, 1, (short)  50);
            ItemMeta MobArenaMeta = MobArena.getItemMeta();
            MobArenaMeta.setDisplayName(ChatColor.GREEN + "Mob Arena");
            MobArena.setItemMeta(MobArenaMeta);

            ItemStack PvPArena = new ItemStack(Material.DIAMOND_SWORD);
            ItemMeta PvPArenaMeta = PvPArena.getItemMeta();
            PvPArenaMeta.setDisplayName(ChatColor.BLUE + "PvP Arena");
            PvPArena.setItemMeta(PvPArenaMeta);

            ItemStack SG = new ItemStack(Material.GOLD_AXE);
            ItemMeta SGMeta = SG.getItemMeta();
            SGMeta.setDisplayName(ChatColor.GOLD + "Survival Games");
            SG.setItemMeta(SGMeta);

            ItemStack PB = new ItemStack(Material.SNOW_BALL);
            ItemMeta PBMeta = PB.getItemMeta();
            PBMeta.setDisplayName("Spleef");
            PB.setItemMeta(PBMeta);

            ItemStack kb = new ItemStack(Material.DRAGON_EGG);
            ItemMeta kbMeta = kb.getItemMeta();
            kbMeta.setDisplayName(ChatColor.DARK_PURPLE + "Egg Wars");
            kb.setItemMeta(kbMeta);

            ItemStack spleef = new ItemStack(Material.GOLD_SPADE);
            ItemMeta spleefMeta = kb.getItemMeta();
            kbMeta.setDisplayName(ChatColor.DARK_BLUE + "Spleef");
            spleef.setItemMeta(spleefMeta);

            ItemStack parkour = new ItemStack(Material.COBBLESTONE_STAIRS);
            ItemMeta parkourMeta = parkour.getItemMeta();
            parkourMeta.setDisplayName(ChatColor.GOLD + "Parkour");
            parkour.setItemMeta(parkourMeta);

            ItemStack nothing = new ItemStack(Material.REDSTONE_BLOCK);
            ItemMeta nothingMeta = nothing.getItemMeta();
            nothingMeta.setDisplayName(ChatColor.RED + "More Minigames to come soon!");
            nothing.setItemMeta(nothingMeta);

            ItemStack blank = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 10);
            ItemMeta blankMeta = blank.getItemMeta();
            blankMeta.setDisplayName(" ");
            blank.setItemMeta(blankMeta);

            if (ChatColor.translateAlternateColorCodes('&', event.getClickedInventory().getTitle()).equals(ChatColor.DARK_PURPLE + "Minigame Selector")) {

                switch (event.getCurrentItem().getType()) {
                    case REDSTONE_BLOCK:

                        player.closeInventory();
                        player.sendMessage(ChatColor.RED + "Coming Soon");
                        break;
                    case STAINED_GLASS_PANE:

                        player.closeInventory();
                        break;

                    case MONSTER_EGG:
                        player.closeInventory();

                        mobArena.mobGUI(player);

                        break;

                    case DRAGON_EGG:
                        player.closeInventory();
                        player.performCommand("/ew join CastleWars");

                    case BOW:
                        player.closeInventory();
                        player.performCommand("/oitb join");
                        break;

                    case GOLD_AXE:
                        player.closeInventory();
                        player.performCommand("/sg join SkyValley");

                        break;

                    case SNOW_BALL:
                        player.closeInventory();
                        player.performCommand("/pb join Colosseum");

                        break;

                    default:
                        return;
                }
            }


        }



                }



     

    Code (Java):
    package me.zoibox.minigameselector.GUI;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;

    public class Main implements Listener {

        public void GUICreateor(Player player){

           Inventory s = Bukkit.createInventory(null, 27, ChatColor.DARK_PURPLE + "Minigame Selector");

            ItemStack MobArena = new ItemStack(Material.MONSTER_EGG, 1, (short)  50);
            ItemMeta MobArenaMeta = MobArena.getItemMeta();
            MobArenaMeta.setDisplayName(ChatColor.GREEN + "Mob Arena");
            MobArena.setItemMeta(MobArenaMeta);

            ItemStack PvPArena = new ItemStack(Material.DIAMOND_SWORD);
            ItemMeta PvPArenaMeta = PvPArena.getItemMeta();
            PvPArenaMeta.setDisplayName(ChatColor.BLUE + "PvP Arena");
            PvPArena.setItemMeta(PvPArenaMeta);

            ItemStack SG = new ItemStack(Material.GOLD_AXE);
            ItemMeta SGMeta = SG.getItemMeta();
            SGMeta.setDisplayName(ChatColor.GOLD + "Survival Games");
            SG.setItemMeta(SGMeta);

            ItemStack PB = new ItemStack(Material.SNOW_BALL);
            ItemMeta PBMeta = PB.getItemMeta();
            PBMeta.setDisplayName("Spleef");
            PB.setItemMeta(PBMeta);

            ItemStack kb = new ItemStack(Material.DRAGON_EGG);
            ItemMeta kbMeta = kb.getItemMeta();
            kbMeta.setDisplayName(ChatColor.DARK_PURPLE + "Egg Wars");
            kb.setItemMeta(kbMeta);

         ItemStack spleef = new ItemStack(Material.GOLD_SPADE);
         ItemMeta spleefMeta = kb.getItemMeta();
         kbMeta.setDisplayName(ChatColor.DARK_BLUE + "Spleef");
         spleef.setItemMeta(spleefMeta);

         ItemStack parkour = new ItemStack(Material.COBBLESTONE_STAIRS);
         ItemMeta parkourMeta = parkour.getItemMeta();
         parkourMeta.setDisplayName(ChatColor.GOLD + "Parkour");
         parkour.setItemMeta(parkourMeta);

         ItemStack nothing = new ItemStack(Material.REDSTONE_BLOCK);
         ItemMeta nothingMeta = nothing.getItemMeta();
         nothingMeta.setDisplayName(ChatColor.RED + "More Minigames to come soon!");
         nothing.setItemMeta(nothingMeta);

            ItemStack blank = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 10);
            ItemMeta blankMeta = blank.getItemMeta();
            blankMeta.setDisplayName(" ");
            blank.setItemMeta(blankMeta);

            s.setItem(0, blank);
            s.setItem(1, blank);
            s.setItem(2, MobArena);
            s.setItem(3, blank);
            s.setItem(4, PvPArena);
            s.setItem(5, blank);
            s.setItem(6, SG);
            s.setItem(7, blank);
            s.setItem(8, blank);
            s.setItem(9, blank);
            s.setItem(10, PB);
            s.setItem(11, blank);
            s.setItem(12, kb);
            s.setItem(13, blank);
            s.setItem(14, spleef);
            s.setItem(15, blank);
            s.setItem(16, parkour);
            s.setItem(17, blank);
            s.setItem(18, blank);
            s.setItem(19, blank);
            s.setItem(20, nothing);
            s.setItem(21, blank);
            s.setItem(22, nothing);
            s.setItem(23, blank);
            s.setItem(24, nothing);
            s.setItem(25, blank);
            s.setItem(26, blank);




           player.openInventory(s);

        }

    }
     
    Code (Java):
    package me.zoibox.minigameselector.GUI;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.inventory.ClickType;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.inventory.InventoryCloseEvent;
    import org.bukkit.event.inventory.InventoryOpenEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.InventoryHolder;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;

    import java.awt.*;

    public class MobArena  implements Listener {


    public void mobGUI(Player player){

        Inventory maInv = Bukkit.createInventory(null, 9, ChatColor.GREEN + "Select Arena");

        ItemStack ma = new ItemStack(Material.ITEM_FRAME);
        ItemMeta maMeta = ma.getItemMeta();
        maMeta.setDisplayName(ChatColor.GREEN + "NorthLands");
        ma.setItemMeta(maMeta);

        ItemStack Dungeon = new ItemStack(Material.ITEM_FRAME);
        ItemMeta DungeonMeta = Dungeon.getItemMeta();
        DungeonMeta.setDisplayName(ChatColor.GREEN + "Dungeon");
        Dungeon.setItemMeta(DungeonMeta);

        maInv.setItem(0, ma);
        maInv.setItem(1, Dungeon);

        player.openInventory(maInv);


    }

        @EventHandler
        public void onPlayerClickOnItem(InventoryClickEvent e){

        Player player = (Player) e.getWhoClicked();

        player.sendMessage("sodiujgfh");

            if(e.getInventory().getTitle().equals(ChatColor.GREEN + "Select Arena")){
                switch(e.getSlot()){
                    case 0:
                        player.closeInventory();
                        player.sendMessage(ChatColor.RED + "debug");
                        break;

                        default:
                            return;
                }
                e.setCancelled(true);
            }
        }


    }




     

    Code (Java):
    package me.zoibox.minigameselector;

    import me.zoibox.minigameselector.Events.Click;
    import me.zoibox.minigameselector.Events.Join;
    import me.zoibox.minigameselector.Events.NoMove;
    import me.zoibox.minigameselector.GUI.Handler;
    import me.zoibox.minigameselector.GUI.Main;
    import me.zoibox.minigameselector.GUI.MobArena;
    import org.bukkit.plugin.java.JavaPlugin;

    public class MinigameSelector extends JavaPlugin {

        @Override
        public void onEnable() {
            getServer().getPluginManager().registerEvents(new Click(), this);
            getServer().getPluginManager().registerEvents(new Join(), this);
            getServer().getPluginManager().registerEvents(new NoMove(), this);
            getServer().getPluginManager().registerEvents(new Handler(this), this);

            getServer().getPluginManager().registerEvents(new MobArena(), this);
            getServer().getPluginManager().registerEvents(new Main(), this);

        }

        @Override
        public void onDisable() {
            // Plugin shutdown logic
        }
    }
     
    thanks to anyone who can help me!


    edit:

    The class "main" is used to generate the main gui as shown below

    Code (Java):
    package me.zoibox.minigameselector.Events;

    import me.zoibox.minigameselector.GUI.Main;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;

    public class Click implements Listener {

        private Main guiHandler = new Main();


        @EventHandler
        public void onPlayerClicks(PlayerInteractEvent event) {
            Player player = event.getPlayer();
            Action action = event.getAction();

            ItemStack selectorItem = new ItemStack(Material.BOOK);
            ItemMeta selectorMeta = selectorItem.getItemMeta();
            selectorMeta.setDisplayName(ChatColor.DARK_PURPLE + "Minigame Selector");
            selectorItem.setItemMeta(selectorMeta);


            if (action.equals(Action.RIGHT_CLICK_AIR) || action.equals(Action.RIGHT_CLICK_BLOCK)) {
                if (player.getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.DARK_PURPLE + "Minigame Selector")) {

                    player.playSound(player.getLocation(), Sound.CLICK, 1F, 1F);

                    guiHandler.GUICreateor(player);



                }

                if (event.getItem().getType() == null) {
                    return;
                }
            }

        }

    }
     
     
  2. You have no & in the title,just check if the title is ChatColor.PURPLE+...
     
  3. What do I have no? The colour changing thing did not fix it as you are looking at the main gui not the mobarena one as it is not working on only that one.

    Thanks fir the reply but it didnt fix it
     
  4. The title doesn't have '&' inside of it. Color codes aren't actually represented with '&'.
     
  5. Still doesent work when I translate alternate colour codes
     
  6. You should strip colors then just compare the string
     
    • Agree Agree x 1
  7. drives_a_ford

    Moderator

    You do realize the MobArena instance that you registered as a listener is not the same as the instance which you create (and store) in the Handler class, right?

    You most likely want to create one instance and share it - i.e register it as a listener and then pass it as a dependency to Handler constructor.
     
    • Agree Agree x 1
  8. Can you please give me a worked example. However even when I put all my code for it in the same class it still does not work
     
  9. How could I do that?
     
  10. drives_a_ford

    Moderator

    This is really basic stuff, my friend. I'm afraid you'll have to figure it out for yourself.
    If you're having trouble, you can start by reading Beginner Programming Mistakes and Why You're Making Them, but it seems you might need help with things that are even more basic than that. I'd suggest looking for Java courses. The README thread has some links (haven't followed them myself, but they're stickied so can't be horrible):
     
    • Agree Agree x 1
  11. Sorry, I scimmed over message and it is 11 at night for me so am tired sorry. However could possibly just refractoring the class work.

    Edit:

    I just got off my computer so I cant test it now but will tommrrow (aest)
     
  12. drives_a_ford

    Moderator

    I've not looked much into your code, but it's entirely possible. That's what I suggested after all:
     
  13. Your "plugin" field in your "Handler" class is null. In the constructor, you only set the value of the parameter to the value of the parameter.
     
  14. I.e, in the Handler constructor, change “plugin=plugin” to “this.plugin = plugin”


    Also, as a style note, make sure to use camelCase for naming variables (at one point you name a variable MobArena, which is also a class name. That could be complicating things as well.)
     
    • Agree Agree x 1
  15. I am not using the constructor I made it when testing a solution. and about class and package naming, I know about it now so I will do it from now on but you are not the first person to tell me about it.

    edit: I will remove the constructor.
     
  16. it didnt work