Solved Event isn't working

Discussion in 'Plugin Development' started by danichef, Mar 25, 2017.

Thread Status:
Not open for further replies.
  1. Hi I'm coding a plugin and I want it to cancel an event while happening in this case a pocion and a firework but it seems that it isnt working and theres no error in console that gives me a clue.
    Code:
        @EventHandler(priority=EventPriority.HIGH)
        public void onPlayerInteractEvent(PlayerInteractEvent e)
        {
            if(e.getItem() == null)
            {
                return;
            }
            else if((e.getItem().getType().equals(Material.POTION)) && (!(e.getPlayer().hasPermission("sc.pocion"))))
            {
                e.setCancelled(true);
               
            }
            else if((e.getItem().getType().equals(Material.FIREWORK)) && (!(e.getPlayer().hasPermission("sc.firework"))))
            {
                e.setCancelled(true);
               
            }
     
  2. Offline

    timtower Administrator Administrator Moderator

    @danichef Did you register the event?
    And enums are compared with ==
     
  3. Yes I did:
    Code:
    getServer().getPluginManager().registerEvents(new BanHandStuff(), this);
     
  4. Offline

    mehboss

    @danichef
    If you compare using enums (==) instead of .equals, it should work.
     
    Last edited: Mar 25, 2017
  5. Offline

    ipodtouch0218

    @mehboss
    You do need a null check, what if they right click with no item?
     
  6. Offline

    mehboss

    Nothing would happen?
     
  7. Offline

    ipodtouch0218

    @mehboss
    PlayerInteractEvent fires even if the player is holding no item, so the null check is required.
     
    mine-care likes this.
  8. Still doesen't work. Anything else I could try?
     
  9. Offline

    mine-care

    @danichef
    "Doesnt work" is interpreted as? Error? Code not executed?

    The only difference between the two is that .equals will throw a NullPointerException if the left-most reference is null, so for example:
    Code:
    SomeEnum e = null;
    e.equals(SomeEnum.SOME_VALUE);
    
    This errors cause e = null and .equals is a method invoked on this null reference.
    In that situation == would work just fine returning false.
    However .equals works fine with null arguments:
    Code:
    SomeEnum e = null;
    SomeEnum.SOME_VALUE.equals(null);
    
    This would simply return false which makes sense if you read the code behind .equals for Enums:
    Code:
    public final boolean equals(Object other) {
          return this==other;
    }
    
    Now having said all that i dont think that .getType() would return null rather than a Material in any case so .equals should be just fine :p

    I think the OP has to do some debuging himself to track down the issue and work a bit on the logic of the conditional statement ;)
     
  10. I got other way to do it anyways thx!
     
Thread Status:
Not open for further replies.

Share This Page