Improved accuracy of location - string conversion.

This commit is contained in:
Tastybento 2018-04-18 10:47:20 -07:00
parent 2e1c055cd7
commit f98c5527bc

View File

@ -4,6 +4,7 @@ import java.io.File;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType; import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.text.DecimalFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -30,6 +31,7 @@ import us.tastybento.bskyblock.api.user.User;
*/ */
public class Util { public class Util {
private static final DecimalFormat df = new DecimalFormat("#.###");
private static String serverVersion = null; private static String serverVersion = null;
private static BSkyBlock plugin = BSkyBlock.getInstance(); private static BSkyBlock plugin = BSkyBlock.getInstance();
@ -54,31 +56,24 @@ public class Util {
* @return Location * @return Location
*/ */
public static Location getLocationString(final String s) { public static Location getLocationString(final String s) {
plugin.getLogger().info("DEBUG: getting location from string");
if (s == null || s.trim().equals("")) { if (s == null || s.trim().equals("")) {
return null; return null;
} }
final String[] parts = s.split(":"); final String[] parts = s.split(":");
if (parts.length == 4) { if (parts.length == 6) {
final World w = Bukkit.getServer().getWorld(parts[0]); final World w = Bukkit.getServer().getWorld(parts[0]);
if (w == null) { if (w == null) {
return null; return null;
} }
final int x = Integer.parseInt(parts[1]); double x = Double.parseDouble(parts[1]);
final int y = Integer.parseInt(parts[2]); double y = Double.parseDouble(parts[2]);
final int z = Integer.parseInt(parts[3]); double z = Double.parseDouble(parts[3]);
return new Location(w, x, y, z);
} else if (parts.length == 6) {
final World w = Bukkit.getServer().getWorld(parts[0]);
if (w == null) {
return null;
}
final int x = Integer.parseInt(parts[1]);
final int y = Integer.parseInt(parts[2]);
final int z = Integer.parseInt(parts[3]);
final float yaw = Float.intBitsToFloat(Integer.parseInt(parts[4])); final float yaw = Float.intBitsToFloat(Integer.parseInt(parts[4]));
final float pitch = Float.intBitsToFloat(Integer.parseInt(parts[5])); final float pitch = Float.intBitsToFloat(Integer.parseInt(parts[5]));
return new Location(w, x, y, z, yaw, pitch); return new Location(w, x, y, z, yaw, pitch);
} }
plugin.getLogger().info("DEBUG: not right length");
return null; return null;
} }
@ -86,14 +81,14 @@ public class Util {
* Converts a location to a simple string representation * Converts a location to a simple string representation
* If location is null, returns empty string * If location is null, returns empty string
* *
* @param location - the location * @param l - the location
* @return String of location * @return String of location
*/ */
public static String getStringLocation(final Location location) { public static String getStringLocation(final Location l) {
if (location == null || location.getWorld() == null) { if (l == null || l.getWorld() == null) {
return ""; return "";
} }
return location.getWorld().getName() + ":" + location.getBlockX() + ":" + location.getBlockY() + ":" + location.getBlockZ() + ":" + Float.floatToIntBits(location.getYaw()) + ":" + Float.floatToIntBits(location.getPitch()); return l.getWorld().getName() + ":" + df.format(l.getX()) + ":" + df.format(l.getY()) + ":" + df.format(l.getZ()) + ":" + Float.floatToIntBits(l.getYaw()) + ":" + Float.floatToIntBits(l.getPitch());
} }
/** /**