From 40b396f8a2eb1e884e290cf91c57ff04291da641 Mon Sep 17 00:00:00 2001 From: Tobias Ottenweller Date: Sat, 31 Dec 2016 11:59:11 +0100 Subject: [PATCH] code formatter applied. --- .../craftinc/gates/commands/BaseCommand.java | 396 ++++++------ .../gates/commands/BaseLocationCommand.java | 44 +- .../gates/commands/CommandAllowRiding.java | 12 +- .../craftinc/gates/commands/CommandClose.java | 63 +- .../gates/commands/CommandDenyRiding.java | 14 +- .../craftinc/gates/commands/CommandExit.java | 58 +- .../gates/commands/CommandExitOpen.java | 22 +- .../craftinc/gates/commands/CommandHelp.java | 153 +++-- .../craftinc/gates/commands/CommandHide.java | 57 +- .../craftinc/gates/commands/CommandInfo.java | 57 +- .../craftinc/gates/commands/CommandList.java | 593 ++++++++---------- .../gates/commands/CommandLocation.java | 66 +- .../gates/commands/CommandNearby.java | 22 +- .../craftinc/gates/commands/CommandNew.java | 89 ++- .../craftinc/gates/commands/CommandOpen.java | 54 +- .../gates/commands/CommandRemove.java | 50 +- .../gates/commands/CommandRename.java | 73 +-- .../gates/commands/CommandUnhide.java | 55 +- 18 files changed, 857 insertions(+), 1021 deletions(-) diff --git a/src/de/craftinc/gates/commands/BaseCommand.java b/src/de/craftinc/gates/commands/BaseCommand.java index 9ded795..b9da0ec 100644 --- a/src/de/craftinc/gates/commands/BaseCommand.java +++ b/src/de/craftinc/gates/commands/BaseCommand.java @@ -22,6 +22,7 @@ import java.util.List; import de.craftinc.gates.util.ConfigurationUtil; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; +import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Player; import de.craftinc.gates.Gate; @@ -29,224 +30,213 @@ import de.craftinc.gates.GatesManager; import de.craftinc.gates.Plugin; import de.craftinc.gates.util.TextUtil; -public abstract class BaseCommand -{ - protected List aliases = new ArrayList(); - protected List requiredParameters = new ArrayList(); - protected List optionalParameters = new ArrayList(); - - protected String helpDescription = "no description"; - - protected List parameters; - protected CommandSender sender; - protected Player player; - protected Gate gate; - - protected boolean senderMustBePlayer = true; - protected boolean hasGateParam = true; - - protected String requiredPermission; - protected boolean needsPermissionAtCurrentLocation; - - protected boolean shouldPersistToDisk; +public abstract class BaseCommand { - - public List getAliases() { - return aliases; - } - - - public void execute(CommandSender sender, List parameters) { - this.sender = sender; - this.parameters = parameters; - - if (!this.validateCall()) { - return; - } - - if (this.senderMustBePlayer) { - this.player = (Player)sender; - } - - this.perform(); - - if (this.shouldPersistToDisk && Plugin.getPlugin().getConfig().getBoolean(ConfigurationUtil.confSaveOnChangesKey)) { - Plugin.getPlugin().getGatesManager().saveGatesToDisk(); - } - } - - - abstract protected void perform(); - - - protected void sendMessage(String message) { - sender.sendMessage(message); - } - - - protected void sendMessage(List messages) { - for(String message : messages) { - this.sendMessage(message); - } - } - - - protected boolean validateCall() - { - boolean allParametersThere = parameters.size() >= requiredParameters.size(); - boolean senderIsPlayer = this.sender instanceof Player; - boolean hasGateParameter = false; - - if (this.hasGateParam && this.parameters.size() > 0 && this.setGateUsingParameter(this.parameters.get(0))) { - hasGateParameter = true; - } - - boolean senderHasPermission = this.hasPermission(); - boolean valid; - - if (this.senderMustBePlayer && !senderIsPlayer) { - sendMessage(ChatColor.RED + "This command can only be used by ingame players."); - valid = false; - } - else { + protected List aliases = new ArrayList(); + protected List requiredParameters = new ArrayList(); + protected List optionalParameters = new ArrayList(); + + protected String helpDescription = "no description"; + + protected List parameters; + protected CommandSender sender; + protected Player player; + protected Gate gate; + + protected boolean senderMustBePlayer = true; + protected boolean hasGateParam = true; + + protected String requiredPermission; + protected boolean needsPermissionAtCurrentLocation; + + protected boolean shouldPersistToDisk; + + + public List getAliases() { + return aliases; + } + + + public void execute(CommandSender sender, List parameters) { + this.sender = sender; + this.parameters = parameters; + + if (!this.validateCall()) { + return; + } + + if (this.senderMustBePlayer) { + this.player = (Player) sender; + } + + this.perform(); + + if (this.shouldPersistToDisk && getSaveOnChanges()) { + Plugin.getPlugin().getGatesManager().saveGatesToDisk(); + } + } + + private boolean getSaveOnChanges() { + FileConfiguration config = Plugin.getPlugin().getConfig(); + return config.getBoolean(ConfigurationUtil.confSaveOnChangesKey); + } + + + abstract protected void perform(); + + + protected void sendMessage(String message) { + sender.sendMessage(message); + } + + + protected void sendMessage(List messages) { + for (String message : messages) { + this.sendMessage(message); + } + } + + + protected boolean validateCall() { + boolean allParametersThere = parameters.size() >= requiredParameters.size(); + boolean senderIsPlayer = this.sender instanceof Player; + boolean hasGateParameter = false; + + if (this.hasGateParam && this.parameters.size() > 0 && this.setGateUsingParameter(this.parameters.get(0))) { + hasGateParameter = true; + } + + boolean senderHasPermission = this.hasPermission(); + boolean valid; + + if (this.senderMustBePlayer && !senderIsPlayer) { + sendMessage(ChatColor.RED + "This command can only be used by ingame players."); + valid = false; + } else { if (!allParametersThere) { sendMessage(ChatColor.RED + "Some parameters are missing! " + ChatColor.AQUA + "Usage: " + this.getUsageTemplate(true)); valid = false; - } - else if ((!senderHasPermission && this.hasGateParam) || - (!senderHasPermission) || - (this.hasGateParam && !hasGateParameter)) { + } else if ((!senderHasPermission && this.hasGateParam) || + (!senderHasPermission) || + (this.hasGateParam && !hasGateParameter)) { sendMessage(ChatColor.RED + "You either provided a invalid gate or do not have permission to " + this.helpDescription.toLowerCase()); valid = false; - } - else { + } else { valid = true; } } - - return valid; - } - - - protected boolean setGateUsingParameter(String param) - { - GatesManager gateManager = Plugin.getPlugin().getGatesManager(); - - if (!gateManager.gateExists(param)) { - return false; - } - else { - gate = gateManager.getGateWithId(param); - return true; - } - } - - - /** - * This will return false if a gate is required for this command but this.gate == null. - */ - protected boolean hasPermission() - { - if (Plugin.getPermission() == null) { // fallback - use the standard bukkit permission system - return this.sender.hasPermission(this.requiredPermission); - } - if (!(this.sender instanceof Player)) { + return valid; + } + + + protected boolean setGateUsingParameter(String param) { + GatesManager gateManager = Plugin.getPlugin().getGatesManager(); + + if (!gateManager.gateExists(param)) { + return false; + } else { + gate = gateManager.getGateWithId(param); + return true; + } + } + + + /** + * This will return false if a gate is required for this command but this.gate == null. + */ + protected boolean hasPermission() { + if (Plugin.getPermission() == null) { // fallback - use the standard bukkit permission system + return this.sender.hasPermission(this.requiredPermission); + } + + if (!(this.sender instanceof Player)) { // sender is no player - there is no information about the senders locations return Plugin.getPermission().has(this.sender, this.requiredPermission); - } + } - - Player p = (Player) this.sender; - boolean hasPermission = false; - - if (this.requiredPermission.equals(Plugin.permissionInfo)) { - if (this.hasGateParam) { - hasPermission = this.hasPermissionAtGateLocationAndExit(p); - } - else { - hasPermission = Plugin.getPermission().has(p.getWorld(), p.getName(), this.requiredPermission); - } - } - else if (this.requiredPermission.equals(Plugin.permissionUse) ) { - hasPermission = this.hasPermissionAtGateLocationAndExit(p); - } - else if (this.requiredPermission.equals(Plugin.permissionManage)) { + Player p = (Player) this.sender; + boolean hasPermission = false; - if (this.needsPermissionAtCurrentLocation && this.hasGateParam) { - boolean hasPersmissionAtCurrentLocation = Plugin.getPermission().has(p.getWorld(), p.getName(), this.requiredPermission); - hasPermission = hasPersmissionAtCurrentLocation && this.hasPermissionAtGateLocationAndExit(p); - } - else if (this.needsPermissionAtCurrentLocation) { - hasPermission = Plugin.getPermission().has(p.getWorld(), p.getName(), this.requiredPermission); - } - else { - hasPermission = this.hasPermissionAtGateLocationAndExit(p); - } - } - - return hasPermission; - } - - - protected boolean hasPermissionAtGateLocationAndExit(Player p) - { - if (this.gate == null || p == null) { // make sure we don't run into a nullpointer exception - return false; - } - - boolean permAtLocation = this.gate.getLocation() == null || Plugin.getPermission().has(this.gate.getLocation().getWorld(), p.getName(), this.requiredPermission); - boolean permAtExit = this.gate.getExit() == null || Plugin.getPermission().has(this.gate.getExit().getWorld(), p.getName(), this.requiredPermission); + if (this.requiredPermission.equals(Plugin.permissionInfo)) { - return permAtLocation & permAtExit; - } - - - // -------------------------------------------- // - // Help and usage description - // -------------------------------------------- // - protected String getUsageTemplate(boolean withColor, boolean withDescription) - { - String ret = ""; - - if (withColor) { - ret += ChatColor.AQUA; - } - - ret += "/" + Plugin.getPlugin().getBaseCommand() + " " + TextUtil.implode(this.getAliases(), ",")+" "; - - List parts = new ArrayList(); - - for (String requiredParameter : this.requiredParameters) { - parts.add("["+requiredParameter+"]"); - } - - for (String optionalParameter : this.optionalParameters) { - parts.add("*["+optionalParameter+"]"); - } - - if (withColor) { - ret += ChatColor.DARK_AQUA; - } - - ret += TextUtil.implode(parts, " "); - - if (withDescription) { - ret += " "; - - if (withColor) { - ret += ChatColor.YELLOW; - } - - ret += this.helpDescription; - } - return ret; - } - - protected String getUsageTemplate(boolean withColor) - { - return getUsageTemplate(withColor, false); - } + if (this.hasGateParam) { + hasPermission = this.hasPermissionAtGateLocationAndExit(p); + } else { + hasPermission = Plugin.getPermission().has(p.getWorld(), p.getName(), this.requiredPermission); + } + } else if (this.requiredPermission.equals(Plugin.permissionUse)) { + hasPermission = this.hasPermissionAtGateLocationAndExit(p); + } else if (this.requiredPermission.equals(Plugin.permissionManage)) { + + if (this.needsPermissionAtCurrentLocation && this.hasGateParam) { + boolean hasPersmissionAtCurrentLocation = Plugin.getPermission().has(p.getWorld(), p.getName(), this.requiredPermission); + hasPermission = hasPersmissionAtCurrentLocation && this.hasPermissionAtGateLocationAndExit(p); + } else if (this.needsPermissionAtCurrentLocation) { + hasPermission = Plugin.getPermission().has(p.getWorld(), p.getName(), this.requiredPermission); + } else { + hasPermission = this.hasPermissionAtGateLocationAndExit(p); + } + } + + return hasPermission; + } + + + protected boolean hasPermissionAtGateLocationAndExit(Player p) { + if (this.gate == null || p == null) { + return false; + } + + boolean permAtLocation = this.gate.getLocation() == null || Plugin.getPermission().has(this.gate.getLocation().getWorld(), p.getName(), this.requiredPermission); + boolean permAtExit = this.gate.getExit() == null || Plugin.getPermission().has(this.gate.getExit().getWorld(), p.getName(), this.requiredPermission); + + return permAtLocation & permAtExit; + } + + + // -------------------------------------------- // + // Help and usage description + // -------------------------------------------- // + protected String getUsageTemplate(boolean withColor, boolean withDescription) { + String ret = ""; + + if (withColor) { + ret += ChatColor.AQUA; + } + + ret += "/" + Plugin.getPlugin().getBaseCommand() + " " + TextUtil.implode(this.getAliases(), ",") + " "; + + List parts = new ArrayList(); + + for (String requiredParameter : this.requiredParameters) { + parts.add("[" + requiredParameter + "]"); + } + + for (String optionalParameter : this.optionalParameters) { + parts.add("*[" + optionalParameter + "]"); + } + + if (withColor) { + ret += ChatColor.DARK_AQUA; + } + + ret += TextUtil.implode(parts, " "); + + if (withDescription) { + ret += " "; + + if (withColor) { + ret += ChatColor.YELLOW; + } + ret += this.helpDescription; + } + return ret; + } + + protected String getUsageTemplate(boolean withColor) { + return getUsageTemplate(withColor, false); + } } diff --git a/src/de/craftinc/gates/commands/BaseLocationCommand.java b/src/de/craftinc/gates/commands/BaseLocationCommand.java index f453d1b..d7568e0 100644 --- a/src/de/craftinc/gates/commands/BaseLocationCommand.java +++ b/src/de/craftinc/gates/commands/BaseLocationCommand.java @@ -21,27 +21,25 @@ import org.bukkit.Material; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; -public abstract class BaseLocationCommand extends BaseCommand -{ - protected Location getValidPlayerLocation() - { - // The player might stand in a half block or a sign or whatever - // Therefore we load some extra locations and blocks - Block playerBlock = player.getLocation().getBlock(); - Block upBlock = playerBlock.getRelative(BlockFace.UP); - - if (playerBlock.getType() == Material.AIR) { - return player.getLocation(); - } - else if (upBlock.getType() == Material.AIR) { - return new Location(player.getLocation().getWorld(), - player.getLocation().getX(), - player.getLocation().getY() + 1, - player.getLocation().getZ(), - player.getLocation().getYaw(), - player.getLocation().getPitch()); - } - - return null; - } +public abstract class BaseLocationCommand extends BaseCommand { + + protected Location getValidPlayerLocation() { + // The player might stand in a half block or a sign or whatever + // Therefore we load some extra locations and blocks + Block playerBlock = player.getLocation().getBlock(); + Block upBlock = playerBlock.getRelative(BlockFace.UP); + + if (playerBlock.getType() == Material.AIR) { + return player.getLocation(); + } else if (upBlock.getType() == Material.AIR) { + return new Location(player.getLocation().getWorld(), + player.getLocation().getX(), + player.getLocation().getY() + 1, + player.getLocation().getZ(), + player.getLocation().getYaw(), + player.getLocation().getPitch()); + } + + return null; + } } diff --git a/src/de/craftinc/gates/commands/CommandAllowRiding.java b/src/de/craftinc/gates/commands/CommandAllowRiding.java index 5496125..070a257 100644 --- a/src/de/craftinc/gates/commands/CommandAllowRiding.java +++ b/src/de/craftinc/gates/commands/CommandAllowRiding.java @@ -20,19 +20,16 @@ package de.craftinc.gates.commands; import de.craftinc.gates.Plugin; import org.bukkit.ChatColor; -public class CommandAllowRiding extends BaseCommand -{ - public CommandAllowRiding() - { +public class CommandAllowRiding extends BaseCommand { + + public CommandAllowRiding() { aliases.add("allowRiding"); aliases.add("ar"); requiredParameters.add("id"); helpDescription = "Allow players to travel while riding."; - requiredPermission = Plugin.permissionManage; - needsPermissionAtCurrentLocation = false; shouldPersistToDisk = true; @@ -40,8 +37,7 @@ public class CommandAllowRiding extends BaseCommand } @Override - protected void perform() - { + protected void perform() { gate.setAllowsVehicles(true); sendMessage(ChatColor.GREEN + "Traveling while riding is now enabled for this gate."); } diff --git a/src/de/craftinc/gates/commands/CommandClose.java b/src/de/craftinc/gates/commands/CommandClose.java index bc99a06..43858b7 100644 --- a/src/de/craftinc/gates/commands/CommandClose.java +++ b/src/de/craftinc/gates/commands/CommandClose.java @@ -24,41 +24,30 @@ import org.bukkit.ChatColor; import de.craftinc.gates.Plugin; -public class CommandClose extends BaseCommand -{ - public CommandClose() - { - aliases.add("close"); - aliases.add("c"); - - requiredParameters.add("id"); - - helpDescription = "Closes a gate to prevent players from using it."; - - requiredPermission = Plugin.permissionManage; - - needsPermissionAtCurrentLocation = false; - shouldPersistToDisk = true; - - senderMustBePlayer = false; - } - - - @Override - public void perform() - { - try - { - gate.setOpen(false); - GateBlockChangeSender.updateGateBlocks(gate); - sendMessage(ChatColor.GREEN + "The gate was closed."); - } - catch(Exception e) - { - sendMessage(ChatColor.RED + "Opening the gate failed! See server log for more information"); - Plugin.log(Level.WARNING, e.getMessage()); - e.printStackTrace(); - } - } -} +public class CommandClose extends BaseCommand { + public CommandClose() { + aliases.add("close"); + aliases.add("c"); + + requiredParameters.add("id"); + helpDescription = "Closes a gate to prevent players from using it."; + requiredPermission = Plugin.permissionManage; + needsPermissionAtCurrentLocation = false; + shouldPersistToDisk = true; + senderMustBePlayer = false; + } + + @Override + public void perform() { + try { + gate.setOpen(false); + GateBlockChangeSender.updateGateBlocks(gate); + sendMessage(ChatColor.GREEN + "The gate was closed."); + } catch (Exception e) { + sendMessage(ChatColor.RED + "Opening the gate failed! See server log for more information"); + Plugin.log(Level.WARNING, e.getMessage()); + e.printStackTrace(); + } + } +} diff --git a/src/de/craftinc/gates/commands/CommandDenyRiding.java b/src/de/craftinc/gates/commands/CommandDenyRiding.java index 0e75a33..76f5dba 100644 --- a/src/de/craftinc/gates/commands/CommandDenyRiding.java +++ b/src/de/craftinc/gates/commands/CommandDenyRiding.java @@ -19,28 +19,22 @@ package de.craftinc.gates.commands; import de.craftinc.gates.Plugin; import org.bukkit.ChatColor; -public class CommandDenyRiding extends BaseCommand -{ - public CommandDenyRiding() - { +public class CommandDenyRiding extends BaseCommand { + + public CommandDenyRiding() { aliases.add("denyRiding"); aliases.add("dr"); requiredParameters.add("id"); - helpDescription = "Deny players to travel while riding."; - requiredPermission = Plugin.permissionManage; - needsPermissionAtCurrentLocation = false; shouldPersistToDisk = true; - senderMustBePlayer = false; } @Override - protected void perform() - { + protected void perform() { gate.setAllowsVehicles(false); sendMessage(ChatColor.GREEN + "Traveling while riding is now disabled for this gate."); } diff --git a/src/de/craftinc/gates/commands/CommandExit.java b/src/de/craftinc/gates/commands/CommandExit.java index 0aff595..4250291 100644 --- a/src/de/craftinc/gates/commands/CommandExit.java +++ b/src/de/craftinc/gates/commands/CommandExit.java @@ -24,41 +24,35 @@ import org.bukkit.ChatColor; import de.craftinc.gates.Plugin; import org.bukkit.Location; +public class CommandExit extends BaseCommand { -public class CommandExit extends BaseCommand -{ - public CommandExit() - { - aliases.add("exit"); - aliases.add("e"); - - requiredParameters.add("id"); - - helpDescription = "Change exit of location."; - - requiredPermission = Plugin.permissionManage; - - needsPermissionAtCurrentLocation = true; - shouldPersistToDisk = true; - senderMustBePlayer = true; - } - - - public void perform() - { - try - { + public CommandExit() { + aliases.add("exit"); + aliases.add("e"); + + requiredParameters.add("id"); + + helpDescription = "Change exit of location."; + + requiredPermission = Plugin.permissionManage; + + needsPermissionAtCurrentLocation = true; + shouldPersistToDisk = true; + senderMustBePlayer = true; + } + + + public void perform() { + try { Location oldExit = gate.getExit(); - gate.setExit(player.getLocation()); - sendMessage(ChatColor.GREEN + "The exit of gate '" + gate.getId() + "' is now where you stand."); + gate.setExit(player.getLocation()); + sendMessage(ChatColor.GREEN + "The exit of gate '" + gate.getId() + "' is now where you stand."); Plugin.getPlugin().getGatesManager().handleGateExitChange(gate, oldExit); - } - catch (Exception e) { + } catch (Exception e) { GateBlockChangeSender.updateGateBlocks(gate); sendMessage(ChatColor.RED + "Setting the exit for the gate failed! See server log for more information"); - Plugin.log(Level.WARNING, e.getMessage()); - e.printStackTrace(); - } - } + Plugin.log(Level.WARNING, e.getMessage()); + e.printStackTrace(); + } + } } - diff --git a/src/de/craftinc/gates/commands/CommandExitOpen.java b/src/de/craftinc/gates/commands/CommandExitOpen.java index f131e78..5bc2963 100644 --- a/src/de/craftinc/gates/commands/CommandExitOpen.java +++ b/src/de/craftinc/gates/commands/CommandExitOpen.java @@ -24,29 +24,23 @@ import org.bukkit.Location; import java.util.logging.Level; -public class CommandExitOpen extends BaseCommand -{ - public CommandExitOpen() - { +public class CommandExitOpen extends BaseCommand { + + public CommandExitOpen() { aliases.add("exitopen"); aliases.add("eo"); requiredParameters.add("id"); - helpDescription = "Change exit of location and open that gate afterwards."; - requiredPermission = Plugin.permissionManage; - needsPermissionAtCurrentLocation = true; shouldPersistToDisk = true; senderMustBePlayer = true; } - public void perform() - { - try - { + public void perform() { + try { Location oldExit = gate.getExit(); gate.setExit(player.getLocation()); sendMessage(ChatColor.GREEN + "The exit of gate '" + gate.getId() + "' is now where you stand."); @@ -68,12 +62,10 @@ public class CommandExitOpen extends BaseCommand } sendMessage(ChatColor.GREEN + "The gate was opened."); - } - catch (Exception e) { + } catch (Exception e) { sendMessage(ChatColor.RED + e.getMessage()); } - } - catch (Exception e) { + } catch (Exception e) { GateBlockChangeSender.updateGateBlocks(gate); sendMessage(ChatColor.RED + "Setting the exit for the gate failed! This gate is now closed! (See server log for more information.)"); Plugin.log(Level.WARNING, e.getMessage()); diff --git a/src/de/craftinc/gates/commands/CommandHelp.java b/src/de/craftinc/gates/commands/CommandHelp.java index 76efa42..1adb7b3 100644 --- a/src/de/craftinc/gates/commands/CommandHelp.java +++ b/src/de/craftinc/gates/commands/CommandHelp.java @@ -23,89 +23,84 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; -public class CommandHelp extends BaseCommand -{ - public static List> helpPages; - - static - { - // sort the usage strings - List allUsageStrings = new ArrayList(); - - allUsageStrings.add( new CommandHelp().getUsageTemplate(true, true) ); - allUsageStrings.add( new CommandNew().getUsageTemplate(true, true) ); - allUsageStrings.add( new CommandRemove().getUsageTemplate(true, true) ); - allUsageStrings.add( new CommandLocation().getUsageTemplate(true, true) ); - allUsageStrings.add( new CommandExit().getUsageTemplate(true, true) ); - allUsageStrings.add( new CommandOpen().getUsageTemplate(true, true) ); - allUsageStrings.add( new CommandRename().getUsageTemplate(true, true) ); - allUsageStrings.add( new CommandClose().getUsageTemplate(true, true) ); - allUsageStrings.add( new CommandList().getUsageTemplate(true, true) ); - allUsageStrings.add( new CommandInfo().getUsageTemplate(true, true) ); - allUsageStrings.add( new CommandHide().getUsageTemplate(true, true) ); - allUsageStrings.add( new CommandUnhide().getUsageTemplate(true, true) ); - allUsageStrings.add( new CommandExitOpen().getUsageTemplate(true, true) ); - allUsageStrings.add( new CommandNearby().getUsageTemplate(true, true) ); - - Collections.sort(allUsageStrings); - - - // put 5 commands on one page - helpPages = new ArrayList>(); - - while (!allUsageStrings.isEmpty()) { - int toIndex = allUsageStrings.size() >= 6 ? 5 : allUsageStrings.size(); - List currentHelpPage = new ArrayList(allUsageStrings.subList(0, toIndex)); - helpPages.add(currentHelpPage); - - allUsageStrings.removeAll(currentHelpPage); - } - } - - - public CommandHelp() - { - aliases.add("help"); - aliases.add("?"); - - optionalParameters.add("page"); - helpDescription = "prints this help page"; - - requiredPermission = Plugin.permissionInfo; - - hasGateParam = false; - needsPermissionAtCurrentLocation = false; - shouldPersistToDisk = false; - senderMustBePlayer = false; - } +public class CommandHelp extends BaseCommand { + + public static List> helpPages; + + static { + // sort the usage strings + List allUsageStrings = new ArrayList(); + + allUsageStrings.add(new CommandHelp().getUsageTemplate(true, true)); + allUsageStrings.add(new CommandNew().getUsageTemplate(true, true)); + allUsageStrings.add(new CommandRemove().getUsageTemplate(true, true)); + allUsageStrings.add(new CommandLocation().getUsageTemplate(true, true)); + allUsageStrings.add(new CommandExit().getUsageTemplate(true, true)); + allUsageStrings.add(new CommandOpen().getUsageTemplate(true, true)); + allUsageStrings.add(new CommandRename().getUsageTemplate(true, true)); + allUsageStrings.add(new CommandClose().getUsageTemplate(true, true)); + allUsageStrings.add(new CommandList().getUsageTemplate(true, true)); + allUsageStrings.add(new CommandInfo().getUsageTemplate(true, true)); + allUsageStrings.add(new CommandHide().getUsageTemplate(true, true)); + allUsageStrings.add(new CommandUnhide().getUsageTemplate(true, true)); + allUsageStrings.add(new CommandExitOpen().getUsageTemplate(true, true)); + allUsageStrings.add(new CommandNearby().getUsageTemplate(true, true)); + + Collections.sort(allUsageStrings); - public void perform() - { - int page; - - if (parameters.size() > 0) { - try { - page = Integer.parseInt(parameters.get(0)); - } - catch (NumberFormatException e) { - // wasn't an integer + // put 5 commands on one page + helpPages = new ArrayList>(); + + while (!allUsageStrings.isEmpty()) { + int toIndex = allUsageStrings.size() >= 6 ? 5 : allUsageStrings.size(); + List currentHelpPage = new ArrayList(allUsageStrings.subList(0, toIndex)); + helpPages.add(currentHelpPage); + + allUsageStrings.removeAll(currentHelpPage); + } + } + + + public CommandHelp() { + aliases.add("help"); + aliases.add("?"); + + optionalParameters.add("page"); + helpDescription = "prints this help page"; + + requiredPermission = Plugin.permissionInfo; + + hasGateParam = false; + needsPermissionAtCurrentLocation = false; + shouldPersistToDisk = false; + senderMustBePlayer = false; + } + + + public void perform() { + int page; + + if (parameters.size() > 0) { + try { + page = Integer.parseInt(parameters.get(0)); + } catch (NumberFormatException e) { + // wasn't an integer page = 1; - } - } - else { + } + } else { page = 1; } - - sendMessage(TextUtil.titleize("Craft Inc. Gates Help (" + page + "/" + helpPages.size() + ")")); - - page -= 1; - if (page < 0 || page >= helpPages.size()) { - sendMessage("This page does not exist"); - return; - } - - sendMessage(helpPages.get(page)); - } + + sendMessage(TextUtil.titleize("Craft Inc. Gates Help (" + page + "/" + helpPages.size() + ")")); + + page -= 1; + if (page < 0 || page >= helpPages.size()) { + sendMessage("This page does not exist"); + return; + } + + sendMessage(helpPages.get(page)); + } } diff --git a/src/de/craftinc/gates/commands/CommandHide.java b/src/de/craftinc/gates/commands/CommandHide.java index 5eea78d..877244b 100644 --- a/src/de/craftinc/gates/commands/CommandHide.java +++ b/src/de/craftinc/gates/commands/CommandHide.java @@ -24,36 +24,33 @@ import org.bukkit.ChatColor; import de.craftinc.gates.Plugin; -public class CommandHide extends BaseCommand -{ - public CommandHide() - { - aliases.add("hide"); +public class CommandHide extends BaseCommand { + + public CommandHide() { + aliases.add("hide"); aliases.add("h"); - - requiredParameters.add("id"); - - helpDescription = "Makes a gate NOT consist of gate blocks while open."; - - requiredPermission = Plugin.permissionManage; - - needsPermissionAtCurrentLocation = false; - shouldPersistToDisk = true; - senderMustBePlayer = false; - } - - - public void perform() - { - try { - gate.setHidden(true); + + requiredParameters.add("id"); + + helpDescription = "Makes a gate NOT consist of gate blocks while open."; + + requiredPermission = Plugin.permissionManage; + + needsPermissionAtCurrentLocation = false; + shouldPersistToDisk = true; + senderMustBePlayer = false; + } + + + public void perform() { + try { + gate.setHidden(true); GateBlockChangeSender.updateGateBlocks(gate); - sendMessage(ChatColor.GREEN + "The gate '" + gate.getId() + "' is now hidden."); - } - catch (Exception e) { - sendMessage(ChatColor.RED + "Hiding the gate failed! See server log for more information"); - Plugin.log(Level.WARNING, e.getMessage()); - e.printStackTrace(); - } - } + sendMessage(ChatColor.GREEN + "The gate '" + gate.getId() + "' is now hidden."); + } catch (Exception e) { + sendMessage(ChatColor.RED + "Hiding the gate failed! See server log for more information"); + Plugin.log(Level.WARNING, e.getMessage()); + e.printStackTrace(); + } + } } \ No newline at end of file diff --git a/src/de/craftinc/gates/commands/CommandInfo.java b/src/de/craftinc/gates/commands/CommandInfo.java index bb2bff7..0c6c1d1 100644 --- a/src/de/craftinc/gates/commands/CommandInfo.java +++ b/src/de/craftinc/gates/commands/CommandInfo.java @@ -16,7 +16,6 @@ */ package de.craftinc.gates.commands; - import de.craftinc.gates.util.GateBlockChangeSender; import org.bukkit.ChatColor; @@ -24,30 +23,27 @@ import de.craftinc.gates.Plugin; import de.craftinc.gates.util.TextUtil; import org.bukkit.entity.Player; +public class CommandInfo extends BaseCommand { -public class CommandInfo extends BaseCommand -{ - public CommandInfo() - { - aliases.add("info"); - aliases.add("i"); + public CommandInfo() { + aliases.add("info"); + aliases.add("i"); optionalParameters.add("id"); - - helpDescription = "Print detailed information about a certain or the closest gate."; - - requiredPermission = Plugin.permissionInfo; - - needsPermissionAtCurrentLocation = false; - shouldPersistToDisk = false; - senderMustBePlayer = false; + + helpDescription = "Print detailed information about a certain or the closest gate."; + + requiredPermission = Plugin.permissionInfo; + + needsPermissionAtCurrentLocation = false; + shouldPersistToDisk = false; + senderMustBePlayer = false; hasGateParam = false; - } - - - public void perform() - { - if (this.parameters.size() > 0) { + } + + + public void perform() { + if (this.parameters.size() > 0) { if (!this.setGateUsingParameter(this.parameters.get(0))) { sendMessage(ChatColor.RED + "You either provided a invalid gate or do not have permission to " + this.helpDescription.toLowerCase()); @@ -55,8 +51,7 @@ public class CommandInfo extends BaseCommand } sendMessage(TextUtil.titleize("Information about: '" + ChatColor.WHITE + gate.getId() + ChatColor.YELLOW + "'")); - } - else { + } else { boolean senderIsPlayer = this.sender instanceof Player; if (!senderIsPlayer) { @@ -64,7 +59,7 @@ public class CommandInfo extends BaseCommand return; } - Player p = (Player)this.sender; + Player p = (Player) this.sender; this.gate = Plugin.getPlugin().getGatesManager().getNearestGate(p.getLocation()); if (!this.hasPermission() || this.gate == null) { @@ -85,22 +80,22 @@ public class CommandInfo extends BaseCommand openHiddenMessage += ChatColor.AQUA + " closed"; if (gate.isHidden()) - openHiddenMessage += ChatColor.DARK_AQUA +" and" + ChatColor.AQUA + " hidden"; + openHiddenMessage += ChatColor.DARK_AQUA + " and" + ChatColor.AQUA + " hidden"; openHiddenMessage += ".\n"; sendMessage(openHiddenMessage); if (gate.getLocation() != null) - sendMessage(ChatColor.DARK_AQUA + "location: " + ChatColor.AQUA + "( " + (int)gate.getLocation().getX() + - " | " + (int)gate.getLocation().getY() + " | " + (int)gate.getLocation().getZ() + " ) in " + + sendMessage(ChatColor.DARK_AQUA + "location: " + ChatColor.AQUA + "( " + (int) gate.getLocation().getX() + + " | " + (int) gate.getLocation().getY() + " | " + (int) gate.getLocation().getZ() + " ) in " + gate.getLocation().getWorld().getName()); else sendMessage(ChatColor.DARK_AQUA + "NOTE: this gate has no location"); if (gate.getExit() != null) - sendMessage(ChatColor.DARK_AQUA + "exit: " + ChatColor.AQUA + "( " + (int)gate.getExit().getX() + " | " - + (int)gate.getExit().getY() + " | " + (int)gate.getExit().getZ() + " ) in " + + sendMessage(ChatColor.DARK_AQUA + "exit: " + ChatColor.AQUA + "( " + (int) gate.getExit().getX() + " | " + + (int) gate.getExit().getY() + " | " + (int) gate.getExit().getZ() + " ) in " + gate.getExit().getWorld().getName()); else sendMessage(ChatColor.DARK_AQUA + "NOTE: this gate has no exit"); @@ -111,7 +106,7 @@ public class CommandInfo extends BaseCommand if (this.sender instanceof Player) { - GateBlockChangeSender.temporaryHighlightGateFrame((Player)this.sender, this.gate); + GateBlockChangeSender.temporaryHighlightGateFrame((Player) this.sender, this.gate); } - } + } } diff --git a/src/de/craftinc/gates/commands/CommandList.java b/src/de/craftinc/gates/commands/CommandList.java index 9935ba3..6d234b9 100644 --- a/src/de/craftinc/gates/commands/CommandList.java +++ b/src/de/craftinc/gates/commands/CommandList.java @@ -28,324 +28,287 @@ import de.craftinc.gates.Gate; import de.craftinc.gates.Plugin; import de.craftinc.gates.util.TextUtil; +public class CommandList extends BaseCommand { -public class CommandList extends BaseCommand -{ - protected static final int linesPerPage = 10; - protected static final int charactersPerLine = 52; /* this is actually no true. the - font used by minecraft is not - monospace. but I don't think - there is a (easy) way for a - bukkit plugin to calculate - the drawing-size of a string. - */ - - public CommandList() - { - aliases.add("list"); - aliases.add("ls"); - - optionalParameters.add("page"); - hasGateParam = false; - needsPermissionAtCurrentLocation = false; - - helpDescription = "lists all availible gates."; - - requiredPermission = Plugin.permissionInfo; - shouldPersistToDisk = false; - senderMustBePlayer = false; - } - - - protected static List linesOfGateIds(List gates) - { - List lines = new ArrayList(); - - int index = 0; - List gateIdsForCurrentLine = new ArrayList(); - int numCharactersInCurrentLine = 0; - - - while (index < gates.size()) { - String gateId = gates.get(index); - int gateIdLength = gateId.length() + 2; // actual length + comma + whitespace - - // special case: very long gate id - if (gateIdLength > charactersPerLine && numCharactersInCurrentLine == 0) { - gateIdsForCurrentLine = new ArrayList(); - numCharactersInCurrentLine = 0; - - while ((gateId.length() + 2) > charactersPerLine) { - - int cutPos = charactersPerLine; - - // is the id too long to add comma and whitespace but not longer than the line? - if (gateId.length() <= charactersPerLine) { - cutPos -= 2; - } - - lines.add(gateId.substring(0, cutPos)); - gateId = gateId.substring(cutPos, gateId.length()); - - } + private static final int linesPerPage = 10; - gateIdsForCurrentLine.add(gateId); - - numCharactersInCurrentLine += gateId.length(); - index++; - } - - // gate fits into current line - else if ((numCharactersInCurrentLine + gateIdLength) <= charactersPerLine) { - gateIdsForCurrentLine.add(gateId); - numCharactersInCurrentLine += gateIdLength; - - index++; - } - - // the current gate does not fit on the - else { - lines.add(TextUtil.implode(gateIdsForCurrentLine, ", ") + ", "); - - gateIdsForCurrentLine = new ArrayList(); - numCharactersInCurrentLine = 0; - } - } - - lines.add(TextUtil.implode(gateIdsForCurrentLine, ", ")); - return lines; - } - - - protected static String intToTitleString(int i, boolean addPreviousPageNote, boolean addNextPageNote) - { - String retVal = ChatColor.DARK_AQUA + ""; - - if ( i < 26 ) { - retVal += (char)(i+65); - } - else if ( i == 26 ) { - retVal += "0-9"; - } - else { - retVal += "!@#$"; - } - - if (addPreviousPageNote && addNextPageNote) { - retVal += " (more on previous and next page)"; - } - else if (addPreviousPageNote) { - retVal += " (more on previous page)"; - } - else if (addNextPageNote) { - retVal += " (more on next page)"; - } - - return retVal + "\n"; - } - - - /** - * Method for getting a collection of gates the player is allowed to see. - */ - protected Collection getAllGates() - { - Collection gates = Plugin.getPlugin().getGatesManager().allGates(); - - 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! - Collection gatesCopy = new ArrayList(gates); - - for (Gate gate : gatesCopy) { - - if (gate.getLocation() != null) { - boolean permissionAtGateLocation = Plugin.getPermission().has(gate.getLocation().getWorld(), p.getName(), this.requiredPermission); - if (!permissionAtGateLocation) { - gates.remove(gate); - continue; - } - } - - if (gate.getExit() != null) { - - boolean permissionAtGateExit = Plugin.getPermission().has(gate.getExit().getWorld(), p.getName(), this.requiredPermission); - if (!permissionAtGateExit) { - gates.remove(gate); - } - } - } - } - - return gates; - } - - - /** - * Sorts all gates by there first character. - * Puts gates in corresponding Lists: (all returned lists will be sorted alphabetically) - * list 0-25: a,b,c,..,z - * list 26: 0-9 - * list 27: other - */ - protected static List> gatesSortedByName(Collection allGates) - { - // create the lists - List> ids = new ArrayList>(); - - for (int i=0; i<28; i++) { - ids.add(new ArrayList()); - } - - // put all gates into correct lists - for (Gate gate : allGates) { - String id = gate.getId(); - int first = id.charAt(0); - - if (first > 96 && first < 123) { // convert lower case chars - first -= 97; - } - else if (first > 64 && first < 91) { // convert upper case chars - first -= 65; - } - else if (first > 47 && first < 58) { // convert numbers - first = 26; - } - else { // everything else - first = 27; - } - - ids.get(first).add(id); - } - - // sort everything - for (int i=0; i<28; i++) { - Collections.sort(ids.get(i)); - } - - return ids; - } - - - /** - * Returns a list of strings. - * Each string is the text for a page. - * The maximum number of lines per page is 'linesPerPage' minus 1. - * Will return an empty list if no gates are availible. - */ - protected List pagedGateIds() - { - Collection gates = this.getAllGates(); - - if (gates.size() == 0) { - return null; - } - - List> gatesSortedByName = gatesSortedByName(gates); - List allPages = new ArrayList(); - int linesLeftOnPage = linesPerPage - 1; - String currentPageString = ""; - - for (int i=0; i currentGates = gatesSortedByName.get(i); - - if(currentGates.isEmpty()) { - continue; - } - - List currentGatesAsLines = linesOfGateIds(currentGates); - boolean moreGatesOnLastPage = false; - - while (!currentGatesAsLines.isEmpty()) { - - if (linesLeftOnPage < 2) { - currentPageString = currentPageString.substring(0, currentPageString.length()-2); // remove newlines add the end of the page - allPages.add(currentPageString); - currentPageString = ""; - - linesLeftOnPage = linesPerPage - 1; - } - - // calculate number of lines to add to current page - int linesNecessaryForCurrentGates = currentGatesAsLines.size(); - int linesToFill; - boolean moreGatesOnNextPage; - - if (linesNecessaryForCurrentGates < linesLeftOnPage) { - linesToFill = linesNecessaryForCurrentGates; - moreGatesOnNextPage = false; - } - else { - linesToFill = linesLeftOnPage -1; - moreGatesOnNextPage = true; - } - - // add title - currentPageString += intToTitleString(i, moreGatesOnLastPage, moreGatesOnNextPage); - currentPageString += ChatColor.AQUA; - linesLeftOnPage--; - - // add gate lines - for (int j=0; j allPages = this.pagedGateIds(); + + if (allPages == null) { // no gates exist + sendMessage(ChatColor.RED + "There are no gates yet. " + ChatColor.RESET + + "(Note that you might not be allowed to get information about certain gates)"); + return; + } + + if (page > allPages.size() || page < 1) { + sendMessage(ChatColor.RED + "The requested page is not availible"); + return; + } + + String message = TextUtil.titleize("List of all gates (" + page + "/" + allPages.size() + ")") + "\n"; + message += allPages.get(page - 1); + + sendMessage(message); + } + + private static List linesOfGateIds(List gates) { + List lines = new ArrayList(); + + int index = 0; + List gateIdsForCurrentLine = new ArrayList(); + int numCharactersInCurrentLine = 0; + + while (index < gates.size()) { + String gateId = gates.get(index); + int gateIdLength = gateId.length() + 2; // actual length + comma + whitespace + + if (gateIdLength > charactersPerLine && numCharactersInCurrentLine == 0) { // special case: very long gate id + gateIdsForCurrentLine = new ArrayList(); + numCharactersInCurrentLine = 0; + + while ((gateId.length() + 2) > charactersPerLine) { + int cutPos = charactersPerLine; + + // is the id too long to add comma and whitespace but not longer than the line? + if (gateId.length() <= charactersPerLine) { + cutPos -= 2; + } + + lines.add(gateId.substring(0, cutPos)); + gateId = gateId.substring(cutPos, gateId.length()); + } + + gateIdsForCurrentLine.add(gateId); + + numCharactersInCurrentLine += gateId.length(); + index++; + } else if ((numCharactersInCurrentLine + gateIdLength) <= charactersPerLine) { // gate fits into current line + gateIdsForCurrentLine.add(gateId); + numCharactersInCurrentLine += gateIdLength; + + index++; + } else { // the current gate does not fit on the + lines.add(TextUtil.implode(gateIdsForCurrentLine, ", ") + ", "); + + gateIdsForCurrentLine = new ArrayList(); + numCharactersInCurrentLine = 0; + } + } + + lines.add(TextUtil.implode(gateIdsForCurrentLine, ", ")); + return lines; + } + + private static String intToTitleString(int i, boolean addPreviousPageNote, boolean addNextPageNote) { + String retVal = ChatColor.DARK_AQUA + ""; + + if (i < 26) { + retVal += (char) (i + 65); + } else if (i == 26) { + retVal += "0-9"; + } else { + retVal += "!@#$"; + } + + if (addPreviousPageNote && addNextPageNote) { + retVal += " (more on previous and next page)"; + } else if (addPreviousPageNote) { + retVal += " (more on previous page)"; + } else if (addNextPageNote) { + retVal += " (more on next page)"; + } + + return retVal + "\n"; + } + + /** + * Method for getting a collection of gates the player is allowed to see. + */ + private Collection getAllGates() { + Collection gates = Plugin.getPlugin().getGatesManager().allGates(); + + 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! + Collection gatesCopy = new ArrayList(gates); + + for (Gate gate : gatesCopy) { + if (gate.getLocation() != null) { + boolean permissionAtGateLocation = Plugin.getPermission().has(gate.getLocation().getWorld(), p.getName(), this.requiredPermission); + if (!permissionAtGateLocation) { + gates.remove(gate); + continue; + } + } + + if (gate.getExit() != null) { + boolean permissionAtGateExit = Plugin.getPermission().has(gate.getExit().getWorld(), p.getName(), this.requiredPermission); + if (!permissionAtGateExit) { + gates.remove(gate); + } + } + } + } + + return gates; + } + + /** + * Sorts all gates by there first character. + * Puts gates in corresponding Lists: (all returned lists will be sorted alphabetically) + * list 0-25: a,b,c,..,z + * list 26: 0-9 + * list 27: other + */ + private static List> gatesSortedByName(Collection allGates) { + // create the lists + List> ids = new ArrayList>(); + + for (int i = 0; i < 28; i++) { + ids.add(new ArrayList()); + } + + // put all gates into correct lists + for (Gate gate : allGates) { + String id = gate.getId(); + int first = id.charAt(0); + + if (first > 96 && first < 123) { // convert lower case chars + first -= 97; + } else if (first > 64 && first < 91) { // convert upper case chars + first -= 65; + } else if (first > 47 && first < 58) { // convert numbers + first = 26; + } else { // everything else + first = 27; + } + + ids.get(first).add(id); + } + + // sort everything + for (int i = 0; i < 28; i++) { + Collections.sort(ids.get(i)); + } + + return ids; + } + + /** + * Returns a list of strings. + * Each string is the text for a page. + * The maximum number of lines per page is 'linesPerPage' minus 1. + * Will return an empty list if no gates are availible. + */ + private List pagedGateIds() { + Collection gates = this.getAllGates(); + + if (gates.size() == 0) { + return null; + } + + List> gatesSortedByName = gatesSortedByName(gates); + List allPages = new ArrayList(); + int linesLeftOnPage = linesPerPage - 1; + String currentPageString = ""; + + for (int i = 0; i < gatesSortedByName.size(); i++) { + + List currentGates = gatesSortedByName.get(i); + + if (currentGates.isEmpty()) { + continue; + } + + List currentGatesAsLines = linesOfGateIds(currentGates); + boolean moreGatesOnLastPage = false; + + while (!currentGatesAsLines.isEmpty()) { + + if (linesLeftOnPage < 2) { + currentPageString = currentPageString.substring(0, currentPageString.length() - 2); // remove newlines add the end of the page + allPages.add(currentPageString); + currentPageString = ""; + + linesLeftOnPage = linesPerPage - 1; + } + + // calculate number of lines to add to current page + int linesNecessaryForCurrentGates = currentGatesAsLines.size(); + int linesToFill; + boolean moreGatesOnNextPage; + + if (linesNecessaryForCurrentGates < linesLeftOnPage) { + linesToFill = linesNecessaryForCurrentGates; + moreGatesOnNextPage = false; + } else { + linesToFill = linesLeftOnPage - 1; + moreGatesOnNextPage = true; + } + + // add title + currentPageString += intToTitleString(i, moreGatesOnLastPage, moreGatesOnNextPage); + currentPageString += ChatColor.AQUA; + linesLeftOnPage--; + + // add gate lines + for (int j = 0; j < linesToFill; j++) { + currentPageString += currentGatesAsLines.get(j) + "\n"; + } + + // remove lines added + for (int j = 0; j < linesToFill; j++) { + currentGatesAsLines.remove(0); + } + + // cleanup moreGatesOnLastPage = linesNecessaryForCurrentGates >= linesLeftOnPage; - linesLeftOnPage -= linesToFill; - } - } - - // add the last page - if (!currentPageString.isEmpty()) { - currentPageString = currentPageString.substring(0, currentPageString.length()-2); // remove newlines add the end of the page - allPages.add(currentPageString); - } - - return allPages; - } - - - protected int getPageParameter() - { - int page = 1; - - try { - page = new Integer(parameters.get(0)); - } - catch (Exception ignored) { } - - return page; - } - - - public void perform() - { - int page = this.getPageParameter(); - List allPages = this.pagedGateIds(); + linesLeftOnPage -= linesToFill; + } + } - if (allPages == null) { // no gates exist - sendMessage(ChatColor.RED + "There are no gates yet. " + ChatColor.RESET + - "(Note that you might not be allowed to get information about certain gates)"); - return; - } - - if (page > allPages.size() || page < 1) { - sendMessage(ChatColor.RED + "The requested page is not availible"); - return; - } - - String message = TextUtil.titleize("List of all gates (" + page + "/" + allPages.size() + ")") + "\n"; - message += allPages.get(page-1); + // add the last page + if (!currentPageString.isEmpty()) { + currentPageString = currentPageString.substring(0, currentPageString.length() - 2); // remove newlines add the end of the page + allPages.add(currentPageString); + } - sendMessage(message); - } + return allPages; + } + + + private int getPageParameter() { + int page = 1; + + try { + page = new Integer(parameters.get(0)); + } catch (Exception ignored) { + } + + return page; + } } - diff --git a/src/de/craftinc/gates/commands/CommandLocation.java b/src/de/craftinc/gates/commands/CommandLocation.java index 61b631e..4737872 100644 --- a/src/de/craftinc/gates/commands/CommandLocation.java +++ b/src/de/craftinc/gates/commands/CommandLocation.java @@ -16,7 +16,6 @@ */ package de.craftinc.gates.commands; - import java.util.Set; import de.craftinc.gates.util.GateBlockChangeSender; @@ -26,59 +25,46 @@ import org.bukkit.Location; import de.craftinc.gates.Plugin; import org.bukkit.block.Block; +public class CommandLocation extends BaseLocationCommand { -public class CommandLocation extends BaseLocationCommand -{ - public CommandLocation() - { - aliases.add("location"); - aliases.add("lo"); - - requiredParameters.add("id"); - - helpDescription = "Set the entrance of the gate to your current location."; - - requiredPermission = Plugin.permissionManage; - - needsPermissionAtCurrentLocation = true; - shouldPersistToDisk = true; - senderMustBePlayer = true; - } - - - public void perform() - { - Location playerLocation = getValidPlayerLocation(); - - if (playerLocation == null) - { - sendMessage("There is not enough room for a gate to open here"); - return; - } + public CommandLocation() { + aliases.add("location"); + aliases.add("lo"); + + requiredParameters.add("id"); + helpDescription = "Set the entrance of the gate to your current location."; + requiredPermission = Plugin.permissionManage; + + needsPermissionAtCurrentLocation = true; + shouldPersistToDisk = true; + senderMustBePlayer = true; + } + + public void perform() { + Location playerLocation = getValidPlayerLocation(); + + if (playerLocation == null) { + sendMessage("There is not enough room for a gate to open here"); + return; + } Location oldLocation = gate.getLocation(); Set oldGateBlockLocations = gate.getGateBlockLocations(); Set oldFrameBlocks = gate.getGateFrameBlocks(); - - try - { + + try { if (gate.isOpen()) { GateBlockChangeSender.updateGateBlocks(gate, true); } gate.setLocation(playerLocation); - - sendMessage(ChatColor.GREEN + "The location of '" + gate.getId() + "' is now at your current location."); - } - catch (Exception e) - { + sendMessage(ChatColor.GREEN + "The location of '" + gate.getId() + "' is now at your current location."); + } catch (Exception e) { sendMessage(ChatColor.RED + "There seems to be no frame at your new location! The gate got closed!" + ChatColor.AQUA + " You should build a frame now and execute:"); sendMessage(new CommandOpen().getUsageTemplate(true, true)); - } - finally { + } finally { Plugin.getPlugin().getGatesManager().handleGateLocationChange(gate, oldLocation, oldGateBlockLocations, oldFrameBlocks); GateBlockChangeSender.updateGateBlocks(gate); } } } - diff --git a/src/de/craftinc/gates/commands/CommandNearby.java b/src/de/craftinc/gates/commands/CommandNearby.java index a15b528..1ce2708 100644 --- a/src/de/craftinc/gates/commands/CommandNearby.java +++ b/src/de/craftinc/gates/commands/CommandNearby.java @@ -1,6 +1,5 @@ package de.craftinc.gates.commands; - import de.craftinc.gates.Gate; import de.craftinc.gates.GatesManager; import de.craftinc.gates.Plugin; @@ -10,33 +9,27 @@ import de.craftinc.gates.util.TextUtil; import java.util.ArrayList; import java.util.Set; -public class CommandNearby extends BaseLocationCommand -{ - public CommandNearby() - { +public class CommandNearby extends BaseLocationCommand { + + public CommandNearby() { aliases.add("nearby"); aliases.add("nb"); helpDescription = "Highlight nearby gates"; - requiredPermission = Plugin.permissionInfo; - needsPermissionAtCurrentLocation = true; shouldPersistToDisk = false; senderMustBePlayer = true; hasGateParam = false; } - - public void perform() - { + public void perform() { GatesManager manager = Plugin.getPlugin().getGatesManager(); Set nearbyGates = manager.getNearbyGates(player.getLocation().getChunk()); - if (nearbyGates == null) { + if (nearbyGates == null) { player.sendMessage("There are no gates near you!"); - } - else { + } else { GateBlockChangeSender.temporaryHighlightGatesFrames(player, nearbyGates); ArrayList gateNames = new ArrayList(); @@ -44,10 +37,7 @@ public class CommandNearby extends BaseLocationCommand for (Gate g : nearbyGates) { gateNames.add(g.getId()); } - player.sendMessage("Nearby gates: " + TextUtil.implode(gateNames, ", ")); } - - } } diff --git a/src/de/craftinc/gates/commands/CommandNew.java b/src/de/craftinc/gates/commands/CommandNew.java index 8cd18bb..4f9a090 100644 --- a/src/de/craftinc/gates/commands/CommandNew.java +++ b/src/de/craftinc/gates/commands/CommandNew.java @@ -23,61 +23,48 @@ import de.craftinc.gates.Gate; import de.craftinc.gates.GatesManager; import de.craftinc.gates.Plugin; +public class CommandNew extends BaseLocationCommand { -public class CommandNew extends BaseLocationCommand -{ - public CommandNew() - { - aliases.add("new"); + public CommandNew() { + aliases.add("new"); aliases.add("n"); - requiredParameters.add("id"); - - senderMustBePlayer = true; - hasGateParam = false; - - helpDescription = "Create a gate at your current location."; - - requiredPermission = Plugin.permissionManage; - - needsPermissionAtCurrentLocation = true; - shouldPersistToDisk = true; - - senderMustBePlayer = true; - } - - - public void perform() - { - String id = parameters.get(0); - GatesManager gatesManager = Plugin.getPlugin().getGatesManager(); - - if (gatesManager.gateExists(id)) { - sendMessage(ChatColor.RED + "Creating the gate failed! " + "A gate with the supplied id already exists!"); - return; - } - - gate = new Gate(id); - sendMessage(ChatColor.GREEN + "Gate with id '" + id + "' was created."); + requiredParameters.add("id"); - - Location playerLocation = getValidPlayerLocation(); - - if (playerLocation != null) { - - try { - gate.setLocation(playerLocation); - sendMessage(ChatColor.AQUA + "The gates location has been set to your current location."); - } - catch (Exception ignored) {} - } - else - { - sendMessage(ChatColor.RED + "Your location is invalid!" + ChatColor.AQUA + "Go somewhere else and execute:"); - sendMessage(new CommandLocation().getUsageTemplate(true, true)); - } + senderMustBePlayer = true; + hasGateParam = false; + helpDescription = "Create a gate at your current location."; + requiredPermission = Plugin.permissionManage; + needsPermissionAtCurrentLocation = true; + shouldPersistToDisk = true; + senderMustBePlayer = true; + } + + public void perform() { + String id = parameters.get(0); + GatesManager gatesManager = Plugin.getPlugin().getGatesManager(); + + if (gatesManager.gateExists(id)) { + sendMessage(ChatColor.RED + "Creating the gate failed! " + "A gate with the supplied id already exists!"); + return; + } + + gate = new Gate(id); + sendMessage(ChatColor.GREEN + "Gate with id '" + id + "' was created."); + + Location playerLocation = getValidPlayerLocation(); + + if (playerLocation != null) { + try { + gate.setLocation(playerLocation); + sendMessage(ChatColor.AQUA + "The gates location has been set to your current location."); + } catch (Exception ignored) { + } + } else { + sendMessage(ChatColor.RED + "Your location is invalid!" + ChatColor.AQUA + "Go somewhere else and execute:"); + sendMessage(new CommandLocation().getUsageTemplate(true, true)); + } gatesManager.handleNewGate(gate); - } + } } - diff --git a/src/de/craftinc/gates/commands/CommandOpen.java b/src/de/craftinc/gates/commands/CommandOpen.java index 7dde5cf..b188e75 100644 --- a/src/de/craftinc/gates/commands/CommandOpen.java +++ b/src/de/craftinc/gates/commands/CommandOpen.java @@ -16,39 +16,31 @@ */ package de.craftinc.gates.commands; - import de.craftinc.gates.util.GateBlockChangeSender; import org.bukkit.ChatColor; import de.craftinc.gates.Plugin; +public class CommandOpen extends BaseCommand { -public class CommandOpen extends BaseCommand -{ - - public CommandOpen() - { - aliases.add("open"); - aliases.add("o"); - - requiredParameters.add("id"); - - helpDescription = "Open a gate so players can use it."; - - requiredPermission = Plugin.permissionManage; - - needsPermissionAtCurrentLocation = false; - shouldPersistToDisk = true; - senderMustBePlayer = false; - } - - - public void perform() - { - try { + public CommandOpen() { + aliases.add("open"); + aliases.add("o"); + + requiredParameters.add("id"); + helpDescription = "Open a gate so players can use it."; + requiredPermission = Plugin.permissionManage; + + needsPermissionAtCurrentLocation = false; + shouldPersistToDisk = true; + senderMustBePlayer = false; + } + + public void perform() { + try { boolean needsGateManagerUpdate = false; - if (gate.getGateBlockLocations().isEmpty()) { + if (gate.getGateBlockLocations().isEmpty()) { needsGateManagerUpdate = true; } @@ -60,11 +52,9 @@ public class CommandOpen extends BaseCommand Plugin.getPlugin().getGatesManager().handleGateLocationChange(gate, null, null, null); } - sendMessage(ChatColor.GREEN + "The gate was opened."); - } - catch (Exception e) { - sendMessage(ChatColor.RED + e.getMessage()); - } - } + sendMessage(ChatColor.GREEN + "The gate was opened."); + } catch (Exception e) { + sendMessage(ChatColor.RED + e.getMessage()); + } + } } - diff --git a/src/de/craftinc/gates/commands/CommandRemove.java b/src/de/craftinc/gates/commands/CommandRemove.java index 8055cb5..802d893 100644 --- a/src/de/craftinc/gates/commands/CommandRemove.java +++ b/src/de/craftinc/gates/commands/CommandRemove.java @@ -21,33 +21,29 @@ import org.bukkit.ChatColor; import de.craftinc.gates.Plugin; +public class CommandRemove extends BaseCommand { -public class CommandRemove extends BaseCommand -{ - public CommandRemove() - { - aliases.add("delete"); - aliases.add("del"); - aliases.add("remove"); - - requiredParameters.add("id"); - - senderMustBePlayer = false; - helpDescription = "Removes the gate from the game."; - - requiredPermission = Plugin.permissionManage; - - needsPermissionAtCurrentLocation = false; - shouldPersistToDisk = true; - - senderMustBePlayer = false; - } - - - public void perform() - { - Plugin.getPlugin().getGatesManager().handleDeletion(gate); + public CommandRemove() { + aliases.add("delete"); + aliases.add("del"); + aliases.add("remove"); + + requiredParameters.add("id"); + + senderMustBePlayer = false; + helpDescription = "Removes the gate from the game."; + + requiredPermission = Plugin.permissionManage; + + needsPermissionAtCurrentLocation = false; + shouldPersistToDisk = true; + + senderMustBePlayer = false; + } + + public void perform() { + Plugin.getPlugin().getGatesManager().handleDeletion(gate); GateBlockChangeSender.updateGateBlocks(gate, true); - sendMessage(ChatColor.GREEN + "Gate with id '" + gate.getId() + "' was deleted."); - } + sendMessage(ChatColor.GREEN + "Gate with id '" + gate.getId() + "' was deleted."); + } } diff --git a/src/de/craftinc/gates/commands/CommandRename.java b/src/de/craftinc/gates/commands/CommandRename.java index b4b7cc8..0a424c0 100644 --- a/src/de/craftinc/gates/commands/CommandRename.java +++ b/src/de/craftinc/gates/commands/CommandRename.java @@ -21,46 +21,41 @@ import org.bukkit.ChatColor; import de.craftinc.gates.GatesManager; import de.craftinc.gates.Plugin; +public class CommandRename extends BaseCommand { -public class CommandRename extends BaseCommand -{ - public CommandRename() - { - aliases.add("rename"); - aliases.add("rn"); + public CommandRename() { + aliases.add("rename"); + aliases.add("rn"); - hasGateParam = true; - senderMustBePlayer = false; - - requiredParameters.add("current name"); - requiredParameters.add("new name"); - - helpDescription = "Changes the id of a gate."; - - requiredPermission = Plugin.permissionManage; - - needsPermissionAtCurrentLocation = false; - shouldPersistToDisk = true; - senderMustBePlayer = false; - } - - - public void perform() - { - String newId = parameters.get(1); - GatesManager gatesManager = Plugin.getPlugin().getGatesManager(); - - 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 + "."); - } - } + hasGateParam = true; + senderMustBePlayer = false; + requiredParameters.add("current name"); + requiredParameters.add("new name"); + + helpDescription = "Changes the id of a gate."; + + requiredPermission = Plugin.permissionManage; + + needsPermissionAtCurrentLocation = false; + shouldPersistToDisk = true; + senderMustBePlayer = false; + } + + + public void perform() { + String newId = parameters.get(1); + GatesManager gatesManager = Plugin.getPlugin().getGatesManager(); + + 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/commands/CommandUnhide.java b/src/de/craftinc/gates/commands/CommandUnhide.java index aaad6f6..f747b47 100644 --- a/src/de/craftinc/gates/commands/CommandUnhide.java +++ b/src/de/craftinc/gates/commands/CommandUnhide.java @@ -21,38 +21,27 @@ import org.bukkit.ChatColor; import de.craftinc.gates.Plugin; +public class CommandUnhide extends BaseCommand { -public class CommandUnhide extends BaseCommand -{ - - public CommandUnhide() - { - aliases.add("unhide"); - aliases.add("u"); - - requiredParameters.add("id"); - - helpDescription = "Make that gate visible"; - - requiredPermission = Plugin.permissionManage; - - needsPermissionAtCurrentLocation = false; - shouldPersistToDisk = true; - senderMustBePlayer = false; - } - - - public void perform() - { - try - { - gate.setHidden(false); + public CommandUnhide() { + aliases.add("unhide"); + aliases.add("u"); + + requiredParameters.add("id"); + helpDescription = "Make that gate visible"; + requiredPermission = Plugin.permissionManage; + needsPermissionAtCurrentLocation = false; + shouldPersistToDisk = true; + senderMustBePlayer = false; + } + + public void perform() { + try { + gate.setHidden(false); GateBlockChangeSender.updateGateBlocks(gate); - sendMessage(ChatColor.GREEN + "The gate " + gate.getId() + " is now visible."); - } - catch (Exception e) { - sendMessage(ChatColor.RED + e.getMessage()); - } - } - -} \ No newline at end of file + sendMessage(ChatColor.GREEN + "The gate " + gate.getId() + " is now visible."); + } catch (Exception e) { + sendMessage(ChatColor.RED + e.getMessage()); + } + } +}