Getting Border of an Area

Discussion in 'Plugin Development' started by kampai, Jul 6, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    kampai

    Currently I have a playermoveevent that checks if a player enters an area then sends a message to them, but the problem is when they do it just spams the hell out of them and makes them lag out and the server. How would I only run the playermoveevent once or is there any better method?

    I was thinking of getting the perimeter of the area then when they enter add them to a hasahmap or arraylist (of course remove them from it when they leave or the server shuts down). I'm kind of confused on how to get perimeter.

    Code:
    Code:
            @EventHandler
            public void PlayerMove(PlayerMoveEvent event) {
                Player player = event.getPlayer();
                Location loc1 = new Location(Bukkit.getServer().getWorld("potentia"), 16,4,31);
                Location loc2 = new Location(Bukkit.getServer().getWorld("potentia"), -33,4,-8);
    
                int minx = Math.min(loc1.getBlockX(), loc2.getBlockX()),
                        minz = Math.min(loc1.getBlockZ(), loc2.getBlockZ()),
                        maxx = Math.max(loc1.getBlockX(), loc2.getBlockX()),
                        maxz = Math.max(loc1.getBlockZ(), loc2.getBlockZ());
                    for(int x = minx; x<=maxx;x++){
                        for(int y = 0; y<128;y++) {
                            for(int z = minz; z<=maxz;z++){
                               
                                player.sendMessage("You have entered a new area");
                            }
                        }
                    }
                }
    }
     
  2. Offline

    mine-care

    The move event fires many many many times. What you are doing here is to loop through all the blocks in this cuboid area and for each of them, send a message to that poor player.
    If we have an area that is 20*60 lets say, that means that the code above will run just over 153000 times, and will message the poor player that many. What are you trying to do with all these loops? Also keep in mind that this series of loops take some time to complete, and as i said the move event fires many times, infact it fires even when the player changes their pitch by the tiniest bit, so i would be surprised if you didnt experience lag on the server.

    Remove that, and optimize your move event by checking if the player moved by 1 block before performing all other checks. The way to check if they are in a region is given bellow as an outline:

    Code:
    X = playerLocationX
    Y = playerLocationY
    Z = playerLocationZ
    if X is between MAX X of the area and MIN X of the area then:
        if Y is between MAX Y of the area and MIN Y of the area then:
            if Z is between MAX Z of the area and MIN Z of the area then:
                //Player is in the area
            end if;
         end if;
    end if;
    
    P.s. Please follow Java naming conventions.
     
  3. Offline

    kampai

    @mine-care
    Thank you for your response! This is what I managed to come up with but it's not working, not sure what I did wrong.
    Code:
    @EventHandler
            public void onPlayerMove(PlayerMoveEvent event) {
                Player player = event.getPlayer();
                if(event.getFrom().getBlockX() != event.getTo().getBlockX() || event.getFrom().getBlockZ() != event.getTo().getBlockZ()) {
                    double x = player.getLocation().getX();
                    double y = player.getLocation().getY();
                    double z = player.getLocation().getZ();
                    int maxX = 16;
                    int minX = -33;
                    int maxZ = -31;
                    int minZ = -8;
                    int minY = 0;
                    int maxY = 21;
               
                    if(x > minX && x < maxX) {
                        if(y > minY && y < maxY) {
                            if(z > minZ && z < maxZ) {
                                player.sendMessage("You've enterted this area");
                           
                            }
    
                        }
    
                    }
               
    
                }
            }
    }
        
    EDIT: I realize that -8 is greater than -31 so thats where my problem was, but now when I enter the area it just spams me every block because I'm looping through to check if I'm in the area. Is there any way to check the perimeter from this.
    EDIT 2: I changed to check if the player is standing on wool blocks but that does seem to work

    EDIT3: Now I just need to see if the player is leaving or exiting the area, how would I do that, checking which direction the player is facting
     
    Last edited: Jul 6, 2016
  4. When the player enters the area, add him (uuid/name) to a List (if he isn't in it already) and send the message
    else remove him from the list

    Get the direction: player.getLocation().getDirection() returns the vector
     
  5. Offline

    kampai

    @FisheyLP
    Code:
            @EventHandler
            public void onPlayerMove(PlayerMoveEvent event) {
                Player player = event.getPlayer();
              
                if(event.getFrom().getBlockX() != event.getTo().getBlockX() || event.getFrom().getBlockZ() != event.getTo().getBlockZ()) {
                    double x = player.getLocation().getX();
                    double y = player.getLocation().getY();
                    double z = player.getLocation().getZ();
                  
                    int maxX = 17;
                    int minX = -34;
                    int maxZ = -7;
                    int minZ = -32;
                    int minY = 0;
                    int maxY = 21;
                  
                    if(x > minX && x < maxX) {
                        if(y > minY && y < maxY) {
                            if(z > minZ && z < maxZ) {
                                if(!kitcreation.contains(player.getName())) {
                                player.sendMessage("in area");
                                kitcreation.add(player.getName());
                                return;
                  
                                } else {
                                        player.sendMessage("out area");
                                        kitcreation.remove(player.getName());
                                    }
                                }
                              
                              
                        }
                    }
                }
                                 
                                 
                      
                    }
    
                              
                              
                          
    
                    }
    
                
    I dont see how to get if they left the area. I've tried see if they are not in that location and if they arent check if they are in the hashmap but that doesnt work, I'm now at a loss agian
     
    Last edited: Jul 7, 2016
  6. Offline

    ArsenArsen


    If getFrom is in the area but getTo isn't they are leaving. I don't think you need the first if.
     
  7. combine all 3 if's into 1 and put the else there
     
  8. Offline

    kampai

    @ArsenArsen
    I do that to make it not check as frequently.
    @FisheyLP
    I tried that and it doesnt work.
    Code:
          
            public ArrayList<String> kitcreation = new ArrayList<String>();
    
            @EventHandler
            public void onPlayerMove(PlayerMoveEvent event) {
                Player player = event.getPlayer();
           
                if(event.getFrom().getBlockX() != event.getTo().getBlockX() || event.getFrom().getBlockZ() != event.getTo().getBlockZ()) {
                    double x = player.getLocation().getX();
                    double y = player.getLocation().getY();
                    double z = player.getLocation().getZ();
               
                    int maxX = 17;
                    int minX = -34;
                    int maxZ = -7;
                    int minZ = -32;
                    int minY = 0;
                    int maxY = 21;
                  
                    boolean newx = x > minX && x < maxX;
                    boolean newy = y > minY && y < maxY;
                    boolean newz = z > minZ && z < maxZ;
    
                    if(x > minX && x < maxX) {
                        if(y > minY && y < maxY) {
                            if(z > minZ && z < maxZ) {
                                if(!kitcreation.contains(player.getName())) {
                                player.sendMessage("in area");
                                kitcreation.add(player.getName());
                                return;
               
                                } else {
                                    if(newx == false) {
                                        if(newy == false) {
                                            if(newz == false) {
                                                if(kitcreation.contains(player.getName())) {
                                        player.sendMessage("out area");
                                        kitcreation.remove(player.getName());
                                    }
                                            }
                                }
                            }
                        }
                    }
                           
                           
                        }
                    }
                }
                               
                               
                   
                    }
                      
    It only check if they entered the area but doesn't check if they leave.
     
  9. Code:
    if (x > minX && x < maxX) {
    if (y > minY && y < maxY) {
    if (z > minZ && z < maxZ) {
    if (!kitcreation.contains(player.getName())) {
            player.sendMessage("in area");
            kitcreation.add(player.getName());
            return;
            }
    }
    }
    }
    
    if (kitcreation.contains(player.getName())) {
            player.sendMessage("out area");
            kitcreation.remove(player.getName());
    }
    Try this.
     
  10. Offline

    kampai

    @FisheyLP
    Oddly enough that didnt work and I'm at a loss of what to do at this point.
     
  11. Put the return; 2 lines below, so it returns everytime when the player is in the area, and else the player will be out of the area.
     
Thread Status:
Not open for further replies.

Share This Page