1.13.2 @EventHandler method not called

Discussion in 'Spigot Plugin Development' started by Csabinho, Jul 1, 2021.

  1. I'm quite a newbie in plugin development and tried to use an @Eventhandler method, but it wasn't called.

    My code looks like this:
    Code (Text):

    import org.bukkit.Effect;
    import org.bukkit.Location;
    import org.bukkit.Particle;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.event.player.PlayerMoveEvent;

    import java.util.logging.Level;

    public final class Jump extends JavaPlugin implements Listener {

        //Copied from https://www.spigotmc.org/threads/player-jump.61335/#post-676056
        @EventHandler //(priority = EventPriority.HIGHEST)
        public void onJump(PlayerMoveEvent event) {
            getLogger().log(Level.INFO,"onJump was called!");
            /*...*/
        }

        @Override
        public void onEnable() {
            // Plugin startup logic
            getLogger().log(Level.INFO,"Jump was enabled!");
            getServer().getPluginManager().registerEvents(this, this);

        }

        @Override
        public void onDisable() {
            // Plugin shutdown logic
        }
    }
     
     
    #1 Csabinho, Jul 1, 2021
    Last edited: Jul 1, 2021
  2. What exactly happens? Does the log message appear? Does the jump cancel? And what exactly DOESN'T happen? This code looks fine.
     
  3. First, try to see if this @EventHandler is really Bukkit's and not Bungee's, and second, try to register the event like this: Bukkit.getPluginManager().registerEvents(this, this);
     
  4. getServer() is fine, I use it all the time. But your first advice might be useful.
     
  5. The "enabled" log message is shown, the other one isn't, even if I jump!

    How do I do this?

    I added my imports above!
     
  6. Hm, the code seems fine to me. Are there any errors by chance?
     
  7. Do you mean build errors? If so, sadly no!
     
  8. Something really strange happened.
    I've just added this to my code to see if it works:
    Code (Text):

        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event) {
            event.getPlayer().sendMessage("Heyyyyyyy");
        }
     
    and now the other EventHandler works as well. I didn't change anything else. I don't really get it, but hey, it works! :D
     
  9. Sometimes a fresh compile solves things - don't ask why, just be glad it does.
     
    • Like Like x 1
    • Funny Funny x 1
  10. He pasted the full code so you can see that he's using annotations from bukkit.

    He's using static imports. They have excactly the same result as yours. Also there's no need to call #getPluginManager() over the Bukkit class. He's doing everything correctly when it comes to registering listeners.
     
    #10 MerryChrismas, Jul 1, 2021
    Last edited: Jul 1, 2021
  11. He didn't have imports until we asked for them.
     
    • Like Like x 1
  12. Yes of course. But knowing the reason would be really great as well... :D
    Sorry for that!
     
  13. Add the event below to onDisable because it is above onEnable so it doesn't work
     
    • Funny Funny x 1
  14. What? *confused*
     
  15. Since these are functions it doesn't matter where they're placed - putting onEnable before onDisable is just for aesthetics.
    It usually only matters if you're creating variables (and even then mostly static) and using them before they're defined.
     
  16. Sorry, but that's nonsense.
    This isn't Python.
     
    • Agree Agree x 2
  17. Chill mate... they got something wrong, it isn't the end of the world...