MoveEvent

Discussion in 'Plugin Development' started by TerroDoor, May 25, 2020.

  1. Offline

    TerroDoor

    Hi, been awhile.

    How can I check if a player is inside of an area without using the MoveEvent?

    basically when i reload my server i have an array that stores playernames but if i DON'T MOVE the player doesnt get added back into the list.
     
  2. Offline

    valithor2

    You could always use the join event to check if they are in the area whenever they first join. I would assume anyway they would get into the area after that would trigger a move event.
     
  3. Offline

    KarimAKL

    @TerroDoor You would have to loop the players and check if they're in the area. Do this in the onEnable.
     
  4. Offline

    Strahan

    Why not just use the move event? Yes, it fires a lot but you can filter it so the code only gets ran if they actually moved a block rather than simply moved their head.
     
  5. Offline

    KarimAKL

     
  6. Offline

    Strahan

    Ah, I must be blind heh.
     
  7. Offline

    TerroDoor

    @KarimAKL thanks for the response, Ive come up with a function that checks if a player's coords are in/out of the region and i have this check running every second for every player online, inside my onEnable().

    I mean, it's working and doing what it's supposed to do however i feel like it's using alot of memory.. and inbetween ticks and updating the player lists it sometimes doesn't register in time..

    Code:
        public void onEnable() {
           
            Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
               
                public void run() {
                   
                    for (Player pl : Bukkit.getOnlinePlayers()) {
                       
                        PlayerMove.checkLoc(pl);
                       
                    }
                   
                }
               
            }, 0L, 20L); 
    
    Code:
        public void checkLoc(Player p) {
            int px = p.getLocation().getBlockX();
            int py = p.getLocation().getBlockY();
            int pz = p.getLocation().getBlockZ();
            if (px >= minx && px <= maxx &&
                    py >= miny && py <= maxy &&
                    pz >= minz && pz <= maxz) {
                if (!prot.contains(p.getName())) {
                    prot.add(p.getName());
                }
            } else {
                if (prot.contains(p.getName())) {
                    prot.remove(p.getName());       
                }
            }
    
    @KarimAKL i'm also doing this because i need to manage AFK players inside of this region, that's why every second i need to update player locations. Could i also use the checkLoc method in a move event to prevent errors inbetween ticks?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
  8. Offline

    KarimAKL

    @TerroDoor Just check for the last time they moved using PlayerMoveEvent, instead of having a Runnable check every second.
     
  9. Offline

    TerroDoor

    @KarimAKL but i need to check for the player's that arent moving? what about AFK players?
     
  10. Offline

    KarimAKL

    @TerroDoor Have the Runnable check the last time they moved, then update that time in the PlayerMoveEvent if they're inside the area.
     
  11. Offline

    TerroDoor

    @KarimAKL How do i check the last time they moved?
     
  12. Offline

    KarimAKL

    @TerroDoor Save System#currentTimeMillis() to a Map<UUID, Long> in your PlayerMoveEvent.
     

Share This Page