From ee715b13b00274fb2db69f128b96558b5c6b99c3 Mon Sep 17 00:00:00 2001 From: "main()" Date: Fri, 6 Jan 2012 21:20:15 +0100 Subject: [PATCH] (Re)designed LocationManipulation for extension. --- .../onarandombox/MultiverseCore/MVWorld.java | 8 +- .../MultiverseCore/MultiverseCore.java | 32 ++- .../onarandombox/MultiverseCore/api/Core.java | 16 ++ .../api/LocationManipulation.java | 99 +++++++ .../MultiverseCore/commands/CoordCommand.java | 5 +- .../MultiverseCore/commands/InfoCommand.java | 3 +- .../commands/SetSpawnCommand.java | 3 +- .../commands/TeleportCommand.java | 4 +- .../configuration/LocationConfigProperty.java | 3 + .../destination/WorldDestination.java | 14 +- .../MultiverseCore/utils/AnchorManager.java | 6 +- .../utils/LocationManipulation.java | 3 + .../MultiverseCore/utils/SafeTTeleporter.java | 4 +- .../utils/SimpleBlockSafety.java | 8 +- .../utils/SimpleLocationManipulation.java | 251 ++++++++++++++++++ 15 files changed, 425 insertions(+), 34 deletions(-) create mode 100644 src/main/java/com/onarandombox/MultiverseCore/api/LocationManipulation.java create mode 100644 src/main/java/com/onarandombox/MultiverseCore/utils/SimpleLocationManipulation.java diff --git a/src/main/java/com/onarandombox/MultiverseCore/MVWorld.java b/src/main/java/com/onarandombox/MultiverseCore/MVWorld.java index c7add0e6..496e7826 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MVWorld.java +++ b/src/main/java/com/onarandombox/MultiverseCore/MVWorld.java @@ -15,7 +15,6 @@ import com.onarandombox.MultiverseCore.configuration.MVConfigProperty; import com.onarandombox.MultiverseCore.enums.EnglishChatColor; import com.onarandombox.MultiverseCore.event.MVWorldPropertyChangeEvent; import com.onarandombox.MultiverseCore.exceptions.PropertyDoesNotExistException; -import com.onarandombox.MultiverseCore.utils.LocationManipulation; import com.onarandombox.MultiverseCore.utils.SafeTTeleporter; import org.bukkit.ChatColor; import org.bukkit.Difficulty; @@ -965,7 +964,7 @@ public class MVWorld implements MultiverseWorld { } // If it's not, find a better one. this.plugin.log(Level.WARNING, "Spawn location from world.dat file was unsafe. Adjusting..."); - this.plugin.log(Level.WARNING, "Original Location: " + LocationManipulation.strCoordsRaw(spawnLocation)); + this.plugin.log(Level.WARNING, "Original Location: " + plugin.getLocationManipulation().strCoordsRaw(spawnLocation)); Location newSpawn = teleporter.getSafeLocation(spawnLocation, SPAWN_LOCATION_SEARCH_TOLERANCE, SPAWN_LOCATION_SEARCH_RADIUS); // I think we could also do this, as I think this is what Notch does. @@ -974,7 +973,8 @@ public class MVWorld implements MultiverseWorld { if (newSpawn != null) { this.setSpawnLocation(newSpawn); configLocation = this.getSpawnLocation(); - this.plugin.log(Level.INFO, "New Spawn for '" + this.getName() + "' is Located at: " + LocationManipulation.locationToString(configLocation)); + this.plugin.log(Level.INFO, "New Spawn for '" + this.getName() + + "' is Located at: " + plugin.getLocationManipulation().locationToString(configLocation)); } else { // If it's a standard end world, let's check in a better place: Location newerSpawn = null; @@ -983,7 +983,7 @@ public class MVWorld implements MultiverseWorld { this.setSpawnLocation(newerSpawn); configLocation = this.getSpawnLocation(); this.plugin.log(Level.INFO, "New Spawn for '" + this.getName() - + "' is Located at: " + LocationManipulation.locationToString(configLocation)); + + "' is Located at: " + plugin.getLocationManipulation().locationToString(configLocation)); } else { this.plugin.log(Level.SEVERE, "New safe spawn NOT found!!!"); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java index b12b648e..29593ed6 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java +++ b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java @@ -11,6 +11,7 @@ import com.fernferret.allpay.AllPay; import com.fernferret.allpay.GenericBank; import com.onarandombox.MultiverseCore.api.BlockSafety; import com.onarandombox.MultiverseCore.api.Core; +import com.onarandombox.MultiverseCore.api.LocationManipulation; import com.onarandombox.MultiverseCore.api.MVPlugin; import com.onarandombox.MultiverseCore.api.MVWorldManager; import com.onarandombox.MultiverseCore.api.MultiverseWorld; @@ -28,15 +29,7 @@ import com.onarandombox.MultiverseCore.listeners.MVPlayerListener; import com.onarandombox.MultiverseCore.listeners.MVPluginListener; import com.onarandombox.MultiverseCore.listeners.MVPortalAdjustListener; import com.onarandombox.MultiverseCore.listeners.MVWeatherListener; -import com.onarandombox.MultiverseCore.utils.AnchorManager; -import com.onarandombox.MultiverseCore.utils.DebugLog; -import com.onarandombox.MultiverseCore.utils.MVMessaging; -import com.onarandombox.MultiverseCore.utils.MVPermissions; -import com.onarandombox.MultiverseCore.utils.MVPlayerSession; -import com.onarandombox.MultiverseCore.utils.SafeTTeleporter; -import com.onarandombox.MultiverseCore.utils.SimpleBlockSafety; -import com.onarandombox.MultiverseCore.utils.SpoutInterface; -import com.onarandombox.MultiverseCore.utils.WorldManager; +import com.onarandombox.MultiverseCore.utils.*; import com.pneumaticraft.commandhandler.CommandHandler; import org.bukkit.ChatColor; import org.bukkit.Location; @@ -178,6 +171,7 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core { private static final double CH_VERSION = 4; private MVMessaging messaging; private BlockSafety blockSafety; + private LocationManipulation locationManipulation; private File serverFolder = new File(System.getProperty("user.dir")); @@ -188,7 +182,9 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core { // Setup our Debug Log debugLog = new DebugLog("Multiverse-Core", getDataFolder() + File.separator + "debug.log"); // Setup our BlockSafety - this.blockSafety = new SimpleBlockSafety(); + this.blockSafety = new SimpleBlockSafety(this); + // Setup our LocationManipulation + this.locationManipulation = new SimpleLocationManipulation(); } /** @@ -861,4 +857,20 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core { this.blockSafety = bs; } + /** + * {@inheritDoc} + */ + @Override + public LocationManipulation getLocationManipulation() { + return locationManipulation; + } + + /** + * {@inheritDoc} + */ + @Override + public void setLocationManipulation(LocationManipulation locationManipulation) { + this.locationManipulation = locationManipulation; + } + } diff --git a/src/main/java/com/onarandombox/MultiverseCore/api/Core.java b/src/main/java/com/onarandombox/MultiverseCore/api/Core.java index a5b77481..850cbbd6 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/api/Core.java +++ b/src/main/java/com/onarandombox/MultiverseCore/api/Core.java @@ -179,4 +179,20 @@ public interface Core { * @see SimpleBlockSafety */ void setBlockSafety(BlockSafety blockSafety); + + /** + * Gets the {@link LocationManipulation} this {@link Core} is using. + * @return The {@link LocationManipulation} this {@link Core} is using. + * @see LocationManipulation + * @see SimpleLocationManipulation + */ + LocationManipulation getLocationManipulation(); + + /** + * Sets the {@link LocationManipulation} this {@link Core} is using. + * @param locationManipulation The new {@link LocationManipulation}. + * @see LocationManipulation + * @see SimpleLocationManipulation + */ + void setLocationManipulation(LocationManipulation locationManipulation); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/api/LocationManipulation.java b/src/main/java/com/onarandombox/MultiverseCore/api/LocationManipulation.java new file mode 100644 index 00000000..38c5f3f9 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/api/LocationManipulation.java @@ -0,0 +1,99 @@ +package com.onarandombox.MultiverseCore.api; + +import org.bukkit.Location; +import org.bukkit.entity.Vehicle; +import org.bukkit.util.Vector; + +/** + * Used to manipulate locations. + */ +public interface LocationManipulation { + /** + * Convert a Location into a Colon separated string to allow us to store it in text. + *

+ * WORLD:X,Y,Z:yaw:pitch + *

+ * The corresponding String2Loc function is {@link #stringToLocation} + * + * @param location The Location to save. + * @return The location as a string in this format: WORLD:x,y,z:yaw:pitch + */ + String locationToString(Location location); + + /** + * This method simply does some rounding, rather than forcing a call to the server to get the blockdata. + * + * @param l The location to round to the block location + * @return A rounded location. + */ + Location getBlockLocation(Location l); + + /** + * Returns a new location from a given string. The format is as follows: + *

+ * WORLD:X,Y,Z:yaw:pitch + *

+ * The corresponding Location2String function is {@link #stringToLocation} + * + * @param locationString The location represented as a string (WORLD:X,Y,Z:yaw:pitch) + * @return A new location defined by the string or null if the string was invalid. + */ + Location stringToLocation(String locationString); + + /** + * Returns a colored string with the coords. + * + * @param l The {@link Location} + * @return The {@link String} + */ + String strCoords(Location l); + + /** + * Converts a location to a printable readable formatted string including pitch/yaw. + * + * @param l The {@link Location} + * @return The {@link String} + */ + String strCoordsRaw(Location l); + + /** + * Return the NESW Direction a Location is facing. + * + * @param location The {@link Location} + * @return The NESW Direction + */ + String getDirection(Location location); + + /** + * Returns the float yaw position for the given cardinal direction. + * + * @param orientation The cardinal direction + * @return The yaw + */ + float getYaw(String orientation); + + /** + * Returns a speed float from a given vector. + * + * @param v The {@link Vector} + * @return The speed + */ + float getSpeed(Vector v); + + /** + * Returns a translated vector from the given direction. + * + * @param v The old {@link Vector} + * @param direction The new direction + * @return The translated {@link Vector} + */ + Vector getTranslatedVector(Vector v, String direction); + + /** + * Returns the next Location that a {@link Vehicle} is traveling at. + * + * @param v The {@link Vehicle} + * @return The {@link Location} + */ + Location getNextBlock(Vehicle v); +} \ No newline at end of file diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/CoordCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/CoordCommand.java index f2d09887..944d8029 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/CoordCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/CoordCommand.java @@ -10,7 +10,6 @@ package com.onarandombox.MultiverseCore.commands; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MVWorldManager; import com.onarandombox.MultiverseCore.api.MultiverseWorld; -import com.onarandombox.MultiverseCore.utils.LocationManipulation; import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.World; @@ -61,8 +60,8 @@ public class CoordCommand extends MultiverseCommand { DecimalFormat df = new DecimalFormat(); df.setMinimumFractionDigits(0); df.setMaximumFractionDigits(2); - p.sendMessage(ChatColor.AQUA + "Coordinates: " + ChatColor.WHITE + LocationManipulation.strCoords(p.getLocation())); - p.sendMessage(ChatColor.AQUA + "Direction: " + ChatColor.WHITE + LocationManipulation.getDirection(p.getLocation())); + p.sendMessage(ChatColor.AQUA + "Coordinates: " + ChatColor.WHITE + plugin.getLocationManipulation().strCoords(p.getLocation())); + p.sendMessage(ChatColor.AQUA + "Direction: " + ChatColor.WHITE + plugin.getLocationManipulation().getDirection(p.getLocation())); p.sendMessage(ChatColor.AQUA + "Block: " + ChatColor.WHITE + Material.getMaterial(world.getBlockTypeIdAt(p.getLocation()))); } else { sender.sendMessage("This command needs to be used from a Player."); diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/InfoCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/InfoCommand.java index 39fb5821..8b04903c 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/InfoCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/InfoCommand.java @@ -14,7 +14,6 @@ import com.onarandombox.MultiverseCore.api.MultiverseWorld; import com.onarandombox.MultiverseCore.utils.FancyColorScheme; import com.onarandombox.MultiverseCore.utils.FancyHeader; import com.onarandombox.MultiverseCore.utils.FancyMessage; -import com.onarandombox.MultiverseCore.utils.LocationManipulation; import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.command.CommandSender; @@ -112,7 +111,7 @@ public class InfoCommand extends MultiverseCommand { message.add(new FancyMessage("Game Mode: ", world.getGameMode().toString(), colors)); //message.add(new FancyMessage("Game Mode: ", StringUtils.capitalize(world.getGameMode().toString()), colors)); Location spawn = world.getSpawnLocation(); - message.add(new FancyMessage("Spawn Location: ", LocationManipulation.strCoords(spawn), colors)); + message.add(new FancyMessage("Spawn Location: ", plugin.getLocationManipulation().strCoords(spawn), colors)); message.add(new FancyMessage("World Scale: ", world.getScaling() + "", colors)); if (world.getPrice() > 0) { message.add(new FancyMessage("Price to enter this world: ", diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/SetSpawnCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/SetSpawnCommand.java index e1de667e..0e0cd96d 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/SetSpawnCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/SetSpawnCommand.java @@ -10,7 +10,6 @@ package com.onarandombox.MultiverseCore.commands; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.BlockSafety; import com.onarandombox.MultiverseCore.api.MultiverseWorld; -import com.onarandombox.MultiverseCore.utils.LocationManipulation; import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.World; @@ -64,7 +63,7 @@ public class SetSpawnCommand extends MultiverseCommand { sender.sendMessage(ChatColor.AQUA + "/mvm set adjustspawn true " + foundWorld.getAlias()); foundWorld.setAdjustSpawn(false); } - sender.sendMessage("Spawn was set to: " + LocationManipulation.strCoords(p.getLocation())); + sender.sendMessage("Spawn was set to: " + plugin.getLocationManipulation().strCoords(p.getLocation())); } else { w.setSpawnLocation(l.getBlockX(), l.getBlockY(), l.getBlockZ()); sender.sendMessage("Multiverse does not know about this world, only X,Y and Z set. Please import it to set the spawn fully (Pitch/Yaws)."); diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/TeleportCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/TeleportCommand.java index 8adc91ac..667dacc6 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/TeleportCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/TeleportCommand.java @@ -14,7 +14,6 @@ import com.onarandombox.MultiverseCore.destination.InvalidDestination; import com.onarandombox.MultiverseCore.destination.WorldDestination; import com.onarandombox.MultiverseCore.enums.TeleportResult; import com.onarandombox.MultiverseCore.event.MVTeleportEvent; -import com.onarandombox.MultiverseCore.utils.LocationManipulation; import com.onarandombox.MultiverseCore.utils.SafeTTeleporter; import org.bukkit.ChatColor; import org.bukkit.Location; @@ -163,7 +162,8 @@ public class TeleportCommand extends MultiverseCommand { } TeleportResult result = this.playerTeleporter.safelyTeleport(teleporter, teleportee, d); if (result == TeleportResult.FAIL_UNSAFE) { - this.plugin.log(Level.FINE, "Could not teleport " + teleportee.getName() + " to " + LocationManipulation.strCoordsRaw(d.getLocation(teleportee))); + this.plugin.log(Level.FINE, "Could not teleport " + teleportee.getName() + + " to " + plugin.getLocationManipulation().strCoordsRaw(d.getLocation(teleportee))); this.plugin.log(Level.FINE, "Queueing Command"); Class[] paramTypes = { CommandSender.class, Player.class, Location.class }; List items = new ArrayList(); diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/LocationConfigProperty.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/LocationConfigProperty.java index b1af4ae1..9a7096f7 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/LocationConfigProperty.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/LocationConfigProperty.java @@ -61,6 +61,7 @@ public class LocationConfigProperty implements MVActiveConfigProperty */ @Override public boolean parseValue(String value) { + // TODO: oh my god, what should we do here? Location parsed = LocationManipulation.stringToLocation(value); return this.setValue(parsed); } @@ -108,6 +109,7 @@ public class LocationConfigProperty implements MVActiveConfigProperty double yaw = this.section.getDouble(this.configNode + ".yaw", defaultValue.getYaw()); String w = this.section.getString(this.configNode + ".world", defaultValue.getWorld().getName()); Location found = LocationManipulation.stringToLocation(w + ":" + x + "," + y + "," + z + ":" + yaw + ":" + pitch); + // TODO: oh my god, what should we do here? if (found != null) { return found; } @@ -116,6 +118,7 @@ public class LocationConfigProperty implements MVActiveConfigProperty @Override public String toString() { + // TODO: oh my god, what should we do here? return LocationManipulation.strCoordsRaw(this.value); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/destination/WorldDestination.java b/src/main/java/com/onarandombox/MultiverseCore/destination/WorldDestination.java index a7ba3313..6a584596 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/destination/WorldDestination.java +++ b/src/main/java/com/onarandombox/MultiverseCore/destination/WorldDestination.java @@ -8,9 +8,9 @@ package com.onarandombox.MultiverseCore.destination; import com.onarandombox.MultiverseCore.MultiverseCore; +import com.onarandombox.MultiverseCore.api.Core; import com.onarandombox.MultiverseCore.api.MVDestination; import com.onarandombox.MultiverseCore.api.MultiverseWorld; -import com.onarandombox.MultiverseCore.utils.LocationManipulation; import org.bukkit.Location; import org.bukkit.entity.Entity; import org.bukkit.plugin.java.JavaPlugin; @@ -93,6 +93,9 @@ public class WorldDestination implements MVDestination { */ @Override public void setDestination(JavaPlugin plugin, String destination) { + // TODO Taking a JavaPlugin here is rather useless, if we keep casting it up to MultiverseCore. + // We should change that. + Core core = (Core) plugin; String[] items = destination.split(":"); if (items.length > 3) { isValid = false; @@ -100,19 +103,20 @@ public class WorldDestination implements MVDestination { } if (items.length == 1 && ((MultiverseCore) plugin).getMVWorldManager().isMVWorld(items[0])) { isValid = true; - this.world = ((MultiverseCore) plugin).getMVWorldManager().getMVWorld(items[0]); + this.world = core.getMVWorldManager().getMVWorld(items[0]); return; } if (items.length == 2 && ((MultiverseCore) plugin).getMVWorldManager().isMVWorld(items[0])) { - this.world = ((MultiverseCore) plugin).getMVWorldManager().getMVWorld(items[0]); - this.yaw = LocationManipulation.getYaw(items[1]); + this.world = core.getMVWorldManager().getMVWorld(items[0]); + this.yaw = core.getLocationManipulation().getYaw(items[1]); return; } if (items[0].equalsIgnoreCase("w") && ((MultiverseCore) plugin).getMVWorldManager().isMVWorld(items[1])) { this.world = ((MultiverseCore) plugin).getMVWorldManager().getMVWorld(items[1]); isValid = true; if (items.length == 3) { - this.yaw = LocationManipulation.getYaw(items[2]); + // TODO: oh my god, what should we do here? + this.yaw = core.getLocationManipulation().getYaw(items[2]); } } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/AnchorManager.java b/src/main/java/com/onarandombox/MultiverseCore/utils/AnchorManager.java index 3bb77465..c06c7da8 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/AnchorManager.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/AnchorManager.java @@ -48,7 +48,7 @@ public class AnchorManager { Set anchorKeys = anchorsSection.getKeys(false); for (String key : anchorKeys) { //world:x,y,z:pitch:yaw - Location anchorLocation = LocationManipulation.stringToLocation(anchorsSection.getString(key, "")); + Location anchorLocation = plugin.getLocationManipulation().stringToLocation(anchorsSection.getString(key, "")); if (anchorLocation != null) { MultiverseCore.staticLog(Level.INFO, "Loading anchor: '" + key + "'..."); this.anchors.put(key, anchorLocation); @@ -98,7 +98,7 @@ public class AnchorManager { * @return True if the anchor was successfully saved. */ public boolean saveAnchorLocation(String anchor, String location) { - Location parsed = LocationManipulation.stringToLocation(location); + Location parsed = plugin.getLocationManipulation().stringToLocation(location); return parsed != null && this.saveAnchorLocation(anchor, parsed); } @@ -112,7 +112,7 @@ public class AnchorManager { if (l == null) { return false; } - this.anchorConfig.set("anchors." + anchor, LocationManipulation.locationToString(l)); + this.anchorConfig.set("anchors." + anchor, plugin.getLocationManipulation().locationToString(l)); this.anchors.put(anchor, l); return this.saveAnchors(); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/LocationManipulation.java b/src/main/java/com/onarandombox/MultiverseCore/utils/LocationManipulation.java index 132ff259..ba18ccb3 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/LocationManipulation.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/LocationManipulation.java @@ -21,7 +21,10 @@ import java.util.Map; /** * Utility class to manipulate locations. + * + * @deprecated Use instead: {@link com.onarandombox.MultiverseCore.api.LocationManipulation} and {@link SimpleLocationManipulation}. */ +@Deprecated public class LocationManipulation { private LocationManipulation() { } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/SafeTTeleporter.java b/src/main/java/com/onarandombox/MultiverseCore/utils/SafeTTeleporter.java index 50a56c2f..11916b60 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/SafeTTeleporter.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/SafeTTeleporter.java @@ -65,7 +65,7 @@ public class SafeTTeleporter { if (safe != null) { safe.setX(safe.getBlockX() + .5); // SUPPRESS CHECKSTYLE: MagicNumberCheck safe.setZ(safe.getBlockZ() + .5); // SUPPRESS CHECKSTYLE: MagicNumberCheck - this.plugin.log(Level.FINE, "Hey! I found one: " + LocationManipulation.strCoordsRaw(safe)); + this.plugin.log(Level.FINE, "Hey! I found one: " + plugin.getLocationManipulation().strCoordsRaw(safe)); } else { this.plugin.log(Level.FINE, "Uh oh! No safe place found!"); } @@ -79,7 +79,7 @@ public class SafeTTeleporter { } // We want half of it, so we can go up and down tolerance /= 2; - this.plugin.log(Level.FINER, "Given Location of: " + LocationManipulation.strCoordsRaw(l)); + this.plugin.log(Level.FINER, "Given Location of: " + plugin.getLocationManipulation().strCoordsRaw(l)); this.plugin.log(Level.FINER, "Checking +-" + tolerance + " with a radius of " + radius); // For now this will just do a straight up block. diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/SimpleBlockSafety.java b/src/main/java/com/onarandombox/MultiverseCore/utils/SimpleBlockSafety.java index 87f2b495..e1ed80df 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/utils/SimpleBlockSafety.java +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/SimpleBlockSafety.java @@ -9,6 +9,7 @@ package com.onarandombox.MultiverseCore.utils; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.BlockSafety; +import com.onarandombox.MultiverseCore.api.Core; import org.bukkit.Location; import org.bukkit.Material; @@ -22,6 +23,11 @@ import java.util.logging.Level; * The default-implementation of {@link BlockSafety}. */ public class SimpleBlockSafety implements BlockSafety { + private final Core plugin; + + public SimpleBlockSafety(Core plugin) { + this.plugin = plugin; + } /** * {@inheritDoc} @@ -228,7 +234,7 @@ public class SimpleBlockSafety implements BlockSafety { if (this.isBlockAboveAir(cart.getLocation())) { return true; } - if (this.isEntitiyOnTrack(LocationManipulation.getNextBlock(cart))) { + if (this.isEntitiyOnTrack(plugin.getLocationManipulation().getNextBlock(cart))) { return true; } return false; diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/SimpleLocationManipulation.java b/src/main/java/com/onarandombox/MultiverseCore/utils/SimpleLocationManipulation.java new file mode 100644 index 00000000..3d0436b1 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/SimpleLocationManipulation.java @@ -0,0 +1,251 @@ +/****************************************************************************** + * Multiverse 2 Copyright (c) the Multiverse Team 2011. * + * Multiverse 2 is licensed under the BSD License. * + * For more information please check the README.md file included * + * with this project. * + ******************************************************************************/ + +package com.onarandombox.MultiverseCore.utils; + +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.entity.Vehicle; +import org.bukkit.util.Vector; + +import com.onarandombox.MultiverseCore.api.LocationManipulation; + +import java.text.DecimalFormat; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +/** + * The default-implementation of {@link LocationManipulation}. + */ +public class SimpleLocationManipulation implements LocationManipulation { + private static final Map ORIENTATION_INTS; + + static { + Map orientationInts = new HashMap(); + // BEGIN CHECKSTYLE-SUPPRESSION: MagicNumberCheck + orientationInts.put("n", 180); + orientationInts.put("ne", 225); + orientationInts.put("e", 270); + orientationInts.put("se", 315); + orientationInts.put("s", 0); + orientationInts.put("sw", 45); + orientationInts.put("w", 90); + orientationInts.put("nw", 135); + + // "freeze" the map: + ORIENTATION_INTS = Collections.unmodifiableMap(orientationInts); + // END CHECKSTYLE-SUPPRESSION: MagicNumberCheck + } + + /** + * {@inheritDoc} + */ + @Override + public String locationToString(Location location) { + if (location == null) { + return ""; + } + return String.format("%s:%.2f,%.2f,%.2f:%.2f:%.2f", location.getWorld().getName(), + location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch()); + } + + /** + * {@inheritDoc} + */ + @Override + public Location getBlockLocation(Location l) { + l.setX(l.getBlockX()); + l.setY(l.getBlockY()); + l.setZ(l.getBlockZ()); + return l; + } + + /** + * {@inheritDoc} + */ + @Override + public Location stringToLocation(String locationString) { + //format: + //world:x,y,z:pitch:yaw + if (locationString == null) { + return null; + } + + // Split the whole string, format is: + // {'world', 'x,y,z'[, 'pitch', 'yaw']} + String[] split = locationString.split(":"); + if (split.length < 2 || split.length > 4) { // SUPPRESS CHECKSTYLE: MagicNumberCheck + return null; + } + // Split the xyz string, format is: + // {'x', 'y', 'z'} + String[] xyzsplit = split[1].split(","); + if (xyzsplit.length != 3) { + return null; + } + + // Verify the world is valid + World w = Bukkit.getWorld(split[0]); + if (w == null) { + return null; + } + + try { + float pitch = 0; + float yaw = 0; + if (split.length >= 3) { + yaw = (float) Double.parseDouble(split[2]); + } + if (split.length == 4) { // SUPPRESS CHECKSTYLE: MagicNumberCheck + pitch = (float) Double.parseDouble(split[3]); + } + return new Location(w, Double.parseDouble(xyzsplit[0]), Double.parseDouble(xyzsplit[1]), Double.parseDouble(xyzsplit[2]), yaw, pitch); + } catch (NumberFormatException e) { + return null; + } + } + + /** + * {@inheritDoc} + */ + @Override + public String strCoords(Location l) { + String result = ""; + DecimalFormat df = new DecimalFormat(); + df.setMinimumFractionDigits(0); + df.setMaximumFractionDigits(2); + result += ChatColor.WHITE + "X: " + ChatColor.AQUA + df.format(l.getX()) + " "; + result += ChatColor.WHITE + "Y: " + ChatColor.AQUA + df.format(l.getY()) + " "; + result += ChatColor.WHITE + "Z: " + ChatColor.AQUA + df.format(l.getZ()) + " "; + result += ChatColor.WHITE + "P: " + ChatColor.GOLD + df.format(l.getPitch()) + " "; + result += ChatColor.WHITE + "Y: " + ChatColor.GOLD + df.format(l.getYaw()) + " "; + return result; + } + + /** + * {@inheritDoc} + */ + @Override + public String strCoordsRaw(Location l) { + if (l == null) { + return "null"; + } + String result = ""; + DecimalFormat df = new DecimalFormat(); + df.setMinimumFractionDigits(0); + df.setMaximumFractionDigits(2); + result += "X: " + df.format(l.getX()) + " "; + result += "Y: " + df.format(l.getY()) + " "; + result += "Z: " + df.format(l.getZ()) + " "; + result += "P: " + df.format(l.getPitch()) + " "; + result += "Y: " + df.format(l.getYaw()) + " "; + return result; + } + + /** + * {@inheritDoc} + */ + @Override + public String getDirection(Location location) { + // BEGIN CHECKSTYLE-SUPPRESSION: MagicNumberCheck + double r = (location.getYaw() % 360) + 180; + // Remember, these numbers are every 45 degrees with a 22.5 offset, to detect boundaries. + String dir; + if (r < 22.5) + dir = "n"; + else if (r < 67.5) + dir = "ne"; + else if (r < 112.5) + dir = "e"; + else if (r < 157.5) + dir = "se"; + else if (r < 202.5) + dir = "s"; + else if (r < 247.5) + dir = "sw"; + else if (r < 292.5) + dir = "w"; + else if (r < 337.5) + dir = "nw"; + else + dir = "n"; + // END CHECKSTYLE-SUPPRESSION: MagicNumberCheck + + return dir; + } + + /** + * {@inheritDoc} + */ + @Override + public float getYaw(String orientation) { + if (orientation == null) { + return 0; + } + if (ORIENTATION_INTS.containsKey(orientation.toLowerCase())) { + return ORIENTATION_INTS.get(orientation.toLowerCase()); + } + return 0; + } + + /** + * {@inheritDoc} + */ + @Override + public float getSpeed(Vector v) { + return (float) Math.sqrt(v.getX() * v.getX() + v.getZ() * v.getZ()); + } + + // X, Y, Z + // -W/+E,0, -N/+S + + /** + * {@inheritDoc} + */ + @Override + public Vector getTranslatedVector(Vector v, String direction) { + if (direction == null) { + return v; + } + float speed = getSpeed(v); + float halfSpeed = (float) (speed / 2.0); + // TODO: Mathmatacize this: + if (direction.equalsIgnoreCase("n")) { + return new Vector(0, 0, -1 * speed); + } else if (direction.equalsIgnoreCase("ne")) { + return new Vector(halfSpeed, 0, -1 * halfSpeed); + } else if (direction.equalsIgnoreCase("e")) { + return new Vector(speed, 0, 0); + } else if (direction.equalsIgnoreCase("se")) { + return new Vector(halfSpeed, 0, halfSpeed); + } else if (direction.equalsIgnoreCase("s")) { + return new Vector(0, 0, speed); + } else if (direction.equalsIgnoreCase("sw")) { + return new Vector(-1 * halfSpeed, 0, halfSpeed); + } else if (direction.equalsIgnoreCase("w")) { + return new Vector(-1 * speed, 0, 0); + } else if (direction.equalsIgnoreCase("nw")) { + return new Vector(-1 * halfSpeed, 0, -1 * halfSpeed); + } + return v; + } + + /** + * {@inheritDoc} + */ + @Override + public Location getNextBlock(Vehicle v) { + Vector vector = v.getVelocity(); + Location location = v.getLocation(); + int x = vector.getX() < 0 ? vector.getX() == 0 ? 0 : -1 : 1; + int z = vector.getZ() < 0 ? vector.getZ() == 0 ? 0 : -1 : 1; + return location.add(x, 0, z); + } +}