diff --git a/src/de/craftinc/gates/Gate.java b/src/de/craftinc/gates/Gate.java index 423ece7..9089306 100644 --- a/src/de/craftinc/gates/Gate.java +++ b/src/de/craftinc/gates/Gate.java @@ -27,228 +27,202 @@ import org.bukkit.configuration.serialization.ConfigurationSerializable; import java.util.*; -public class Gate implements ConfigurationSerializable -{ - protected Location location; /* saving both location and gateBlockLocations is redundant but makes it easy to allow players to reshape gates */ - protected Set gateBlockLocations = new HashSet(); /* Locations of the blocks inside the gate */ +public class Gate implements ConfigurationSerializable { + protected Location location; /* saving both location and gateBlockLocations is redundant but makes it easy to allow players to reshape gates */ + protected Set gateBlockLocations = new HashSet(); /* Locations of the blocks inside the gate */ protected Set gateFrameBlocks = new HashSet(); - - protected Location exit; - - protected boolean isHidden = false; - protected boolean isOpen = false; + + protected Location exit; + + protected boolean isHidden = false; + protected boolean isOpen = false; protected boolean allowsVehicles = true; - - protected String id; + + protected String id; /** * You should never create two gates with the same 'id'. Also see 'setId(String id)'. + * * @param id This parameter must not be 'null'. An exception will be thrown otherwise! */ - public Gate(final String id) - { + public Gate(final String id) { setId(id); - } - - - public String toString() - { - return super.toString() + " " + this.getId(); - } + } + + + public String toString() { + return super.toString() + " " + this.getId(); + } /** - * * @return This method might return a 'null' data. */ - public Location getLocation() - { - return location; - } + public Location getLocation() { + return location; + } /** - * * @param location Supplying 'null' is permitted. * @throws Exception Will throw an exception if the gate is open and an invalid (no gate frame) location is - * supplied. Note that the supplied 'location' will be set even if an exception is thrown. Note that this - * gate will be closed if an exception is thrown. + * supplied. Note that the supplied 'location' will be set even if an exception is thrown. Note that this + * gate will be closed if an exception is thrown. */ - public void setLocation(final Location location) throws Exception - { - this.location = location; - - if (isOpen) { + public void setLocation(final Location location) throws Exception { + this.location = location; + + if (isOpen) { findPortalBlocks(); - validate(); - } - else { + validate(); + } else { this.gateBlockLocations = new HashSet(); this.gateFrameBlocks = new HashSet(); } - } + } /** - * * @return This method might return a 'null' value. */ - public Location getExit() - { - return exit; - } + public Location getExit() { + return exit; + } /** - * * @param exit Supplying 'null' is permitted. * @throws Exception An exception will be thrown if 'null' data is supplied and this gate is open. Note that the - * supplied 'exit' will be set even if an exception is thrown. Note that this gate will be closed if an - * exception is thrown. + * supplied 'exit' will be set even if an exception is thrown. Note that this gate will be closed if an + * exception is thrown. */ - public void setExit(final Location exit) throws Exception - { - this.exit = exit; - validate(); - } + public void setExit(final Location exit) throws Exception { + this.exit = exit; + validate(); + } /** - * * @return This method will never return 'null'. */ - public String getId() - { - return id; - } + public String getId() { + return id; + } /** * Every gate should have an unique 'id'. You should therefore check if another gate with the same 'id' exists. * Note that this method will not check if another gate with the same 'id' exists! + * * @param id This parameter must not be 'null'. An exception will be thrown otherwise! */ - public void setId(final String id) - { + public void setId(final String id) { if (id == null) { throw new IllegalArgumentException("gate 'id' cannot be 'null'"); } - this.id = id.toLowerCase(); - } + this.id = id.toLowerCase(); + } - public boolean isHidden() - { - return isHidden; - } - - - public void setHidden(boolean isHidden) throws Exception - { - this.isHidden = isHidden; + public boolean isHidden() { + return isHidden; + } + + + public void setHidden(boolean isHidden) throws Exception { + this.isHidden = isHidden; this.validate(); - } - - - public boolean isOpen() - { - return isOpen; - } - - - public void setOpen(boolean isOpen) throws Exception - { - if (isOpen && !this.isOpen) { + } + + + public boolean isOpen() { + return isOpen; + } + + + public void setOpen(boolean isOpen) throws Exception { + if (isOpen && !this.isOpen) { findPortalBlocks(); - } + } - this.isOpen = isOpen; - validate(); - } + this.isOpen = isOpen; + validate(); + } - public void setAllowsVehicles(boolean allowsVehicles) - { + public void setAllowsVehicles(boolean allowsVehicles) { this.allowsVehicles = allowsVehicles; } - public boolean getAllowsVehicles() - { + public boolean getAllowsVehicles() { return this.allowsVehicles; } /** - * * @return Will never return 'null' but might return an empty Set. */ - public Set getGateBlockLocations() - { - return gateBlockLocations; - } + public Set getGateBlockLocations() { + return gateBlockLocations; + } /** - * * @return Will never return 'null' but might return an empty Set. */ - public Set getGateFrameBlocks() - { + public Set getGateFrameBlocks() { return gateFrameBlocks; } + protected void findPortalBlocks() { + gateBlockLocations = new HashSet(); + Set gateBlocks = FloodUtil.getGatePortalBlocks(location.getBlock()); - protected void findPortalBlocks() - { - gateBlockLocations = new HashSet(); - Set gateBlocks = FloodUtil.getGatePortalBlocks(location.getBlock()); - - if (gateBlocks != null) { - for (Block b : gateBlocks) { - gateBlockLocations.add(b.getLocation()); - } - } + if (gateBlocks != null) { + for (Block b : gateBlocks) { + gateBlockLocations.add(b.getLocation()); + } + } gateFrameBlocks = FloodUtil.getFrame(gateBlocks); - } + } - /** - * Checks if values attributes do add up; will close gate on wrong values. - */ - public void validate() throws Exception - { - if (!isOpen) { - return; - } - - if (location == null) { - isOpen = false; - this.gateBlockLocations = new HashSet(); - this.gateFrameBlocks = new HashSet(); + /** + * Checks if values attributes do add up; will close gate on wrong values. + */ + public void validate() throws Exception { + if (!isOpen) { + return; + } - throw new Exception("Gate got closed. It has no location."); - } - - if (exit == null) { + if (location == null) { isOpen = false; this.gateBlockLocations = new HashSet(); this.gateFrameBlocks = new HashSet(); - throw new Exception("Gate got closed. It has no exit."); - } - - if (gateBlockLocations.size() == 0) { + throw new Exception("Gate got closed. It has no location."); + } + + if (exit == null) { isOpen = false; this.gateBlockLocations = new HashSet(); this.gateFrameBlocks = new HashSet(); - throw new Exception("Gate got closed. The frame is missing or broken. (no gate blocks)"); - } + throw new Exception("Gate got closed. It has no exit."); + } + + if (gateBlockLocations.size() == 0) { + isOpen = false; + this.gateBlockLocations = new HashSet(); + this.gateFrameBlocks = new HashSet(); + + throw new Exception("Gate got closed. The frame is missing or broken. (no gate blocks)"); + } if (!isHidden() && Plugin.getPlugin().getConfig().getBoolean(ConfigurationUtil.confCheckForBrokenGateFramesKey)) { @@ -263,105 +237,101 @@ public class Gate implements ConfigurationSerializable } } } - } + } - /* - * INTERFACE: ConfigurationSerializable - */ - static protected String idKey = "id"; - static protected String locationKey = "location"; - static protected String gateBlocksKey = "gateBlocks"; - static protected String exitKey = "exit"; - static protected String isHiddenKey = "hidden"; - static protected String isOpenKey = "open"; - static protected String locationYawKey = "locationYaw"; - static protected String locationPitchKey = "locationPitch"; - static protected String exitYawKey = "exitYaw"; - static protected String exitPitchKey = "exitPitch"; - static protected String allowsVehiclesKey = "allowsVehiclesKey"; + /* + * INTERFACE: ConfigurationSerializable + */ + static protected String idKey = "id"; + static protected String locationKey = "location"; + static protected String gateBlocksKey = "gateBlocks"; + static protected String exitKey = "exit"; + static protected String isHiddenKey = "hidden"; + static protected String isOpenKey = "open"; + static protected String locationYawKey = "locationYaw"; + static protected String locationPitchKey = "locationPitch"; + static protected String exitYawKey = "exitYaw"; + static protected String exitPitchKey = "exitPitch"; + static protected String allowsVehiclesKey = "allowsVehiclesKey"; - - @SuppressWarnings("unchecked") - public Gate(Map map) - { + + @SuppressWarnings("unchecked") + public Gate(Map map) { try { - id = map.get(idKey).toString().toLowerCase(); + id = map.get(idKey).toString().toLowerCase(); - isHidden = (Boolean)map.get(isHiddenKey); - isOpen = (Boolean)map.get(isOpenKey); - - location = LocationUtil.deserializeLocation((Map) map.get(locationKey)); - exit = LocationUtil.deserializeLocation((Map) map.get(exitKey)); - - if (map.containsKey(exitPitchKey)) { - exit.setPitch(((Number)map.get(exitPitchKey)).floatValue()); - exit.setYaw(((Number)map.get(exitYawKey)).floatValue()); - } - - if (map.containsKey(locationPitchKey)) { - location.setPitch(((Number)map.get(locationPitchKey)).floatValue()); - location.setYaw(((Number)map.get(locationYawKey)).floatValue()); - } + isHidden = (Boolean) map.get(isHiddenKey); + isOpen = (Boolean) map.get(isOpenKey); + + location = LocationUtil.deserializeLocation((Map) map.get(locationKey)); + exit = LocationUtil.deserializeLocation((Map) map.get(exitKey)); + + if (map.containsKey(exitPitchKey)) { + exit.setPitch(((Number) map.get(exitPitchKey)).floatValue()); + exit.setYaw(((Number) map.get(exitYawKey)).floatValue()); + } + + if (map.containsKey(locationPitchKey)) { + location.setPitch(((Number) map.get(locationPitchKey)).floatValue()); + location.setYaw(((Number) map.get(locationYawKey)).floatValue()); + } if (map.containsKey(allowsVehiclesKey)) { - allowsVehicles = (Boolean)map.get(allowsVehiclesKey); + allowsVehicles = (Boolean) map.get(allowsVehiclesKey); + } + + gateBlockLocations = new HashSet(); + List> serializedGateBlocks = (List>) map.get(gateBlocksKey); + + for (Map sgb : serializedGateBlocks) { + gateBlockLocations.add(LocationUtil.deserializeLocation(sgb)); } - - gateBlockLocations = new HashSet(); - List> serializedGateBlocks = (List>)map.get(gateBlocksKey); - - for (Map sgb : serializedGateBlocks) { - gateBlockLocations.add(LocationUtil.deserializeLocation(sgb)); - } gateFrameBlocks = FloodUtil.getFrameWithLocations(gateBlockLocations); - } - catch (Exception e) { - Plugin.log("ERROR: Failed to load gate '" + id + "'! (" + e.getMessage() + ")"); - Plugin.log("NOTE: This gate will be removed from 'gates.yml' and added to 'invalid_gates.yml'!"); - - Plugin.getPlugin().getGatesManager().storeInvalidGate(map); - } + } catch (Exception e) { + Plugin.log("ERROR: Failed to load gate '" + id + "'! (" + e.getMessage() + ")"); + Plugin.log("NOTE: This gate will be removed from 'gates.yml' and added to 'invalid_gates.yml'!"); + + Plugin.getPlugin().getGatesManager().storeInvalidGate(map); + } try { validate(); // make sure to not write invalid stuff to disk - } - catch (Exception e) { + } catch (Exception e) { Plugin.log("The loaded gate " + this.getId() + " seems to be not valid: " + e.getMessage()); } - } - - - public Map serialize() - { - Map retVal = new HashMap(); - - retVal.put(idKey, id); - retVal.put(locationKey, LocationUtil.serializeLocation(location)); - retVal.put(exitKey, LocationUtil.serializeLocation(exit)); - retVal.put(isHiddenKey, isHidden); - retVal.put(isOpenKey, isOpen); + } + + + public Map serialize() { + Map retVal = new HashMap(); + + retVal.put(idKey, id); + retVal.put(locationKey, LocationUtil.serializeLocation(location)); + retVal.put(exitKey, LocationUtil.serializeLocation(exit)); + retVal.put(isHiddenKey, isHidden); + retVal.put(isOpenKey, isOpen); retVal.put(allowsVehiclesKey, allowsVehicles); - - if (exit != null) { - retVal.put(exitPitchKey, exit.getPitch()); - retVal.put(exitYawKey, exit.getYaw()); - } - - if (location != null) { - retVal.put(locationPitchKey, location.getPitch()); - retVal.put(locationYawKey, location.getYaw()); - } - - List> serializedGateBlocks = new ArrayList>(); - - for (Location l : gateBlockLocations) { - serializedGateBlocks.add(LocationUtil.serializeLocation(l)); - } - - retVal.put(gateBlocksKey, serializedGateBlocks); - - return retVal; - } + + if (exit != null) { + retVal.put(exitPitchKey, exit.getPitch()); + retVal.put(exitYawKey, exit.getYaw()); + } + + if (location != null) { + retVal.put(locationPitchKey, location.getPitch()); + retVal.put(locationYawKey, location.getYaw()); + } + + List> serializedGateBlocks = new ArrayList>(); + + for (Location l : gateBlockLocations) { + serializedGateBlocks.add(LocationUtil.serializeLocation(l)); + } + + retVal.put(gateBlocksKey, serializedGateBlocks); + + return retVal; + } } diff --git a/src/de/craftinc/gates/GateChangeListener.java b/src/de/craftinc/gates/GateChangeListener.java index 49917f6..fb41bca 100644 --- a/src/de/craftinc/gates/GateChangeListener.java +++ b/src/de/craftinc/gates/GateChangeListener.java @@ -19,13 +19,12 @@ package de.craftinc.gates; import java.util.Map; -public interface GateChangeListener -{ +public interface GateChangeListener { public static final String newGate = "GateChangeListener-newGate"; // value will be null public static final String removedGate = "GateChangeListener-removedGate"; // value will be null public static final String changedID = "GateChangeListener-changedID"; // value will be the old ID public static final String changedLocation = "GateChangeListener-changedLocation"; // value will the old location public static final String changedExit = "GateChangeListener-changedExit"; // value will be the old exit - public void gateChangedHandler(final Gate g, final MapchangeSet); + public void gateChangedHandler(final Gate g, final Map changeSet); } diff --git a/src/de/craftinc/gates/GatesManager.java b/src/de/craftinc/gates/GatesManager.java index b7e7797..bcc7fbb 100644 --- a/src/de/craftinc/gates/GatesManager.java +++ b/src/de/craftinc/gates/GatesManager.java @@ -35,8 +35,7 @@ import de.craftinc.gates.util.SimpleChunk; import de.craftinc.gates.util.SimpleLocation; -public class GatesManager -{ +public class GatesManager { protected File gatesConfigFile; protected FileConfiguration gatesConfig; protected static final String gatesPath = "gates"; // path to gates inside the yaml file @@ -57,38 +56,34 @@ public class GatesManager protected Set changeListeners = new HashSet(); - public void addGateChangeListener(GateChangeListener listener) - { + public void addGateChangeListener(GateChangeListener listener) { this.changeListeners.add(listener); } - public void removeGateChangeListener(GateChangeListener listener) - { + public void removeGateChangeListener(GateChangeListener listener) { this.changeListeners.remove(listener); } - public Gate getGateWithId(final String id) - { - return gatesById.get(id.toLowerCase()); - } - - - public Set getNearbyGates(final Chunk chunk) - { - SimpleChunk simpleChunk = new SimpleChunk(chunk); - return gatesByChunk.get(simpleChunk); - } + public Gate getGateWithId(final String id) { + return gatesById.get(id.toLowerCase()); + } + + + public Set getNearbyGates(final Chunk chunk) { + SimpleChunk simpleChunk = new SimpleChunk(chunk); + return gatesByChunk.get(simpleChunk); + } /** * Returns the closest gate. + * * @param location The location at which to look for a gate. * @return Might return null if there are no nearby gates. */ - public Gate getNearestGate(final Location location) - { + public Gate getNearestGate(final Location location) { Set nearbyGates = getNearbyGates(location.getChunk()); if (nearbyGates == null) { @@ -110,105 +105,97 @@ public class GatesManager return nearestGate; } - - public Gate getGateAtLocation(final Location location) - { - SimpleLocation simpleLocation = new SimpleLocation(location); - return gatesByLocation.get(simpleLocation); - } + + public Gate getGateAtLocation(final Location location) { + SimpleLocation simpleLocation = new SimpleLocation(location); + return gatesByLocation.get(simpleLocation); + } - public Gate getGateAtFrameLocation(final Location location) - { + public Gate getGateAtFrameLocation(final Location location) { SimpleLocation simpleLocation = new SimpleLocation(location); return gatesByFrameLocation.get(simpleLocation); } - - public void saveGatesToDisk() - { - if (storageFileIsInvalid) { + + public void saveGatesToDisk() { + if (storageFileIsInvalid) { Plugin.log(Level.SEVERE, "ERROR: Not saving gates to disk. Storage file is invalid or corrupted!"); return; } gatesConfig.set(gatesPath, gates); gatesConfig.set(storageVersionPath, storageVersion); - - try { - gatesConfig.save(gatesConfigFile); - Plugin.log("Saved gates to disk."); - } - catch (IOException e) { - Plugin.log(Level.SEVERE, "ERROR: Could not save gates to disk."); - e.printStackTrace(); - } - } - - - @SuppressWarnings("unchecked") - public boolean loadGatesFromDisk() - { - this.gatesConfigFile = new File(Plugin.getPlugin().getDataFolder(), "gates.yml"); - - if(!this.gatesConfigFile.exists()) { - try { + + try { + gatesConfig.save(gatesConfigFile); + Plugin.log("Saved gates to disk."); + } catch (IOException e) { + Plugin.log(Level.SEVERE, "ERROR: Could not save gates to disk."); + e.printStackTrace(); + } + } + + + @SuppressWarnings("unchecked") + public boolean loadGatesFromDisk() { + this.gatesConfigFile = new File(Plugin.getPlugin().getDataFolder(), "gates.yml"); + + if (!this.gatesConfigFile.exists()) { + try { boolean isNew = this.gatesConfigFile.createNewFile(); if (isNew) { Plugin.log(Level.FINEST, "Created gate storage file."); } - } - catch (IOException e) { + } catch (IOException e) { this.storageFileIsInvalid = true; - Plugin.log(Level.SEVERE, "Cannot create gate storage file! No gates will be persisted."); + Plugin.log(Level.SEVERE, "Cannot create gate storage file! No gates will be persisted."); return false; - } - } - - this.gatesConfig = new YamlConfiguration(); + } + } + + this.gatesConfig = new YamlConfiguration(); try { this.gatesConfig.load(this.gatesConfigFile); - } - catch (Exception e) { + } catch (Exception e) { this.storageFileIsInvalid = true; Plugin.log(Level.SEVERE, "Gate file on disk is invalid. No gates loaded. Plugin will be disabled! (" + Arrays.toString(e.getStackTrace()) + ")"); return false; } - this.gates = (List)gatesConfig.getList(gatesPath); + this.gates = (List) gatesConfig.getList(gatesPath); if (this.gates == null) { this.gates = new ArrayList(); } for (Object o : this.gates) { - - if (!(o instanceof Gate)) { + + if (!(o instanceof Gate)) { this.storageFileIsInvalid = true; - Plugin.log(Level.SEVERE, "Gate file on disk is invalid. No gates loaded. Plugin will be disabled! (Invalid gate class detected)"); + Plugin.log(Level.SEVERE, "Gate file on disk is invalid. No gates loaded. Plugin will be disabled! (Invalid gate class detected)"); return false; - } - } + } + } for (Gate g : this.gates) { try { g.validate(); - } - catch (Exception e) { + } catch (Exception e) { try { g.setOpen(false); + } catch (Exception ignored) { } - catch (Exception ignored) { } Plugin.log(Level.FINER, "closed gate '" + g.getId() + "' reason: " + e.getMessage()); } } - fillGatesById(); - fillGatesByChunk(); - fillGatesByLocation(); + fillGatesById(); + fillGatesByChunk(); + fillGatesByLocation(); fillGatesByFrameLocation(); Plugin.log("Loaded " + this.gates.size() + " gates."); @@ -231,11 +218,10 @@ public class GatesManager } return true; - } + } - protected int getChunkRadius() - { + protected int getChunkRadius() { if (this.chunkRadius == 0) { this.chunkRadius = Plugin.getPlugin().getConfig().getInt(ConfigurationUtil.confPlayerGateBlockUpdateRadiusKey); this.chunkRadius = this.chunkRadius >> 4; @@ -245,21 +231,19 @@ public class GatesManager } - protected void fillGatesById() - { - gatesById = new HashMap((int)(gates.size() * 1.25)); - - for (Gate g : gates) { - this.addGateWithId(g); - } - } + protected void fillGatesById() { + gatesById = new HashMap((int) (gates.size() * 1.25)); + + for (Gate g : gates) { + this.addGateWithId(g); + } + } - protected void fillGatesByChunk() - { - HashSet chunksUsedByGates = new HashSet(); - - for (Gate g : gates) { + protected void fillGatesByChunk() { + HashSet chunksUsedByGates = new HashSet(); + + for (Gate g : gates) { if (g.getLocation() != null) { @@ -268,59 +252,57 @@ public class GatesManager int x = c.getX(); int z = c.getZ(); - for (int i = x-getChunkRadius(); i < x+getChunkRadius(); i++) { + for (int i = x - getChunkRadius(); i < x + getChunkRadius(); i++) { - for (int j = z-getChunkRadius(); j < z+getChunkRadius(); j++) { + for (int j = z - getChunkRadius(); j < z + getChunkRadius(); j++) { chunksUsedByGates.add(new SimpleChunk(i, j, c.getWorld())); } } } - } - - gatesByChunk = new HashMap>((int)(chunksUsedByGates.size() * 1.25)); - - for (Gate g : gates) { - this.addGateByChunk(g); - } - } + } + + gatesByChunk = new HashMap>((int) (chunksUsedByGates.size() * 1.25)); + + for (Gate g : gates) { + this.addGateByChunk(g); + } + } - protected void fillGatesByLocation() - { - Set gateBlocks = new HashSet(); - - for (Gate g : gates) { + protected void fillGatesByLocation() { + Set gateBlocks = new HashSet(); + + for (Gate g : gates) { for (Location l : g.getGateBlockLocations()) { gateBlocks.add(l); Location headLocation = new Location(l.getWorld(), - l.getX(), - l.getY()+1, - l.getZ()); + l.getX(), + l.getY() + 1, + l.getZ()); gateBlocks.add(headLocation); } - } - - gatesByLocation = new HashMap((int)(gateBlocks.size()*1.25)); - - for (Gate g : gates) { - this.addGateByLocations(g); - } - } + } + + gatesByLocation = new HashMap((int) (gateBlocks.size() * 1.25)); + + for (Gate g : gates) { + this.addGateByLocations(g); + } + } - protected void fillGatesByFrameLocation() - { + protected void fillGatesByFrameLocation() { int numFrameBlocks = 0; for (Gate g : gates) { numFrameBlocks += g.gateFrameBlocks.size(); } - gatesByFrameLocation = new HashMap((int)(numFrameBlocks*1.25)); + gatesByFrameLocation = new HashMap((int) (numFrameBlocks * 1.25)); for (Gate g : gates) { this.addGateByFrameLocations(g); @@ -328,21 +310,18 @@ public class GatesManager } - protected void removeGateById(final String id) - { - gatesById.remove(id); - } + protected void removeGateById(final String id) { + gatesById.remove(id); + } - protected void addGateWithId(final Gate g) - { - gatesById.put(g.getId(), g); - } + protected void addGateWithId(final Gate g) { + gatesById.put(g.getId(), g); + } - protected void removeGateByLocation(final Set gateBlocks) - { - if (gateBlocks != null) { + protected void removeGateByLocation(final Set gateBlocks) { + if (gateBlocks != null) { for (Location l : gateBlocks) { @@ -353,11 +332,10 @@ public class GatesManager gatesByLocation.remove(headLocation); } } - } + } - protected void removeGateByFrameLocation(final Set gateFrameBlocks) - { + protected void removeGateByFrameLocation(final Set gateFrameBlocks) { if (gateFrameBlocks != null) { for (Block block : gateFrameBlocks) { @@ -368,21 +346,19 @@ public class GatesManager } - protected void addGateByLocations(final Gate g) - { + protected void addGateByLocations(final Gate g) { for (Location l : g.getGateBlockLocations()) { - SimpleLocation sl = new SimpleLocation(l); - gatesByLocation.put(sl, g); + SimpleLocation sl = new SimpleLocation(l); + gatesByLocation.put(sl, g); SimpleLocation headLocation = new SimpleLocation(l, true); gatesByLocation.put(headLocation, g); - } - } + } + } - protected void addGateByFrameLocations(final Gate g) - { + protected void addGateByFrameLocations(final Gate g) { for (Block block : g.getGateFrameBlocks()) { SimpleLocation sl = new SimpleLocation(block.getLocation()); gatesByFrameLocation.put(sl, g); @@ -390,20 +366,19 @@ public class GatesManager } - protected void removeGateFromChunk(final Gate g, final Location l) - { - if (l != null) { + protected void removeGateFromChunk(final Gate g, final Location l) { + if (l != null) { Chunk c = l.getChunk(); int x = c.getX(); int z = c.getZ(); - for (int i = x-getChunkRadius(); i < x+getChunkRadius(); i++) { + for (int i = x - getChunkRadius(); i < x + getChunkRadius(); i++) { - for (int j = z-getChunkRadius(); j < z+getChunkRadius(); j++) { + for (int j = z - getChunkRadius(); j < z + getChunkRadius(); j++) { SimpleChunk sc = new SimpleChunk(i, j, c.getWorld()); - Set gatesInChunk = gatesByChunk.get(sc); + Set gatesInChunk = gatesByChunk.get(sc); if (gatesInChunk != null) { gatesInChunk.remove(g); @@ -412,22 +387,21 @@ public class GatesManager } } } - } + } - protected void addGateByChunk(final Gate g) - { + protected void addGateByChunk(final Gate g) { Location gateLocation = g.getLocation(); - if (gateLocation != null) { + if (gateLocation != null) { Chunk c = g.getLocation().getChunk(); int x = c.getX(); int z = c.getZ(); - for (int i = x-getChunkRadius(); i < x+getChunkRadius(); i++) { + for (int i = x - getChunkRadius(); i < x + getChunkRadius(); i++) { - for (int j = z-getChunkRadius(); j < z+getChunkRadius(); j++) { + for (int j = z - getChunkRadius(); j < z + getChunkRadius(); j++) { SimpleChunk sc = new SimpleChunk(i, j, c.getWorld()); @@ -442,67 +416,63 @@ public class GatesManager } } } - } - - - public void storeInvalidGate(Map map) - { - File invalidGatesFile = new File(Plugin.getPlugin().getDataFolder(), "invalid_gates.yml"); - Boolean invalidGatesFileExists = invalidGatesFile.exists(); - - try { - FileWriter fileWriter = new FileWriter(invalidGatesFile, true); - - if (!invalidGatesFileExists) { - fileWriter.write("gates:\n"); - } - - fileWriter.write("- ==: "); - fileWriter.write(map.get("==").toString() + "\n"); - map.remove("=="); - - fileWriter.write("\topen: false\n"); - map.remove("open"); - - fileWriter.write("\tgateBlocks: []\n"); - map.remove("gateBlocks"); - - - for (String key : map.keySet()) { - Object value = map.get(key); - - fileWriter.write("\t" + key + ": "); - - if (value instanceof Map) { - fileWriter.write("\n"); - - @SuppressWarnings("unchecked") - Map valueMap = (Map)value; - - for (String k : valueMap.keySet()) { - Object v = valueMap.get(k); - - fileWriter.write("\t\t" + k + ": " + v.toString() + "\n"); - } + } - } - else { - fileWriter.write(value.toString() + "\n"); - } - } - - fileWriter.close(); - } - catch (IOException e) { - Plugin.log("ERROR: Could not save invalid gates to disk. Reason: \n" + Arrays.toString(e.getStackTrace())); - } - } - - - public void handleGateIdChange(final Gate g, final String oldId) - { - this.removeGateById(oldId); - this.addGateWithId(g); + + public void storeInvalidGate(Map map) { + File invalidGatesFile = new File(Plugin.getPlugin().getDataFolder(), "invalid_gates.yml"); + Boolean invalidGatesFileExists = invalidGatesFile.exists(); + + try { + FileWriter fileWriter = new FileWriter(invalidGatesFile, true); + + if (!invalidGatesFileExists) { + fileWriter.write("gates:\n"); + } + + fileWriter.write("- ==: "); + fileWriter.write(map.get("==").toString() + "\n"); + map.remove("=="); + + fileWriter.write("\topen: false\n"); + map.remove("open"); + + fileWriter.write("\tgateBlocks: []\n"); + map.remove("gateBlocks"); + + + for (String key : map.keySet()) { + Object value = map.get(key); + + fileWriter.write("\t" + key + ": "); + + if (value instanceof Map) { + fileWriter.write("\n"); + + @SuppressWarnings("unchecked") + Map valueMap = (Map) value; + + for (String k : valueMap.keySet()) { + Object v = valueMap.get(k); + + fileWriter.write("\t\t" + k + ": " + v.toString() + "\n"); + } + + } else { + fileWriter.write(value.toString() + "\n"); + } + } + + fileWriter.close(); + } catch (IOException e) { + Plugin.log("ERROR: Could not save invalid gates to disk. Reason: \n" + Arrays.toString(e.getStackTrace())); + } + } + + + public void handleGateIdChange(final Gate g, final String oldId) { + this.removeGateById(oldId); + this.addGateWithId(g); Map changeSet = new HashMap(); changeSet.put(GateChangeListener.changedID, oldId); @@ -510,19 +480,18 @@ public class GatesManager for (GateChangeListener l : this.changeListeners) { l.gateChangedHandler(g, changeSet); } - } - - - public void handleGateLocationChange(final Gate g, + } + + + public void handleGateLocationChange(final Gate g, final Location oldLocation, final Set oldGateBlockLocations, - final Set oldGateFrameBlocks) - { - this.removeGateFromChunk(g, oldLocation); - this.addGateByChunk(g); - - this.removeGateByLocation(oldGateBlockLocations); - this.addGateByLocations(g); + final Set oldGateFrameBlocks) { + this.removeGateFromChunk(g, oldLocation); + this.addGateByChunk(g); + + this.removeGateByLocation(oldGateBlockLocations); + this.addGateByLocations(g); this.removeGateByFrameLocation(oldGateFrameBlocks); this.addGateByFrameLocations(g); @@ -533,11 +502,10 @@ public class GatesManager for (GateChangeListener l : this.changeListeners) { l.gateChangedHandler(g, changeSet); } - } + } - public void handleGateExitChange(final Gate g, final Location oldExit) - { + public void handleGateExitChange(final Gate g, final Location oldExit) { // nothing to do Map changeSet = new HashMap(); @@ -547,15 +515,14 @@ public class GatesManager l.gateChangedHandler(g, changeSet); } } - - - public void handleNewGate(final Gate g) - { - this.gates.add(g); + + + public void handleNewGate(final Gate g) { + this.gates.add(g); this.addGateByChunk(g); - this.addGateByLocations(g); - this.addGateWithId(g); + this.addGateByLocations(g); + this.addGateWithId(g); this.addGateByFrameLocations(g); @@ -565,16 +532,15 @@ public class GatesManager for (GateChangeListener l : this.changeListeners) { l.gateChangedHandler(g, changeSet); } - } - - - public void handleDeletion(final Gate g) - { + } + + + public void handleDeletion(final Gate g) { this.gates.remove(g); this.removeGateById(g.getId()); - this.removeGateFromChunk(g, g.getLocation()); - this.removeGateByLocation(g.getGateBlockLocations()); + this.removeGateFromChunk(g, g.getLocation()); + this.removeGateByLocation(g.getGateBlockLocations()); this.removeGateByFrameLocation(g.getGateFrameBlocks()); Map changeSet = new HashMap(); @@ -583,17 +549,15 @@ public class GatesManager for (GateChangeListener l : this.changeListeners) { l.gateChangedHandler(g, changeSet); } - } - - - public boolean gateExists(final String id) - { - return gatesById.containsKey(id.toLowerCase()); - } - - - public List allGates () - { - return gates; - } + } + + + public boolean gateExists(final String id) { + return gatesById.containsKey(id.toLowerCase()); + } + + + public List allGates() { + return gates; + } } diff --git a/src/de/craftinc/gates/Plugin.java b/src/de/craftinc/gates/Plugin.java index d690f37..ef86dfa 100644 --- a/src/de/craftinc/gates/Plugin.java +++ b/src/de/craftinc/gates/Plugin.java @@ -38,16 +38,15 @@ import de.craftinc.gates.commands.*; import org.mcstats.Metrics; -public class Plugin extends JavaPlugin -{ - public static final String permissionInfo = "craftincgates.info"; - public static final String permissionManage = "craftincgates.manage"; - public static final String permissionUse = "craftincgates.use"; - - private static Plugin instance; - private static Permission permission; - - protected String baseCommand; +public class Plugin extends JavaPlugin { + public static final String permissionInfo = "craftincgates.info"; + public static final String permissionManage = "craftincgates.manage"; + public static final String permissionUse = "craftincgates.use"; + + private static Plugin instance; + private static Permission permission; + + protected String baseCommand; protected List commands = new ArrayList(); protected GatesManager gatesManager = new GatesManager(); @@ -57,118 +56,105 @@ public class Plugin extends JavaPlugin protected PlayerChangedWorldListener worldChangeListener = new PlayerChangedWorldListener(); protected PlayerJoinListener joinListener = new PlayerJoinListener(); protected BlockBreakListener blockBreakListener = new BlockBreakListener(); - - - public Plugin() - { - instance = this; - } - - - public static Plugin getPlugin() - { - return instance; - } - - - public GatesManager getGatesManager() - { - return gatesManager; - } - @Override - public void onLoad() - { - ConfigurationSerialization.registerClass(Gate.class); - } + public Plugin() { + instance = this; + } - protected void setupPermissions() - { - if (getServer().getPluginManager().getPlugin("Vault") == null) { + public static Plugin getPlugin() { + return instance; + } + + + public GatesManager getGatesManager() { + return gatesManager; + } + + + @Override + public void onLoad() { + ConfigurationSerialization.registerClass(Gate.class); + } + + + protected void setupPermissions() { + if (getServer().getPluginManager().getPlugin("Vault") == null) { return; } - RegisteredServiceProvider rsp = getServer().getServicesManager().getRegistration(Permission.class); - - if (rsp != null) - { - log("Using permission provider provided by Vault."); - permission = rsp.getProvider(); - } - else - { - log("Not using setup permission provider provided by Vault."); - } - } - + RegisteredServiceProvider rsp = getServer().getServicesManager().getRegistration(Permission.class); - @Override - public void onDisable() - { - // Save gates - gatesManager.saveGatesToDisk(); - - log("Disabled"); - } + if (rsp != null) { + log("Using permission provider provided by Vault."); + permission = rsp.getProvider(); + } else { + log("Not using setup permission provider provided by Vault."); + } + } - - @Override - public void onEnable() - { - // Setup Metrics + + @Override + public void onDisable() { + // Save gates + gatesManager.saveGatesToDisk(); + + log("Disabled"); + } + + + @Override + public void onEnable() { + // Setup Metrics try { Metrics metrics = new Metrics(this); metrics.start(); - } - catch (IOException e) { + } catch (IOException e) { log("Failed to start metrics!"); } - // Setup configuration + // Setup configuration this.saveDefaultConfig(); - // Setup permissions - setupPermissions(); - - // Add the commands - commands.add(new CommandHelp()); - commands.add(new CommandNew()); - commands.add(new CommandRemove()); - commands.add(new CommandLocation()); - commands.add(new CommandExit()); - commands.add(new CommandOpen()); - commands.add(new CommandRename()); - commands.add(new CommandClose()); - commands.add(new CommandList()); - commands.add(new CommandInfo()); - commands.add(new CommandHide()); - commands.add(new CommandUnhide()); + // Setup permissions + setupPermissions(); + + // Add the commands + commands.add(new CommandHelp()); + commands.add(new CommandNew()); + commands.add(new CommandRemove()); + commands.add(new CommandLocation()); + commands.add(new CommandExit()); + commands.add(new CommandOpen()); + commands.add(new CommandRename()); + commands.add(new CommandClose()); + commands.add(new CommandList()); + commands.add(new CommandInfo()); + commands.add(new CommandHide()); + commands.add(new CommandUnhide()); commands.add(new CommandExitOpen()); commands.add(new CommandNearby()); commands.add(new CommandAllowRiding()); commands.add(new CommandDenyRiding()); - - // Register events - this.registerEventListeners(); - - // Load gates - boolean success = gatesManager.loadGatesFromDisk(); + + // Register events + this.registerEventListeners(); + + // Load gates + boolean success = gatesManager.loadGatesFromDisk(); if (success) { log("Enabled"); - } - else { + } else { PluginManager pm = this.getServer().getPluginManager(); pm.disablePlugin(this); } - } + } - protected void registerEventListeners() - { + protected void registerEventListeners() { PluginManager pm = this.getServer().getPluginManager(); pm.registerEvents(this.moveListener, this); @@ -182,74 +168,66 @@ public class Plugin extends JavaPlugin } } - - // -------------------------------------------- // - // Commands - // -------------------------------------------- // - - public String getBaseCommand() - { - if (this.baseCommand != null) - return this.baseCommand; - - Map> Commands = this.getDescription().getCommands(); - - this.baseCommand = Commands.keySet().iterator().next(); - - return this.baseCommand; - } - - - @Override - public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) - { - List parameters = new ArrayList(Arrays.asList(args)); - this.handleCommand(sender, parameters); - return true; - } - - - public void handleCommand(CommandSender sender, List parameters) - { - if (parameters.size() == 0) - { - this.commands.get(0).execute(sender, parameters); - return; - } - - String commandName = parameters.get(0).toLowerCase(); - parameters.remove(0); - - for (BaseCommand fcommand : this.commands) - { - if (fcommand.getAliases().contains(commandName)) - { - fcommand.execute(sender, parameters); - return; - } - } - - sender.sendMessage(ChatColor.RED + "Unknown gate-command \"" + commandName + "\"." + - ChatColor.GREEN + " Try " + "/" + getBaseCommand() + " help"); - } - - - /* - * Logging - */ - public static void log(String msg) - { - log(Level.INFO, msg); - } - - - public static void log(Level level, String msg) - { - Logger.getLogger("Minecraft").log(level, "["+instance.getDescription().getFullName()+"] "+msg); - } + + // -------------------------------------------- // + // Commands + // -------------------------------------------- // + + public String getBaseCommand() { + if (this.baseCommand != null) + return this.baseCommand; + + Map> Commands = this.getDescription().getCommands(); + + this.baseCommand = Commands.keySet().iterator().next(); + + return this.baseCommand; + } - public static Permission getPermission() { - return permission; - } + @Override + public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) { + List parameters = new ArrayList(Arrays.asList(args)); + this.handleCommand(sender, parameters); + return true; + } + + + public void handleCommand(CommandSender sender, List parameters) { + if (parameters.size() == 0) { + this.commands.get(0).execute(sender, parameters); + return; + } + + String commandName = parameters.get(0).toLowerCase(); + parameters.remove(0); + + for (BaseCommand fcommand : this.commands) { + if (fcommand.getAliases().contains(commandName)) { + fcommand.execute(sender, parameters); + return; + } + } + + sender.sendMessage(ChatColor.RED + "Unknown gate-command \"" + commandName + "\"." + + ChatColor.GREEN + " Try " + "/" + getBaseCommand() + " help"); + } + + + /* + * Logging + */ + public static void log(String msg) { + log(Level.INFO, msg); + } + + + public static void log(Level level, String msg) { + Logger.getLogger("Minecraft").log(level, "[" + instance.getDescription().getFullName() + "] " + msg); + } + + + public static Permission getPermission() { + return permission; + } } diff --git a/src/de/craftinc/gates/listeners/BlockBreakListener.java b/src/de/craftinc/gates/listeners/BlockBreakListener.java index 2e89291..a96b27c 100644 --- a/src/de/craftinc/gates/listeners/BlockBreakListener.java +++ b/src/de/craftinc/gates/listeners/BlockBreakListener.java @@ -25,11 +25,9 @@ import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.block.BlockBreakEvent; -public class BlockBreakListener implements Listener -{ +public class BlockBreakListener implements Listener { @EventHandler(priority = EventPriority.MONITOR) - public void onBlockBreak(BlockBreakEvent event) - { + public void onBlockBreak(BlockBreakEvent event) { if (event.isCancelled()) { return; } @@ -39,8 +37,8 @@ public class BlockBreakListener implements Listener if (gate != null && !gate.isHidden()) { try { gate.setOpen(false); + } catch (Exception ignored) { } - catch (Exception ignored) { } GateBlockChangeSender.updateGateBlocks(gate); } diff --git a/src/de/craftinc/gates/listeners/PlayerChangedWorldListener.java b/src/de/craftinc/gates/listeners/PlayerChangedWorldListener.java index 5284437..dace944 100644 --- a/src/de/craftinc/gates/listeners/PlayerChangedWorldListener.java +++ b/src/de/craftinc/gates/listeners/PlayerChangedWorldListener.java @@ -24,11 +24,9 @@ import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerChangedWorldEvent; -public class PlayerChangedWorldListener implements Listener -{ +public class PlayerChangedWorldListener implements Listener { @EventHandler(priority = EventPriority.NORMAL) - public void onPlayerChangeWorld(PlayerChangedWorldEvent event) - { + public void onPlayerChangeWorld(PlayerChangedWorldEvent event) { GateBlockChangeSender.updateGateBlocks(event.getPlayer()); } } diff --git a/src/de/craftinc/gates/listeners/PlayerJoinListener.java b/src/de/craftinc/gates/listeners/PlayerJoinListener.java index 8012618..da33562 100644 --- a/src/de/craftinc/gates/listeners/PlayerJoinListener.java +++ b/src/de/craftinc/gates/listeners/PlayerJoinListener.java @@ -23,11 +23,9 @@ import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerJoinEvent; -public class PlayerJoinListener implements Listener -{ +public class PlayerJoinListener implements Listener { @EventHandler(priority = EventPriority.NORMAL) - public void onPlayerJoin(PlayerJoinEvent event) - { + public void onPlayerJoin(PlayerJoinEvent event) { GateBlockChangeSender.updateGateBlocks(event.getPlayer()); } } diff --git a/src/de/craftinc/gates/listeners/PlayerMoveListener.java b/src/de/craftinc/gates/listeners/PlayerMoveListener.java index 1a88b93..52bd70a 100644 --- a/src/de/craftinc/gates/listeners/PlayerMoveListener.java +++ b/src/de/craftinc/gates/listeners/PlayerMoveListener.java @@ -38,71 +38,68 @@ import de.craftinc.gates.Plugin; import org.bukkit.scheduler.BukkitScheduler; -public class PlayerMoveListener implements Listener -{ - protected HashMap lastNoPermissionMessages = new HashMap(); - - @EventHandler(priority = EventPriority.NORMAL) - public void onPlayerMove(PlayerMoveEvent event) - { - if (event.isCancelled()) { - return; - } +public class PlayerMoveListener implements Listener { + protected HashMap lastNoPermissionMessages = new HashMap(); + + @EventHandler(priority = EventPriority.NORMAL) + public void onPlayerMove(PlayerMoveEvent event) { + if (event.isCancelled()) { + return; + } if (event.getFrom().getChunk() != event.getTo().getChunk()) { GateBlockChangeSender.updateGateBlocks(event.getPlayer(), event.getTo()); } - final GatesManager gateManager = Plugin.getPlugin().getGatesManager(); - final Gate gateAtLocation = gateManager.getGateAtLocation(event.getTo()); + final GatesManager gateManager = Plugin.getPlugin().getGatesManager(); + final Gate gateAtLocation = gateManager.getGateAtLocation(event.getTo()); - if ((gateAtLocation == null) || !gateAtLocation.isOpen()) { + if ((gateAtLocation == null) || !gateAtLocation.isOpen()) { + return; + } + + // Check for permission + if (!hasPermission(event.getPlayer(), gateAtLocation) + && Plugin.getPlugin().getConfig().getBoolean(ConfigurationUtil.confShowTeleportNoPermissionMessageKey)) { + + final String playerName = event.getPlayer().getName(); + + if (playerName == null) { return; - } + } - // Check for permission - if (!hasPermission(event.getPlayer(), gateAtLocation) - && Plugin.getPlugin().getConfig().getBoolean(ConfigurationUtil.confShowTeleportNoPermissionMessageKey)) { - - final String playerName = event.getPlayer().getName(); - - if (playerName == null) { - return; - } - - // get the current time - final Long now = Calendar.getInstance().getTimeInMillis(); - - // do not display messages more often than once per second - if (!this.lastNoPermissionMessages.containsKey(playerName) || this.lastNoPermissionMessages.get(playerName) < now - 10000L) { + // get the current time + final Long now = Calendar.getInstance().getTimeInMillis(); + + // do not display messages more often than once per second + if (!this.lastNoPermissionMessages.containsKey(playerName) || this.lastNoPermissionMessages.get(playerName) < now - 10000L) { final String noPermissionString = Plugin.getPlugin().getConfig().getString(ConfigurationUtil.confGateTeleportNoPermissionMessageKey); event.getPlayer().sendMessage(ChatColor.RED + noPermissionString); - this.lastNoPermissionMessages.put(playerName, now); - } - } - else { + this.lastNoPermissionMessages.put(playerName, now); + } + } else { this.teleportPlayer(event.getPlayer(), gateAtLocation); } - } + } /** * Teleports a player. + * * @param player The player to teleport. - * @param gate The gate to which exit the player will be teleported. + * @param gate The gate to which exit the player will be teleported. */ - private void teleportPlayer(final Player player, final Gate gate) - { + private void teleportPlayer(final Player player, final Gate gate) { // Destination final Float newYaw = gate.getExit().getYaw() - gate.getLocation().getYaw() + player.getLocation().getYaw(); - final Location destLocation = new Location( gate.getExit().getWorld(), - gate.getExit().getX(), - gate.getExit().getY(), - gate.getExit().getZ(), - newYaw, - player.getLocation().getPitch() - ); + final Location destLocation = new Location(gate.getExit().getWorld(), + gate.getExit().getX(), + gate.getExit().getY(), + gate.getExit().getZ(), + newYaw, + player.getLocation().getPitch() + ); // Riding final Entity vehicle = player.getVehicle(); @@ -135,12 +132,11 @@ public class PlayerMoveListener implements Listener destLocation.getChunk().load(); // load the destination chunk, no new entity will be created otherwise scheduler.scheduleSyncDelayedTask(plugin, new Runnable() { - public void run() - { + public void run() { // FIXME: the code below should be executed after the chunk got loaded and not after a fixed time! // create a new entity at the destination location - final Vehicle newVehicle = VehicleCloner.clone((Vehicle)vehicle, destLocation); + final Vehicle newVehicle = VehicleCloner.clone((Vehicle) vehicle, destLocation); newVehicle.setPassenger(player); } }, 2); @@ -151,19 +147,17 @@ public class PlayerMoveListener implements Listener final String teleportMessage = Plugin.getPlugin().getConfig().getString(ConfigurationUtil.confGateTeleportMessageKey); player.sendMessage(ChatColor.DARK_AQUA + teleportMessage); } - } - - - protected boolean hasPermission(final Player player, final Gate gate) - { - if (Plugin.getPermission() == null) { // fallback: use the standard bukkit permission system - return player.hasPermission(Plugin.permissionUse); - } - else { - final boolean permAtLocation = Plugin.getPermission().has(gate.getLocation().getWorld(), player.getName(), Plugin.permissionUse); - final boolean permAtExit = Plugin.getPermission().has(gate.getExit().getWorld(), player.getName(), Plugin.permissionUse); - - return permAtLocation && permAtExit; - } + } + + + protected boolean hasPermission(final Player player, final Gate gate) { + if (Plugin.getPermission() == null) { // fallback: use the standard bukkit permission system + return player.hasPermission(Plugin.permissionUse); + } else { + final boolean permAtLocation = Plugin.getPermission().has(gate.getLocation().getWorld(), player.getName(), Plugin.permissionUse); + final boolean permAtExit = Plugin.getPermission().has(gate.getExit().getWorld(), player.getName(), Plugin.permissionUse); + + return permAtLocation && permAtExit; + } } } diff --git a/src/de/craftinc/gates/listeners/PlayerRespawnListener.java b/src/de/craftinc/gates/listeners/PlayerRespawnListener.java index e60f79a..0649e21 100644 --- a/src/de/craftinc/gates/listeners/PlayerRespawnListener.java +++ b/src/de/craftinc/gates/listeners/PlayerRespawnListener.java @@ -24,11 +24,9 @@ import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerRespawnEvent; -public class PlayerRespawnListener implements Listener -{ +public class PlayerRespawnListener implements Listener { @EventHandler(priority = EventPriority.NORMAL) - public void onPlayerRespawn(PlayerRespawnEvent event) - { + public void onPlayerRespawn(PlayerRespawnEvent event) { GateBlockChangeSender.updateGateBlocks(event.getPlayer(), event.getRespawnLocation(), true); } } diff --git a/src/de/craftinc/gates/listeners/PlayerTeleportListener.java b/src/de/craftinc/gates/listeners/PlayerTeleportListener.java index 23f6d41..3b5d079 100644 --- a/src/de/craftinc/gates/listeners/PlayerTeleportListener.java +++ b/src/de/craftinc/gates/listeners/PlayerTeleportListener.java @@ -23,11 +23,9 @@ import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerTeleportEvent; -public class PlayerTeleportListener implements Listener -{ +public class PlayerTeleportListener implements Listener { @EventHandler(priority = EventPriority.MONITOR) - public void onPlayerTeleport(PlayerTeleportEvent event) - { + public void onPlayerTeleport(PlayerTeleportEvent event) { if (event.isCancelled()) { return; } diff --git a/src/de/craftinc/gates/persistence/LocationUtil.java b/src/de/craftinc/gates/persistence/LocationUtil.java index e90dba7..58b7cd4 100644 --- a/src/de/craftinc/gates/persistence/LocationUtil.java +++ b/src/de/craftinc/gates/persistence/LocationUtil.java @@ -18,34 +18,33 @@ package de.craftinc.gates.persistence; import java.util.HashMap; import java.util.Map; + import org.bukkit.Location; import org.bukkit.World; import de.craftinc.gates.Plugin; -public class LocationUtil -{ - protected final static String worldKey = "world"; - protected final static String xKey = "x"; - protected final static String yKey = "y"; - protected final static String zKey = "z"; +public class LocationUtil { + protected final static String worldKey = "world"; + protected final static String xKey = "x"; + protected final static String yKey = "y"; + protected final static String zKey = "z"; - - protected static World getWorld(final String name) throws Exception - { - if (name == null) { + + protected static World getWorld(final String name) throws Exception { + if (name == null) { throw new IllegalArgumentException("The name of the world must not be 'null"); } World world = Plugin.getPlugin().getServer().getWorld(name); - if (world == null) { - throw new Exception("World '" + name + "' does not exists anymore! Cannot get instance!"); - } - - return world; - } + if (world == null) { + throw new Exception("World '" + name + "' does not exists anymore! Cannot get instance!"); + } + + return world; + } /** @@ -55,46 +54,43 @@ public class LocationUtil * @param l The location to serialize. Supplying 'null' is ok.. * @return A Map object ready for storing inside a yaml file. Will return 'null' if 'l' is null. */ - public static Map serializeLocation(final Location l) - { - if (l == null) { - return null; - } - - Map serializedLocation = new HashMap(); - - serializedLocation.put(worldKey, l.getWorld().getName()); - serializedLocation.put(xKey, l.getX()); - serializedLocation.put(yKey, l.getY()); - serializedLocation.put(zKey, l.getZ()); - - return serializedLocation; - } + public static Map serializeLocation(final Location l) { + if (l == null) { + return null; + } + + Map serializedLocation = new HashMap(); + + serializedLocation.put(worldKey, l.getWorld().getName()); + serializedLocation.put(xKey, l.getX()); + serializedLocation.put(yKey, l.getY()); + serializedLocation.put(zKey, l.getZ()); + + return serializedLocation; + } /** - * * @param map A map generated with the 'serializeLocation' method. Supplying 'null' is ok. * @return A deserialized location. This method will return 'null' if 'map' is null! * @throws Exception This method will throw an exception if the world of the supplied serialized location - * does not exist or if 'map' does not contain all necessary keys! + * does not exist or if 'map' does not contain all necessary keys! */ - public static Location deserializeLocation(final Map map) throws Exception - { - if (map == null) { - return null; - } - - World w = getWorld((String)map.get(worldKey)); + public static Location deserializeLocation(final Map map) throws Exception { + if (map == null) { + return null; + } - Number x = (Number)map.get(xKey); - Number y = (Number)map.get(yKey); - Number z = (Number)map.get(zKey); + World w = getWorld((String) map.get(worldKey)); + + Number x = (Number) map.get(xKey); + Number y = (Number) map.get(yKey); + Number z = (Number) map.get(zKey); if (x == null || y == null || z == null) { throw new IllegalArgumentException("Supplied map is invalid x, y or z coordinate was not supplied"); } - - return new Location(w, x.doubleValue(), y.doubleValue(), z.doubleValue()); - } + + return new Location(w, x.doubleValue(), y.doubleValue(), z.doubleValue()); + } } diff --git a/src/de/craftinc/gates/persistence/MigrationUtil.java b/src/de/craftinc/gates/persistence/MigrationUtil.java index bcd9cc4..314f38b 100644 --- a/src/de/craftinc/gates/persistence/MigrationUtil.java +++ b/src/de/craftinc/gates/persistence/MigrationUtil.java @@ -27,30 +27,25 @@ import java.util.List; import java.util.logging.Level; -public class MigrationUtil -{ - public static boolean performMigration(int storageVersion, int currentVersion, List gates) - { +public class MigrationUtil { + public static boolean performMigration(int storageVersion, int currentVersion, List gates) { if (storageVersion == 0 && currentVersion >= 2) { removePortalBlocks(gates); updateAllowVehicles(gates); return true; - } - else if (storageVersion == 1 && currentVersion >= 2) { + } else if (storageVersion == 1 && currentVersion >= 2) { updateAllowVehicles(gates); return true; - } - else { + } else { Plugin.log(Level.SEVERE, "Supplied storage version is currently not supported! Make sure you have the latest version of Craft Inc. Gates installed. Plugin will be disabled!"); return false; } } - protected static void removePortalBlocks(List gates) - { + protected static void removePortalBlocks(List gates) { for (Gate g : gates) { for (Location l : g.getGateBlockLocations()) { @@ -64,8 +59,7 @@ public class MigrationUtil } - protected static void updateAllowVehicles(List gates) - { + protected static void updateAllowVehicles(List gates) { for (Gate g : gates) { g.setAllowsVehicles(true); diff --git a/src/de/craftinc/gates/util/ConfigurationUtil.java b/src/de/craftinc/gates/util/ConfigurationUtil.java index ed49eba..253e1bc 100644 --- a/src/de/craftinc/gates/util/ConfigurationUtil.java +++ b/src/de/craftinc/gates/util/ConfigurationUtil.java @@ -23,8 +23,7 @@ import org.bukkit.Material; import java.util.logging.Level; -public class ConfigurationUtil -{ +public class ConfigurationUtil { public static final String confMaxGateBlocksKey = "maxGateBlocks"; public static final String confPlayerGateBlockUpdateRadiusKey = "playerGateBlockUpdateRadius"; public static final String confCheckForBrokenGateFramesKey = "checkForBrokenGateFrames"; @@ -38,79 +37,56 @@ public class ConfigurationUtil public static final String confGateMaterialKey = "gateMaterial"; - public static GateMaterial getPortalMaterial() - { + public static GateMaterial getPortalMaterial() { String materialString = Plugin.getPlugin().getConfig().getString(confGateMaterialKey); GateMaterial material = new GateMaterial(); if (materialString.equals("sapling")) { material.material = Material.SAPLING; - } - else if (materialString.equals("water")) { + } else if (materialString.equals("water")) { material.material = Material.STATIONARY_WATER; - } - else if (materialString.equals("lava")) { + } else if (materialString.equals("lava")) { material.material = Material.STATIONARY_LAVA; - } - else if (materialString.equals("cobweb")) { + } else if (materialString.equals("cobweb")) { material.material = Material.WEB; - } - else if (materialString.equals("grass")) { + } else if (materialString.equals("grass")) { material.material = Material.LONG_GRASS; material.data = 1; - } - else if (materialString.equals("dead bush")) { + } else if (materialString.equals("dead bush")) { material.material = Material.DEAD_BUSH; - } - else if (materialString.equals("dandelion")) { + } else if (materialString.equals("dandelion")) { material.material = Material.YELLOW_FLOWER; - } - else if (materialString.equals("poppy")) { + } else if (materialString.equals("poppy")) { material.material = Material.RED_ROSE; - } - else if (materialString.equals("brown mushroom")) { + } else if (materialString.equals("brown mushroom")) { material.material = Material.BROWN_MUSHROOM; - } - else if (materialString.equals("red mushroom")) { + } else if (materialString.equals("red mushroom")) { material.material = Material.RED_MUSHROOM; - } - else if (materialString.equals("torch")) { + } else if (materialString.equals("torch")) { material.material = Material.TORCH; - } - else if (materialString.equals("redstone torch (off)")) { + } else if (materialString.equals("redstone torch (off)")) { material.material = Material.REDSTONE_TORCH_OFF; - } - else if (materialString.equals("redstone torch (on)")) { + } else if (materialString.equals("redstone torch (on)")) { material.material = Material.REDSTONE_TORCH_ON; - } - else if (materialString.equals("fence")) { + } else if (materialString.equals("fence")) { material.material = Material.FENCE; - } - else if (materialString.equals("nether portal")) { + } else if (materialString.equals("nether portal")) { material.material = Material.PORTAL; - } - else if (materialString.equals("iron bars")) { + } else if (materialString.equals("iron bars")) { material.material = Material.IRON_FENCE; - } - else if (materialString.equals("glass pane")) { + } else if (materialString.equals("glass pane")) { material.material = Material.THIN_GLASS; - } - else if (materialString.equals("fence gate")) { + } else if (materialString.equals("fence gate")) { material.material = Material.FENCE_GATE; - } - else if (materialString.equals("nether brick fence")) { + } else if (materialString.equals("nether brick fence")) { material.material = Material.NETHER_FENCE; - } - else if (materialString.equals("nether wart")) { + } else if (materialString.equals("nether wart")) { material.material = Material.NETHER_WARTS; - } - else if (materialString.equals("end portal")) { + } else if (materialString.equals("end portal")) { material.material = Material.ENDER_PORTAL; - } - else if (materialString.equals("cobblestone wall")) { + } else if (materialString.equals("cobblestone wall")) { material.material = Material.COBBLE_WALL; - } - else { // fallback! + } else { // fallback! material.material = Material.PORTAL; Plugin.log(Level.WARNING, "Gate material invalid! Please check and correct your configuration file!"); } @@ -120,8 +96,7 @@ public class ConfigurationUtil } -class GateMaterial -{ +class GateMaterial { public Material material = Material.PORTAL; public byte data = 0; } diff --git a/src/de/craftinc/gates/util/FloodUtil.java b/src/de/craftinc/gates/util/FloodUtil.java index f01d55b..28e4cbf 100644 --- a/src/de/craftinc/gates/util/FloodUtil.java +++ b/src/de/craftinc/gates/util/FloodUtil.java @@ -28,32 +28,30 @@ import org.bukkit.block.BlockFace; import de.craftinc.gates.Plugin; -public class FloodUtil -{ - protected static final Set exp1 = new HashSet(); +public class FloodUtil { + protected static final Set exp1 = new HashSet(); protected static final Set exp2 = new HashSet(); - - static - { - exp1.add(BlockFace.UP); - exp1.add(BlockFace.DOWN); - exp1.add(BlockFace.EAST); - exp1.add(BlockFace.WEST); - - exp2.add(BlockFace.UP); - exp2.add(BlockFace.DOWN); - exp2.add(BlockFace.NORTH); - exp2.add(BlockFace.SOUTH); - } + + static { + exp1.add(BlockFace.UP); + exp1.add(BlockFace.DOWN); + exp1.add(BlockFace.EAST); + exp1.add(BlockFace.WEST); + + exp2.add(BlockFace.UP); + exp2.add(BlockFace.DOWN); + exp2.add(BlockFace.NORTH); + exp2.add(BlockFace.SOUTH); + } /** * Returns the all frame blocks of an gate. + * * @param blocks All blocks inside the gate. * @return A Set containing all frame block. Will never return 'null'. */ - public static Set getFrame(final Set blocks) - { + public static Set getFrame(final Set blocks) { if (blocks == null || blocks.isEmpty()) { return new HashSet(); } @@ -64,7 +62,7 @@ public class FloodUtil for (Block b : blocks) { if (blocks.contains(b.getRelative(BlockFace.EAST)) || - blocks.contains(b.getRelative(BlockFace.WEST))) { + blocks.contains(b.getRelative(BlockFace.WEST))) { gateFrameSearchFaces = exp1; break; @@ -81,8 +79,7 @@ public class FloodUtil if (gateFrameSearchFaces != null) { return _getFrame(blocks, gateFrameSearchFaces); - } - else { // no direction found (the gate might only consist of blocks one over another) + } else { // no direction found (the gate might only consist of blocks one over another) // Try one direction and check if the found blocks are not air. // If air is found (frame broken or wrong direction) return the other direction @@ -100,9 +97,7 @@ public class FloodUtil } - - protected static Set _getFrame(final Set blocks, final Set searchDirections) - { + protected static Set _getFrame(final Set blocks, final Set searchDirections) { Set frameBlocks = new HashSet(); for (Block b : blocks) { @@ -122,11 +117,11 @@ public class FloodUtil /** * Returns the all frame blocks of an gate. + * * @param locations All locations inside the gate. * @return A Set containing all frame block. Will never return 'null'. */ - public static Set getFrameWithLocations(final Set locations) - { + public static Set getFrameWithLocations(final Set locations) { if (locations == null) { throw new IllegalArgumentException("'locations' must not be 'null'"); } @@ -140,10 +135,9 @@ public class FloodUtil return getFrame(blocks); } - - // For the same frame and location this set of blocks is deterministic - public static Set getGatePortalBlocks(final Block block) - { + + // For the same frame and location this set of blocks is deterministic + public static Set getGatePortalBlocks(final Block block) { if (block == null) { throw new IllegalArgumentException("'block' must not be 'null'"); } @@ -151,57 +145,56 @@ public class FloodUtil int frameBlockSearchLimit = Plugin.getPlugin().getConfig().getInt(ConfigurationUtil.confMaxGateBlocksKey); Set blocks1 = getAirFloodBlocks(block, new HashSet(), exp1, frameBlockSearchLimit); - Set blocks2 = getAirFloodBlocks(block, new HashSet(), exp2, frameBlockSearchLimit); - - if (blocks1 == null && blocks2 == null) { - return null; - } - - if (blocks1 == null) { - return blocks2; - } - - if (blocks2 == null) { - return blocks1; - } - - if (blocks1.size() > blocks2.size()) { - return blocks2; - } - - return blocks1; - } + Set blocks2 = getAirFloodBlocks(block, new HashSet(), exp2, frameBlockSearchLimit); - - protected static Set getAirFloodBlocks(final Block startBlock, - Set foundBlocks, - final Set expandFaces, - int limit) - { - if (foundBlocks == null) { - return null; - } - - if (foundBlocks.size() > limit) { - Plugin.log(Level.ALL, "exceeding gate size limit."); - return null; - } - - if (foundBlocks.contains(startBlock)) { - return foundBlocks; + if (blocks1 == null && blocks2 == null) { + return null; } - - if (startBlock.getType() == Material.AIR) { - // ... We found a block :D ... - foundBlocks.add(startBlock); - - // ... And flood away ! - for (BlockFace face : expandFaces) { - Block potentialBlock = startBlock.getRelative(face); - foundBlocks = getAirFloodBlocks(potentialBlock, foundBlocks, expandFaces, limit); - } - } - - return foundBlocks; - } + + if (blocks1 == null) { + return blocks2; + } + + if (blocks2 == null) { + return blocks1; + } + + if (blocks1.size() > blocks2.size()) { + return blocks2; + } + + return blocks1; + } + + + protected static Set getAirFloodBlocks(final Block startBlock, + Set foundBlocks, + final Set expandFaces, + int limit) { + if (foundBlocks == null) { + return null; + } + + if (foundBlocks.size() > limit) { + Plugin.log(Level.ALL, "exceeding gate size limit."); + return null; + } + + if (foundBlocks.contains(startBlock)) { + return foundBlocks; + } + + if (startBlock.getType() == Material.AIR) { + // ... We found a block :D ... + foundBlocks.add(startBlock); + + // ... And flood away ! + for (BlockFace face : expandFaces) { + Block potentialBlock = startBlock.getRelative(face); + foundBlocks = getAirFloodBlocks(potentialBlock, foundBlocks, expandFaces, limit); + } + } + + return foundBlocks; + } } diff --git a/src/de/craftinc/gates/util/GateBlockChangeSender.java b/src/de/craftinc/gates/util/GateBlockChangeSender.java index 9791407..e685d70 100644 --- a/src/de/craftinc/gates/util/GateBlockChangeSender.java +++ b/src/de/craftinc/gates/util/GateBlockChangeSender.java @@ -32,17 +32,16 @@ import java.util.Set; import static de.craftinc.gates.util.ConfigurationUtil.*; -public class GateBlockChangeSender -{ +public class GateBlockChangeSender { /** * Replaces gate frame blocks with glowstone for a short period of time. * Uses the data stored in 'highlightDuration' inside the config file * for determining when to de-highlight the frames. + * * @param player The player for whom the frame should be highlighted. * Must not be null! */ - public static void temporaryHighlightGatesFrames(final Player player, final Set gates) - { + public static void temporaryHighlightGatesFrames(final Player player, final Set gates) { if (player == null) { throw new IllegalArgumentException("'player' must not be 'null'!"); } @@ -55,7 +54,7 @@ public class GateBlockChangeSender Set frameBlocks = g.getGateFrameBlocks(); for (Block b : frameBlocks) { - player.sendBlockChange(b.getLocation(), Material.GLOWSTONE, (byte)0); + player.sendBlockChange(b.getLocation(), Material.GLOWSTONE, (byte) 0); } } @@ -71,8 +70,7 @@ public class GateBlockChangeSender } - public static void temporaryHighlightGateFrame(final Player player, final Gate gate) - { + public static void temporaryHighlightGateFrame(final Player player, final Gate gate) { if (gate == null) { throw new IllegalArgumentException("'gate' must not be 'null!"); } @@ -84,7 +82,7 @@ public class GateBlockChangeSender Set frameBlocks = gate.getGateFrameBlocks(); for (Block b : frameBlocks) { - player.sendBlockChange(b.getLocation(), Material.GLOWSTONE, (byte)0); + player.sendBlockChange(b.getLocation(), Material.GLOWSTONE, (byte) 0); } Plugin plugin = Plugin.getPlugin(); @@ -99,24 +97,22 @@ public class GateBlockChangeSender } - protected static void dehighlightGatesFrames(final Player player, final Set gates) - { + protected static void dehighlightGatesFrames(final Player player, final Set gates) { for (Gate g : gates) { Set frameBlocks = g.getGateFrameBlocks(); for (Block b : frameBlocks) { - player.sendBlockChange(b.getLocation(), b.getType(), (byte)0); + player.sendBlockChange(b.getLocation(), b.getType(), (byte) 0); } } } - protected static void dehighlightGateFrame(final Player player, final Gate gate) - { + protected static void dehighlightGateFrame(final Player player, final Gate gate) { Set frameBlocks = gate.getGateFrameBlocks(); for (Block b : frameBlocks) { - player.sendBlockChange(b.getLocation(), b.getType(), (byte)0); + player.sendBlockChange(b.getLocation(), b.getType(), (byte) 0); } } @@ -124,13 +120,13 @@ public class GateBlockChangeSender /** * Sends gate blocks to player at a given location. Will send the updates either immediately or * immediately and after a short delay. + * * @param player A player to send block changes to. Must not be null! * @param location The location to look for gates nearby. Must not be null! * @param sendDelayed Set to 'true' if the block changes shall be send a second time after a one * second delay. */ - public static void updateGateBlocks(final Player player, final Location location, boolean sendDelayed) - { + public static void updateGateBlocks(final Player player, final Location location, boolean sendDelayed) { if (player == null) { throw new IllegalArgumentException("'player' must not be 'null'!"); } @@ -161,11 +157,9 @@ public class GateBlockChangeSender } if (sendDelayed) { - Bukkit.getScheduler().scheduleSyncDelayedTask(Plugin.getPlugin(), new Runnable() - { + Bukkit.getScheduler().scheduleSyncDelayedTask(Plugin.getPlugin(), new Runnable() { @Override - public void run() - { + public void run() { updateGateBlocks(player, location, false); } }, 20L); @@ -176,16 +170,14 @@ public class GateBlockChangeSender /** * This method calls: updateGateBlocks(player, location, false); */ - public static void updateGateBlocks(final Player player, final Location location) - { + public static void updateGateBlocks(final Player player, final Location location) { updateGateBlocks(player, location, false); } /** * This method calls: updateGateBlocks(player, player.getLocation(), false); */ - public static void updateGateBlocks(final Player player) - { + public static void updateGateBlocks(final Player player) { if (player == null) { throw new IllegalArgumentException("'player' must not be 'null'!"); } @@ -194,18 +186,17 @@ public class GateBlockChangeSender } - public static void updateGateBlocks(final Gate gate) - { + public static void updateGateBlocks(final Gate gate) { updateGateBlocks(gate, false); } /** * Sends block changes to players near a given gate. - * @param gate Must not be 'null'! + * + * @param gate Must not be 'null'! * @param remove Set to true if all visible gate blocks shall be removed. */ - public static void updateGateBlocks(final Gate gate, boolean remove) - { + public static void updateGateBlocks(final Gate gate, boolean remove) { if (gate == null) { throw new IllegalArgumentException("'gate must not be 'null'!"); } @@ -234,8 +225,7 @@ public class GateBlockChangeSender if (gate.isOpen() && !gate.isHidden() && !remove) { material = gateMaterial.material; data = gateMaterial.data; - } - else { + } else { material = Material.AIR; } diff --git a/src/de/craftinc/gates/util/SimpleChunk.java b/src/de/craftinc/gates/util/SimpleChunk.java index da3f1de..6dd07c3 100644 --- a/src/de/craftinc/gates/util/SimpleChunk.java +++ b/src/de/craftinc/gates/util/SimpleChunk.java @@ -19,61 +19,55 @@ package de.craftinc.gates.util; import org.bukkit.Chunk; import org.bukkit.World; -public class SimpleChunk -{ - private int x; - private int z; - private String world; - - public SimpleChunk(Chunk c) - { - this.x = c.getX(); - this.z = c.getZ(); - this.world = c.getWorld().getName(); - } +public class SimpleChunk { + private int x; + private int z; + private String world; + + public SimpleChunk(Chunk c) { + this.x = c.getX(); + this.z = c.getZ(); + this.world = c.getWorld().getName(); + } - public SimpleChunk(int x, int z, World w) - { + public SimpleChunk(int x, int z, World w) { this.x = x; this.z = z; this.world = w.getName(); } - - @Override - public boolean equals(Object o) - { - if (o instanceof SimpleChunk) { - SimpleChunk otherLocation = (SimpleChunk)o; - - if (otherLocation.x == this.x - && otherLocation.z == this.z - && otherLocation.world.equals(this.world)) { - - return true; - } - } - - return false; - } - @Override - public int hashCode() - { - int hash = 11; - hash = 29 * hash + x; - hash = 37 * hash + z; - hash = 29 * hash + world.hashCode(); - - return hash; + public boolean equals(Object o) { + if (o instanceof SimpleChunk) { + SimpleChunk otherLocation = (SimpleChunk) o; + + if (otherLocation.x == this.x + && otherLocation.z == this.z + && otherLocation.world.equals(this.world)) { + + return true; + } + } + + return false; } @Override - public String toString() - { + public int hashCode() { + int hash = 11; + hash = 29 * hash + x; + hash = 37 * hash + z; + hash = 29 * hash + world.hashCode(); + + return hash; + } + + + @Override + public String toString() { return this.getClass().toString() + " (x=" + this.x + " z=" + this.z + " world='" + this.world + "')"; } } diff --git a/src/de/craftinc/gates/util/SimpleLocation.java b/src/de/craftinc/gates/util/SimpleLocation.java index 4fdd035..308b66c 100644 --- a/src/de/craftinc/gates/util/SimpleLocation.java +++ b/src/de/craftinc/gates/util/SimpleLocation.java @@ -18,28 +18,25 @@ package de.craftinc.gates.util; import org.bukkit.Location; -public class SimpleLocation -{ - private String world; +public class SimpleLocation { + private String world; private int x; private int y; private int z; - - - public SimpleLocation(Location l) - { - this.world = l.getWorld().getName(); + + + public SimpleLocation(Location l) { + this.world = l.getWorld().getName(); // Using Block coordinates makes it possible to compare block locations with player locations. // There might be an offset of 1 otherwise. - this.x = l.getBlockX(); - this.y = l.getBlockY(); - this.z = l.getBlockZ(); + this.x = l.getBlockX(); + this.y = l.getBlockY(); + this.z = l.getBlockZ(); } - public SimpleLocation(Location l, boolean isHeadPosition) - { + public SimpleLocation(Location l, boolean isHeadPosition) { this.world = l.getWorld().getName(); // Using Block coordinates makes it possible to compare block locations with player locations. @@ -55,41 +52,37 @@ public class SimpleLocation @Override - public String toString() - { + public String toString() { return super.toString() + " x: " + x + " y: " + y + " z: " + z + " world: " + world; } - - + @Override - public boolean equals(final Object o) - { - if (o instanceof SimpleLocation) { - SimpleLocation otherLocation = (SimpleLocation)o; - - if (otherLocation.x == this.x - && otherLocation.y == this.y - && otherLocation.z == this.z - && otherLocation.world.equals(this.world)) { - - return true; - } - } - - return false; + public boolean equals(final Object o) { + if (o instanceof SimpleLocation) { + SimpleLocation otherLocation = (SimpleLocation) o; + + if (otherLocation.x == this.x + && otherLocation.y == this.y + && otherLocation.z == this.z + && otherLocation.world.equals(this.world)) { + + return true; + } + } + + return false; } - + @Override - public int hashCode() - { - int hash = 13; - hash = 37 * hash + x; - hash = 31 * hash + y; - hash = 37 * hash + z; - hash = 31 * hash + world.hashCode(); - - return hash; + public int hashCode() { + int hash = 13; + hash = 37 * hash + x; + hash = 31 * hash + y; + hash = 37 * hash + z; + hash = 31 * hash + world.hashCode(); + + return hash; } } diff --git a/src/de/craftinc/gates/util/TextUtil.java b/src/de/craftinc/gates/util/TextUtil.java index 77d93cc..4fbcd20 100644 --- a/src/de/craftinc/gates/util/TextUtil.java +++ b/src/de/craftinc/gates/util/TextUtil.java @@ -20,16 +20,13 @@ import org.bukkit.ChatColor; import java.util.List; -public class TextUtil -{ - public static String titleize(String str) - { - String center = ".[ " + ChatColor.YELLOW + str + ChatColor.GOLD + " ]."; +public class TextUtil { + public static String titleize(String str) { + String center = ".[ " + ChatColor.YELLOW + str + ChatColor.GOLD + " ]."; if (center.length() >= 60) { return ChatColor.GOLD + center; - } - else { + } else { String line = ChatColor.GOLD + repeat("_", 60); int pivot = line.length() / 2; @@ -38,35 +35,33 @@ public class TextUtil return line.substring(0, pivot - eatLeft) + center + line.substring(pivot + eatRight); } - } - - - public static String repeat(String s, int times) - { - if (times <= 0) - return ""; - - return s + repeat(s, times-1); - } - - - /** - * Joins all elements of list into a single string, separating the original strings with glue. - */ - public static String implode(List list, String glue) - { - if (list.size() == 0) { - return ""; - } - - String ret = list.get(0); - - for (int i=1; i list, String glue) { + if (list.size() == 0) { + return ""; + } + + String ret = list.get(0); + + for (int i = 1; i < list.size(); i++) { + ret += glue + list.get(i); + } + + return ret; + } } diff --git a/src/de/craftinc/gates/util/VehicleCloner.java b/src/de/craftinc/gates/util/VehicleCloner.java index cfd852c..3aba655 100644 --- a/src/de/craftinc/gates/util/VehicleCloner.java +++ b/src/de/craftinc/gates/util/VehicleCloner.java @@ -20,10 +20,8 @@ import org.bukkit.Location; import org.bukkit.entity.*; -public class VehicleCloner -{ - public static Vehicle clone(Vehicle parent, Location cloneLocation) - { +public class VehicleCloner { + public static Vehicle clone(Vehicle parent, Location cloneLocation) { Vehicle clone = cloneLocation.getWorld().spawn(cloneLocation, parent.getClass()); clone.setFallDistance(parent.getFallDistance()); @@ -33,18 +31,17 @@ public class VehicleCloner clone.setLastDamageCause(parent.getLastDamageCause()); if (clone instanceof Boat) { - Boat boat = (Boat)clone; - Boat parentBoat = (Boat)parent; + Boat boat = (Boat) clone; + Boat parentBoat = (Boat) parent; boat.setMaxSpeed(parentBoat.getMaxSpeed()); boat.setOccupiedDeceleration(parentBoat.getOccupiedDeceleration()); boat.setUnoccupiedDeceleration(parentBoat.getUnoccupiedDeceleration()); boat.setWorkOnLand(parentBoat.getWorkOnLand()); boat.setVelocity(parentBoat.getVelocity()); - } - else if (clone instanceof Animals) { - Animals animal = (Animals)clone; - Animals parentAnimal = (Animals)parent; + } else if (clone instanceof Animals) { + Animals animal = (Animals) clone; + Animals parentAnimal = (Animals) parent; animal.setMaxHealth(parentAnimal.getMaxHealth()); animal.setHealth(parentAnimal.getMaxHealth()); @@ -63,8 +60,8 @@ public class VehicleCloner animal.setAgeLock(parentAnimal.getAgeLock()); if (clone instanceof Horse) { - Horse horse = (Horse)clone; - Horse parentHorse = (Horse)parent; + Horse horse = (Horse) clone; + Horse parentHorse = (Horse) parent; horse.getInventory().setArmor(parentHorse.getInventory().getArmor()); horse.getInventory().setSaddle(parentHorse.getInventory().getSaddle()); @@ -81,23 +78,20 @@ public class VehicleCloner if (parentHorse.isAdult()) { horse.setAdult(); - } - else { + } else { horse.setBaby(); } horse.setBreed(parentHorse.canBreed()); - } - else if (clone instanceof Pig) { - Pig pig = (Pig)clone; - Pig parentPig = (Pig)parent; + } else if (clone instanceof Pig) { + Pig pig = (Pig) clone; + Pig parentPig = (Pig) parent; pig.setSaddle(parentPig.hasSaddle()); } - } - else if (clone instanceof Minecart) { - Minecart minecart = (Minecart)clone; - Minecart parentMinecart = (Minecart)parent; + } else if (clone instanceof Minecart) { + Minecart minecart = (Minecart) clone; + Minecart parentMinecart = (Minecart) parent; minecart.setDerailedVelocityMod(parentMinecart.getDerailedVelocityMod()); minecart.setFlyingVelocityMod(parentMinecart.getFlyingVelocityMod());