code formatter applied.

This commit is contained in:
Tobias Ottenweller 2016-12-31 11:59:11 +01:00
parent 238dbe3e51
commit 40b396f8a2
18 changed files with 857 additions and 1021 deletions

View File

@ -22,6 +22,7 @@ import java.util.List;
import de.craftinc.gates.util.ConfigurationUtil; import de.craftinc.gates.util.ConfigurationUtil;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import de.craftinc.gates.Gate; import de.craftinc.gates.Gate;
@ -29,224 +30,213 @@ import de.craftinc.gates.GatesManager;
import de.craftinc.gates.Plugin; import de.craftinc.gates.Plugin;
import de.craftinc.gates.util.TextUtil; import de.craftinc.gates.util.TextUtil;
public abstract class BaseCommand public abstract class BaseCommand {
{
protected List<String> aliases = new ArrayList<String>();
protected List<String> requiredParameters = new ArrayList<String>();
protected List<String> optionalParameters = new ArrayList<String>();
protected String helpDescription = "no description";
protected List<String> 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;
protected List<String> aliases = new ArrayList<String>();
public List<String> getAliases() { protected List<String> requiredParameters = new ArrayList<String>();
return aliases; protected List<String> optionalParameters = new ArrayList<String>();
}
protected String helpDescription = "no description";
public void execute(CommandSender sender, List<String> parameters) { protected List<String> parameters;
this.sender = sender; protected CommandSender sender;
this.parameters = parameters; protected Player player;
protected Gate gate;
if (!this.validateCall()) {
return; protected boolean senderMustBePlayer = true;
} protected boolean hasGateParam = true;
if (this.senderMustBePlayer) { protected String requiredPermission;
this.player = (Player)sender; protected boolean needsPermissionAtCurrentLocation;
}
protected boolean shouldPersistToDisk;
this.perform();
if (this.shouldPersistToDisk && Plugin.getPlugin().getConfig().getBoolean(ConfigurationUtil.confSaveOnChangesKey)) { public List<String> getAliases() {
Plugin.getPlugin().getGatesManager().saveGatesToDisk(); return aliases;
} }
}
public void execute(CommandSender sender, List<String> parameters) {
abstract protected void perform(); this.sender = sender;
this.parameters = parameters;
protected void sendMessage(String message) { if (!this.validateCall()) {
sender.sendMessage(message); return;
} }
if (this.senderMustBePlayer) {
protected void sendMessage(List<String> messages) { this.player = (Player) sender;
for(String message : messages) { }
this.sendMessage(message);
} this.perform();
}
if (this.shouldPersistToDisk && getSaveOnChanges()) {
Plugin.getPlugin().getGatesManager().saveGatesToDisk();
protected boolean validateCall() }
{ }
boolean allParametersThere = parameters.size() >= requiredParameters.size();
boolean senderIsPlayer = this.sender instanceof Player; private boolean getSaveOnChanges() {
boolean hasGateParameter = false; FileConfiguration config = Plugin.getPlugin().getConfig();
return config.getBoolean(ConfigurationUtil.confSaveOnChangesKey);
if (this.hasGateParam && this.parameters.size() > 0 && this.setGateUsingParameter(this.parameters.get(0))) { }
hasGateParameter = true;
}
abstract protected void perform();
boolean senderHasPermission = this.hasPermission();
boolean valid;
protected void sendMessage(String message) {
if (this.senderMustBePlayer && !senderIsPlayer) { sender.sendMessage(message);
sendMessage(ChatColor.RED + "This command can only be used by ingame players."); }
valid = false;
}
else { protected void sendMessage(List<String> 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) { if (!allParametersThere) {
sendMessage(ChatColor.RED + "Some parameters are missing! " + ChatColor.AQUA + "Usage: " + this.getUsageTemplate(true)); sendMessage(ChatColor.RED + "Some parameters are missing! " + ChatColor.AQUA + "Usage: " + this.getUsageTemplate(true));
valid = false; valid = false;
} } else if ((!senderHasPermission && this.hasGateParam) ||
else if ((!senderHasPermission && this.hasGateParam) || (!senderHasPermission) ||
(!senderHasPermission) || (this.hasGateParam && !hasGateParameter)) {
(this.hasGateParam && !hasGateParameter)) {
sendMessage(ChatColor.RED + "You either provided a invalid gate or do not have permission to " + this.helpDescription.toLowerCase()); sendMessage(ChatColor.RED + "You either provided a invalid gate or do not have permission to " + this.helpDescription.toLowerCase());
valid = false; valid = false;
} } else {
else {
valid = true; 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 // sender is no player - there is no information about the senders locations
return Plugin.getPermission().has(this.sender, this.requiredPermission); 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) { Player p = (Player) this.sender;
hasPermission = this.hasPermissionAtGateLocationAndExit(p); boolean hasPermission = false;
}
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) { if (this.requiredPermission.equals(Plugin.permissionInfo)) {
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);
return permAtLocation & permAtExit; if (this.hasGateParam) {
} hasPermission = this.hasPermissionAtGateLocationAndExit(p);
} else {
hasPermission = Plugin.getPermission().has(p.getWorld(), p.getName(), this.requiredPermission);
// -------------------------------------------- // }
// Help and usage description } else if (this.requiredPermission.equals(Plugin.permissionUse)) {
// -------------------------------------------- // hasPermission = this.hasPermissionAtGateLocationAndExit(p);
protected String getUsageTemplate(boolean withColor, boolean withDescription) } else if (this.requiredPermission.equals(Plugin.permissionManage)) {
{
String ret = ""; if (this.needsPermissionAtCurrentLocation && this.hasGateParam) {
boolean hasPersmissionAtCurrentLocation = Plugin.getPermission().has(p.getWorld(), p.getName(), this.requiredPermission);
if (withColor) { hasPermission = hasPersmissionAtCurrentLocation && this.hasPermissionAtGateLocationAndExit(p);
ret += ChatColor.AQUA; } else if (this.needsPermissionAtCurrentLocation) {
} hasPermission = Plugin.getPermission().has(p.getWorld(), p.getName(), this.requiredPermission);
} else {
ret += "/" + Plugin.getPlugin().getBaseCommand() + " " + TextUtil.implode(this.getAliases(), ",")+" "; hasPermission = this.hasPermissionAtGateLocationAndExit(p);
}
List<String> parts = new ArrayList<String>(); }
for (String requiredParameter : this.requiredParameters) { return hasPermission;
parts.add("["+requiredParameter+"]"); }
}
for (String optionalParameter : this.optionalParameters) { protected boolean hasPermissionAtGateLocationAndExit(Player p) {
parts.add("*["+optionalParameter+"]"); if (this.gate == null || p == null) {
} return false;
}
if (withColor) {
ret += ChatColor.DARK_AQUA; 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);
ret += TextUtil.implode(parts, " "); return permAtLocation & permAtExit;
}
if (withDescription) {
ret += " ";
// -------------------------------------------- //
if (withColor) { // Help and usage description
ret += ChatColor.YELLOW; // -------------------------------------------- //
} protected String getUsageTemplate(boolean withColor, boolean withDescription) {
String ret = "";
ret += this.helpDescription;
} if (withColor) {
return ret; ret += ChatColor.AQUA;
} }
protected String getUsageTemplate(boolean withColor) ret += "/" + Plugin.getPlugin().getBaseCommand() + " " + TextUtil.implode(this.getAliases(), ",") + " ";
{
return getUsageTemplate(withColor, false); List<String> parts = new ArrayList<String>();
}
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);
}
} }

View File

@ -21,27 +21,25 @@ import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
public abstract class BaseLocationCommand extends BaseCommand public abstract class BaseLocationCommand extends BaseCommand {
{
protected Location getValidPlayerLocation() protected Location getValidPlayerLocation() {
{ // The player might stand in a half block or a sign or whatever
// The player might stand in a half block or a sign or whatever // Therefore we load some extra locations and blocks
// Therefore we load some extra locations and blocks Block playerBlock = player.getLocation().getBlock();
Block playerBlock = player.getLocation().getBlock(); Block upBlock = playerBlock.getRelative(BlockFace.UP);
Block upBlock = playerBlock.getRelative(BlockFace.UP);
if (playerBlock.getType() == Material.AIR) {
if (playerBlock.getType() == Material.AIR) { return player.getLocation();
return player.getLocation(); } else if (upBlock.getType() == Material.AIR) {
} return new Location(player.getLocation().getWorld(),
else if (upBlock.getType() == Material.AIR) { player.getLocation().getX(),
return new Location(player.getLocation().getWorld(), player.getLocation().getY() + 1,
player.getLocation().getX(), player.getLocation().getZ(),
player.getLocation().getY() + 1, player.getLocation().getYaw(),
player.getLocation().getZ(), player.getLocation().getPitch());
player.getLocation().getYaw(), }
player.getLocation().getPitch());
} return null;
}
return null;
}
} }

View File

@ -20,19 +20,16 @@ package de.craftinc.gates.commands;
import de.craftinc.gates.Plugin; import de.craftinc.gates.Plugin;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
public class CommandAllowRiding extends BaseCommand public class CommandAllowRiding extends BaseCommand {
{
public CommandAllowRiding() public CommandAllowRiding() {
{
aliases.add("allowRiding"); aliases.add("allowRiding");
aliases.add("ar"); aliases.add("ar");
requiredParameters.add("id"); requiredParameters.add("id");
helpDescription = "Allow players to travel while riding."; helpDescription = "Allow players to travel while riding.";
requiredPermission = Plugin.permissionManage; requiredPermission = Plugin.permissionManage;
needsPermissionAtCurrentLocation = false; needsPermissionAtCurrentLocation = false;
shouldPersistToDisk = true; shouldPersistToDisk = true;
@ -40,8 +37,7 @@ public class CommandAllowRiding extends BaseCommand
} }
@Override @Override
protected void perform() protected void perform() {
{
gate.setAllowsVehicles(true); gate.setAllowsVehicles(true);
sendMessage(ChatColor.GREEN + "Traveling while riding is now enabled for this gate."); sendMessage(ChatColor.GREEN + "Traveling while riding is now enabled for this gate.");
} }

View File

@ -24,41 +24,30 @@ import org.bukkit.ChatColor;
import de.craftinc.gates.Plugin; import de.craftinc.gates.Plugin;
public class CommandClose extends BaseCommand 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 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();
}
}
}

View File

@ -19,28 +19,22 @@ package de.craftinc.gates.commands;
import de.craftinc.gates.Plugin; import de.craftinc.gates.Plugin;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
public class CommandDenyRiding extends BaseCommand public class CommandDenyRiding extends BaseCommand {
{
public CommandDenyRiding() public CommandDenyRiding() {
{
aliases.add("denyRiding"); aliases.add("denyRiding");
aliases.add("dr"); aliases.add("dr");
requiredParameters.add("id"); requiredParameters.add("id");
helpDescription = "Deny players to travel while riding."; helpDescription = "Deny players to travel while riding.";
requiredPermission = Plugin.permissionManage; requiredPermission = Plugin.permissionManage;
needsPermissionAtCurrentLocation = false; needsPermissionAtCurrentLocation = false;
shouldPersistToDisk = true; shouldPersistToDisk = true;
senderMustBePlayer = false; senderMustBePlayer = false;
} }
@Override @Override
protected void perform() protected void perform() {
{
gate.setAllowsVehicles(false); gate.setAllowsVehicles(false);
sendMessage(ChatColor.GREEN + "Traveling while riding is now disabled for this gate."); sendMessage(ChatColor.GREEN + "Traveling while riding is now disabled for this gate.");
} }

View File

@ -24,41 +24,35 @@ import org.bukkit.ChatColor;
import de.craftinc.gates.Plugin; import de.craftinc.gates.Plugin;
import org.bukkit.Location; import org.bukkit.Location;
public class CommandExit extends BaseCommand {
public class CommandExit extends BaseCommand public CommandExit() {
{ aliases.add("exit");
public CommandExit() aliases.add("e");
{
aliases.add("exit"); requiredParameters.add("id");
aliases.add("e");
helpDescription = "Change exit of location.";
requiredParameters.add("id");
requiredPermission = Plugin.permissionManage;
helpDescription = "Change exit of location.";
needsPermissionAtCurrentLocation = true;
requiredPermission = Plugin.permissionManage; shouldPersistToDisk = true;
senderMustBePlayer = true;
needsPermissionAtCurrentLocation = true; }
shouldPersistToDisk = true;
senderMustBePlayer = true;
} public void perform() {
try {
public void perform()
{
try
{
Location oldExit = gate.getExit(); Location oldExit = gate.getExit();
gate.setExit(player.getLocation()); gate.setExit(player.getLocation());
sendMessage(ChatColor.GREEN + "The exit of gate '" + gate.getId() + "' is now where you stand."); sendMessage(ChatColor.GREEN + "The exit of gate '" + gate.getId() + "' is now where you stand.");
Plugin.getPlugin().getGatesManager().handleGateExitChange(gate, oldExit); Plugin.getPlugin().getGatesManager().handleGateExitChange(gate, oldExit);
} } catch (Exception e) {
catch (Exception e) {
GateBlockChangeSender.updateGateBlocks(gate); GateBlockChangeSender.updateGateBlocks(gate);
sendMessage(ChatColor.RED + "Setting the exit for the gate failed! See server log for more information"); sendMessage(ChatColor.RED + "Setting the exit for the gate failed! See server log for more information");
Plugin.log(Level.WARNING, e.getMessage()); Plugin.log(Level.WARNING, e.getMessage());
e.printStackTrace(); e.printStackTrace();
} }
} }
} }

View File

@ -24,29 +24,23 @@ import org.bukkit.Location;
import java.util.logging.Level; import java.util.logging.Level;
public class CommandExitOpen extends BaseCommand public class CommandExitOpen extends BaseCommand {
{
public CommandExitOpen() public CommandExitOpen() {
{
aliases.add("exitopen"); aliases.add("exitopen");
aliases.add("eo"); aliases.add("eo");
requiredParameters.add("id"); requiredParameters.add("id");
helpDescription = "Change exit of location and open that gate afterwards."; helpDescription = "Change exit of location and open that gate afterwards.";
requiredPermission = Plugin.permissionManage; requiredPermission = Plugin.permissionManage;
needsPermissionAtCurrentLocation = true; needsPermissionAtCurrentLocation = true;
shouldPersistToDisk = true; shouldPersistToDisk = true;
senderMustBePlayer = true; senderMustBePlayer = true;
} }
public void perform() public void perform() {
{ try {
try
{
Location oldExit = gate.getExit(); Location oldExit = gate.getExit();
gate.setExit(player.getLocation()); gate.setExit(player.getLocation());
sendMessage(ChatColor.GREEN + "The exit of gate '" + gate.getId() + "' is now where you stand."); 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."); sendMessage(ChatColor.GREEN + "The gate was opened.");
} } catch (Exception e) {
catch (Exception e) {
sendMessage(ChatColor.RED + e.getMessage()); sendMessage(ChatColor.RED + e.getMessage());
} }
} } catch (Exception e) {
catch (Exception e) {
GateBlockChangeSender.updateGateBlocks(gate); GateBlockChangeSender.updateGateBlocks(gate);
sendMessage(ChatColor.RED + "Setting the exit for the gate failed! This gate is now closed! (See server log for more information.)"); 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()); Plugin.log(Level.WARNING, e.getMessage());

View File

@ -23,89 +23,84 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
public class CommandHelp extends BaseCommand public class CommandHelp extends BaseCommand {
{
public static List<List<String>> helpPages; public static List<List<String>> helpPages;
static static {
{ // sort the usage strings
// sort the usage strings List<String> allUsageStrings = new ArrayList<String>();
List<String> allUsageStrings = new ArrayList<String>();
allUsageStrings.add(new CommandHelp().getUsageTemplate(true, true));
allUsageStrings.add( new CommandHelp().getUsageTemplate(true, true) ); allUsageStrings.add(new CommandNew().getUsageTemplate(true, true));
allUsageStrings.add( new CommandNew().getUsageTemplate(true, true) ); allUsageStrings.add(new CommandRemove().getUsageTemplate(true, true));
allUsageStrings.add( new CommandRemove().getUsageTemplate(true, true) ); allUsageStrings.add(new CommandLocation().getUsageTemplate(true, true));
allUsageStrings.add( new CommandLocation().getUsageTemplate(true, true) ); allUsageStrings.add(new CommandExit().getUsageTemplate(true, true));
allUsageStrings.add( new CommandExit().getUsageTemplate(true, true) ); allUsageStrings.add(new CommandOpen().getUsageTemplate(true, true));
allUsageStrings.add( new CommandOpen().getUsageTemplate(true, true) ); allUsageStrings.add(new CommandRename().getUsageTemplate(true, true));
allUsageStrings.add( new CommandRename().getUsageTemplate(true, true) ); allUsageStrings.add(new CommandClose().getUsageTemplate(true, true));
allUsageStrings.add( new CommandClose().getUsageTemplate(true, true) ); allUsageStrings.add(new CommandList().getUsageTemplate(true, true));
allUsageStrings.add( new CommandList().getUsageTemplate(true, true) ); allUsageStrings.add(new CommandInfo().getUsageTemplate(true, true));
allUsageStrings.add( new CommandInfo().getUsageTemplate(true, true) ); allUsageStrings.add(new CommandHide().getUsageTemplate(true, true));
allUsageStrings.add( new CommandHide().getUsageTemplate(true, true) ); allUsageStrings.add(new CommandUnhide().getUsageTemplate(true, true));
allUsageStrings.add( new CommandUnhide().getUsageTemplate(true, true) ); allUsageStrings.add(new CommandExitOpen().getUsageTemplate(true, true));
allUsageStrings.add( new CommandExitOpen().getUsageTemplate(true, true) ); allUsageStrings.add(new CommandNearby().getUsageTemplate(true, true));
allUsageStrings.add( new CommandNearby().getUsageTemplate(true, true) );
Collections.sort(allUsageStrings);
Collections.sort(allUsageStrings);
// put 5 commands on one page
helpPages = new ArrayList<List<String>>();
while (!allUsageStrings.isEmpty()) {
int toIndex = allUsageStrings.size() >= 6 ? 5 : allUsageStrings.size();
List<String> currentHelpPage = new ArrayList<String>(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() // put 5 commands on one page
{ helpPages = new ArrayList<List<String>>();
int page;
while (!allUsageStrings.isEmpty()) {
if (parameters.size() > 0) { int toIndex = allUsageStrings.size() >= 6 ? 5 : allUsageStrings.size();
try { List<String> currentHelpPage = new ArrayList<String>(allUsageStrings.subList(0, toIndex));
page = Integer.parseInt(parameters.get(0)); helpPages.add(currentHelpPage);
}
catch (NumberFormatException e) { allUsageStrings.removeAll(currentHelpPage);
// wasn't an integer }
}
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; page = 1;
} }
} } else {
else {
page = 1; page = 1;
} }
sendMessage(TextUtil.titleize("Craft Inc. Gates Help (" + page + "/" + helpPages.size() + ")")); sendMessage(TextUtil.titleize("Craft Inc. Gates Help (" + page + "/" + helpPages.size() + ")"));
page -= 1; page -= 1;
if (page < 0 || page >= helpPages.size()) { if (page < 0 || page >= helpPages.size()) {
sendMessage("This page does not exist"); sendMessage("This page does not exist");
return; return;
} }
sendMessage(helpPages.get(page)); sendMessage(helpPages.get(page));
} }
} }

View File

@ -24,36 +24,33 @@ import org.bukkit.ChatColor;
import de.craftinc.gates.Plugin; import de.craftinc.gates.Plugin;
public class CommandHide extends BaseCommand public class CommandHide extends BaseCommand {
{
public CommandHide() public CommandHide() {
{ aliases.add("hide");
aliases.add("hide");
aliases.add("h"); aliases.add("h");
requiredParameters.add("id"); requiredParameters.add("id");
helpDescription = "Makes a gate NOT consist of gate blocks while open."; helpDescription = "Makes a gate NOT consist of gate blocks while open.";
requiredPermission = Plugin.permissionManage; requiredPermission = Plugin.permissionManage;
needsPermissionAtCurrentLocation = false; needsPermissionAtCurrentLocation = false;
shouldPersistToDisk = true; shouldPersistToDisk = true;
senderMustBePlayer = false; senderMustBePlayer = false;
} }
public void perform() public void perform() {
{ try {
try { gate.setHidden(true);
gate.setHidden(true);
GateBlockChangeSender.updateGateBlocks(gate); GateBlockChangeSender.updateGateBlocks(gate);
sendMessage(ChatColor.GREEN + "The gate '" + gate.getId() + "' is now hidden."); sendMessage(ChatColor.GREEN + "The gate '" + gate.getId() + "' is now hidden.");
} } catch (Exception e) {
catch (Exception e) { sendMessage(ChatColor.RED + "Hiding the gate failed! See server log for more information");
sendMessage(ChatColor.RED + "Hiding the gate failed! See server log for more information"); Plugin.log(Level.WARNING, e.getMessage());
Plugin.log(Level.WARNING, e.getMessage()); e.printStackTrace();
e.printStackTrace(); }
} }
}
} }

View File

@ -16,7 +16,6 @@
*/ */
package de.craftinc.gates.commands; package de.craftinc.gates.commands;
import de.craftinc.gates.util.GateBlockChangeSender; import de.craftinc.gates.util.GateBlockChangeSender;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
@ -24,30 +23,27 @@ import de.craftinc.gates.Plugin;
import de.craftinc.gates.util.TextUtil; import de.craftinc.gates.util.TextUtil;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
public class CommandInfo extends BaseCommand {
public class CommandInfo extends BaseCommand public CommandInfo() {
{ aliases.add("info");
public CommandInfo() aliases.add("i");
{
aliases.add("info");
aliases.add("i");
optionalParameters.add("id"); optionalParameters.add("id");
helpDescription = "Print detailed information about a certain or the closest gate."; helpDescription = "Print detailed information about a certain or the closest gate.";
requiredPermission = Plugin.permissionInfo; requiredPermission = Plugin.permissionInfo;
needsPermissionAtCurrentLocation = false; needsPermissionAtCurrentLocation = false;
shouldPersistToDisk = false; shouldPersistToDisk = false;
senderMustBePlayer = false; senderMustBePlayer = false;
hasGateParam = false; hasGateParam = false;
} }
public void perform() public void perform() {
{ if (this.parameters.size() > 0) {
if (this.parameters.size() > 0) {
if (!this.setGateUsingParameter(this.parameters.get(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()); 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 + "'")); sendMessage(TextUtil.titleize("Information about: '" + ChatColor.WHITE + gate.getId() + ChatColor.YELLOW + "'"));
} } else {
else {
boolean senderIsPlayer = this.sender instanceof Player; boolean senderIsPlayer = this.sender instanceof Player;
if (!senderIsPlayer) { if (!senderIsPlayer) {
@ -64,7 +59,7 @@ public class CommandInfo extends BaseCommand
return; return;
} }
Player p = (Player)this.sender; Player p = (Player) this.sender;
this.gate = Plugin.getPlugin().getGatesManager().getNearestGate(p.getLocation()); this.gate = Plugin.getPlugin().getGatesManager().getNearestGate(p.getLocation());
if (!this.hasPermission() || this.gate == null) { if (!this.hasPermission() || this.gate == null) {
@ -85,22 +80,22 @@ public class CommandInfo extends BaseCommand
openHiddenMessage += ChatColor.AQUA + " closed"; openHiddenMessage += ChatColor.AQUA + " closed";
if (gate.isHidden()) if (gate.isHidden())
openHiddenMessage += ChatColor.DARK_AQUA +" and" + ChatColor.AQUA + " hidden"; openHiddenMessage += ChatColor.DARK_AQUA + " and" + ChatColor.AQUA + " hidden";
openHiddenMessage += ".\n"; openHiddenMessage += ".\n";
sendMessage(openHiddenMessage); sendMessage(openHiddenMessage);
if (gate.getLocation() != null) if (gate.getLocation() != null)
sendMessage(ChatColor.DARK_AQUA + "location: " + ChatColor.AQUA + "( " + (int)gate.getLocation().getX() + sendMessage(ChatColor.DARK_AQUA + "location: " + ChatColor.AQUA + "( " + (int) gate.getLocation().getX() +
" | " + (int)gate.getLocation().getY() + " | " + (int)gate.getLocation().getZ() + " ) in " + " | " + (int) gate.getLocation().getY() + " | " + (int) gate.getLocation().getZ() + " ) in " +
gate.getLocation().getWorld().getName()); gate.getLocation().getWorld().getName());
else else
sendMessage(ChatColor.DARK_AQUA + "NOTE: this gate has no location"); sendMessage(ChatColor.DARK_AQUA + "NOTE: this gate has no location");
if (gate.getExit() != null) if (gate.getExit() != null)
sendMessage(ChatColor.DARK_AQUA + "exit: " + ChatColor.AQUA + "( " + (int)gate.getExit().getX() + " | " sendMessage(ChatColor.DARK_AQUA + "exit: " + ChatColor.AQUA + "( " + (int) gate.getExit().getX() + " | "
+ (int)gate.getExit().getY() + " | " + (int)gate.getExit().getZ() + " ) in " + + (int) gate.getExit().getY() + " | " + (int) gate.getExit().getZ() + " ) in " +
gate.getExit().getWorld().getName()); gate.getExit().getWorld().getName());
else else
sendMessage(ChatColor.DARK_AQUA + "NOTE: this gate has no exit"); sendMessage(ChatColor.DARK_AQUA + "NOTE: this gate has no exit");
@ -111,7 +106,7 @@ public class CommandInfo extends BaseCommand
if (this.sender instanceof Player) { if (this.sender instanceof Player) {
GateBlockChangeSender.temporaryHighlightGateFrame((Player)this.sender, this.gate); GateBlockChangeSender.temporaryHighlightGateFrame((Player) this.sender, this.gate);
} }
} }
} }

View File

@ -28,324 +28,287 @@ import de.craftinc.gates.Gate;
import de.craftinc.gates.Plugin; import de.craftinc.gates.Plugin;
import de.craftinc.gates.util.TextUtil; import de.craftinc.gates.util.TextUtil;
public class CommandList extends BaseCommand {
public class CommandList extends BaseCommand private static final int linesPerPage = 10;
{
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<String> linesOfGateIds(List<String> gates)
{
List<String> lines = new ArrayList<String>();
int index = 0;
List<String> gateIdsForCurrentLine = new ArrayList<String>();
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<String>();
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); /* this is actually not true. the font used by Minecraft is not
monospaced. but there seems to be no (easy) way to calculate
numCharactersInCurrentLine += gateId.length(); the drawing-size of a string.
index++; */
} private static final int charactersPerLine = 52;
// gate fits into current line public CommandList() {
else if ((numCharactersInCurrentLine + gateIdLength) <= charactersPerLine) { aliases.add("list");
gateIdsForCurrentLine.add(gateId); aliases.add("ls");
numCharactersInCurrentLine += gateIdLength;
optionalParameters.add("page");
index++; hasGateParam = false;
} needsPermissionAtCurrentLocation = false;
// the current gate does not fit on the helpDescription = "lists all availible gates.";
else {
lines.add(TextUtil.implode(gateIdsForCurrentLine, ", ") + ", "); requiredPermission = Plugin.permissionInfo;
shouldPersistToDisk = false;
gateIdsForCurrentLine = new ArrayList<String>(); senderMustBePlayer = false;
numCharactersInCurrentLine = 0; }
}
} public void perform() {
int page = this.getPageParameter();
lines.add(TextUtil.implode(gateIdsForCurrentLine, ", ")); List<String> allPages = this.pagedGateIds();
return lines;
} 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)");
protected static String intToTitleString(int i, boolean addPreviousPageNote, boolean addNextPageNote) return;
{ }
String retVal = ChatColor.DARK_AQUA + "";
if (page > allPages.size() || page < 1) {
if ( i < 26 ) { sendMessage(ChatColor.RED + "The requested page is not availible");
retVal += (char)(i+65); return;
} }
else if ( i == 26 ) {
retVal += "0-9"; String message = TextUtil.titleize("List of all gates (" + page + "/" + allPages.size() + ")") + "\n";
} message += allPages.get(page - 1);
else {
retVal += "!@#$"; sendMessage(message);
} }
if (addPreviousPageNote && addNextPageNote) { private static List<String> linesOfGateIds(List<String> gates) {
retVal += " (more on previous and next page)"; List<String> lines = new ArrayList<String>();
}
else if (addPreviousPageNote) { int index = 0;
retVal += " (more on previous page)"; List<String> gateIdsForCurrentLine = new ArrayList<String>();
} int numCharactersInCurrentLine = 0;
else if (addNextPageNote) {
retVal += " (more on next page)"; while (index < gates.size()) {
} String gateId = gates.get(index);
int gateIdLength = gateId.length() + 2; // actual length + comma + whitespace
return retVal + "\n";
} if (gateIdLength > charactersPerLine && numCharactersInCurrentLine == 0) { // special case: very long gate id
gateIdsForCurrentLine = new ArrayList<String>();
numCharactersInCurrentLine = 0;
/**
* Method for getting a collection of gates the player is allowed to see. while ((gateId.length() + 2) > charactersPerLine) {
*/ int cutPos = charactersPerLine;
protected Collection<Gate> getAllGates()
{ // is the id too long to add comma and whitespace but not longer than the line?
Collection<Gate> gates = Plugin.getPlugin().getGatesManager().allGates(); if (gateId.length() <= charactersPerLine) {
cutPos -= 2;
if (this.sender instanceof Player && Plugin.getPermission() != null) { }
Player p = (Player)this.sender;
lines.add(gateId.substring(0, cutPos));
// create a copy since we cannot iterate over a collection while modifying it! gateId = gateId.substring(cutPos, gateId.length());
Collection<Gate> gatesCopy = new ArrayList<Gate>(gates); }
for (Gate gate : gatesCopy) { gateIdsForCurrentLine.add(gateId);
if (gate.getLocation() != null) { numCharactersInCurrentLine += gateId.length();
boolean permissionAtGateLocation = Plugin.getPermission().has(gate.getLocation().getWorld(), p.getName(), this.requiredPermission); index++;
if (!permissionAtGateLocation) { } else if ((numCharactersInCurrentLine + gateIdLength) <= charactersPerLine) { // gate fits into current line
gates.remove(gate); gateIdsForCurrentLine.add(gateId);
continue; numCharactersInCurrentLine += gateIdLength;
}
} index++;
} else { // the current gate does not fit on the
if (gate.getExit() != null) { lines.add(TextUtil.implode(gateIdsForCurrentLine, ", ") + ", ");
boolean permissionAtGateExit = Plugin.getPermission().has(gate.getExit().getWorld(), p.getName(), this.requiredPermission); gateIdsForCurrentLine = new ArrayList<String>();
if (!permissionAtGateExit) { numCharactersInCurrentLine = 0;
gates.remove(gate); }
} }
}
} lines.add(TextUtil.implode(gateIdsForCurrentLine, ", "));
} return lines;
}
return gates;
} private static String intToTitleString(int i, boolean addPreviousPageNote, boolean addNextPageNote) {
String retVal = ChatColor.DARK_AQUA + "";
/** if (i < 26) {
* Sorts all gates by there first character. retVal += (char) (i + 65);
* Puts gates in corresponding Lists: (all returned lists will be sorted alphabetically) } else if (i == 26) {
* list 0-25: a,b,c,..,z retVal += "0-9";
* list 26: 0-9 } else {
* list 27: other retVal += "!@#$";
*/ }
protected static List<List<String>> gatesSortedByName(Collection<Gate> allGates)
{ if (addPreviousPageNote && addNextPageNote) {
// create the lists retVal += " (more on previous and next page)";
List<List<String>> ids = new ArrayList<List<String>>(); } else if (addPreviousPageNote) {
retVal += " (more on previous page)";
for (int i=0; i<28; i++) { } else if (addNextPageNote) {
ids.add(new ArrayList<String>()); retVal += " (more on next page)";
} }
// put all gates into correct lists return retVal + "\n";
for (Gate gate : allGates) { }
String id = gate.getId();
int first = id.charAt(0); /**
* Method for getting a collection of gates the player is allowed to see.
if (first > 96 && first < 123) { // convert lower case chars */
first -= 97; private Collection<Gate> getAllGates() {
} Collection<Gate> gates = Plugin.getPlugin().getGatesManager().allGates();
else if (first > 64 && first < 91) { // convert upper case chars
first -= 65; if (this.sender instanceof Player && Plugin.getPermission() != null) {
} Player p = (Player) this.sender;
else if (first > 47 && first < 58) { // convert numbers
first = 26; // create a copy since we cannot iterate over a collection while modifying it!
} Collection<Gate> gatesCopy = new ArrayList<Gate>(gates);
else { // everything else
first = 27; for (Gate gate : gatesCopy) {
} if (gate.getLocation() != null) {
boolean permissionAtGateLocation = Plugin.getPermission().has(gate.getLocation().getWorld(), p.getName(), this.requiredPermission);
ids.get(first).add(id); if (!permissionAtGateLocation) {
} gates.remove(gate);
continue;
// sort everything }
for (int i=0; i<28; i++) { }
Collections.sort(ids.get(i));
} if (gate.getExit() != null) {
boolean permissionAtGateExit = Plugin.getPermission().has(gate.getExit().getWorld(), p.getName(), this.requiredPermission);
return ids; if (!permissionAtGateExit) {
} gates.remove(gate);
}
}
/** }
* Returns a list of strings. }
* Each string is the text for a page.
* The maximum number of lines per page is 'linesPerPage' minus 1. return gates;
* Will return an empty list if no gates are availible. }
*/
protected List<String> pagedGateIds() /**
{ * Sorts all gates by there first character.
Collection<Gate> gates = this.getAllGates(); * Puts gates in corresponding Lists: (all returned lists will be sorted alphabetically)
* list 0-25: a,b,c,..,z
if (gates.size() == 0) { * list 26: 0-9
return null; * list 27: other
} */
private static List<List<String>> gatesSortedByName(Collection<Gate> allGates) {
List<List<String>> gatesSortedByName = gatesSortedByName(gates); // create the lists
List<String> allPages = new ArrayList<String>(); List<List<String>> ids = new ArrayList<List<String>>();
int linesLeftOnPage = linesPerPage - 1;
String currentPageString = ""; for (int i = 0; i < 28; i++) {
ids.add(new ArrayList<String>());
for (int i=0; i<gatesSortedByName.size(); i++) { }
List<String> currentGates = gatesSortedByName.get(i); // put all gates into correct lists
for (Gate gate : allGates) {
if(currentGates.isEmpty()) { String id = gate.getId();
continue; int first = id.charAt(0);
}
if (first > 96 && first < 123) { // convert lower case chars
List<String> currentGatesAsLines = linesOfGateIds(currentGates); first -= 97;
boolean moreGatesOnLastPage = false; } else if (first > 64 && first < 91) { // convert upper case chars
first -= 65;
while (!currentGatesAsLines.isEmpty()) { } else if (first > 47 && first < 58) { // convert numbers
first = 26;
if (linesLeftOnPage < 2) { } else { // everything else
currentPageString = currentPageString.substring(0, currentPageString.length()-2); // remove newlines add the end of the page first = 27;
allPages.add(currentPageString); }
currentPageString = "";
ids.get(first).add(id);
linesLeftOnPage = linesPerPage - 1; }
}
// sort everything
// calculate number of lines to add to current page for (int i = 0; i < 28; i++) {
int linesNecessaryForCurrentGates = currentGatesAsLines.size(); Collections.sort(ids.get(i));
int linesToFill; }
boolean moreGatesOnNextPage;
return ids;
if (linesNecessaryForCurrentGates < linesLeftOnPage) { }
linesToFill = linesNecessaryForCurrentGates;
moreGatesOnNextPage = false; /**
} * Returns a list of strings.
else { * Each string is the text for a page.
linesToFill = linesLeftOnPage -1; * The maximum number of lines per page is 'linesPerPage' minus 1.
moreGatesOnNextPage = true; * Will return an empty list if no gates are availible.
} */
private List<String> pagedGateIds() {
// add title Collection<Gate> gates = this.getAllGates();
currentPageString += intToTitleString(i, moreGatesOnLastPage, moreGatesOnNextPage);
currentPageString += ChatColor.AQUA; if (gates.size() == 0) {
linesLeftOnPage--; return null;
}
// add gate lines
for (int j=0; j<linesToFill; j++) { List<List<String>> gatesSortedByName = gatesSortedByName(gates);
currentPageString += currentGatesAsLines.get(j) + "\n"; List<String> allPages = new ArrayList<String>();
} int linesLeftOnPage = linesPerPage - 1;
String currentPageString = "";
// remove lines added
for (int j=0; j<linesToFill; j++) { for (int i = 0; i < gatesSortedByName.size(); i++) {
currentGatesAsLines.remove(0);
} List<String> currentGates = gatesSortedByName.get(i);
// cleanup if (currentGates.isEmpty()) {
continue;
}
List<String> 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; moreGatesOnLastPage = linesNecessaryForCurrentGates >= linesLeftOnPage;
linesLeftOnPage -= linesToFill; 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<String> allPages = this.pagedGateIds();
if (allPages == null) { // no gates exist // add the last page
sendMessage(ChatColor.RED + "There are no gates yet. " + ChatColor.RESET + if (!currentPageString.isEmpty()) {
"(Note that you might not be allowed to get information about certain gates)"); currentPageString = currentPageString.substring(0, currentPageString.length() - 2); // remove newlines add the end of the page
return; allPages.add(currentPageString);
} }
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); return allPages;
} }
private int getPageParameter() {
int page = 1;
try {
page = new Integer(parameters.get(0));
} catch (Exception ignored) {
}
return page;
}
} }

View File

@ -16,7 +16,6 @@
*/ */
package de.craftinc.gates.commands; package de.craftinc.gates.commands;
import java.util.Set; import java.util.Set;
import de.craftinc.gates.util.GateBlockChangeSender; import de.craftinc.gates.util.GateBlockChangeSender;
@ -26,59 +25,46 @@ import org.bukkit.Location;
import de.craftinc.gates.Plugin; import de.craftinc.gates.Plugin;
import org.bukkit.block.Block; import org.bukkit.block.Block;
public class CommandLocation extends BaseLocationCommand {
public class CommandLocation extends BaseLocationCommand public CommandLocation() {
{ aliases.add("location");
public CommandLocation() aliases.add("lo");
{
aliases.add("location"); requiredParameters.add("id");
aliases.add("lo"); helpDescription = "Set the entrance of the gate to your current location.";
requiredPermission = Plugin.permissionManage;
requiredParameters.add("id");
needsPermissionAtCurrentLocation = true;
helpDescription = "Set the entrance of the gate to your current location."; shouldPersistToDisk = true;
senderMustBePlayer = true;
requiredPermission = Plugin.permissionManage; }
needsPermissionAtCurrentLocation = true; public void perform() {
shouldPersistToDisk = true; Location playerLocation = getValidPlayerLocation();
senderMustBePlayer = true;
} if (playerLocation == null) {
sendMessage("There is not enough room for a gate to open here");
return;
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(); Location oldLocation = gate.getLocation();
Set<Location> oldGateBlockLocations = gate.getGateBlockLocations(); Set<Location> oldGateBlockLocations = gate.getGateBlockLocations();
Set<Block> oldFrameBlocks = gate.getGateFrameBlocks(); Set<Block> oldFrameBlocks = gate.getGateFrameBlocks();
try try {
{
if (gate.isOpen()) { if (gate.isOpen()) {
GateBlockChangeSender.updateGateBlocks(gate, true); GateBlockChangeSender.updateGateBlocks(gate, true);
} }
gate.setLocation(playerLocation); gate.setLocation(playerLocation);
sendMessage(ChatColor.GREEN + "The location of '" + gate.getId() + "' is now at your current location.");
sendMessage(ChatColor.GREEN + "The location of '" + gate.getId() + "' is now at your current location."); } catch (Exception e) {
}
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(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)); sendMessage(new CommandOpen().getUsageTemplate(true, true));
} } finally {
finally {
Plugin.getPlugin().getGatesManager().handleGateLocationChange(gate, oldLocation, oldGateBlockLocations, oldFrameBlocks); Plugin.getPlugin().getGatesManager().handleGateLocationChange(gate, oldLocation, oldGateBlockLocations, oldFrameBlocks);
GateBlockChangeSender.updateGateBlocks(gate); GateBlockChangeSender.updateGateBlocks(gate);
} }
} }
} }

View File

@ -1,6 +1,5 @@
package de.craftinc.gates.commands; package de.craftinc.gates.commands;
import de.craftinc.gates.Gate; import de.craftinc.gates.Gate;
import de.craftinc.gates.GatesManager; import de.craftinc.gates.GatesManager;
import de.craftinc.gates.Plugin; import de.craftinc.gates.Plugin;
@ -10,33 +9,27 @@ import de.craftinc.gates.util.TextUtil;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Set; import java.util.Set;
public class CommandNearby extends BaseLocationCommand public class CommandNearby extends BaseLocationCommand {
{
public CommandNearby() public CommandNearby() {
{
aliases.add("nearby"); aliases.add("nearby");
aliases.add("nb"); aliases.add("nb");
helpDescription = "Highlight nearby gates"; helpDescription = "Highlight nearby gates";
requiredPermission = Plugin.permissionInfo; requiredPermission = Plugin.permissionInfo;
needsPermissionAtCurrentLocation = true; needsPermissionAtCurrentLocation = true;
shouldPersistToDisk = false; shouldPersistToDisk = false;
senderMustBePlayer = true; senderMustBePlayer = true;
hasGateParam = false; hasGateParam = false;
} }
public void perform() {
public void perform()
{
GatesManager manager = Plugin.getPlugin().getGatesManager(); GatesManager manager = Plugin.getPlugin().getGatesManager();
Set<Gate> nearbyGates = manager.getNearbyGates(player.getLocation().getChunk()); Set<Gate> nearbyGates = manager.getNearbyGates(player.getLocation().getChunk());
if (nearbyGates == null) { if (nearbyGates == null) {
player.sendMessage("There are no gates near you!"); player.sendMessage("There are no gates near you!");
} } else {
else {
GateBlockChangeSender.temporaryHighlightGatesFrames(player, nearbyGates); GateBlockChangeSender.temporaryHighlightGatesFrames(player, nearbyGates);
ArrayList<String> gateNames = new ArrayList<String>(); ArrayList<String> gateNames = new ArrayList<String>();
@ -44,10 +37,7 @@ public class CommandNearby extends BaseLocationCommand
for (Gate g : nearbyGates) { for (Gate g : nearbyGates) {
gateNames.add(g.getId()); gateNames.add(g.getId());
} }
player.sendMessage("Nearby gates: " + TextUtil.implode(gateNames, ", ")); player.sendMessage("Nearby gates: " + TextUtil.implode(gateNames, ", "));
} }
} }
} }

View File

@ -23,61 +23,48 @@ import de.craftinc.gates.Gate;
import de.craftinc.gates.GatesManager; import de.craftinc.gates.GatesManager;
import de.craftinc.gates.Plugin; 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"); aliases.add("n");
requiredParameters.add("id"); 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.");
senderMustBePlayer = true;
Location playerLocation = getValidPlayerLocation(); hasGateParam = false;
helpDescription = "Create a gate at your current location.";
if (playerLocation != null) { requiredPermission = Plugin.permissionManage;
needsPermissionAtCurrentLocation = true;
try { shouldPersistToDisk = true;
gate.setLocation(playerLocation); senderMustBePlayer = true;
sendMessage(ChatColor.AQUA + "The gates location has been set to your current location."); }
}
catch (Exception ignored) {} public void perform() {
} String id = parameters.get(0);
else GatesManager gatesManager = Plugin.getPlugin().getGatesManager();
{
sendMessage(ChatColor.RED + "Your location is invalid!" + ChatColor.AQUA + "Go somewhere else and execute:"); if (gatesManager.gateExists(id)) {
sendMessage(new CommandLocation().getUsageTemplate(true, true)); 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); gatesManager.handleNewGate(gate);
} }
} }

View File

@ -16,39 +16,31 @@
*/ */
package de.craftinc.gates.commands; package de.craftinc.gates.commands;
import de.craftinc.gates.util.GateBlockChangeSender; import de.craftinc.gates.util.GateBlockChangeSender;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import de.craftinc.gates.Plugin; import de.craftinc.gates.Plugin;
public class CommandOpen extends BaseCommand {
public class CommandOpen extends BaseCommand public CommandOpen() {
{ aliases.add("open");
aliases.add("o");
public CommandOpen()
{ requiredParameters.add("id");
aliases.add("open"); helpDescription = "Open a gate so players can use it.";
aliases.add("o"); requiredPermission = Plugin.permissionManage;
requiredParameters.add("id"); needsPermissionAtCurrentLocation = false;
shouldPersistToDisk = true;
helpDescription = "Open a gate so players can use it."; senderMustBePlayer = false;
}
requiredPermission = Plugin.permissionManage;
public void perform() {
needsPermissionAtCurrentLocation = false; try {
shouldPersistToDisk = true;
senderMustBePlayer = false;
}
public void perform()
{
try {
boolean needsGateManagerUpdate = false; boolean needsGateManagerUpdate = false;
if (gate.getGateBlockLocations().isEmpty()) { if (gate.getGateBlockLocations().isEmpty()) {
needsGateManagerUpdate = true; needsGateManagerUpdate = true;
} }
@ -60,11 +52,9 @@ public class CommandOpen extends BaseCommand
Plugin.getPlugin().getGatesManager().handleGateLocationChange(gate, null, null, null); Plugin.getPlugin().getGatesManager().handleGateLocationChange(gate, null, null, null);
} }
sendMessage(ChatColor.GREEN + "The gate was opened."); sendMessage(ChatColor.GREEN + "The gate was opened.");
} } catch (Exception e) {
catch (Exception e) { sendMessage(ChatColor.RED + e.getMessage());
sendMessage(ChatColor.RED + e.getMessage()); }
} }
}
} }

View File

@ -21,33 +21,29 @@ import org.bukkit.ChatColor;
import de.craftinc.gates.Plugin; import de.craftinc.gates.Plugin;
public class CommandRemove extends BaseCommand {
public class CommandRemove extends BaseCommand public CommandRemove() {
{ aliases.add("delete");
public CommandRemove() aliases.add("del");
{ aliases.add("remove");
aliases.add("delete");
aliases.add("del"); requiredParameters.add("id");
aliases.add("remove");
senderMustBePlayer = false;
requiredParameters.add("id"); helpDescription = "Removes the gate from the game.";
senderMustBePlayer = false; requiredPermission = Plugin.permissionManage;
helpDescription = "Removes the gate from the game.";
needsPermissionAtCurrentLocation = false;
requiredPermission = Plugin.permissionManage; shouldPersistToDisk = true;
needsPermissionAtCurrentLocation = false; senderMustBePlayer = false;
shouldPersistToDisk = true; }
senderMustBePlayer = false; public void perform() {
} Plugin.getPlugin().getGatesManager().handleDeletion(gate);
public void perform()
{
Plugin.getPlugin().getGatesManager().handleDeletion(gate);
GateBlockChangeSender.updateGateBlocks(gate, true); GateBlockChangeSender.updateGateBlocks(gate, true);
sendMessage(ChatColor.GREEN + "Gate with id '" + gate.getId() + "' was deleted."); sendMessage(ChatColor.GREEN + "Gate with id '" + gate.getId() + "' was deleted.");
} }
} }

View File

@ -21,46 +21,41 @@ import org.bukkit.ChatColor;
import de.craftinc.gates.GatesManager; import de.craftinc.gates.GatesManager;
import de.craftinc.gates.Plugin; import de.craftinc.gates.Plugin;
public class CommandRename extends BaseCommand {
public class CommandRename extends BaseCommand public CommandRename() {
{ aliases.add("rename");
public CommandRename() aliases.add("rn");
{
aliases.add("rename");
aliases.add("rn");
hasGateParam = true; hasGateParam = true;
senderMustBePlayer = false; 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 + ".");
}
}
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 + ".");
}
}
} }

View File

@ -21,38 +21,27 @@ import org.bukkit.ChatColor;
import de.craftinc.gates.Plugin; import de.craftinc.gates.Plugin;
public class CommandUnhide extends BaseCommand {
public class CommandUnhide extends BaseCommand public CommandUnhide() {
{ aliases.add("unhide");
aliases.add("u");
public CommandUnhide()
{ requiredParameters.add("id");
aliases.add("unhide"); helpDescription = "Make that gate visible";
aliases.add("u"); requiredPermission = Plugin.permissionManage;
needsPermissionAtCurrentLocation = false;
requiredParameters.add("id"); shouldPersistToDisk = true;
senderMustBePlayer = false;
helpDescription = "Make that gate visible"; }
requiredPermission = Plugin.permissionManage; public void perform() {
try {
needsPermissionAtCurrentLocation = false; gate.setHidden(false);
shouldPersistToDisk = true;
senderMustBePlayer = false;
}
public void perform()
{
try
{
gate.setHidden(false);
GateBlockChangeSender.updateGateBlocks(gate); GateBlockChangeSender.updateGateBlocks(gate);
sendMessage(ChatColor.GREEN + "The gate " + gate.getId() + " is now visible."); sendMessage(ChatColor.GREEN + "The gate " + gate.getId() + " is now visible.");
} } catch (Exception e) {
catch (Exception e) { sendMessage(ChatColor.RED + e.getMessage());
sendMessage(ChatColor.RED + e.getMessage()); }
} }
} }
}