How to check if a player is between 2 locations?

Discussion in 'Plugin Development' started by 16davidjm6, Mar 2, 2015.

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

    16davidjm6

    So some players using one of my plugins have reported, and proven lag related to my plugin after an update I did. The lag was traced back to this section of code that trys to prevent blocks between a certain area from updating.

    Code:
        @EventHandler
        protected void preventUpdate(BlockPhysicsEvent event){
            Location loc = event.getBlock().getLocation();
            int x = loc.getBlockX();
            int y = loc.getBlockY();
            int z = loc.getBlockZ();
          
            if(inPortal(x, y, z)){
                event.setCancelled(true);
            }
        }
     
        private boolean inPortal(int x, int y, int z){
            Set<String> portals = rtp.portals().getConfig().getKeys(false);
          
            for(String portal: portals){
                String s = rtp.portals().getConfig().getString(portal);
              
                String[] split = s.split(":");
                int x1 = Integer.parseInt(split[1]);
                int y1 = Integer.parseInt(split[2]);
                int z1 = Integer.parseInt(split[3]);
                int x2 = Integer.parseInt(split[4]);
                int y2 = Integer.parseInt(split[5]);
                int z2 = Integer.parseInt(split[6]);
     
                if(((x <= x1 && x >= x2) || (x >= x1 && x <= x2)) &&
                   ((y <= y1 && y >= y2) || (y >= y1 && y <= y2)) &&
                   ((z <= z1 && z >= z2) || (z >= z1 && z <= z2))){
                    return true;
                }
            }
          
            return false;
        }
    String s = rtp.portals().getConfig().getString(portal); gives the following string format
    name:x1:y1:z1:x2:y2:z2:material

    That string is then split into the two sets of coordinates. I then check to make sure that the coordinates of the given location are between each of the coordinates for those two locations.

    For some reason this is causing lag though. Even when only looping 4 times.

    Does anyone have any ideas?
     
  2. Offline

    Funergy

Thread Status:
Not open for further replies.

Share This Page