diff --git a/src/de/craftinc/gates/BaseGate.java b/src/de/craftinc/gates/BaseGate.java deleted file mode 100644 index 271ef01..0000000 --- a/src/de/craftinc/gates/BaseGate.java +++ /dev/null @@ -1,195 +0,0 @@ -package de.craftinc.gates; - -import java.util.HashSet; -import java.util.Set; - -import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.block.Block; - -import de.craftinc.gates.util.FloodUtil; - - -public abstract class BaseGate -{ - /* - * ATTRIBUTES - */ - 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 Location exit; - - protected boolean isHidden = false; - protected boolean isOpen = false; - - - /* - * SETTER & GETTER - */ - - public Location getLocation() - { - return location; - } - - - public void setLocation(Location location) throws Exception - { - this.location = location; - - if (isOpen) { - fillGate(); - validate(); - } - } - - - public Location getExit() - { - return exit; - } - - - public void setExit(Location exit) throws Exception - { - this.exit = exit; - validate(); - } - - - public boolean isHidden() - { - return isHidden; - } - - - public void setHidden(boolean isHidden) throws Exception - { - this.isHidden = isHidden; - - if (isHidden == true) { - emptyGate(); - } - else if (isOpen()) { - fillGate(); - } - - validate(); - } - - - public boolean isOpen() - { - return isOpen; - } - - - public void setOpen(boolean isOpen) throws Exception - { - if (isOpen == true && this.isOpen == false) { - findPortalBlocks(); - - if (!isHidden) { - fillGate(); - } - } - else if (isOpen == false && this.isOpen == true) { - emptyGate(); - } - - this.isOpen = isOpen; - - validate(); - } - - - public Set getGateBlockLocations() - { - return gateBlockLocations; - } - - - /* - * GATE BLOCK HANDLING - */ - - protected void fillGate() - { - emptyGate(); - findPortalBlocks(); - - // This is not to do an effect - // It is to stop portal blocks from destroying themself as they cant rely on non created blocks :P - for (Location l : gateBlockLocations) { - l.getBlock().setType(Material.GLOWSTONE); - } - - for (Location l : gateBlockLocations) { - l.getBlock().setType(Material.PORTAL); - } - } - - - protected void emptyGate() - { - for (Location l : gateBlockLocations) { - if (l.getBlock().getType() == Material.PORTAL) { - l.getBlock().setType(Material.AIR); - } - } - } - - - protected void findPortalBlocks() - { - gateBlockLocations = new HashSet(); - Set gateBlocks = FloodUtil.getGateFrameBlocks(location.getBlock()); - - if (gateBlocks != null) { - for (Block b : gateBlocks) { - gateBlockLocations.add(b.getLocation()); - } - } - } - - - /* - * VALIDATION - */ - - /** - * Checks if valus attributes do add up; will close gate on wrong values. - */ - public void validate() throws Exception - { - if (!isOpen) { - return; - } - - if (location == null) { - setOpen(false); - throw new Exception("Gate got closed. It has no location."); - } - - if (exit == null) { - setOpen(false); - throw new Exception("Gate got closed. It has no exit."); - } - - if (gateBlockLocations.size() == 0) { - setOpen(false); - throw new Exception("Gate got closed. The frame is missing or broken."); - } - - - if (isHidden == false) { - for (Location l : gateBlockLocations) { - if (l.getBlock().getType() == Material.AIR) { - setOpen(false); - throw new Exception("Gate got closed. The frame is missing or broken."); - } - } - } - } -} diff --git a/src/de/craftinc/gates/Gate.java b/src/de/craftinc/gates/Gate.java index d6a4fd7..aa2a583 100644 --- a/src/de/craftinc/gates/Gate.java +++ b/src/de/craftinc/gates/Gate.java @@ -1,67 +1,227 @@ package de.craftinc.gates; import java.util.ArrayList; -import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.block.Block; import org.bukkit.configuration.serialization.ConfigurationSerializable; +import de.craftinc.gates.util.FloodUtil; import de.craftinc.gates.util.LocationUtil; - -/** - * Adds persistence and serialization to the base gate class. - */ -public class Gate extends BaseGate implements ConfigurationSerializable +public class Gate implements ConfigurationSerializable { /* * ATTRIBUTES */ + 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 Location exit; + + protected boolean isHidden = false; + protected boolean isOpen = false; protected String id; - protected static Map instances = new HashMap(); - /* - * CONSTRUCTORS - */ - - public Gate(String id) throws Exception + public Gate(String id) { setId(id); } - /* - * SETTER & GETTER - */ - - public String getId() - { - return id; - } - - - - public void setId(String id) throws Exception - { - if (exists(id)) { - throw new Exception("A gate with '" + id + "' already exists"); - } - - this.id = id; - } - - public String toString() { return super.toString() + " " + this.getId(); } + + + /* + * SETTER & GETTER + */ + + public Location getLocation() + { + return location; + } + + + public void setLocation(Location location) throws Exception + { + this.location = location; + + if (isOpen) { + fillGate(); + validate(); + } + } + + + public Location getExit() + { + return exit; + } + + + public void setExit(Location exit) throws Exception + { + this.exit = exit; + validate(); + } + + + public String getId() { + return id; + } + + + public void setId(String id) { + this.id = id; + } + + + public boolean isHidden() + { + return isHidden; + } + + + public void setHidden(boolean isHidden) throws Exception + { + this.isHidden = isHidden; + + if (isHidden == true) { + emptyGate(); + } + else if (isOpen()) { + fillGate(); + } + + validate(); + } + + + public boolean isOpen() + { + return isOpen; + } + + + public void setOpen(boolean isOpen) throws Exception + { + if (isOpen == true && this.isOpen == false) { + findPortalBlocks(); + + if (!isHidden) { + fillGate(); + } + } + else if (isOpen == false && this.isOpen == true) { + emptyGate(); + } + + this.isOpen = isOpen; + + validate(); + } + + + public Set getGateBlockLocations() + { + return gateBlockLocations; + } + + + /* + * GATE BLOCK HANDLING + */ + + protected void fillGate() + { + emptyGate(); + findPortalBlocks(); + + // This is not to do an effect + // It is to stop portal blocks from destroying themself as they cant rely on non created blocks :P + for (Location l : gateBlockLocations) { + l.getBlock().setType(Material.GLOWSTONE); + } + + for (Location l : gateBlockLocations) { + l.getBlock().setType(Material.PORTAL); + } + } + + + protected void emptyGate() + { + for (Location l : gateBlockLocations) { + if (l.getBlock().getType() == Material.PORTAL) { + l.getBlock().setType(Material.AIR); + } + } + } + + + protected void findPortalBlocks() + { + gateBlockLocations = new HashSet(); + Set gateBlocks = FloodUtil.getGateFrameBlocks(location.getBlock()); + + if (gateBlocks != null) { + for (Block b : gateBlocks) { + gateBlockLocations.add(b.getLocation()); + } + } + } + + + /* + * VALIDATION + */ + + /** + * Checks if valus attributes do add up; will close gate on wrong values. + */ + public void validate() throws Exception + { + if (!isOpen) { + return; + } + + if (location == null) { + setOpen(false); + throw new Exception("Gate got closed. It has no location."); + } + + if (exit == null) { + setOpen(false); + throw new Exception("Gate got closed. It has no exit."); + } + + if (gateBlockLocations.size() == 0) { + setOpen(false); + throw new Exception("Gate got closed. The frame is missing or broken."); + } + + + if (isHidden == false) { + for (Location l : gateBlockLocations) { + if (l.getBlock().getType() == Material.AIR) { + setOpen(false); + throw new Exception("Gate got closed. The frame is missing or broken."); + } + } + } + } /* @@ -111,12 +271,10 @@ public class Gate extends BaseGate implements ConfigurationSerializable 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.instance.storeInvalidGate(map); + Plugin.getPlugin().getGatesManager().storeInvalidGate(map); return; } - - instances.put(id, this); } @@ -155,58 +313,4 @@ public class Gate extends BaseGate implements ConfigurationSerializable return retVal; } - - - /* - * ENTITY MANAGEMENT - */ - - public static Gate get(String id) - { - return instances.get(id); - } - - - public static boolean exists(String id) - { - return instances.containsKey(id); - } - - - public static Gate create(String id) throws Exception - { - Gate gate = new Gate(id); - - instances.put(gate.id, gate); - return gate; - } - - - public static void rename(String oldId, String newId) throws Exception - { - Gate gate = get(oldId); - - gate.setId(newId); - - instances.remove(oldId); - instances.put(gate.id, gate); - } - - - public static void delete(String id) - { - Gate g = get(id); - - if (g != null) { - g.emptyGate(); - } - - instances.remove(id); - } - - - public static Collection getAll() - { - return instances.values(); - } } diff --git a/src/de/craftinc/gates/GatesManager.java b/src/de/craftinc/gates/GatesManager.java new file mode 100644 index 0000000..83d890d --- /dev/null +++ b/src/de/craftinc/gates/GatesManager.java @@ -0,0 +1,246 @@ +package de.craftinc.gates; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; + +import java.util.*; +import java.util.logging.Level; + +import org.bukkit.Chunk; +import org.bukkit.Location; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; + + +public class GatesManager +{ + private File gatesConfigFile; + private FileConfiguration gatesConfig; + private String gatesPath = "gates"; // path to gates inside the yaml file + + private Map gatesById; + private Map> gatesByChunk; + private Map gatesByLocation; + + private List gates; + + + public Gate getGateWithId(String id) + { + return gatesById.get(id); + } + + + public Set getGatesInsideChunk(Chunk c) + { + return gatesByChunk.get(c); + } + + + public Gate getGateAtLocation(Location l) + { + return gatesByLocation.get(l); + } + + + public void saveGatesToDisk() + { + gatesConfig.set(gatesPath, new ArrayList(gatesById.values())); + + try { + gatesConfig.save(gatesConfigFile); + Plugin.log("Saved gates to disk."); + } + catch (IOException e) { + Plugin.log("ERROR: Could not save gates to disk."); + e.printStackTrace(); + } + } + + + @SuppressWarnings("unchecked") + public void loadGatesFromDisk() + { + this.gatesConfigFile = new File(Plugin.getPlugin().getDataFolder(), "gates.yml"); + + if(!this.gatesConfigFile.exists()) { + try { + this.gatesConfigFile.createNewFile(); + } catch (IOException e) { + Plugin.log(Level.SEVERE, "Cannot create gate config file! No gates will be persisted."); + } + } + + this.gatesConfig = YamlConfiguration.loadConfiguration(gatesConfigFile); + this.gates = (List)gatesConfig.getList(gatesPath); + + for (Object o : this.gates) { + + if (!(o instanceof Gate)) { + Plugin.log(Level.SEVERE, "Gate file on disk is invalid. No gates loaded."); + // TODO: gates.yml will be empty after save/reload/server stop! All gates will be lost! No user will expect this! + this.gates = new ArrayList(); + break; + } + } + + fillGatesById(); + fillGatesByChunk(); + fillGatesByLocation(); + } + + + private void fillGatesById() + { + gatesById = new HashMap((int)(gates.size() * 1.25)); + + for (Gate g : gates) { + gatesById.put(g.getId(), g); + } + } + + + private void fillGatesByChunk() + { + HashSet chunksUsedByGates = new HashSet(gates.size()); + + for (Gate g : gates) { + chunksUsedByGates.add(g.getLocation().getChunk()); + } + + gatesByChunk = new HashMap>((int)(chunksUsedByGates.size() * 1.25)); + + for (Gate g : gates) { + Chunk c = g.getLocation().getChunk(); + Set gatesForC = gatesByChunk.get(c); + + if (gatesForC == null) { + gatesForC = new HashSet(); // NOTE: not optimizing size here + } + + gatesForC.add(g); + } + } + + + private void fillGatesByLocation() + { + int numGateBlocks = 0; + + for (Gate g : gates) { + numGateBlocks += g.gateBlockLocations.size(); + } + + gatesByLocation = new HashMap((int)(numGateBlocks*1.25)); + + for (Gate g : gates) { + gatesByLocation.put(g.getLocation(), 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" + e.getStackTrace()); + } + } + + + public void handleGateIdChange(Gate g, String oldId) + { + gatesById.remove(oldId); + gatesById.put(g.getId(), g); + } + + + public void handleGateLocationChange(Gate g, Location oldLocation) + { + gatesByLocation.remove(oldLocation); + gatesByLocation.put(g.getLocation(), g); + + + gatesByChunk.get(oldLocation.getChunk()).remove(g); + + Set newChunkGates = gatesByChunk.get(g.getLocation().getChunk()); + + if (newChunkGates == null) { + newChunkGates = new HashSet(); // NOTE: not optimizing size here + } + + newChunkGates.add(g); + gatesByChunk.put(g.getLocation().getChunk(), newChunkGates); + } + + + public void handleNewGate(Gate g) + { + // TODO: implement! + } + + + public void handleDeletion(Gate g) + { + // TODO: implement! + } + + + public boolean gateExists(String id) + { + return gatesById.containsKey(id); + } + + + public List allGates () + { + return gates; + } +} diff --git a/src/de/craftinc/gates/Plugin.java b/src/de/craftinc/gates/Plugin.java index d8669b5..e23fb96 100644 --- a/src/de/craftinc/gates/Plugin.java +++ b/src/de/craftinc/gates/Plugin.java @@ -1,57 +1,36 @@ package de.craftinc.gates; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.logging.Level; import java.util.logging.Logger; + import net.milkbowl.vault.permission.Permission; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.serialization.ConfigurationSerialization; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.java.JavaPlugin; import de.craftinc.gates.commands.*; -import de.craftinc.gates.listeners.PluginBlockListener; import de.craftinc.gates.listeners.PluginPlayerListener; -import de.craftinc.gates.listeners.PluginPortalListener; public class Plugin extends JavaPlugin { - public static Plugin instance; - public static final String permissionInfo = "craftincgates.info"; public static final String permissionManage = "craftincgates.manage"; -// public static final String permissionAll = "craftincgates.*"; public static final String permissionUse = "craftincgates.use"; - public static Permission permission = null; - - public PluginPlayerListener playerListener = new PluginPlayerListener(); - public PluginBlockListener blockListener = new PluginBlockListener(); - public PluginPortalListener portalListener = new PluginPortalListener(); - - private File gatesConfigFile; - private FileConfiguration gatesConfig; + private static Plugin instance; + private static Permission permission; private String baseCommand; - - private String gatesPath = "gates"; - - - // Commands - public List commands = new ArrayList(); + private PluginPlayerListener playerListener = new PluginPlayerListener(); + private List commands = new ArrayList(); + private GatesManager gatesManager = new GatesManager(); public Plugin() @@ -60,6 +39,18 @@ public class Plugin extends JavaPlugin } + public static Plugin getPlugin() + { + return instance; + } + + + public GatesManager getGatesManager() + { + return gatesManager; + } + + @Override public void onLoad() { @@ -91,7 +82,8 @@ public class Plugin extends JavaPlugin public void onDisable() { // Save gates - saveGates(); + gatesManager.saveGatesToDisk(); + log("Disabled"); } @@ -120,24 +112,9 @@ public class Plugin extends JavaPlugin // Register events PluginManager pm = this.getServer().getPluginManager(); pm.registerEvents(this.playerListener, this); - pm.registerEvents(this.blockListener, this); - pm.registerEvents(this.portalListener, this); // Load gates - this.gatesConfigFile = new File(getDataFolder(), "gates.yml"); - - if(!this.gatesConfigFile.exists()) - { - try { - this.gatesConfigFile.createNewFile(); - } catch (IOException e) { - log(Level.SEVERE, "Cannot create gate config file! No gates will be persisted."); - } - } - - this.gatesConfig = YamlConfiguration.loadConfiguration(gatesConfigFile); - - loadGates(); + gatesManager.loadGatesFromDisk(); log("Enabled"); } @@ -202,89 +179,13 @@ public class Plugin extends JavaPlugin } - public static void log(Level level, String msg) { + public static void log(Level level, String msg) + { Logger.getLogger("Minecraft").log(level, "["+instance.getDescription().getFullName()+"] "+msg); } - - /* - * Saving and Loading Gates - */ - public void loadGates() - { - File gatesFile = new File(getDataFolder(), "gates.yml"); - FileConfiguration gatesConfig = YamlConfiguration.loadConfiguration(gatesFile); - - gatesConfig.getList(gatesPath); // this will create all the gates - } - - - public void saveGates() - { - gatesConfig.set(gatesPath, new ArrayList(Gate.getAll())); - - try { - gatesConfig.save(gatesConfigFile); - log("Saved gates to disk."); - } - catch (IOException e) { - log("ERROR: Could not save gates to disk."); - e.printStackTrace(); - } - } - - - public void storeInvalidGate(Map map) - { - File invalidGatesFile = new File(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) { - log("ERROR: Could not save invalid gates to disk."); - e.printStackTrace(); - } + + public static Permission getPermission() { + return permission; } } diff --git a/src/de/craftinc/gates/commands/BaseCommand.java b/src/de/craftinc/gates/commands/BaseCommand.java index d3c1634..cef218a 100644 --- a/src/de/craftinc/gates/commands/BaseCommand.java +++ b/src/de/craftinc/gates/commands/BaseCommand.java @@ -8,6 +8,7 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import de.craftinc.gates.Gate; +import de.craftinc.gates.GatesManager; import de.craftinc.gates.Plugin; import de.craftinc.gates.util.TextUtil; @@ -53,7 +54,7 @@ public abstract class BaseCommand this.perform(); if (this.shouldPersistToDisk) { - Plugin.instance.saveGates(); + Plugin.getPlugin().getGatesManager().saveGatesToDisk(); } } @@ -125,13 +126,13 @@ public abstract class BaseCommand protected boolean setGateUsingParameter(String param) { - if (!Gate.exists(param)) - { + GatesManager gateManager = Plugin.getPlugin().getGatesManager(); + + if (!gateManager.gateExists(param)) { return false; } - else - { - gate = Gate.get(param); + else { + gate = gateManager.getGateWithId(param); return true; } } @@ -142,7 +143,7 @@ public abstract class BaseCommand */ protected boolean hasPermission() { - if (Plugin.permission == null) // fallback Рuse the standard bukkit permission system + if (Plugin.getPermission() == null) // fallback � use the standard bukkit permission system { return this.sender.hasPermission(this.requiredPermission); } @@ -156,8 +157,8 @@ public abstract class BaseCommand } else { - // sender is no player Рthere is no information about the senders locations - return Plugin.permission.has(this.sender, this.requiredPermission); + // sender is no player � there is no information about the senders locations + return Plugin.getPermission().has(this.sender, this.requiredPermission); } @@ -171,7 +172,7 @@ public abstract class BaseCommand } else { - hasPermission = Plugin.permission.has(p.getWorld(), p.getName(), this.requiredPermission); + hasPermission = Plugin.getPermission().has(p.getWorld(), p.getName(), this.requiredPermission); } } else if (this.requiredPermission.equals(Plugin.permissionUse) ) @@ -182,12 +183,12 @@ public abstract class BaseCommand { if (this.needsPermissionAtCurrentLocation && this.hasGateParam) { - boolean hasPersmissionAtCurrentLocation = Plugin.permission.has(p.getWorld(), p.getName(), this.requiredPermission); + boolean hasPersmissionAtCurrentLocation = Plugin.getPermission().has(p.getWorld(), p.getName(), this.requiredPermission); hasPermission = hasPersmissionAtCurrentLocation && this.hasPermissionAtGateLocationAndExit(p); } else if (this.needsPermissionAtCurrentLocation) { - hasPermission = Plugin.permission.has(p.getWorld(), p.getName(), this.requiredPermission); + hasPermission = Plugin.getPermission().has(p.getWorld(), p.getName(), this.requiredPermission); } else { @@ -206,7 +207,7 @@ public abstract class BaseCommand return false; } - boolean permAtLocation = Plugin.permission.has(this.gate.getLocation().getWorld(), p.getName(), this.requiredPermission); + boolean permAtLocation = Plugin.getPermission().has(this.gate.getLocation().getWorld(), p.getName(), this.requiredPermission); boolean permAtExit; @@ -216,7 +217,7 @@ public abstract class BaseCommand } else { - permAtExit = Plugin.permission.has(this.gate.getExit().getWorld(), p.getName(), this.requiredPermission); + permAtExit = Plugin.getPermission().has(this.gate.getExit().getWorld(), p.getName(), this.requiredPermission); } return permAtLocation & permAtExit; @@ -233,7 +234,7 @@ public abstract class BaseCommand ret += ChatColor.AQUA; } - ret += "/" + Plugin.instance.getBaseCommand() + " " + TextUtil.implode(this.getAliases(), ",")+" "; + ret += "/" + Plugin.getPlugin().getBaseCommand() + " " + TextUtil.implode(this.getAliases(), ",")+" "; List parts = new ArrayList(); diff --git a/src/de/craftinc/gates/commands/CommandCreate.java b/src/de/craftinc/gates/commands/CommandCreate.java index df3f7f2..7d85bd6 100644 --- a/src/de/craftinc/gates/commands/CommandCreate.java +++ b/src/de/craftinc/gates/commands/CommandCreate.java @@ -1,10 +1,10 @@ package de.craftinc.gates.commands; - import org.bukkit.ChatColor; import org.bukkit.Location; import de.craftinc.gates.Gate; +import de.craftinc.gates.GatesManager; import de.craftinc.gates.Plugin; @@ -34,39 +34,33 @@ public class CommandCreate extends BaseLocationCommand public void perform() { String id = parameters.get(0); + GatesManager gatesManager = Plugin.getPlugin().getGatesManager(); - try - { - gate = Gate.create(id); - sendMessage(ChatColor.GREEN + "Gate with id '" + id + "' was created."); - } - catch (Exception e) - { - sendMessage(ChatColor.RED + "Creating the gate failed!" + e.getMessage() + "See server log for more information"); + if (gatesManager.gateExists(id)) { + sendMessage(ChatColor.RED + "Creating the gate failed!" + "A gate with the supplied id already exists!"); return; } + gate = new Gate(id); + gatesManager.handleNewGate(gate); + sendMessage(ChatColor.GREEN + "Gate with id '" + id + "' was created."); + + Location playerLocation = getValidPlayerLocation(); - if (playerLocation != null) - { - try - { + if (playerLocation != null) { + + try { gate.setLocation(playerLocation); sendMessage(ChatColor.AQUA + "The gates location has been set to your current location."); } - catch (Exception e) - { - } - + catch (Exception e) {} } - else - { + else { sendMessage(ChatColor.GREEN + "Gate with id \"" + id + "\" was created."); sendMessage("Now you should build a frame and:"); sendMessage(new CommandSetLocation().getUsageTemplate(true, true)); } } - } diff --git a/src/de/craftinc/gates/commands/CommandDelete.java b/src/de/craftinc/gates/commands/CommandDelete.java index 3835b7f..abc8575 100644 --- a/src/de/craftinc/gates/commands/CommandDelete.java +++ b/src/de/craftinc/gates/commands/CommandDelete.java @@ -2,7 +2,6 @@ package de.craftinc.gates.commands; import org.bukkit.ChatColor; -import de.craftinc.gates.Gate; import de.craftinc.gates.Plugin; @@ -31,7 +30,7 @@ public class CommandDelete extends BaseCommand public void perform() { - Gate.delete(gate.getId()); + Plugin.getPlugin().getGatesManager().handleDeletion(gate); sendMessage(ChatColor.GREEN + "Gate with id '" + gate.getId() + "' was deleted."); } } diff --git a/src/de/craftinc/gates/commands/CommandList.java b/src/de/craftinc/gates/commands/CommandList.java index 6dd575d..5cdc98c 100644 --- a/src/de/craftinc/gates/commands/CommandList.java +++ b/src/de/craftinc/gates/commands/CommandList.java @@ -134,9 +134,9 @@ public class CommandList extends BaseCommand */ protected Collection getAllGates() { - Collection gates = Gate.getAll(); + Collection gates = Plugin.getPlugin().getGatesManager().allGates(); - if (this.sender instanceof Player && Plugin.permission != null) { + if (this.sender instanceof Player && Plugin.getPermission() != null) { Player p = (Player)this.sender; // create a copy since we cannot iterate over a collection while modifying it! @@ -144,7 +144,7 @@ public class CommandList extends BaseCommand for (Gate gate : gatesCopy) { - boolean permissionAtGateLocation = Plugin.permission.has(gate.getLocation().getWorld(), p.getName(), this.requiredPermission); + boolean permissionAtGateLocation = Plugin.getPermission().has(gate.getLocation().getWorld(), p.getName(), this.requiredPermission); if (!permissionAtGateLocation) { gates.remove(gate); continue; @@ -152,7 +152,7 @@ public class CommandList extends BaseCommand if (gate.getExit() != null) { - boolean permissionAtGateExit = Plugin.permission.has(gate.getExit().getWorld(), p.getName(), this.requiredPermission); + boolean permissionAtGateExit = Plugin.getPermission().has(gate.getExit().getWorld(), p.getName(), this.requiredPermission); if (!permissionAtGateExit) { gates.remove(gate); } diff --git a/src/de/craftinc/gates/commands/CommandRename.java b/src/de/craftinc/gates/commands/CommandRename.java index d0caad0..269776a 100644 --- a/src/de/craftinc/gates/commands/CommandRename.java +++ b/src/de/craftinc/gates/commands/CommandRename.java @@ -2,7 +2,7 @@ package de.craftinc.gates.commands; import org.bukkit.ChatColor; -import de.craftinc.gates.Gate; +import de.craftinc.gates.GatesManager; import de.craftinc.gates.Plugin; @@ -33,16 +33,19 @@ public class CommandRename extends BaseCommand public void perform() { String newId = parameters.get(1); + GatesManager gatesManager = Plugin.getPlugin().getGatesManager(); - try - { - Gate.rename(gate.getId(), newId); - sendMessage(ChatColor.GREEN + "Gate " + gate.getId() + " is now known as " + newId + "."); - } - catch (Exception e) - { + if (gatesManager.gateExists(newId)) { sendMessage(ChatColor.RED + "Cannot rename " + gate.getId() + ". There is already a gate named " + newId + "."); } + else { + String oldId = gate.getId(); + + gate.setId(newId); + gatesManager.handleGateIdChange(gate, oldId); + + sendMessage(ChatColor.GREEN + "Gate " + gate.getId() + " is now known as " + newId + "."); + } } } diff --git a/src/de/craftinc/gates/listeners/PluginBlockListener.java b/src/de/craftinc/gates/listeners/PluginBlockListener.java index 627b1b6..1a534c3 100644 --- a/src/de/craftinc/gates/listeners/PluginBlockListener.java +++ b/src/de/craftinc/gates/listeners/PluginBlockListener.java @@ -9,6 +9,7 @@ import org.bukkit.event.Listener; import org.bukkit.event.block.BlockPhysicsEvent; +// TODO: remove this class!!! public class PluginBlockListener implements Listener diff --git a/src/de/craftinc/gates/listeners/PluginPlayerListener.java b/src/de/craftinc/gates/listeners/PluginPlayerListener.java index 51f90b8..e884b34 100644 --- a/src/de/craftinc/gates/listeners/PluginPlayerListener.java +++ b/src/de/craftinc/gates/listeners/PluginPlayerListener.java @@ -99,13 +99,13 @@ public class PluginPlayerListener implements Listener protected boolean hasPermission(Player player, Gate gate) { - if (Plugin.permission == null) // fallback: use the standard bukkit permission system + if (Plugin.getPermission() == null) // fallback: use the standard bukkit permission system { return player.hasPermission(Plugin.permissionUse); } else { - boolean permAtLocation = Plugin.permission.has(gate.getLocation().getWorld(), player.getName(), Plugin.permissionUse); - boolean permAtExit = Plugin.permission.has(gate.getExit().getWorld(), player.getName(), Plugin.permissionUse); + boolean permAtLocation = Plugin.getPermission().has(gate.getLocation().getWorld(), player.getName(), Plugin.permissionUse); + boolean permAtExit = Plugin.getPermission().has(gate.getExit().getWorld(), player.getName(), Plugin.permissionUse); return permAtLocation && permAtExit; } diff --git a/src/de/craftinc/gates/listeners/PluginPortalListener.java b/src/de/craftinc/gates/listeners/PluginPortalListener.java index 1c99d9b..c38fc39 100644 --- a/src/de/craftinc/gates/listeners/PluginPortalListener.java +++ b/src/de/craftinc/gates/listeners/PluginPortalListener.java @@ -19,6 +19,8 @@ public class PluginPortalListener implements Listener { private HashMap currentGateAtEvent = new HashMap(); + // TODO: check if this class can be deleted! + @EventHandler(priority = EventPriority.NORMAL) public void onPlayerPortal(PlayerPortalEvent event) @@ -31,7 +33,6 @@ public class PluginPortalListener implements Listener Location playerLocation = event.getPlayer().getLocation(); Gate gateAtLocation = GateUtil.getGateAtPlayerLocation(playerLocation); - // If the player's gamemode is creative no gate might be found! // It seems like players get teleported on a move event when the 'to' location is // inside a gate. This meens the location obtained earlier is NOT inside a gate. diff --git a/src/de/craftinc/gates/util/GateUtil.java b/src/de/craftinc/gates/util/GateUtil.java index 168fdb9..015743d 100644 --- a/src/de/craftinc/gates/util/GateUtil.java +++ b/src/de/craftinc/gates/util/GateUtil.java @@ -5,6 +5,7 @@ import org.bukkit.World; import org.bukkit.block.BlockFace; import de.craftinc.gates.Gate; +import de.craftinc.gates.Plugin; public class GateUtil { @@ -13,7 +14,7 @@ public class GateUtil Gate gate = null; double minmalDist = Double.MAX_VALUE; - for (Gate g : Gate.getAll()) { + for (Gate g : Plugin.getPlugin().getGatesManager().allGates()) { if (!g.getLocation().getWorld().equals(location.getWorld())) { @@ -43,7 +44,7 @@ public class GateUtil // players are sometime stuck into the ground Location locationUp = location.getBlock().getRelative(BlockFace.UP).getLocation(); - for (Gate g : Gate.getAll()) + for (Gate g : Plugin.getPlugin().getGatesManager().allGates()) { if (gate != null) { diff --git a/src/de/craftinc/gates/util/LocationUtil.java b/src/de/craftinc/gates/util/LocationUtil.java index f93940c..b8fb9f0 100644 --- a/src/de/craftinc/gates/util/LocationUtil.java +++ b/src/de/craftinc/gates/util/LocationUtil.java @@ -22,7 +22,7 @@ public class LocationUtil protected static World getWorld(String name) throws Exception { - World world = Plugin.instance.getServer().getWorld(name); + World world = Plugin.getPlugin().getServer().getWorld(name); if (world == null) { throw new Exception("World '" + name + "' does not exists anymore! Cannot get instance!");