Solved EventListeners don't listen

Discussion in 'Plugin Development' started by PluginMan100, May 24, 2014.

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

    PluginMan100

    Greetings. I have problem with EventListeners. They don't react on events:
    Code:java
    1. public void onEnable() {
    2. ...
    3. getServer().getPluginManager().registerEvents(new ToolListener(), this);
    4. }

    ToolListener:
    Code:java
    1. @EventHandler(priority = EventPriority.HIGH)
    2. public void onPlayerMove(PlayerMoveEvent e) {
    3. if (e instanceof Player){
    4. if (((Player) e).getInventory().getHelmet().getItemMeta().getDisplayName() == ChatColor.YELLOW+"Glowstone Hat") {
    5. Player p = (Player) e;
    6. ((Player) e).addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, 2, 1));
    7. }
    8. }
    9. }
    10.  
    11. @EventHandler
    12. public void onPlayerInteract(PlayerInteractEvent e) {
    13. if (e instanceof Player) {
    14. if (e.getItem().getItemMeta().getDisplayName() == ChatColor.GOLD+"Fireball Gun" && e.getAction() == Action.RIGHT_CLICK_AIR) {
    15.  
    16. if (((Player) e).getInventory().contains(Material.FIREBALL)) {
    17. ((Player) e).launchProjectile(Fireball.class);
    18. ((Player) e).getInventory().remove(Material.FIREBALL);
    19.  
    20. } else {
    21. ((Player) e).sendMessage(ChatColor.RED+"Out of fireballs!");
    22. }
    23.  
    24. }
    25. }
    26. }

    What's wrong?
     
  2. Offline

    Westini

    You are checking if the event is an instance of a player, which the event itself will never be, you just have to use e.getPlayer() to get the according player ;)
     
    PluginMan100 likes this.
  3. Your putting if the event is an instance of a Player.
     
    PluginMan100 likes this.
  4. Offline

    PluginStudios

    Indeed you are. Change it to if e.getEntity() instanceof Player
     
    PluginMan100 likes this.
  5. Offline

    AronTheGamer

    After that:
    - You are first checking if the e.getItem() has a certain displayname before you event check if the player right clicks (&& means: first check the thing before the &&, and if its true the thing behind the &&)

    e.getItem() can be null, because if a player stands on a pressureplate there's no item used. My tip:
    First check for e.getAction (maybe you need both RIGHT_CLICK_AIR an RIGHT_CLICK_BLOCK)
     
    PluginMan100 likes this.
  6. Offline

    PluginMan100

    Thanks a lot.
     
  7. Why do that when the events already contain player in the name, so the entity will be a player regardless.
     
Thread Status:
Not open for further replies.

Share This Page