PlayerMoveEvent not working

Discussion in 'Spigot Plugin Development' started by Vytrix, Jun 30, 2018.

  1. Take a look at my code, when I hop on my server the event does not work. Can someone tell me what is wrong? I've tried looking on other forums, with people that have the same problem, and I can not figure out why this is not working. Thanks.

    This is in a old version of bukkit, b1.7.3 to be specific. I hope that I can still get some help on here.

    Code (Java):
    package me.taylor.zPlayerMove;

    import org.bukkit.event.Event;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin {

        private final MoveClass movelistener = new MoveClass();

        @Override
        public void onEnable() {

            PluginManager pm = getServer().getPluginManager();
            pm.registerEvent(Event.Type.PLAYER_MOVE, this.movelistener, Event.Priority.Normal, this);

        }

        @Override
        public void onDisable() {

        }

    }
     
    Code (Java):
    package me.taylor.zPlayerMove;

    import org.bukkit.entity.Player;
    import org.bukkit.event.player.PlayerListener;
    import org.bukkit.event.player.PlayerMoveEvent;

    public class MoveClass extends PlayerListener {

        public void onMove(PlayerMoveEvent e) {
            Player p = e.getPlayer();
            p.sendMessage("test");
        }

    }
     
     
  2. Implement Listener in your main class and I would recommend not using PlayerListener instead just implement Listener in your MoveClass
     
    • Agree Agree x 1
    • Agree Agree x 1
  3. Try
    public class MoveClass extends PlayerListener {
    @EventHandler
    public void onMove(PlayerMoveEvent e) {
    Player p = e.getPlayer();
    p.sendMessage("test");
    }

    }
     
  4. Why implement Listener in main class
     
  5. Yeah, there is no need to lol
     
  6. Just do pm.registerEvents (this, new MoveClass ());
    Im on my phone maybe the args needs to be swapped :shrug:
     
  7. Yea I guess that is true. I do it whenever I am making a plugin that will ever use listeners because I have to register the events and such. My bad got caught up in my own practices... xD
     
  8. pls dont
     
  9. It was how I was taught. Maybe I picked up bad practices but it hasn't ever affected my plugins negatively?
     
  10. Yes but why bother doing unneeded things? ;)
     
  11. Because unneeded things can sometimes make code look cleaner and more readable, can make debugging easier, and have zero negative effect on the result, as there it probably gets optimized away... Well, maybe this is not the case for implementing the listener class ;) but for some things...

    First of all, a tip. You should probably move away from 1.7! Version 1.13 is around the corner, and I think, after years, most servers switched to the post 1.8 updates. Moreover, Bukkit 1.7 is programmed and compiled on Java 7, whilst Oracle dropped support for Java 7 since 2015.

    Second of all, I highly recommend that you use the `registerEvents` which makes registering event listeners way easier. It gives you the option to just put methods in one class, and register all methods of that class instantly, without having to call the `registerEvent` method for every listener. It is heavily based upon the Java annotations API, so if you never used that, you might want to learn a little about that, but it is pretty straight froward!
     
  12. Meeeeeehh