PlayerMoveEvent Not Working

Discussion in 'Spigot Plugin Development' started by xSinclare, Aug 14, 2017.

  1. I've been working out this little bug with my plugin for awhile now, but can't seem to understand why the PlayerMoveEvent does not work. Here's my code for it using the EventHandler:

    Code (Text):
        public void onPlayerMove(PlayerMoveEvent e) {
            Player target = e.getPlayer();
            if (frozen.contains(target.getName())) {
                //e.setTo(e.getFrom());
                e.setCancelled(true);
                target.sendMessage(ChatColor.RED + "You have been frozen! Please join the Discord server in the next 5 minutes! Logging out or refusing to screenshare will result in a permanent ban!" );
            }
        }
    Any help would be appreciated. And I also wanted to add on the events (using the EventHandler) in general are not working.
     
  2. Can you post all of the code in full this really for both the listner and the mai class.

    Also can tell you are missing
    @EventHandler(priority = EventPriority.NORMAL) at the top of your event.
     
  3. Code (Text):
    public class myScreenshare extends JavaPlugin {

        public static myScreenshare plugin;

        public void onEnable () {
            System.out.println("Plugin enabled!");

            plugin = this;
            //
            getCommand("freeze").setExecutor(new cmdFreeze());
        }

        public void onDisable() {

            System.out.println("Plugin disabled!");
            plugin = null;
        }

    }
     
  4. Any errors? if not, you probaly havnt registered the event.
    To do that do:
    Code (Text):
    Bukkit.getServer().getPluginManager().registerEvents(this, this);
    (do it in ur onEnable)
     
  5. Setting the event to cancelled is a bad idea, just set the e#getTo() to e.getFrom(). You'll see when you register your events. :)
     
  6. I've never had to register an event while using the @EventHandler method before. I don't understand how that could now be an issue.
     
  7. Um, everysingle time you make an event you have to register it. Just try it.
     
  8. Didn't work just how I expected.
     
  9. Oh hun, try to register it for your Event class. Here is an example:
    Code (Text):
    Bukkit.getServer().getPluginManager().registerEvents(new EventClass(this), this);
    Only include the this in the new EventClass(), if you have a constructor for it!
     
  10. Have you added @EventHandler aswell?

    EDIT: update your code aswell please
     
  11. I have. Of all the plugins I have made in the past all I need is @EventHandler and the event properly registers and works from there.
     
  12. You havnt implemented listener
     
  13. Unsure of how that is possible? But, as far as I’ve ever known and currently know, you have to register the events. Don’t forget to make sure your classes are extending/implementing the right stuff.

    E.g. public class Main extends JavaPlugin {} or public clas Events Implements Listener
     
  14. If he did that, he would have gotten and error and wouldn't be able to compile, all depending on his IDE of course though, and all that depending on if he used a separate class, which I'm pretty sure he did. Just not sure how he registered other events then...
     
    • Agree Agree x 2
  15. Exactly.....
     
  16. I don't need to implement the listener in the main class using the @EventHandler method.

    I've checked. I have two classes. My main class and the class for my screenshare command. I have two events in my screenshare command class using the @EventHandler method. Everything is being implemented/executed correctly.


    Not using a separate class. Using the @EventHandler method.
     
  17. Post your full code. You’re leaving something out — no other reason for running plugin, w/ no errors, to not execute an event as thought out to.
     
  18. You HAVE to implement listener every time you use @EventHandler.... i think you need to learn up on some java
     
  19. Unless Spigot has made some major API changes I'm not aware of I don't know why I'm having this issue all of a sudden. Here's the full code for both of my classes:

    Main Class:
    Code (Text):
    package us.thenerdcave;

    import org.bukkit.Bukkit;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    import us.thenerdcave.Commands.cmdFreeze;

    /**
     * Created by ANTHONY on 08/13/2017.
     */
    public class myScreenshare extends JavaPlugin implements Listener {

        public static myScreenshare plugin;

        public void onEnable () {
            System.out.println("Plugin enabled!");

            plugin = this;
            //
            Bukkit.getServer().getPluginManager().registerEvents(new cmdFreeze(), this);
            //
            getCommand("freeze").setExecutor(new cmdFreeze());
        }

        public void onDisable() {

            System.out.println("Plugin disabled!");
            plugin = null;
        }

    }
     
    Freeze Class:
    Code (Text):
    package us.thenerdcave.Commands;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;

    import java.util.ArrayList;

    /**
     * Created by ANTHONY on 08/13/2017.
     */
    public class cmdFreeze implements CommandExecutor, Listener {

        private ArrayList<String> frozen;

        {
            frozen = new ArrayList<>();
        }

        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel,
                                 String[] args) {

            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "This command is for players only!");
                return true;
            }

            //Player p = (Player) sender;

            if (args.length == 0) {
                sender.sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "ERROR » " + ChatColor.RED + "Correct Usage: /freeze <player>");
                return true;
            }

            Player target = Bukkit.getServer().getPlayer(args[0]);
            if (target == null) {
                sender.sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "ERROR » " + ChatColor.RED + "Could not find player " + args[0]);
                return true;
            }

            if (target == sender) {
                sender.sendMessage(ChatColor.GOLD + "" + ChatColor.BOLD + "ERROR » " + ChatColor.RED + "You cannot freeze yourself!");
                return true;
            }

            if (frozen.contains(target.getName())) {
                frozen.remove(target.getName());
                target.sendMessage(ChatColor.RED + "You have been unfrozen!");
                sender.sendMessage(ChatColor.GRAY + "Player " + ChatColor.GOLD + target.getName() + ChatColor.GRAY + " has been unfrozen!");
                for (PotionEffect potionEffect  : target.getActivePotionEffects()) {
                    target.removePotionEffect(potionEffect.getType());
                }
                return true;
            }
            frozen.add(target.getName());
            sender.sendMessage(ChatColor.GRAY + "Player " + ChatColor.GOLD + target.getName() + ChatColor.GRAY + " has been frozen!");
            target.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 72000, 10));

            return true;
        }

        @EventHandler
        public void onPlayerMove(PlayerMoveEvent e) {
            Player target = e.getPlayer();
            if (frozen.contains(target.getName())) {
                e.setTo(e.getFrom());
                //e.setCancelled(true);
                target.sendMessage(ChatColor.RED + "You have been frozen! Please join the Discord server in the next 5 minutes! Logging out or refusing to screenshare will result in a permanent ban!" );
            }
        }

        @EventHandler
        public void onPlayerQuit(PlayerQuitEvent e) {
            Player target = e.getPlayer();
            if (frozen.contains(target.getName())) {
                Bukkit.broadcastMessage(ChatColor.GRAY + "Player " + ChatColor.GOLD + target.getName() + ChatColor.GRAY + " has logged out while frozen.");
            }
            frozen.remove(target.getName());
        }
    }
    If that is how it is then sure I will do that. But in the past when I didn't implement the listener in the main class when using @EventHandler I've never had an issue with it until now.
     
  20. Jups implement Listener, Register the Event listener, and have the event with a @EventHandler
     

Share This Page