Fastest code at "region"-check

Discussion in 'Plugin Development' started by black_ixx, Dec 10, 2012.

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

    black_ixx

    Hey

    Well Im currently working at a big plugin which cancels some teleport events if they are not wanted. My plugin contains different "regions" or "arenas". When a player wants to leave thim with a command the event is canceled. It already works but I think that my current code is very bad. Some months ago Ive wanted to check some locations so Ive developed the lines. Ive used the same code this time and Ive thinked about a faster way many times but I could not get any idea how to. My current code looks like this:

    Code:
    package org.black_ixx.war.helpers;
     
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.entity.Player;
     
    public class TeleportCheck
    {
     
        public static boolean check(Player p, Location to, Location l1, Location l2)
        {
     
            if (!p.getWorld().equals(to.getWorld())){
                return false;
            }
            World w = to.getWorld();
            //Get X1
            int x1x = l1.getBlockX();
            Location x1 = p.getLocation();
            x1.setX(x1x);
            x1.setY(0);
            x1.setZ(0);
            x1.setWorld(w);
     
            //Get X2
            int x2x = l2.getBlockX();
            Location x2 = p.getLocation();
            x2.setX(x2x);
            x2.setY(0);
            x2.setZ(0);
            x2.setWorld(w);
     
            //Get Z1
            int z1z = l1.getBlockZ();
            Location z1 = p.getLocation();
            z1.setX(0);
            z1.setY(0);
            z1.setZ(z1z);
            z1.setWorld(w);
     
            //Get Z2
            int z2z = l2.getBlockZ();
            Location z2 = p.getLocation();
            z2.setX(0);
            z2.setY(0);
            z2.setZ(z2z);
            z2.setWorld(w);
           
            //distanzen
           
            //Zone
            double distanzZ = z2.distance(z1);
            double distanzX = x2.distance(x1);
           
            //player
            Location forZ = new Location (p.getWorld(), 0,0,to.getZ());
            Location forX = new Location (p.getWorld(), to.getX(),0,0);
           
           
            double distanzPZ1 = forZ.distance(z1);
            double distanzPZ2 = forZ.distance(z2);
            double distanzPX1 = forX.distance(x1);
            double distanzPX2 = forX.distance(x2);
           
       
           
           
            if (distanzPZ1 > distanzZ ||distanzPZ2 > distanzZ ||distanzPX1 > distanzX ||distanzPX2 > distanzX){
                return false;
            }
            return true;
        }
     
    }
    
    Ideas are welcome!
     
  2. Offline

    black_ixx

    Any ideas?
     
Thread Status:
Not open for further replies.

Share This Page