From e9a454182a5db231c3fc97a59f42d87ffb61e566 Mon Sep 17 00:00:00 2001 From: Tobias Ottenweller Date: Sun, 15 Sep 2013 17:07:25 +0200 Subject: [PATCH] Added the ability to change gate block types. Issue #26. Refactoring: Added a ConfigurationUtil class. Moved all configuration keys into it. --- resources/config.yml | 1 + src/de/craftinc/gates/Gate.java | 7 +- src/de/craftinc/gates/GatesManager.java | 3 +- src/de/craftinc/gates/Plugin.java | 13 +-- .../craftinc/gates/commands/BaseCommand.java | 3 +- .../gates/listeners/PlayerMoveListener.java | 9 +- .../gates/util/ConfigurationUtil.java | 110 ++++++++++++++++++ src/de/craftinc/gates/util/FloodUtil.java | 2 +- .../gates/util/GateBlockChangeSender.java | 17 ++- 9 files changed, 138 insertions(+), 27 deletions(-) create mode 100644 src/de/craftinc/gates/util/ConfigurationUtil.java diff --git a/resources/config.yml b/resources/config.yml index 9e81c94..1e2f188 100644 --- a/resources/config.yml +++ b/resources/config.yml @@ -7,3 +7,4 @@ gateTeleportMessage: "Thank you for traveling with Craft Inc. Gates." showTeleportMessage: true gateTeleportNoPermissionMessage: "You are not allowed to use this gate!" showTeleportNoPermissionMessage: true +gateMaterial: "nether portal" \ No newline at end of file diff --git a/src/de/craftinc/gates/Gate.java b/src/de/craftinc/gates/Gate.java index cb20d44..5f83a25 100644 --- a/src/de/craftinc/gates/Gate.java +++ b/src/de/craftinc/gates/Gate.java @@ -16,6 +16,7 @@ */ package de.craftinc.gates; +import de.craftinc.gates.util.ConfigurationUtil; import de.craftinc.gates.util.FloodUtil; import de.craftinc.gates.persistence.LocationUtil; import org.bukkit.Location; @@ -57,7 +58,7 @@ public class Gate implements ConfigurationSerializable /** * - * @return This method might return a 'null' value. + * @return This method might return a 'null' data. */ public Location getLocation() { @@ -100,7 +101,7 @@ public class Gate implements ConfigurationSerializable /** * * @param exit Supplying 'null' is permitted. - * @throws Exception An exception will be thrown if 'null' value is supplied and this gate is open. Note that the + * @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. */ @@ -235,7 +236,7 @@ public class Gate implements ConfigurationSerializable throw new Exception("Gate got closed. The frame is missing or broken. (no gate blocks)"); } - if (!isHidden() && Plugin.getPlugin().getConfig().getBoolean(Plugin.confCheckForBrokenGateFramesKey)) { + if (!isHidden() && Plugin.getPlugin().getConfig().getBoolean(ConfigurationUtil.confCheckForBrokenGateFramesKey)) { for (Block b : gateFrameBlocks) { diff --git a/src/de/craftinc/gates/GatesManager.java b/src/de/craftinc/gates/GatesManager.java index a7eaf78..c1c3cf3 100644 --- a/src/de/craftinc/gates/GatesManager.java +++ b/src/de/craftinc/gates/GatesManager.java @@ -24,6 +24,7 @@ import java.util.*; import java.util.logging.Level; import de.craftinc.gates.persistence.MigrationUtil; +import de.craftinc.gates.util.ConfigurationUtil; import org.bukkit.Chunk; import org.bukkit.Location; import org.bukkit.block.Block; @@ -193,7 +194,7 @@ public class GatesManager protected int getChunkRadius() { if (this.chunkRadius == 0) { - this.chunkRadius = Plugin.getPlugin().getConfig().getInt(Plugin.confPlayerGateBlockUpdateRadiusKey); + this.chunkRadius = Plugin.getPlugin().getConfig().getInt(ConfigurationUtil.confPlayerGateBlockUpdateRadiusKey); this.chunkRadius = this.chunkRadius >> 4; } diff --git a/src/de/craftinc/gates/Plugin.java b/src/de/craftinc/gates/Plugin.java index db90339..18b7e63 100644 --- a/src/de/craftinc/gates/Plugin.java +++ b/src/de/craftinc/gates/Plugin.java @@ -23,6 +23,7 @@ import java.util.logging.Logger; import de.craftinc.gates.listeners.*; +import de.craftinc.gates.util.ConfigurationUtil; import net.milkbowl.vault.permission.Permission; import org.bukkit.ChatColor; @@ -42,16 +43,6 @@ 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"; - public static final String confSaveOnChangesKey = "saveOnChanges"; - public static final String confHighlightDurationKey = "highlightDuration"; private static Plugin instance; private static Permission permission; @@ -184,7 +175,7 @@ public class Plugin extends JavaPlugin pm.registerEvents(this.worldChangeListener, this); pm.registerEvents(this.joinListener, this); - if (getConfig().getBoolean(confCheckForBrokenGateFramesKey)) { + if (getConfig().getBoolean(ConfigurationUtil.confCheckForBrokenGateFramesKey)) { pm.registerEvents(this.blockBreakListener, this); } } diff --git a/src/de/craftinc/gates/commands/BaseCommand.java b/src/de/craftinc/gates/commands/BaseCommand.java index b29e53d..9ded795 100644 --- a/src/de/craftinc/gates/commands/BaseCommand.java +++ b/src/de/craftinc/gates/commands/BaseCommand.java @@ -19,6 +19,7 @@ package de.craftinc.gates.commands; import java.util.ArrayList; import java.util.List; +import de.craftinc.gates.util.ConfigurationUtil; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -69,7 +70,7 @@ public abstract class BaseCommand this.perform(); - if (this.shouldPersistToDisk && Plugin.getPlugin().getConfig().getBoolean(Plugin.confSaveOnChangesKey)) { + if (this.shouldPersistToDisk && Plugin.getPlugin().getConfig().getBoolean(ConfigurationUtil.confSaveOnChangesKey)) { Plugin.getPlugin().getGatesManager().saveGatesToDisk(); } } diff --git a/src/de/craftinc/gates/listeners/PlayerMoveListener.java b/src/de/craftinc/gates/listeners/PlayerMoveListener.java index b7c23de..43cf249 100644 --- a/src/de/craftinc/gates/listeners/PlayerMoveListener.java +++ b/src/de/craftinc/gates/listeners/PlayerMoveListener.java @@ -19,6 +19,7 @@ package de.craftinc.gates.listeners; import java.util.Calendar; import java.util.HashMap; +import de.craftinc.gates.util.ConfigurationUtil; import de.craftinc.gates.util.GateBlockChangeSender; import org.bukkit.ChatColor; import org.bukkit.Location; @@ -70,7 +71,7 @@ public class PlayerMoveListener implements Listener // Check for permission if (!hasPermission(event.getPlayer(), gateAtLocation) - && Plugin.getPlugin().getConfig().getBoolean(Plugin.confShowTeleportNoPermissionMessageKey)) { + && Plugin.getPlugin().getConfig().getBoolean(ConfigurationUtil.confShowTeleportNoPermissionMessageKey)) { String playerName = event.getPlayer().getName(); @@ -84,7 +85,7 @@ 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) { - String noPermissionString = Plugin.getPlugin().getConfig().getString(Plugin.confGateTeleportNoPermissionMessageKey); + String noPermissionString = Plugin.getPlugin().getConfig().getString(ConfigurationUtil.confGateTeleportNoPermissionMessageKey); event.getPlayer().sendMessage(ChatColor.RED + noPermissionString); this.lastNoPermissionMessages.put(playerName, now); } @@ -114,8 +115,8 @@ public class PlayerMoveListener implements Listener p.teleport(destLocation); - if (Plugin.getPlugin().getConfig().getBoolean(Plugin.confShowTeleportMessageKey)) { - String teleporMessage = Plugin.getPlugin().getConfig().getString(Plugin.confGateTeleportMessageKey); + if (Plugin.getPlugin().getConfig().getBoolean(ConfigurationUtil.confShowTeleportMessageKey)) { + String teleporMessage = Plugin.getPlugin().getConfig().getString(ConfigurationUtil.confGateTeleportMessageKey); p.sendMessage(ChatColor.DARK_AQUA + teleporMessage); } } diff --git a/src/de/craftinc/gates/util/ConfigurationUtil.java b/src/de/craftinc/gates/util/ConfigurationUtil.java new file mode 100644 index 0000000..4ea0e9a --- /dev/null +++ b/src/de/craftinc/gates/util/ConfigurationUtil.java @@ -0,0 +1,110 @@ +package de.craftinc.gates.util; + + +import de.craftinc.gates.Plugin; +import org.bukkit.Material; + +import java.util.logging.Level; + + +public class ConfigurationUtil +{ + 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"; + public static final String confSaveOnChangesKey = "saveOnChanges"; + public static final String confHighlightDurationKey = "highlightDuration"; + public static final String confGateMaterialKey = "gateMaterial"; + + + 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")) { + material.material = Material.STATIONARY_WATER; + } + else if (materialString.equals("lava")) { + material.material = Material.STATIONARY_LAVA; + } + else if (materialString.equals("cobweb")) { + material.material = Material.WEB; + } + else if (materialString.equals("grass")) { + material.material = Material.LONG_GRASS; + material.data = 1; + } + else if (materialString.equals("dead bush")) { + material.material = Material.DEAD_BUSH; + } + else if (materialString.equals("dandelion")) { + material.material = Material.YELLOW_FLOWER; // TODO: this will change with minecraft 1.7 + } + else if (materialString.equals("poppy")) { + material.material = Material.RED_ROSE; // TODO: this will change with minecraft 1.7 + } + else if (materialString.equals("brown mushroom")) { + material.material = Material.BROWN_MUSHROOM; + } + else if (materialString.equals("red mushroom")) { + material.material = Material.RED_MUSHROOM; + } + else if (materialString.equals("torch")) { + material.material = Material.TORCH; + } + else if (materialString.equals("redstone torch (off)")) { + material.material = Material.REDSTONE_TORCH_OFF; + } + else if (materialString.equals("redstone torch (on)")) { + material.material = Material.REDSTONE_TORCH_ON; + } + else if (materialString.equals("fence")) { + material.material = Material.FENCE; + } + else if (materialString.equals("nether portal")) { + material.material = Material.PORTAL; + } + else if (materialString.equals("iron bars")) { + material.material = Material.IRON_FENCE; + } + else if (materialString.equals("glass pane")) { + material.material = Material.THIN_GLASS; + } + else if (materialString.equals("fence gate")) { + material.material = Material.FENCE_GATE; + } + else if (materialString.equals("nether brick fence")) { + material.material = Material.NETHER_FENCE; + } + else if (materialString.equals("nether wart")) { + material.material = Material.NETHER_WARTS; + } + else if (materialString.equals("end portal")) { + material.material = Material.ENDER_PORTAL; + } + else if (materialString.equals("cobblestone wall")) { + material.material = Material.COBBLE_WALL; + } + else { // fallback! + material.material = Material.PORTAL; + Plugin.log(Level.WARNING, "Gate material invalid! Please check and correct your configuration file!"); + } + + return material; + } +} + + +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 f8c36d5..f01d55b 100644 --- a/src/de/craftinc/gates/util/FloodUtil.java +++ b/src/de/craftinc/gates/util/FloodUtil.java @@ -148,7 +148,7 @@ public class FloodUtil throw new IllegalArgumentException("'block' must not be 'null'"); } - int frameBlockSearchLimit = Plugin.getPlugin().getConfig().getInt(Plugin.confMaxGateBlocksKey); + int frameBlockSearchLimit = Plugin.getPlugin().getConfig().getInt(ConfigurationUtil.confMaxGateBlocksKey); Set blocks1 = getAirFloodBlocks(block, new HashSet(), exp1, frameBlockSearchLimit); Set blocks2 = getAirFloodBlocks(block, new HashSet(), exp2, frameBlockSearchLimit); diff --git a/src/de/craftinc/gates/util/GateBlockChangeSender.java b/src/de/craftinc/gates/util/GateBlockChangeSender.java index 93a412b..6f87bfe 100644 --- a/src/de/craftinc/gates/util/GateBlockChangeSender.java +++ b/src/de/craftinc/gates/util/GateBlockChangeSender.java @@ -19,6 +19,7 @@ package de.craftinc.gates.util; import de.craftinc.gates.Plugin; import de.craftinc.gates.Gate; + import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; @@ -33,7 +34,7 @@ public class GateBlockChangeSender { /** * Replaces gate frame blocks with glowstone for a short period of time. - * Uses the value stored in 'highlightDuration' inside the config file + * 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! @@ -55,7 +56,7 @@ public class GateBlockChangeSender Plugin plugin = Plugin.getPlugin(); - long highlightDuration = 20 * plugin.getConfig().getLong(Plugin.confHighlightDurationKey); + long highlightDuration = 20 * plugin.getConfig().getLong(ConfigurationUtil.confHighlightDurationKey); Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() { @Override @@ -109,6 +110,7 @@ public class GateBlockChangeSender } Set gatesNearby = Plugin.getPlugin().getGatesManager().getNearbyGates(location.getChunk()); + GateMaterial gateMaterial = ConfigurationUtil.getPortalMaterial(); if (gatesNearby == null) { return; // no gates nearby @@ -123,7 +125,7 @@ public class GateBlockChangeSender for (Location l : g.getGateBlockLocations()) { if (l.getBlock().getType() == Material.AIR) { - player.sendBlockChange(l, Material.PORTAL, (byte)0); + player.sendBlockChange(l, gateMaterial.material, gateMaterial.data); } } } @@ -186,7 +188,7 @@ public class GateBlockChangeSender ArrayList playersNearby = new ArrayList(); - int searchRadius = Plugin.getPlugin().getConfig().getInt(Plugin.confPlayerGateBlockUpdateRadiusKey); + int searchRadius = Plugin.getPlugin().getConfig().getInt(ConfigurationUtil.confPlayerGateBlockUpdateRadiusKey); for (Player p : Plugin.getPlugin().getServer().getOnlinePlayers()) { @@ -195,10 +197,13 @@ public class GateBlockChangeSender } } + GateMaterial gateMaterial = ConfigurationUtil.getPortalMaterial(); Material material; + byte data = 0; if (gate.isOpen() && !gate.isHidden() && !remove) { - material = Material.PORTAL; + material = gateMaterial.material; + data = gateMaterial.data; } else { material = Material.AIR; @@ -209,7 +214,7 @@ public class GateBlockChangeSender for (Location l : gate.getGateBlockLocations()) { if (l.getBlock().getType() == Material.AIR) { // on server-side a gate is always made out of AIR - p.sendBlockChange(l, material, (byte)0); + p.sendBlockChange(l, material, data); } } }