Solved Is my brain dead?

Discussion in 'Spigot Plugin Development' started by Plochem, Oct 16, 2018.

  1. Hi, I'm making a custom event that listens for when a player equips and unequips a skull. No errors appear in console, but the event isn't firing.

    The following code listens when to fire the custom event:
    Code (Java):
    public class SkullEquipListeners implements Listener{
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e){

           // Implemented code not shown
         
            if(e.getCurrentItem().getType() == Material.SKULL_ITEM && e.getSlot() == 39) { //removing skull from head
                SkullEquipEvent skullEquipEvent = new SkullEquipEvent((Player) e.getWhoClicked(), e.getCurrentItem(), SkullActionType.UNEQUIP);
                Bukkit.getServer().getPluginManager().callEvent(skullEquipEvent);
                System.out.println("here"); // this part runs
            } else if(e.getCursor().getType() == Material.SKULL_ITEM && e.getSlot() == 39) { //equipping skull on head
                SkullEquipEvent skullEquipEvent = new SkullEquipEvent((Player) e.getWhoClicked(), e.getCurrentItem(), SkullActionType.EQUIP);
                Bukkit.getServer().getPluginManager().callEvent(skullEquipEvent);
                System.out.println("here"); // this part runs
            }
        }
    }
     
    This is the SkullEquipEvent class:

    Code (Java):
    public class SkullEquipEvent extends PlayerEvent{
        private final HandlerList handlers = new HandlerList();
        private ItemStack skull;
        private SkullActionType type;
     
        public SkullEquipEvent(Player player, ItemStack skull, SkullActionType type){
            super(player);
            this.skull = skull;
            this.type = type;
        }

        public final HandlerList getHandlerList(){
            return handlers;
        }

        public HandlerList getHandlers(){
            return handlers;
        }

        public ItemStack getSkull() {
            return skull;
        }
     
        public SkullActionType getType() {
            return type;
        }

    }
    This is where I'm listening for when the event is fired:

    Code (Java):
    public class SkullAbility implements Listener{
     
        @EventHandler
        public void onSkullEquip(SkullEquipEvent e) {
             // implemented code not shown
             System.out.println("hi"); // this does not display
        }
    }
    Am I missing anything?
     
  2. What place is associated with the 39 slot?
     
    • Agree Agree x 1
  3. That's the slot number for the helmet in survival mode.

    Ok, I followed the tutorial, but the custom event still isn't firing. It only "fires" when I register the custom event in the PluginManager#registerEvents(Listener, Plugin), but NPEs errors are thrown.
     
  4. All classes that have an event need to be registered using PluginManager#registerEvents(Listener, Plugin)
    They also need to implement Listener. The events won't be listened to otherwise

    Not sure what you meant to say there.
     
    #5 Shin1gamiX, Oct 16, 2018
    Last edited: Oct 16, 2018
  5. nvm, im retarded. The NPE is another problem in the event that I just fixed lol