PlayerMoveEvent not working

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

  1. Hello, help me please, I can't use the PlayerMoveEvent because as soon as the the player moves, when the event is canceled, the player returns to his initial place and this is considered as a move so 1 little move => infinity of PlayerMoveEvent...
    Code (Text):
    @EventHandler
        public void onPlayerMove(PlayerMoveEvent event) {
            Player Player = event.getPlayer();
            if(Player.getWorld().getName().equals("teamkingdom")&&IsInGame(Player)){
                //Now, let's search if GameDatas.Players has 1 player in it
                String playtip="";
                int playcount=0;
                Set<String> SetPlayers = getConfig().getConfigurationSection("GameDatas.Players").getKeys(false);
                for(String player: SetPlayers){
                    playtip = player;
                    playcount=playcount+1;
                }
               //it means : "if GameDatas.Players has 1 player in it"
                if(playcount==1&&!playtip.equals("")){
                    event.getPlayer().sendMessage(ChatColor.RED + " WAIT !!!!");
                    event.setCancelled(true);//it works but implies directly an other onPlayerMove() :(
                }
            }
        }
    So I can't manage to cancel it properly :(
     
  2. Cancelling the event doesn't cause another one to be fired. What evidence do you have of this happening?
     
  3. As soon as I move (1 key press then I do nothing more) I have an epileptic crisis with the message WAIT flooding the chat :p

    EDIT : solved
    Code (Text):
     if (event.getFrom().getX() != event.getTo().getX() || event.getFrom().getY() != event.getTo().getY() || event.getFrom().getZ() != event.getTo().getZ()) {
                            Location loc = event.getFrom();
                            event.getPlayer().teleport(loc.setDirection(event.getTo().getDirection()));
     
    #3 to175, Jul 14, 2015
    Last edited: Jul 14, 2015
    • Like Like x 1
  4. You could've just done event.setTo(event.getFrom());, which would cause a lot less issues in the long run.
     
  5. It says solved, but I do

    player.teleport(event.getFrom())
     
  6. Firing teleport events from the move event is not an appropriate way to solve this
     
  7. Thanks, but I have the same issue with :
    Code (Text):
    double x = event.getPlayer().getLocation().getX();
                    double z = event.getPlayer().getLocation().getZ();
                    double distance = Math.sqrt(x*x+z*z);
                    long distancei = Math.round(distance);
                    if(distancei%10==0){//TODO tester ça
                        event.getPlayer().sendMessage(ChatColor.GRAY + " Distance from enemy : "+distancei);
                    }
    The message is spam ! :(
     
  8. To check distance do

    Code (Text):
    Location#distance(location) // Returns a double.
     
    • Like Like x 1
  9. Ok thanks :) but how to not be spamed with the message ?
     
  10. Well 1, make sure you only check for walking movements, not looking movements. Everytime you slightly move your mouse, if you're in the correct range, it will send the message.

    2. Switch to the distance method, I feel like the Math you did was incorrect causing the spam, but tell me what happens when you switch to distance method and check only if they walk not look.
     
  11. That's right, but Location#distanceSquared(location) is much better for the performance which is important in the move event.
     
    • Agree Agree x 1
  12. Thanks :) How to verify the movement and not the look ?
     
    #12 to175, Jul 15, 2015
    Last edited: Jul 15, 2015
  13. I did it :) but the message is spamed 4 times even if look is not checked !
    Code (Text):
                    if (event.getFrom().getX() != event.getTo().getX() || event.getFrom().getY() != event.getTo().getY() || event.getFrom().getZ() != event.getTo().getZ()) {
                        World world = Bukkit.getServer().getWorld(worldName);
                        Location location1 = new Location(world, event.getFrom().getX(), event.getFrom().getY(), event.getFrom().getZ());
                        Location location2 = new Location(world, -24.5, 72.0, -13.5);;
                     
                        String team = getConfig().getString("GameDatas.Players."+event.getPlayer().getName()+".Team");
                        Set<String> SetTeams = getConfig().getConfigurationSection("GameDatas.Teams").getKeys(false);
                        Set<String> SetHearts = getConfig().getConfigurationSection("GameDatas.Teams").getKeys(false);
     
                        for(String Team:SetTeams){
                            if(!team.equals(Team)){
                                SetHearts = getConfig().getConfigurationSection("GameDatas.Teams."+Team+".Hearts").getKeys(false);
                                for(String Heart: SetHearts){
                                    if(getConfig().getString("GameDatas.Teams."+Team+".Hearts."+Heart).equals("true")){
                                        String StringHeartsXYZ = getConfig().getString("TeamsConfig."+Team+".Hearts."+Heart);
                                        String[] HeartsXYX = StringHeartsXYZ.split("\\$");//les coordonnées de ce coeur
                                        String HeartX = HeartsXYX[0];
                                        String HeartY = HeartsXYX[1];
                                        String HeartZ = HeartsXYX[2];
                                        location2 = new Location(world, Integer.decode(HeartX), Integer.decode(HeartY), Integer.decode(HeartZ));
                                        int distancei = (int) Math.round(location1.distance(location2));
                                        if(distancei%10==0){
                                            String EnemyDistance = getConfig().getString("EnemyDistance");//"Aaaaaaaarrrrg ça fait mal !"
                                            event.getPlayer().sendMessage(ChatColor.GRAY + " "+EnemyDistance+" : "+distancei);
                                        }
                                        return;
                                    }
                                }
                            }
                        }
                    }