Advertisement
FisheyLP

LocationUtils.java

Aug 19th, 2015
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.36 KB | None | 0 0
  1. package com.FisheyLP.Utils;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import org.bukkit.Bukkit;
  7. import org.bukkit.Location;
  8.  
  9. public class LocationUtils {
  10.    
  11.     /**
  12.      * @author FisheyLP
  13.      */
  14.    
  15.     /**
  16.      * The character where it serializes and deserializes the different coordinates
  17.      */
  18.     public static char token = ':';
  19.    
  20.     /**
  21.      * Gets the all Locations in the area of pos1 and pos2.
  22.      * @param pos1 The first location of the area.
  23.      * @param pos2 The second location of the area.
  24.      * @return A List<Location> of all block locations in the area.
  25.      */
  26.     public static List<Location> getArea(Location pos1, Location pos2) {
  27.         Location[] sorted = getMinMaxLocations(pos1, pos2);
  28.         pos1 = sorted[0];
  29.         pos2 = sorted[1];
  30.         List<Location> locs = new ArrayList<Location>();
  31.         for (int x = pos1.getBlockX(); x < pos2.getX(); x++) {
  32.             for (int y = pos1.getBlockY(); y < pos2.getY(); y++) {
  33.                 for (int z = pos1.getBlockZ(); z < pos2.getZ(); z++) {
  34.                     locs.add(new Location(pos1.getWorld(), x, y, z));
  35.                 }
  36.             }
  37.         }
  38.         return locs;
  39.     }
  40.    
  41.     /**
  42.      * Checks if a location is inside of an area.
  43.      * @param pos1 The first location of the area.
  44.      * @param pos2 The second location of the area.
  45.      * @param loc The location.
  46.      * @return If loc is inside the area of pos1 and pos2.
  47.      */
  48.     public static boolean isInside(Location pos1, Location pos2, Location loc) {
  49.         Location[] minMax = getMinMaxLocations(pos1, pos2);
  50.         pos1 = minMax[0];
  51.         pos2 = minMax[1];
  52.        
  53.         return pos1.getX() <= loc.getX() && pos2.getX() >= loc.getX() &&
  54.                 pos1.getY() <= loc.getY() && pos2.getY() >= loc.getY() &&
  55.                 pos1.getZ() <= loc.getZ() && pos2.getZ() >= loc.getZ();
  56.     }
  57.    
  58.     /**
  59.      * Gets the smaller and bigger locations of the coordinates.
  60.      * @param pos1 The first location.
  61.      * @param pos2 The second location.
  62.      * @return A Location array holding the smaller and the bigger location.
  63.      */
  64.     public static Location[] getMinMaxLocations(Location pos1, Location pos2) {
  65.         double minX = Math.min(pos1.getX(), pos2.getX());
  66.         double minY = Math.min(pos1.getY(), pos2.getY());
  67.         double minZ = Math.min(pos1.getZ(), pos2.getZ());
  68.        
  69.         double maxX = Math.max(pos1.getX(), pos2.getX());
  70.         double maxY = Math.max(pos1.getY(), pos2.getY());
  71.         double maxZ = Math.max(pos1.getZ(), pos2.getZ());
  72.        
  73.         pos1 = new Location(pos1.getWorld(), minX, minY, minZ);
  74.         pos2 = new Location(pos2.getWorld(), maxX, maxY, maxZ);
  75.         return new Location[] {pos1, pos2};
  76.     }
  77.    
  78.    
  79.     /**
  80.      * Serializes a location into a String that contains the world name, x, y and z.
  81.      * @param loc The location that will be serialized.
  82.      * @return A String that contains the world name, x, y and z.
  83.      */
  84.     public static String serialize(Location loc) {
  85.         if (loc == null) return null;
  86.         return loc.getWorld().getName()
  87.                 +token+loc.getX()+token+loc.getY()+token+loc.getZ();
  88.     }
  89.    
  90.     /**
  91.      * Serializes a location into a String that contains the world name, block x, block y and block z.
  92.      * @param loc The location that will be serialized.
  93.      * @return A String that contains the world name, block x, block y and block z.
  94.      */
  95.     public static String serializeSimple(Location loc) {
  96.         if (loc == null) return null;
  97.         return loc.getWorld().getName()+token+
  98.     loc.getBlockX()+token+loc.getBlockY()+token+loc.getBlockZ();
  99.     }
  100.    
  101.     /**
  102.      * Serializes a location into a String that contains the world name, x, y, z, yaw and pitch.
  103.      * @param loc The location that will be serialized.
  104.      * @return A String that contains the world name, x, y, z, yaw and pitch.
  105.      */
  106.     public static String serializeFully(Location loc) {
  107.         if (loc == null) return null;
  108.         return serialize(loc)+token+loc.getYaw()+token+loc.getPitch();
  109.     }
  110.    
  111.     /**
  112.      * Deserializes a String that contains the world name, x, y and z into a Location.
  113.      * @param str The string that is deserialized.
  114.      * @return A Location that contains the world, x, y and z.
  115.      */
  116.     public static Location deserialize(String str) {
  117.         if (str == null) return null;
  118.         Location loc = new Location(null, 0, 0, 0);
  119.         String[] split = str.split(String.valueOf(token));
  120.         loc.setWorld(Bukkit.getWorld(split[0]));
  121.         loc.setX(Double.parseDouble(split[1]));
  122.         loc.setY(Double.parseDouble(split[2]));
  123.         loc.setZ(Double.parseDouble(split[3]));
  124.         return loc;
  125.     }
  126.    
  127.     /**
  128.      * Deserializes a String that contains the world name, block x, block y and block z into a Location.
  129.      * @param str The string that is deserialized.
  130.      * @return A Location that contains the world, block x, block y and block z.
  131.      */
  132.     public static Location deserializeSimple(String str) {
  133.         if (str == null) return null;
  134.         Location loc = new Location(null, 0, 0, 0);
  135.         String[] split = str.split(String.valueOf(token));
  136.         loc.setWorld(Bukkit.getWorld(split[0]));
  137.         loc.setX((int) Double.parseDouble(split[1]));
  138.         loc.setY((int) Double.parseDouble(split[2]));
  139.         loc.setZ((int) Double.parseDouble(split[3]));
  140.         return loc;
  141.     }
  142.    
  143.     /**
  144.      * Deserializes a String that contains the world name, x, y, z, yaw and pitch into a Location.
  145.      * @param str The string that is deserialized.
  146.      * @return A Location that contains the world, x, y, z, yaw and pitch.
  147.      */
  148.     public static Location deserializeFully(String str) {
  149.         if (str == null) return null;
  150.         Location loc = deserialize(str);
  151.         String[] split = str.split(String.valueOf(token));
  152.         loc.setYaw(Float.parseFloat(split[4]));
  153.         loc.setPitch(Float.parseFloat(split[5]));
  154.         return loc;
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement