PlayerMoveEvent and AsyncPlayerChatEvent seem to have no effect[Solved]

Discussion in 'Spigot Plugin Development' started by suitinacow, Jul 14, 2015.

  1. Code (Text):

    public class UnAfk implements Listener {

        private final Afk afk;
        public UnAfk(Afk afk) {
            this.afk = afk;
        }
     
        @EventHandler(priority = EventPriority.HIGHEST)
        public void moving(final PlayerMoveEvent event){
            Player player = (Player) event.getPlayer();
            if(afk.playerAFK.contains(player)){
                if(player.getVelocity() != null){
                    afk.playerAFK.remove(player);
                    Bukkit.getServer().broadcastMessage(ChatColor.BLUE + player.getName()+ ChatColor.DARK_PURPLE + "is no longer AFK");
                    player.sendMessage("Hi");
                }
            }
        }
     
        @EventHandler(priority = EventPriority.HIGHEST)
        public void chat(final AsyncPlayerChatEvent event){
            Player player = (Player) event.getPlayer();
            if(afk.playerAFK.contains(player)){
                afk.playerAFK.remove(player);
                Bukkit.getServer().broadcastMessage(ChatColor.BLUE + player.getName()+ ChatColor.DARK_PURPLE + "is no longer AFK");
                player.sendMessage("Hi");
            }
        }

    }

     
    these two codes are supposed to check when the player moves or chats to turn off the afk label for the player in a custom afk plugin
     
  2. Put a message as soon as the event is fired to check that the event is even firing, and I don't know how good you are with plugins but I do this too... Register your listener in your main class (everyone makes this mistake sometimes, just checking)

    And remove the final from the event, it shouldnt need the final to work and the final might be what's breaking it
     
  3. I got the registerevents down yep, and i tested your idea about removing final and adding a message and it doesn't seem to fire.. could be the eventpriority
     
  4. Yup it was the eventpriority.. i set it to MONITOR and the messages worked fine but the code afterwards didn't
     
  5. Mark solved?
     
  6. well the code afterwards didn't workout and how do i mark it solved?
     
  7. At the top, thread tools, change title and add a [SOLVED] to the title :)
     
    • Like Like x 1
  8. it must be array list to do that
     
  9. Don't forget to set this to [SOLVED].
     
  10. still not solved.. but trying :p

    can it be a hashSet? because playerAFK is an hashSet atm
     
  11. heres my /afk class if that helps
    Code (Text):
    package commands;

    import java.util.ArrayList;
    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 main.PluginHandler;

    public class Afk implements CommandExecutor {

        private final PluginHandler pluginHandler;
        public Afk(PluginHandler pluginHandler) {
            this.pluginHandler = pluginHandler;
        }
        public ArrayList<Player> playerAFK = new ArrayList<Player>();

        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if(!(sender instanceof Player))
            {
                return true;
            }
            Player player = (Player) sender;
            boolean secondTime = false;
           
            if(pluginHandler.safeTimes.keySet().contains(player.getName())){
                player.sendMessage(ChatColor.RED + "You can't afk when you're in battle!");
                return true;
            }else{
                if(args.length == 0){
                    if(!secondTime){
                        Bukkit.getServer().broadcastMessage(ChatColor.BLUE + player.getName()+ ChatColor.DARK_PURPLE + " is now AFK");
                        playerAFK.add(player);
                        secondTime = true;
                        return true;
                    }else{
                        Bukkit.getServer().broadcastMessage(ChatColor.BLUE + player.getName()+ ChatColor.DARK_PURPLE + " is no longer AFK");
                        playerAFK.remove(player);
                        secondTime = false;
                        return true;
                    }
                }else{
                        player.sendMessage(ChatColor.RED + "Don't put anything in front of /afk!");
                        return true;
                }
            }
        }
    }
     
  12. @suitinacow you have a memory leak. Store UUIDs or remove players from your list when they log out.
     
  13. You'd have to remove the UUIDs when they log out too, otherwise same problem.