Added a configuration file. Made various classes read from the configuration.

This commit is contained in:
Tobias Ottenweller 2013-06-17 14:32:49 +02:00
parent 78283a4f34
commit 9e36cf189b
6 changed files with 99 additions and 60 deletions

7
resources/config.yml Normal file
View File

@ -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

View File

@ -35,21 +35,21 @@ import de.craftinc.gates.util.SimpleLocation;
public class GatesManager public class GatesManager
{ {
private File gatesConfigFile; protected File gatesConfigFile;
private FileConfiguration gatesConfig; protected FileConfiguration gatesConfig;
private static final String gatesPath = "gates"; // path to gates inside the yaml file protected static final String gatesPath = "gates"; // path to gates inside the yaml file
private static final String storageVersionPath = "version"; protected static final String storageVersionPath = "version";
private static final int storageVersion = 1; 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 protected int chunkRadius;
private Map<String, Gate> gatesById; protected Map<String, Gate> gatesById;
private Map<SimpleChunk, Set<Gate>> gatesByChunk; protected Map<SimpleChunk, Set<Gate>> gatesByChunk;
private Map<SimpleLocation, Gate> gatesByLocation; protected Map<SimpleLocation, Gate> gatesByLocation;
private List<Gate> gates; private List<Gate> gates;
public Gate getGateWithId(String id) public Gate getGateWithId(String id)
{ {
return gatesById.get(id); return gatesById.get(id);
@ -134,9 +134,20 @@ public class GatesManager
MigrationUtil.performMigration(fileStorageVersion, storageVersion, this.gates); 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<String, Gate>((int)(gates.size() * 1.25)); gatesById = new HashMap<String, Gate>((int)(gates.size() * 1.25));
@ -144,9 +155,9 @@ public class GatesManager
this.addGateWithId(g); this.addGateWithId(g);
} }
} }
private void fillGatesByChunk() protected void fillGatesByChunk()
{ {
HashSet<SimpleChunk> chunksUsedByGates = new HashSet<SimpleChunk>(); HashSet<SimpleChunk> chunksUsedByGates = new HashSet<SimpleChunk>();
@ -159,9 +170,9 @@ public class GatesManager
int x = c.getX(); int x = c.getX();
int z = c.getZ(); 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())); chunksUsedByGates.add(new SimpleChunk(i, j, c.getWorld()));
} }
@ -175,9 +186,9 @@ public class GatesManager
this.addGateByChunk(g); this.addGateByChunk(g);
} }
} }
private void fillGatesByLocation() protected void fillGatesByLocation()
{ {
int numGateBlocks = 0; int numGateBlocks = 0;
@ -191,39 +202,39 @@ public class GatesManager
this.addGateByLocations(g); this.addGateByLocations(g);
} }
} }
private void removeGateById(String id) protected void removeGateById(String id)
{ {
gatesById.remove(id); gatesById.remove(id);
} }
private void addGateWithId(Gate g) protected void addGateWithId(Gate g)
{ {
gatesById.put(g.getId(), g); gatesById.put(g.getId(), g);
} }
private void removeGateFromLocations(Set<Location> gateBlocks) protected void removeGateFromLocations(Set<Location> gateBlocks)
{ {
for (Location l : gateBlocks) { for (Location l : gateBlocks) {
SimpleLocation sl = new SimpleLocation(l); SimpleLocation sl = new SimpleLocation(l);
gatesByLocation.remove(sl); gatesByLocation.remove(sl);
} }
} }
private void addGateByLocations(Gate g) protected void addGateByLocations(Gate g)
{ {
for (Location l : g.getGateBlockLocations()) { for (Location l : g.getGateBlockLocations()) {
SimpleLocation sl = new SimpleLocation(l); SimpleLocation sl = new SimpleLocation(l);
gatesByLocation.put(sl, g); gatesByLocation.put(sl, g);
} }
} }
private void removeGateFromChunk(Gate g, Location l) protected void removeGateFromChunk(Gate g, Location l)
{ {
if (l != null) { if (l != null) {
@ -231,9 +242,9 @@ public class GatesManager
int x = c.getX(); int x = c.getX();
int z = c.getZ(); 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()); SimpleChunk sc = new SimpleChunk(i, j, c.getWorld());
Set<Gate> gatesInChunk = gatesByChunk.get(sc); Set<Gate> 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(); Location gateLocation = g.getLocation();
@ -258,9 +269,9 @@ public class GatesManager
int x = c.getX(); int x = c.getX();
int z = c.getZ(); 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()); SimpleChunk sc = new SimpleChunk(i, j, c.getWorld());

View File

@ -24,6 +24,7 @@ import java.util.logging.Logger;
import de.craftinc.gates.listeners.*; import de.craftinc.gates.listeners.*;
import net.milkbowl.vault.permission.Permission; import net.milkbowl.vault.permission.Permission;
import org.bukkit.ChatColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.configuration.serialization.ConfigurationSerialization; 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 permissionInfo = "craftincgates.info";
public static final String permissionManage = "craftincgates.manage"; public static final String permissionManage = "craftincgates.manage";
public static final String permissionUse = "craftincgates.use"; 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 Plugin instance;
private static Permission permission; private static Permission permission;
@ -112,6 +121,9 @@ public class Plugin extends JavaPlugin
@Override @Override
public void onEnable() public void onEnable()
{ {
// Setup configuration
this.saveDefaultConfig();
// Setup permissions // Setup permissions
setupPermissions(); setupPermissions();
@ -149,6 +161,8 @@ public class Plugin extends JavaPlugin
pm.registerEvents(this.respawnListener, this); pm.registerEvents(this.respawnListener, this);
pm.registerEvents(this.worldChangeListener, this); pm.registerEvents(this.worldChangeListener, this);
pm.registerEvents(this.joinListener, 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");
} }

View File

@ -62,7 +62,8 @@ public class PlayerMoveListener implements Listener
// Check for permission // Check for permission
if (!hasPermission(event.getPlayer(), gateAtLocation)) { if (!hasPermission(event.getPlayer(), gateAtLocation)
&& Plugin.getPlugin().getConfig().getBoolean(Plugin.confShowTeleportNoPermissionMessageKey)) {
String playerName = event.getPlayer().getName(); String playerName = event.getPlayer().getName();
@ -75,7 +76,9 @@ public class PlayerMoveListener implements Listener
// do not display messages more often than once per second // do not display messages more often than once per second
if (!this.lastNoPermissionMessages.containsKey(playerName) || this.lastNoPermissionMessages.get(playerName) < now - 10000L) { 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); this.lastNoPermissionMessages.put(playerName, now);
} }
} }
@ -103,7 +106,11 @@ public class PlayerMoveListener implements Listener
); );
p.teleport(destLocation); 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);
}
} }

View File

@ -29,10 +29,8 @@ import de.craftinc.gates.Plugin;
public class FloodUtil public class FloodUtil
{ {
private final static int frameBlockSearchLimit = 100; // TODO: move search radius into a config file / get value from config class protected static final Set<BlockFace> exp1 = new HashSet<BlockFace>();
protected static final Set<BlockFace> exp2 = new HashSet<BlockFace>();
private static final Set<BlockFace> exp1 = new HashSet<BlockFace>();
private static final Set<BlockFace> exp2 = new HashSet<BlockFace>();
static static
{ {
@ -51,7 +49,9 @@ public class FloodUtil
// For the same frame and location this set of blocks is deterministic // For the same frame and location this set of blocks is deterministic
public static Set<Block> getGateFrameBlocks(Block block) public static Set<Block> getGateFrameBlocks(Block block)
{ {
Set<Block> blocks1 = getAirFloodBlocks(block, new HashSet<Block>(), exp1, frameBlockSearchLimit); int frameBlockSearchLimit = Plugin.getPlugin().getConfig().getInt(Plugin.confMaxGateBlocksKey);
Set<Block> blocks1 = getAirFloodBlocks(block, new HashSet<Block>(), exp1, frameBlockSearchLimit);
Set<Block> blocks2 = getAirFloodBlocks(block, new HashSet<Block>(), exp2, frameBlockSearchLimit); Set<Block> blocks2 = getAirFloodBlocks(block, new HashSet<Block>(), exp2, frameBlockSearchLimit);
if (blocks1 == null && blocks2 == null) { if (blocks1 == null && blocks2 == null) {
@ -72,9 +72,9 @@ public class FloodUtil
return blocks1; return blocks1;
} }
private static Set<Block> getAirFloodBlocks(Block startBlock, Set<Block> foundBlocks, Set<BlockFace> expandFaces, int limit) protected static Set<Block> getAirFloodBlocks(Block startBlock, Set<Block> foundBlocks, Set<BlockFace> expandFaces, int limit)
{ {
if (foundBlocks == null) { if (foundBlocks == null) {
return null; return null;

View File

@ -29,9 +29,6 @@ import java.util.Set;
public class GateBlockChangeSender 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) public static void updateGateBlocks(final Player player)
{ {
if (player == null) { if (player == null) {
@ -84,6 +81,8 @@ public class GateBlockChangeSender
Location gateLocation = gate.getLocation(); Location gateLocation = gate.getLocation();
ArrayList<Player> playersNearby = new ArrayList<Player>(); ArrayList<Player> playersNearby = new ArrayList<Player>();
int searchRadius = Plugin.getPlugin().getConfig().getInt(Plugin.confPlayerGateBlockUpdateRadiusKey);
for (Player p : Plugin.getPlugin().getServer().getOnlinePlayers()) { for (Player p : Plugin.getPlugin().getServer().getOnlinePlayers()) {
if (p.getLocation().distance(gateLocation) < searchRadius) { if (p.getLocation().distance(gateLocation) < searchRadius) {