From 9e36cf189b752dc25b00416b6e69872b2f77edcd Mon Sep 17 00:00:00 2001 From: Tobias Ottenweller Date: Mon, 17 Jun 2013 14:32:49 +0200 Subject: [PATCH] Added a configuration file. Made various classes read from the configuration. --- resources/config.yml | 7 ++ src/de/craftinc/gates/GatesManager.java | 101 ++++++++++-------- src/de/craftinc/gates/Plugin.java | 17 ++- .../gates/listeners/PlayerMoveListener.java | 13 ++- src/de/craftinc/gates/util/FloodUtil.java | 16 +-- .../gates/util/GateBlockChangeSender.java | 5 +- 6 files changed, 99 insertions(+), 60 deletions(-) create mode 100644 resources/config.yml diff --git a/resources/config.yml b/resources/config.yml new file mode 100644 index 0000000..e168a7f --- /dev/null +++ b/resources/config.yml @@ -0,0 +1,7 @@ +maxGateBlocks: 50 +playerGateBlockUpdateRadius: 64 +checkForBrokenGateFrames: true +gateTeleportMessage: "Thank you for traveling with Craft Inc. Gates." +showTeleportMessage: true +gateTeleportNoPermissionMessage: "You are not allowed to use this gate!" +showTeleportNoPermissionMessage: true diff --git a/src/de/craftinc/gates/GatesManager.java b/src/de/craftinc/gates/GatesManager.java index 3582ccc..336d171 100644 --- a/src/de/craftinc/gates/GatesManager.java +++ b/src/de/craftinc/gates/GatesManager.java @@ -35,21 +35,21 @@ import de.craftinc.gates.util.SimpleLocation; public class GatesManager { - private File gatesConfigFile; - private FileConfiguration gatesConfig; - private static final String gatesPath = "gates"; // path to gates inside the yaml file - private static final String storageVersionPath = "version"; - private static final int storageVersion = 1; + protected File gatesConfigFile; + protected FileConfiguration gatesConfig; + protected static final String gatesPath = "gates"; // path to gates inside the yaml file + protected static final String storageVersionPath = "version"; + protected static final int storageVersion = 1; - private static final int chunkRadius = 4; // TODO: move search radius into a config file / get value from config class - - private Map gatesById; - private Map> gatesByChunk; - private Map gatesByLocation; + protected int chunkRadius; + + protected Map gatesById; + protected Map> gatesByChunk; + protected Map gatesByLocation; private List gates; - - + + public Gate getGateWithId(String id) { return gatesById.get(id); @@ -134,9 +134,20 @@ public class GatesManager MigrationUtil.performMigration(fileStorageVersion, storageVersion, this.gates); } } - - - private void fillGatesById() + + + protected int getChunkRadius() + { + if (this.chunkRadius == 0) { + this.chunkRadius = Plugin.getPlugin().getConfig().getInt(Plugin.confPlayerGateBlockUpdateRadiusKey); + this.chunkRadius = this.chunkRadius >> 4; + } + + return this.chunkRadius; + } + + + protected void fillGatesById() { gatesById = new HashMap((int)(gates.size() * 1.25)); @@ -144,9 +155,9 @@ public class GatesManager this.addGateWithId(g); } } - - - private void fillGatesByChunk() + + + protected void fillGatesByChunk() { HashSet chunksUsedByGates = new HashSet(); @@ -159,9 +170,9 @@ public class GatesManager int x = c.getX(); int z = c.getZ(); - for (int i = x-chunkRadius; i < x+chunkRadius; i++) { + for (int i = x-getChunkRadius(); i < x+getChunkRadius(); i++) { - for (int j = z-chunkRadius; j < z+chunkRadius; j++) { + for (int j = z-getChunkRadius(); j < z+getChunkRadius(); j++) { chunksUsedByGates.add(new SimpleChunk(i, j, c.getWorld())); } @@ -175,9 +186,9 @@ public class GatesManager this.addGateByChunk(g); } } - - - private void fillGatesByLocation() + + + protected void fillGatesByLocation() { int numGateBlocks = 0; @@ -191,39 +202,39 @@ public class GatesManager this.addGateByLocations(g); } } - - - private void removeGateById(String id) + + + protected void removeGateById(String id) { gatesById.remove(id); } - - - private void addGateWithId(Gate g) + + + protected void addGateWithId(Gate g) { gatesById.put(g.getId(), g); } - - - private void removeGateFromLocations(Set gateBlocks) + + + protected void removeGateFromLocations(Set gateBlocks) { for (Location l : gateBlocks) { SimpleLocation sl = new SimpleLocation(l); gatesByLocation.remove(sl); } } - - - private void addGateByLocations(Gate g) + + + protected void addGateByLocations(Gate g) { for (Location l : g.getGateBlockLocations()) { SimpleLocation sl = new SimpleLocation(l); gatesByLocation.put(sl, g); } } - - - private void removeGateFromChunk(Gate g, Location l) + + + protected void removeGateFromChunk(Gate g, Location l) { if (l != null) { @@ -231,9 +242,9 @@ public class GatesManager int x = c.getX(); int z = c.getZ(); - for (int i = x-chunkRadius; i < x+chunkRadius; i++) { + for (int i = x-getChunkRadius(); i < x+getChunkRadius(); i++) { - for (int j = z-chunkRadius; j < z+chunkRadius; j++) { + for (int j = z-getChunkRadius(); j < z+getChunkRadius(); j++) { SimpleChunk sc = new SimpleChunk(i, j, c.getWorld()); Set gatesInChunk = gatesByChunk.get(sc); @@ -246,9 +257,9 @@ public class GatesManager } } } - - - private void addGateByChunk(Gate g) + + + protected void addGateByChunk(Gate g) { Location gateLocation = g.getLocation(); @@ -258,9 +269,9 @@ public class GatesManager int x = c.getX(); int z = c.getZ(); - for (int i = x-chunkRadius; i < x+chunkRadius; i++) { + for (int i = x-getChunkRadius(); i < x+getChunkRadius(); i++) { - for (int j = z-chunkRadius; j < z+chunkRadius; j++) { + for (int j = z-getChunkRadius(); j < z+getChunkRadius(); j++) { SimpleChunk sc = new SimpleChunk(i, j, c.getWorld()); diff --git a/src/de/craftinc/gates/Plugin.java b/src/de/craftinc/gates/Plugin.java index 5a46a7f..7fa0071 100644 --- a/src/de/craftinc/gates/Plugin.java +++ b/src/de/craftinc/gates/Plugin.java @@ -24,6 +24,7 @@ import java.util.logging.Logger; import de.craftinc.gates.listeners.*; import net.milkbowl.vault.permission.Permission; +import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.configuration.serialization.ConfigurationSerialization; @@ -39,6 +40,14 @@ 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"; + + public static final String confMaxGateBlocksKey = "maxGateBlocks"; + public static final String confPlayerGateBlockUpdateRadiusKey = "playerGateBlockUpdateRadius"; + public static final String confCheckForBrokenGateFramesKey = "checkForBrokenGateFrames"; + public static final String confGateTeleportMessageKey = "gateTeleportMessage"; + public static final String confShowTeleportMessageKey = "showTeleportMessage"; + public static final String confGateTeleportNoPermissionMessageKey = "gateTeleportNoPermissionMessage"; + public static final String confShowTeleportNoPermissionMessageKey = "showTeleportNoPermissionMessage"; private static Plugin instance; private static Permission permission; @@ -112,6 +121,9 @@ public class Plugin extends JavaPlugin @Override public void onEnable() { + // Setup configuration + this.saveDefaultConfig(); + // Setup permissions setupPermissions(); @@ -149,6 +161,8 @@ public class Plugin extends JavaPlugin pm.registerEvents(this.respawnListener, this); pm.registerEvents(this.worldChangeListener, this); pm.registerEvents(this.joinListener, this); + + } @@ -199,7 +213,8 @@ public class Plugin extends JavaPlugin } } - sender.sendMessage("Unknown gate-command \"" + commandName + "\". Try " + "/" + getBaseCommand() + " help"); + sender.sendMessage(ChatColor.RED + "Unknown gate-command \"" + commandName + "\"." + + ChatColor.GREEN + " Try " + "/" + getBaseCommand() + " help"); } diff --git a/src/de/craftinc/gates/listeners/PlayerMoveListener.java b/src/de/craftinc/gates/listeners/PlayerMoveListener.java index e5adc7e..6ed8859 100644 --- a/src/de/craftinc/gates/listeners/PlayerMoveListener.java +++ b/src/de/craftinc/gates/listeners/PlayerMoveListener.java @@ -62,7 +62,8 @@ public class PlayerMoveListener implements Listener // Check for permission - if (!hasPermission(event.getPlayer(), gateAtLocation)) { + if (!hasPermission(event.getPlayer(), gateAtLocation) + && Plugin.getPlugin().getConfig().getBoolean(Plugin.confShowTeleportNoPermissionMessageKey)) { String playerName = event.getPlayer().getName(); @@ -75,7 +76,9 @@ public class PlayerMoveListener implements Listener // do not display messages more often than once per second if (!this.lastNoPermissionMessages.containsKey(playerName) || this.lastNoPermissionMessages.get(playerName) < now - 10000L) { - event.getPlayer().sendMessage(ChatColor.RED + "You are not allowed to use this gate!"); + + String noPermissionString = Plugin.getPlugin().getConfig().getString(Plugin.confGateTeleportNoPermissionMessageKey); + event.getPlayer().sendMessage(ChatColor.RED + noPermissionString); this.lastNoPermissionMessages.put(playerName, now); } } @@ -103,7 +106,11 @@ public class PlayerMoveListener implements Listener ); p.teleport(destLocation); - p.sendMessage(ChatColor.DARK_AQUA + "Thank you for traveling with Craft Inc. Gates."); + + if (Plugin.getPlugin().getConfig().getBoolean(Plugin.confShowTeleportMessageKey)) { + String teleporMessage = Plugin.getPlugin().getConfig().getString(Plugin.confGateTeleportMessageKey); + p.sendMessage(ChatColor.DARK_AQUA + teleporMessage); + } } diff --git a/src/de/craftinc/gates/util/FloodUtil.java b/src/de/craftinc/gates/util/FloodUtil.java index 4e27f20..e517ed7 100644 --- a/src/de/craftinc/gates/util/FloodUtil.java +++ b/src/de/craftinc/gates/util/FloodUtil.java @@ -29,10 +29,8 @@ import de.craftinc.gates.Plugin; public class FloodUtil { - private final static int frameBlockSearchLimit = 100; // TODO: move search radius into a config file / get value from config class - - private static final Set exp1 = new HashSet(); - private static final Set exp2 = new HashSet(); + protected static final Set exp1 = new HashSet(); + protected static final Set exp2 = new HashSet(); static { @@ -51,7 +49,9 @@ public class FloodUtil // For the same frame and location this set of blocks is deterministic public static Set getGateFrameBlocks(Block block) { - Set blocks1 = getAirFloodBlocks(block, new HashSet(), exp1, frameBlockSearchLimit); + int frameBlockSearchLimit = Plugin.getPlugin().getConfig().getInt(Plugin.confMaxGateBlocksKey); + + Set blocks1 = getAirFloodBlocks(block, new HashSet(), exp1, frameBlockSearchLimit); Set blocks2 = getAirFloodBlocks(block, new HashSet(), exp2, frameBlockSearchLimit); if (blocks1 == null && blocks2 == null) { @@ -72,9 +72,9 @@ public class FloodUtil return blocks1; } - - - private static Set getAirFloodBlocks(Block startBlock, Set foundBlocks, Set expandFaces, int limit) + + + protected static Set getAirFloodBlocks(Block startBlock, Set foundBlocks, Set expandFaces, int limit) { if (foundBlocks == null) { return null; diff --git a/src/de/craftinc/gates/util/GateBlockChangeSender.java b/src/de/craftinc/gates/util/GateBlockChangeSender.java index 1dc1dbd..ccbb434 100644 --- a/src/de/craftinc/gates/util/GateBlockChangeSender.java +++ b/src/de/craftinc/gates/util/GateBlockChangeSender.java @@ -29,9 +29,6 @@ import java.util.Set; public class GateBlockChangeSender { - protected static final int searchRadius = 64; // TODO: move search radius into a config file / get value from config class - - public static void updateGateBlocks(final Player player) { if (player == null) { @@ -84,6 +81,8 @@ public class GateBlockChangeSender Location gateLocation = gate.getLocation(); ArrayList playersNearby = new ArrayList(); + int searchRadius = Plugin.getPlugin().getConfig().getInt(Plugin.confPlayerGateBlockUpdateRadiusKey); + for (Player p : Plugin.getPlugin().getServer().getOnlinePlayers()) { if (p.getLocation().distance(gateLocation) < searchRadius) {