Solved Custom event cannot get handler list.

Discussion in 'Plugin Help/Development/Requests' started by meguy26, Feb 27, 2015.

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

    meguy26

    When i start my server, my plugin's custom event runs a stack trace that says it it cannot get the handler list.

    Code for event:
    Code:
    package com.gamil.neonblue858.itemrestrict.api.events;
    
    import org.bukkit.entity.Player;
    import org.bukkit.event.Cancellable;
    import org.bukkit.event.Event;
    import org.bukkit.event.HandlerList;
    import org.bukkit.inventory.ItemStack;
    
    import com.gamil.neonblue858.itemrestrict.api.enums.RestrictState;
    
    
    public class ItemRestrictOnInteractEvent extends Event implements Cancellable {
        private static final HandlerList handlers = new HandlerList();
        private boolean cancelled = false;
        private RestrictState state;
        private Player player;
        private ItemStack itemInHand;
    
        public ItemRestrictOnInteractEvent(Player player, ItemStack itemInHand){
            this.player = player;
            this.itemInHand = itemInHand;
        }
       
    
        public boolean isCancelled() {
            return cancelled;
        }
    
    
        public void setCancelled(boolean setCancelled) {
            cancelled = setCancelled;
        }
    
        public HandlerList getHandlers() {
            return handlers;
           
        }
       
        public HandlerList getHandlerList() {
            return handlers;
           
        }
    
        public void setRestrictState(RestrictState newState){
            state = newState;
        }
       
       
        public RestrictState getRestrictState(){
            return state;
        }
       
        public Player getPlayer(){
            return player;
        }
       
        public ItemStack getItemUsedOnInteract(){
            return itemInHand;
        }
       
    }
    please help...
     
  2. Offline

    Tecno_Wizard

    @meguy26, we need the actual stack trace.
     
  3. Offline

    The Fancy Whale

    --Snipped for wrong answer--
     
    Last edited: Feb 27, 2015
  4. Offline

    meguy26

    Techno_Wizard
    Capture.PNG

    Fancy Whale:

    http://wiki.bukkit.org/Event_API_Reference <-- here, in the example, @overide is not used. Alos, using it makes no difference.

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

    Tecno_Wizard

    Doesn't matter. @Override does not compile
     
    The Fancy Whale likes this.
  6. Offline

    The Fancy Whale

    Looks like an error with registering events...
     
  7. Offline

    meguy26

    how so?
     
  8. Offline

    The Fancy Whale

    What is line 64 of Main.java?
     
  9. Offline

    meguy26

    in the event API reference it says to create the event with "
    CustomEvent event = new CustomEvent();"

    Should this go in the on enable?

    line 64 is: " getServer().getPluginManager().registerEvents(new MyListener(), this);"

    line 64 is in on enable...

    in the previous post, i meant should the event creation go in onenable

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

    The Fancy Whale

    What do you mean the creation of the event?
     
  11. Offline

    Tecno_Wizard

    @meguy26, hmm. Check the custom event documentation again. It is definitely failing to initialize the event.
     
  12. Offline

    meguy26

    "
    // Create the event here
    CustomEvent event = new CustomEvent("Sample Message");"
    - Event API Referecne

    Should i put my constructor for my event in the onEnable();
     
  13. Offline

    Tecno_Wizard

    @meguy26, no, i'm fairly sure the issue is within the event itself.
     
  14. Offline

    The Fancy Whale

    What other events are you listening to in the MyListener class?
     
  15. Offline

    meguy26

    playerinteract, playerpickupitem, playerdamageentity
    ill try removing those and seeing what happens
     
  16. Offline

    Tecno_Wizard

    @The Fancy Whale, null pointer would make me think that there's an issue with the handlers.

    1. public HandlerList getHandlers() {
    2. return handlers;
    3. }
    4. public HandlerList getHandlerList() {
    5. return handlers;
    6. }
    The fact that there is no setter makes me suspicious.
     
  17. Offline

    The Fancy Whale

    Yeah I'm just confused as I use super similar code for a custom event I'm working on and have tested to work fine.
    Code:
    public class PlayerJoinArenaEvent extends Event implements Cancellable {
    
        private boolean isCancelled;
        private String arenaName;
        private Player player;
        private static final HandlerList handlers = new HandlerList();
        
        @Override
        public HandlerList getHandlers() {
            return handlers;
        }
        
        public static HandlerList getHandlerList() {
            return handlers;
    
        }
       
    
        public PlayerJoinArenaEvent(Player p, String arena){
            this.arenaName = arena;
            this.player = p;
            this.isCancelled = false;
        }
       
        @Override
        public boolean isCancelled() {
            return this.isCancelled;
        }
    
        @Override
        public void setCancelled(boolean arg0) {
            this.isCancelled = arg0;
        }
    
    
        public String getArena(){
            return this.arenaName;
        }
       
        public Player getPlayer(){
            return this.player;
        }
    }
     
  18. Offline

    Tecno_Wizard

    @The Fancy Whale, but are you running with spigot or bukkit? spigot might be incompatible.
     
  19. Offline

    The Fancy Whale

    Running with spigot.
     
  20. Offline

    meguy26

    me too
     
  21. Offline

    Tecno_Wizard

    @meguy26, hmm, I really don't know then.
    I'll go decompile spigot and see what that line is

    This threw the error
    Code:
    private HandlerList getEventListeners(Class<? extends Event> type)
      {
        try
        {
          Method method = getRegistrationClass(type).getDeclaredMethod("getHandlerList", new Class[0]);
          method.setAccessible(true);
          return (HandlerList)method.invoke(null, new Object[0]);
        }
        catch (Exception e)
        {
          throw new IllegalPluginAccessException(e.toString());
        }
      }
     
  22. Offline

    meguy26

    Ok... So what i do not understand is:
    When the event is defined with the exact example from the Event API Reference:
    Code:
    public final class CustomListener implements Listener {
    org.bukkit.event.Event;
    import org.bukkit.event.HandlerList;
    import org.bukkit.event.Cancellable;
    public final class CustomEvent extends Event implements Cancellable {
        private static final HandlerList handlers = new HandlerList();
        private String message;
        private boolean cancelled;
        public CustomEvent(String example) {
            message = example;
        }
        public String getMessage() {
            return message;
        }
        public boolean isCancelled() {
            return cancelled;
        }
        public void setCancelled(boolean cancel) {
            cancelled = cancel;
        }
        public HandlerList getHandlers() {
            return handlers;
        }
        public static HandlerList getHandlerList() {
            return handlers;
        }
    }
    No error is throw, but when i use my event:
    Code:
    package com.gamil.neonblue858.itemrestrict.api.events;
    
    import org.bukkit.entity.Player;
    import org.bukkit.event.Cancellable;
    import org.bukkit.event.Event;
    import org.bukkit.event.HandlerList;
    import org.bukkit.inventory.ItemStack;
    
    import com.gamil.neonblue858.itemrestrict.api.enums.RestrictState;
    
    
    public class ItemRestrictOnInteractEvent extends Event implements Cancellable {
        private static final HandlerList handlers = new HandlerList();
        private boolean cancelled = false;
        private RestrictState state;
        private Player playerf;
        private ItemStack itemInHandf;
    
        public ItemRestrictOnInteractEvent(Player player, ItemStack itemInHand){
            playerf = player;
            itemInHandf = itemInHand;
        }
      
    
        public boolean isCancelled() {
            return cancelled;
        }
    
    
        public void setCancelled(boolean setCancelled) {
            cancelled = setCancelled;
        }
    
        public HandlerList getHandlers() {
            return handlers;
          
        }
      
        public HandlerList getHandlerList() {
            return handlers;
          
        }
    
        public void setRestrictState(RestrictState newState){
            state = newState;
        }
      
      
        public RestrictState getRestrictState(){
            return state;
        }
      
        public Player getPlayer(){
            return playerf;
        }
      
        public ItemStack getItemUsedOnInteract(){
            return itemInHandf;
        }
      
    }
    
    Throws an error.

    Umm... I found the error!
    Code:
        public HandlerList getHandlerList() {
            return handlers;
         }
    in my event, it needs to be static as it is in the example event, thank you all for your help!

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

    Tecno_Wizard

    @meguy26, not a problem. That would explain why that was happening. It was trying to invoke a non-existent method.
     
  24. Offline

    meguy26

    @timtower
    I do not no if it was you that moved this post, but your the only admin i know so:
    Why would this be moved to bukkit alternatives? It discusses code solely from the bukkit API, the solution and the easy-to-make mistake applies to all code, the only alternative nature is that it was tested on spigot.
     
  25. Online

    timtower Administrator Administrator Moderator

    @meguy26 It wasn't me who moved it ( I was sleeping ), I am not an admin either ( I am a moderator )
    And because you are using spigot.
     
Thread Status:
Not open for further replies.

Share This Page