Refactoring: Moved code into LocationUtil and GateUtil.
This commit is contained in:
parent
59fdb54a09
commit
3ea11da591
@ -10,7 +10,7 @@ import java.util.Map;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
||||
|
||||
import de.craftinc.gates.util.LocationSerializer;
|
||||
import de.craftinc.gates.util.LocationUtil;
|
||||
|
||||
|
||||
|
||||
@ -81,8 +81,8 @@ public class Gate extends BaseGate implements ConfigurationSerializable
|
||||
isHidden = (Boolean)map.get(isHiddenKey);
|
||||
isOpen = (Boolean)map.get(isOpenKey);
|
||||
|
||||
location = LocationSerializer.deserializeLocation((Map<String, Object>) map.get(locationKey));
|
||||
exit = LocationSerializer.deserializeLocation((Map<String, Object>) map.get(exitKey));
|
||||
location = LocationUtil.deserializeLocation((Map<String, Object>) map.get(locationKey));
|
||||
exit = LocationUtil.deserializeLocation((Map<String, Object>) map.get(exitKey));
|
||||
|
||||
if (map.containsKey(exitPitchKey)) {
|
||||
exit.setPitch(((Double)map.get(exitPitchKey)).floatValue());
|
||||
@ -98,7 +98,7 @@ public class Gate extends BaseGate implements ConfigurationSerializable
|
||||
List<Map<String, Object>> serializedGateBlocks = (List<Map<String, Object>>)map.get(gateBlocksKey);
|
||||
|
||||
for (Map<String, Object> sgb : serializedGateBlocks) {
|
||||
gateBlockLocations.add(LocationSerializer.deserializeLocation(sgb));
|
||||
gateBlockLocations.add(LocationUtil.deserializeLocation(sgb));
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
@ -126,8 +126,8 @@ public class Gate extends BaseGate implements ConfigurationSerializable
|
||||
Map<String, Object> retVal = new HashMap<String, Object>();
|
||||
|
||||
retVal.put(idKey, id);
|
||||
retVal.put(locationKey, LocationSerializer.serializeLocation(location));
|
||||
retVal.put(exitKey, LocationSerializer.serializeLocation(exit));
|
||||
retVal.put(locationKey, LocationUtil.serializeLocation(location));
|
||||
retVal.put(exitKey, LocationUtil.serializeLocation(exit));
|
||||
retVal.put(isHiddenKey, isHidden);
|
||||
retVal.put(isOpenKey, isOpen);
|
||||
|
||||
@ -142,7 +142,7 @@ public class Gate extends BaseGate implements ConfigurationSerializable
|
||||
List<Map<String, Object>> serializedGateBlocks = new ArrayList<Map<String, Object>>();
|
||||
|
||||
for (Location l : gateBlockLocations) {
|
||||
serializedGateBlocks.add(LocationSerializer.serializeLocation(l));
|
||||
serializedGateBlocks.add(LocationUtil.serializeLocation(l));
|
||||
}
|
||||
|
||||
retVal.put(gateBlocksKey, serializedGateBlocks);
|
||||
|
@ -1,96 +0,0 @@
|
||||
package de.craftinc.gates.listeners;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.BlockFace;
|
||||
import org.bukkit.event.player.PlayerMoveEvent;
|
||||
|
||||
import de.craftinc.gates.Gate;
|
||||
|
||||
|
||||
public abstract class BaseLocationListener
|
||||
{
|
||||
protected Gate getValidGateAtPlayerLocation(PlayerMoveEvent e) {
|
||||
Gate gate = null;
|
||||
|
||||
Location playerLocation = e.getPlayer().getLocation();
|
||||
World playerWorld = playerLocation.getWorld();
|
||||
|
||||
Block blockTo = e.getFrom().getBlock();
|
||||
Block blockToUp = blockTo.getRelative(BlockFace.UP);
|
||||
|
||||
|
||||
for (Gate g : Gate.getAll()) {
|
||||
// Check if the gate is open and useable
|
||||
World gateWorld = g.getLocation().getWorld();
|
||||
|
||||
if (!g.isOpen() || !gateWorld.equals(playerWorld)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
// Check if the location matches
|
||||
for (Location l: g.getGateBlockLocations()) {
|
||||
|
||||
if (locationsAreAtSamePositions(l, blockTo.getLocation()) || locationsAreAtSamePositions(l, blockToUp.getLocation())) {
|
||||
// Check if the gate is still valid
|
||||
try {
|
||||
g.validate();
|
||||
|
||||
gate = g;
|
||||
break;
|
||||
}
|
||||
catch (Exception e2) {
|
||||
break; // do nothing - gate got closed
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return gate;
|
||||
}
|
||||
|
||||
|
||||
protected Gate getGateAtPlayerLocation(PlayerMoveEvent e) {
|
||||
Gate gate = null;
|
||||
|
||||
Block blockTo = e.getFrom().getBlock();
|
||||
Block blockToUp = blockTo.getRelative(BlockFace.UP);
|
||||
|
||||
|
||||
for (Gate g : Gate.getAll()) {
|
||||
// Check if the location matches
|
||||
for (Location l: g.getGateBlockLocations()) {
|
||||
|
||||
if (locationsAreAtSamePositions(l, blockTo.getLocation()) || locationsAreAtSamePositions(l, blockToUp.getLocation())) {
|
||||
gate = g;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return gate;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Does the same as the equal method of Location but ignores pitch and yaw.
|
||||
*/
|
||||
protected boolean locationsAreAtSamePositions(final Location l1, final Location l2)
|
||||
{
|
||||
if (l1.getWorld() != l2.getWorld() && (l1.getWorld() == null || !l1.getWorld().equals(l2.getWorld()))) {
|
||||
return false;
|
||||
}
|
||||
if (Double.doubleToLongBits(l1.getX()) != Double.doubleToLongBits(l2.getX())) {
|
||||
return false;
|
||||
}
|
||||
if (Double.doubleToLongBits(l1.getY()) != Double.doubleToLongBits(l2.getY())) {
|
||||
return false;
|
||||
}
|
||||
if (Double.doubleToLongBits(l1.getZ()) != Double.doubleToLongBits(l2.getZ())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
@ -14,9 +14,10 @@ import org.bukkit.event.player.PlayerMoveEvent;
|
||||
|
||||
import de.craftinc.gates.Gate;
|
||||
import de.craftinc.gates.Plugin;
|
||||
import de.craftinc.gates.util.GateUtil;
|
||||
|
||||
|
||||
public class PluginPlayerListener extends BaseLocationListener implements Listener
|
||||
public class PluginPlayerListener implements Listener
|
||||
{
|
||||
@EventHandler(priority = EventPriority.NORMAL)
|
||||
public void onPlayerMove(PlayerMoveEvent event)
|
||||
@ -27,7 +28,7 @@ public class PluginPlayerListener extends BaseLocationListener implements Listen
|
||||
|
||||
|
||||
// Find the gate at the current location.
|
||||
Gate gateAtLocation = getValidGateAtPlayerLocation(event);
|
||||
Gate gateAtLocation = GateUtil.getGateAtPlayerLocation(event.getTo());
|
||||
|
||||
|
||||
if (gateAtLocation == null) {
|
||||
|
88
src/de/craftinc/gates/util/GateUtil.java
Normal file
88
src/de/craftinc/gates/util/GateUtil.java
Normal file
@ -0,0 +1,88 @@
|
||||
package de.craftinc.gates.util;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.BlockFace;
|
||||
|
||||
import de.craftinc.gates.Gate;
|
||||
|
||||
public class GateUtil
|
||||
{
|
||||
public static Gate closestGate(Location location)
|
||||
{
|
||||
Gate gate = null;
|
||||
double minmalDist = Double.MAX_VALUE;
|
||||
|
||||
for (Gate g : Gate.getAll()) {
|
||||
|
||||
if (!g.getLocation().getWorld().equals(location.getWorld()))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
double tempDist = g.getLocation().distance(location);
|
||||
|
||||
if (tempDist < minmalDist)
|
||||
{
|
||||
gate = g;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return gate;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static Gate getGateAtPlayerLocation(Location location)
|
||||
{
|
||||
Gate gate = null;
|
||||
World playerWorld = location.getWorld();
|
||||
|
||||
// players are sometime stuck into the ground
|
||||
Location locationUp = location.getBlock().getRelative(BlockFace.UP).getLocation();
|
||||
|
||||
System.out.println("player location: " + location);
|
||||
|
||||
for (Gate g : Gate.getAll())
|
||||
{
|
||||
if (gate != null)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
// Check if the gate is open and useable
|
||||
World gateWorld = g.getLocation().getWorld();
|
||||
|
||||
if (!g.isOpen() || !gateWorld.equals(playerWorld))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
// Check if the location matches
|
||||
for (Location l: g.getGateBlockLocations()) {
|
||||
|
||||
if (LocationUtil.locationsAreAtSamePositions(l, location) || LocationUtil.locationsAreAtSamePositions(l, locationUp))
|
||||
{
|
||||
System.out.println("gate location: " + l);
|
||||
|
||||
// Check if the gate is still valid
|
||||
try {
|
||||
g.validate();
|
||||
|
||||
gate = g;
|
||||
break;
|
||||
}
|
||||
catch (Exception e2) {
|
||||
break; // do nothing - gate got closed
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return gate;
|
||||
}
|
||||
}
|
@ -2,8 +2,8 @@ package de.craftinc.gates.util;
|
||||
|
||||
import org.bukkit.Location;
|
||||
|
||||
public class GeometryUtil {
|
||||
|
||||
public class GeometryUtil
|
||||
{
|
||||
// How long between two locations?
|
||||
public static double distanceBetweenLocations(Location location1, Location location2)
|
||||
{
|
||||
|
@ -12,7 +12,7 @@ import de.craftinc.gates.Plugin;
|
||||
/**
|
||||
* NOTE: We do not care about yaw and pitch for gate locations. So we won't serialize them.
|
||||
*/
|
||||
public class LocationSerializer
|
||||
public class LocationUtil
|
||||
{
|
||||
protected static String worldKey = "world";
|
||||
protected static String xKey = "x";
|
||||
@ -83,4 +83,23 @@ public class LocationSerializer
|
||||
|
||||
return new Location(w, x, y, z);
|
||||
}
|
||||
|
||||
|
||||
public static boolean locationsAreAtSamePositions(final Location l1, final Location l2)
|
||||
{
|
||||
if (l1.getWorld() != l2.getWorld() && (l1.getWorld() == null || !l1.getWorld().equals(l2.getWorld()))) {
|
||||
return false;
|
||||
}
|
||||
if (new Double(l1.getX()).longValue() != new Double(l2.getX()).longValue()) {
|
||||
return false;
|
||||
}
|
||||
if (new Double(l1.getY()).longValue() != new Double(l2.getY()).longValue()) {
|
||||
return false;
|
||||
}
|
||||
if (new Double(l1.getZ()).longValue() != new Double(l2.getZ()).longValue()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user