From 040377cb36b6839207b9c7ea4dc3e05ab9841c45 Mon Sep 17 00:00:00 2001 From: Tobias Ottenweller Date: Sat, 18 May 2013 11:26:45 +0200 Subject: [PATCH] Refactoring and code cleanup. --- src/de/craftinc/gates/Gate.java | 12 +-- src/de/craftinc/gates/GatesManager.java | 2 +- .../craftinc/gates/commands/BaseCommand.java | 7 +- .../gates/commands/BaseLocationCommand.java | 2 +- .../gates/commands/CommandCreate.java | 2 +- .../craftinc/gates/commands/CommandHelp.java | 42 ++++----- .../craftinc/gates/commands/CommandList.java | 10 +- .../gates/listeners/PluginBlockListener.java | 52 ----------- .../gates/listeners/PluginPortalListener.java | 91 ------------------- src/de/craftinc/gates/util/GateUtil.java | 87 ------------------ src/de/craftinc/gates/util/TextUtil.java | 25 +---- 11 files changed, 30 insertions(+), 302 deletions(-) delete mode 100644 src/de/craftinc/gates/listeners/PluginBlockListener.java delete mode 100644 src/de/craftinc/gates/listeners/PluginPortalListener.java delete mode 100644 src/de/craftinc/gates/util/GateUtil.java diff --git a/src/de/craftinc/gates/Gate.java b/src/de/craftinc/gates/Gate.java index 4bd8f06..1653a6b 100644 --- a/src/de/craftinc/gates/Gate.java +++ b/src/de/craftinc/gates/Gate.java @@ -98,7 +98,7 @@ public class Gate implements ConfigurationSerializable { this.isHidden = isHidden; - if (isHidden == true) { + if (isHidden) { emptyGate(); } else if (isOpen()) { @@ -117,14 +117,14 @@ public class Gate implements ConfigurationSerializable public void setOpen(boolean isOpen) throws Exception { - if (isOpen == true && this.isOpen == false) { + if (isOpen && !this.isOpen) { findPortalBlocks(); if (!isHidden) { fillGate(); } } - else if (isOpen == false && this.isOpen == true) { + else if (!isOpen && this.isOpen) { emptyGate(); } @@ -213,7 +213,7 @@ public class Gate implements ConfigurationSerializable } - if (isHidden == false) { + if (!isHidden) { for (Location l : gateBlockLocations) { if (l.getBlock().getType() == Material.AIR) { setOpen(false); @@ -240,7 +240,7 @@ public class Gate implements ConfigurationSerializable @SuppressWarnings("unchecked") - public Gate(Map map) + private Gate(Map map) { try { id = map.get(idKey).toString(); @@ -272,8 +272,6 @@ public class Gate implements ConfigurationSerializable Plugin.log("NOTE: This gate will be removed from 'gates.yml' and added to 'invalid_gates.yml'!"); Plugin.getPlugin().getGatesManager().storeInvalidGate(map); - - return; } } diff --git a/src/de/craftinc/gates/GatesManager.java b/src/de/craftinc/gates/GatesManager.java index 9aa7cf7..dcf77bf 100644 --- a/src/de/craftinc/gates/GatesManager.java +++ b/src/de/craftinc/gates/GatesManager.java @@ -240,7 +240,7 @@ public class GatesManager fileWriter.close(); } catch (IOException e) { - Plugin.log("ERROR: Could not save invalid gates to disk. Reason: \n" + e.getStackTrace()); + Plugin.log("ERROR: Could not save invalid gates to disk. Reason: \n" + Arrays.toString(e.getStackTrace())); } } diff --git a/src/de/craftinc/gates/commands/BaseCommand.java b/src/de/craftinc/gates/commands/BaseCommand.java index 5f03929..4c63268 100644 --- a/src/de/craftinc/gates/commands/BaseCommand.java +++ b/src/de/craftinc/gates/commands/BaseCommand.java @@ -85,7 +85,7 @@ public abstract class BaseCommand } boolean senderHasPermission = this.hasPermission(); - boolean valid = false; + boolean valid; if (this.senderMustBePlayer && !senderIsPlayer) { sendMessage(ChatColor.RED + "This command can only be used by ingame players."); @@ -233,9 +233,4 @@ public abstract class BaseCommand { return getUsageTemplate(withColor, false); } - - protected String getUsageTemplate() - { - return getUsageTemplate(true); - } } diff --git a/src/de/craftinc/gates/commands/BaseLocationCommand.java b/src/de/craftinc/gates/commands/BaseLocationCommand.java index 1b288b1..348bc58 100644 --- a/src/de/craftinc/gates/commands/BaseLocationCommand.java +++ b/src/de/craftinc/gates/commands/BaseLocationCommand.java @@ -9,7 +9,7 @@ public abstract class BaseLocationCommand extends BaseCommand { protected Location getValidPlayerLocation() { - // The player might stand in a halfblock or a sign or whatever + // The player might stand in a half block or a sign or whatever // Therefore we load som extra locations and blocks Block playerBlock = player.getLocation().getBlock(); Block upBlock = playerBlock.getRelative(BlockFace.UP); diff --git a/src/de/craftinc/gates/commands/CommandCreate.java b/src/de/craftinc/gates/commands/CommandCreate.java index 5de36c7..51d0f73 100644 --- a/src/de/craftinc/gates/commands/CommandCreate.java +++ b/src/de/craftinc/gates/commands/CommandCreate.java @@ -54,7 +54,7 @@ public class CommandCreate extends BaseLocationCommand gate.setLocation(playerLocation); sendMessage(ChatColor.AQUA + "The gates location has been set to your current location."); } - catch (Exception e) {} + catch (Exception ignored) {} } else { diff --git a/src/de/craftinc/gates/commands/CommandHelp.java b/src/de/craftinc/gates/commands/CommandHelp.java index 20bbdac..826e38e 100644 --- a/src/de/craftinc/gates/commands/CommandHelp.java +++ b/src/de/craftinc/gates/commands/CommandHelp.java @@ -1,15 +1,12 @@ package de.craftinc.gates.commands; +import de.craftinc.gates.Plugin; +import de.craftinc.gates.util.TextUtil; + import java.util.ArrayList; import java.util.Collections; import java.util.List; -import org.bukkit.command.CommandSender; - -import de.craftinc.gates.Gate; -import de.craftinc.gates.Plugin; -import de.craftinc.gates.util.TextUtil; - public class CommandHelp extends BaseCommand { public static List> helpPages; @@ -38,8 +35,7 @@ public class CommandHelp extends BaseCommand // put 5 commands on one page helpPages = new ArrayList>(); - while (!allUsageStrings.isEmpty()) - { + while (!allUsageStrings.isEmpty()) { int toIndex = allUsageStrings.size() >= 6 ? 5 : allUsageStrings.size(); List currentHelpPage = new ArrayList(allUsageStrings.subList(0, toIndex)); helpPages.add(currentHelpPage); @@ -64,35 +60,29 @@ public class CommandHelp extends BaseCommand shouldPersistToDisk = false; senderMustBePlayer = false; } - - public boolean hasPermission(CommandSender sender, Gate gate) - { - return true; - } - - - public void perform() - { - int page = 1; + + public void perform() + { + int page; - if (parameters.size() > 0) - { - try - { + if (parameters.size() > 0) { + try { page = Integer.parseInt(parameters.get(0)); } - catch (NumberFormatException e) - { + catch (NumberFormatException e) { // wasn't an integer + page = 1; } } + else { + page = 1; + } sendMessage(TextUtil.titleize("Craft Inc. Gates Help (" + page + "/" + helpPages.size() + ")")); page -= 1; - if (page < 0 || page >= helpPages.size()) - { + if (page < 0 || page >= helpPages.size()) { sendMessage("This page does not exist"); return; } diff --git a/src/de/craftinc/gates/commands/CommandList.java b/src/de/craftinc/gates/commands/CommandList.java index 2a73817..ccd3dfb 100644 --- a/src/de/craftinc/gates/commands/CommandList.java +++ b/src/de/craftinc/gates/commands/CommandList.java @@ -282,13 +282,7 @@ public class CommandList extends BaseCommand } // cleanup - if (linesNecessaryForCurrentGates < linesLeftOnPage) { - moreGatesOnLastPage = false; - } - else { - moreGatesOnLastPage = true; - } - + moreGatesOnLastPage = linesNecessaryForCurrentGates >= linesLeftOnPage; linesLeftOnPage -= linesToFill; } } @@ -310,7 +304,7 @@ public class CommandList extends BaseCommand try { page = new Integer(parameters.get(0)); } - catch (Exception e) { } + catch (Exception ignored) { } return page; } diff --git a/src/de/craftinc/gates/listeners/PluginBlockListener.java b/src/de/craftinc/gates/listeners/PluginBlockListener.java deleted file mode 100644 index 1a534c3..0000000 --- a/src/de/craftinc/gates/listeners/PluginBlockListener.java +++ /dev/null @@ -1,52 +0,0 @@ -package de.craftinc.gates.listeners; - -import org.bukkit.Material; -import org.bukkit.block.Block; -import org.bukkit.block.BlockFace; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.Listener; -import org.bukkit.event.block.BlockPhysicsEvent; - - -// TODO: remove this class!!! - - -public class PluginBlockListener implements Listener -{ - @EventHandler(priority = EventPriority.NORMAL) - public void onBlockPhysics(BlockPhysicsEvent event) - { - if (event.isCancelled()) - return; - - if (event.getBlock().getType() != Material.PORTAL) { - return; - } - - if (isBlockInPortal(event.getBlock())) { - event.setCancelled(true); - } - } - - public boolean isBlockInPortal(Block block) - { - if (block.getRelative(BlockFace.UP).getType() == Material.AIR) { - return false; - } - - if (block.getRelative(BlockFace.DOWN).getType() == Material.AIR) { - return false; - } - - if ( block.getRelative(BlockFace.NORTH).getType() != Material.AIR && block.getRelative(BlockFace.SOUTH).getType() != Material.AIR ) { - return true; - } - - if ( block.getRelative(BlockFace.WEST).getType() != Material.AIR && block.getRelative(BlockFace.EAST).getType() != Material.AIR ) { - return true; - } - - return false; - } -} diff --git a/src/de/craftinc/gates/listeners/PluginPortalListener.java b/src/de/craftinc/gates/listeners/PluginPortalListener.java deleted file mode 100644 index c38fc39..0000000 --- a/src/de/craftinc/gates/listeners/PluginPortalListener.java +++ /dev/null @@ -1,91 +0,0 @@ -package de.craftinc.gates.listeners; - -import java.util.HashMap; - -import org.bukkit.GameMode; -import org.bukkit.Location; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.Listener; -import org.bukkit.event.entity.EntityPortalEnterEvent; -import org.bukkit.event.player.PlayerPortalEvent; - -import de.craftinc.gates.Gate; -import de.craftinc.gates.util.GateUtil; - - -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) - { - if (event.isCancelled()) - { - return; - } - - 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. - if (gateAtLocation == null && event.getPlayer().getGameMode() == GameMode.CREATIVE) - { - gateAtLocation = this.currentGateAtEvent.get(event.getPlayer()); - } - - if (gateAtLocation != null) - { - event.setCancelled(true); - } - - - this.currentGateAtEvent.put(event.getPlayer(), null); - } - - - @EventHandler(priority = EventPriority.NORMAL) - public void onEntityPortalEnterEvent(EntityPortalEnterEvent event) - { - if (event.getEntity() instanceof Player) - { - Player player = (Player)event.getEntity(); - - if (player.getGameMode() == GameMode.CREATIVE) - { - if (this.currentGateAtEvent.get(player) != null) - { - return; - } - - Location eventLocation = event.getLocation(); - Gate closestGate = GateUtil.closestGate(eventLocation); - - if (closestGate != null) - { - // Make sure gate and event locations are on the same height (y-value). - // Otherwise the distance will be messed up when players are flying. - // FIX ME: this could potentially let a nearby nether portal fail! - eventLocation.setY(closestGate.getLocation().getY()); - - double distToClosestGate = closestGate.getLocation().distance(eventLocation); - - if (distToClosestGate < 2.0) { - this.currentGateAtEvent.put(player, closestGate); - return; - } - } - } - - this.currentGateAtEvent.put(player, null); - } - } - -} diff --git a/src/de/craftinc/gates/util/GateUtil.java b/src/de/craftinc/gates/util/GateUtil.java deleted file mode 100644 index 81d500b..0000000 --- a/src/de/craftinc/gates/util/GateUtil.java +++ /dev/null @@ -1,87 +0,0 @@ -package de.craftinc.gates.util; - -import org.bukkit.Location; -import org.bukkit.World; -import org.bukkit.block.BlockFace; - -import de.craftinc.gates.Gate; -import de.craftinc.gates.Plugin; - -public class GateUtil -{ - public static Gate closestGate(Location location) - { - Gate gate = null; - double minmalDist = Double.MAX_VALUE; - - for (Gate g : Plugin.getPlugin().getGatesManager().allGates()) { - - if (!g.getLocation().getWorld().equals(location.getWorld())) - { - continue; - } - - double tempDist = g.getLocation().distance(location); - - if (tempDist < minmalDist) - { - gate = g; - minmalDist = tempDist; - } - - } - - return gate; - } - - - - public static Gate getGateAtPlayerLocation(Location location) - { - Gate gate = null; - World playerWorld = location.getWorld(); - - // players are sometime stuck into the ground - Location locationUp = location.getBlock().getRelative(BlockFace.UP).getLocation(); - - for (Gate g : Plugin.getPlugin().getGatesManager().allGates()) - { - if (gate != null) - { - break; - } - - // Check if the gate is open and useable - if (g.getLocation() == null) { - continue; - } - - World gateWorld = g.getLocation().getWorld(); - - if (!g.isOpen() || !gateWorld.equals(playerWorld)) { - continue; - } - - - // Check if the location matches - for (Location l: g.getGateBlockLocations()) { - - if (LocationUtil.locationsAreAtSamePositions(l, location) || LocationUtil.locationsAreAtSamePositions(l, locationUp)) - { - // Check if the gate is still valid - try { - g.validate(); - - gate = g; - break; - } - catch (Exception e2) { - break; // do nothing - gate got closed - } - } - } - } - - return gate; - } -} diff --git a/src/de/craftinc/gates/util/TextUtil.java b/src/de/craftinc/gates/util/TextUtil.java index f87e1a7..234a470 100644 --- a/src/de/craftinc/gates/util/TextUtil.java +++ b/src/de/craftinc/gates/util/TextUtil.java @@ -1,9 +1,8 @@ package de.craftinc.gates.util; -import java.util.List; - import org.bukkit.ChatColor; -import org.bukkit.Material; + +import java.util.List; public class TextUtil { @@ -29,7 +28,7 @@ public class TextUtil /** - * Joins all emements of list into a single string, sperating the original strings with glue. + * Joins all elements of list into a single string, separating the original strings with glue. */ public static String implode(List list, String glue) { @@ -45,24 +44,6 @@ public class TextUtil return ret; } - - /** - * Joins all emements of list into a single string. - */ - public static String implode(List list) { - return implode(list, ""); - } - - - - public static String getMaterialName(Material material) - { - String ret = material.toString(); - ret = ret.replace('_', ' '); - ret = ret.toLowerCase(); - - return ret.substring(0, 1).toUpperCase() + ret.substring(1); - } }