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,8 +30,8 @@ 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> aliases = new ArrayList<String>();
protected List<String> requiredParameters = new ArrayList<String>(); protected List<String> requiredParameters = new ArrayList<String>();
protected List<String> optionalParameters = new ArrayList<String>(); protected List<String> optionalParameters = new ArrayList<String>();
@ -70,11 +71,16 @@ public abstract class BaseCommand
this.perform(); this.perform();
if (this.shouldPersistToDisk && Plugin.getPlugin().getConfig().getBoolean(ConfigurationUtil.confSaveOnChangesKey)) { if (this.shouldPersistToDisk && getSaveOnChanges()) {
Plugin.getPlugin().getGatesManager().saveGatesToDisk(); Plugin.getPlugin().getGatesManager().saveGatesToDisk();
} }
} }
private boolean getSaveOnChanges() {
FileConfiguration config = Plugin.getPlugin().getConfig();
return config.getBoolean(ConfigurationUtil.confSaveOnChangesKey);
}
abstract protected void perform(); abstract protected void perform();
@ -91,8 +97,7 @@ public abstract class BaseCommand
} }
protected boolean validateCall() protected boolean validateCall() {
{
boolean allParametersThere = parameters.size() >= requiredParameters.size(); boolean allParametersThere = parameters.size() >= requiredParameters.size();
boolean senderIsPlayer = this.sender instanceof Player; boolean senderIsPlayer = this.sender instanceof Player;
boolean hasGateParameter = false; boolean hasGateParameter = false;
@ -107,20 +112,17 @@ public abstract class BaseCommand
if (this.senderMustBePlayer && !senderIsPlayer) { if (this.senderMustBePlayer && !senderIsPlayer) {
sendMessage(ChatColor.RED + "This command can only be used by ingame players."); sendMessage(ChatColor.RED + "This command can only be used by ingame players.");
valid = false; valid = false;
} } else {
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;
} }
} }
@ -129,14 +131,12 @@ public abstract class BaseCommand
} }
protected boolean setGateUsingParameter(String param) protected boolean setGateUsingParameter(String param) {
{
GatesManager gateManager = Plugin.getPlugin().getGatesManager(); GatesManager gateManager = Plugin.getPlugin().getGatesManager();
if (!gateManager.gateExists(param)) { if (!gateManager.gateExists(param)) {
return false; return false;
} } else {
else {
gate = gateManager.getGateWithId(param); gate = gateManager.getGateWithId(param);
return true; return true;
} }
@ -146,8 +146,7 @@ public abstract class BaseCommand
/** /**
* This will return false if a gate is required for this command but this.gate == null. * This will return false if a gate is required for this command but this.gate == null.
*/ */
protected boolean hasPermission() protected boolean hasPermission() {
{
if (Plugin.getPermission() == null) { // fallback - use the standard bukkit permission system if (Plugin.getPermission() == null) { // fallback - use the standard bukkit permission system
return this.sender.hasPermission(this.requiredPermission); return this.sender.hasPermission(this.requiredPermission);
} }
@ -165,24 +164,19 @@ public abstract class BaseCommand
if (this.hasGateParam) { if (this.hasGateParam) {
hasPermission = this.hasPermissionAtGateLocationAndExit(p); hasPermission = this.hasPermissionAtGateLocationAndExit(p);
} } else {
else {
hasPermission = Plugin.getPermission().has(p.getWorld(), p.getName(), this.requiredPermission); hasPermission = Plugin.getPermission().has(p.getWorld(), p.getName(), this.requiredPermission);
} }
} } else if (this.requiredPermission.equals(Plugin.permissionUse)) {
else if (this.requiredPermission.equals(Plugin.permissionUse) ) {
hasPermission = this.hasPermissionAtGateLocationAndExit(p); hasPermission = this.hasPermissionAtGateLocationAndExit(p);
} } else if (this.requiredPermission.equals(Plugin.permissionManage)) {
else if (this.requiredPermission.equals(Plugin.permissionManage)) {
if (this.needsPermissionAtCurrentLocation && this.hasGateParam) { if (this.needsPermissionAtCurrentLocation && this.hasGateParam) {
boolean hasPersmissionAtCurrentLocation = Plugin.getPermission().has(p.getWorld(), p.getName(), this.requiredPermission); boolean hasPersmissionAtCurrentLocation = Plugin.getPermission().has(p.getWorld(), p.getName(), this.requiredPermission);
hasPermission = hasPersmissionAtCurrentLocation && this.hasPermissionAtGateLocationAndExit(p); hasPermission = hasPersmissionAtCurrentLocation && this.hasPermissionAtGateLocationAndExit(p);
} } else if (this.needsPermissionAtCurrentLocation) {
else if (this.needsPermissionAtCurrentLocation) {
hasPermission = Plugin.getPermission().has(p.getWorld(), p.getName(), this.requiredPermission); hasPermission = Plugin.getPermission().has(p.getWorld(), p.getName(), this.requiredPermission);
} } else {
else {
hasPermission = this.hasPermissionAtGateLocationAndExit(p); hasPermission = this.hasPermissionAtGateLocationAndExit(p);
} }
} }
@ -191,9 +185,8 @@ public abstract class BaseCommand
} }
protected boolean hasPermissionAtGateLocationAndExit(Player p) protected boolean hasPermissionAtGateLocationAndExit(Player p) {
{ if (this.gate == null || p == null) {
if (this.gate == null || p == null) { // make sure we don't run into a nullpointer exception
return false; return false;
} }
@ -207,8 +200,7 @@ public abstract class BaseCommand
// -------------------------------------------- // // -------------------------------------------- //
// Help and usage description // Help and usage description
// -------------------------------------------- // // -------------------------------------------- //
protected String getUsageTemplate(boolean withColor, boolean withDescription) protected String getUsageTemplate(boolean withColor, boolean withDescription) {
{
String ret = ""; String ret = "";
if (withColor) { if (withColor) {
@ -239,14 +231,12 @@ public abstract class BaseCommand
if (withColor) { if (withColor) {
ret += ChatColor.YELLOW; ret += ChatColor.YELLOW;
} }
ret += this.helpDescription; ret += this.helpDescription;
} }
return ret; return ret;
} }
protected String getUsageTemplate(boolean withColor) protected String getUsageTemplate(boolean withColor) {
{
return getUsageTemplate(withColor, false); return getUsageTemplate(withColor, false);
} }
} }

View File

@ -21,10 +21,9 @@ 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();
@ -32,8 +31,7 @@ public abstract class BaseLocationCommand extends BaseCommand
if (playerBlock.getType() == Material.AIR) { if (playerBlock.getType() == Material.AIR) {
return player.getLocation(); return player.getLocation();
} } else if (upBlock.getType() == Material.AIR) {
else if (upBlock.getType() == Material.AIR) {
return new Location(player.getLocation().getWorld(), return new Location(player.getLocation().getWorld(),
player.getLocation().getX(), player.getLocation().getX(),
player.getLocation().getY() + 1, player.getLocation().getY() + 1,

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() public CommandClose() {
{
aliases.add("close"); aliases.add("close");
aliases.add("c"); aliases.add("c");
requiredParameters.add("id"); requiredParameters.add("id");
helpDescription = "Closes a gate to prevent players from using it."; helpDescription = "Closes a gate to prevent players from using it.";
requiredPermission = Plugin.permissionManage; requiredPermission = Plugin.permissionManage;
needsPermissionAtCurrentLocation = false; needsPermissionAtCurrentLocation = false;
shouldPersistToDisk = true; shouldPersistToDisk = true;
senderMustBePlayer = false; senderMustBePlayer = false;
} }
@Override @Override
public void perform() public void perform() {
{ try {
try
{
gate.setOpen(false); gate.setOpen(false);
GateBlockChangeSender.updateGateBlocks(gate); GateBlockChangeSender.updateGateBlocks(gate);
sendMessage(ChatColor.GREEN + "The gate was closed."); sendMessage(ChatColor.GREEN + "The gate was closed.");
} } catch (Exception e) {
catch(Exception e)
{
sendMessage(ChatColor.RED + "Opening the gate failed! See server log for more information"); sendMessage(ChatColor.RED + "Opening 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

@ -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,11 +24,9 @@ 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() {
{
public CommandExit()
{
aliases.add("exit"); aliases.add("exit");
aliases.add("e"); aliases.add("e");
@ -44,16 +42,13 @@ public class CommandExit extends BaseCommand
} }
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.");
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());
@ -61,4 +56,3 @@ public class CommandExit extends BaseCommand
} }
} }
} }

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,12 +23,11 @@ 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>();
@ -63,8 +62,7 @@ public class CommandHelp extends BaseCommand
} }
public CommandHelp() public CommandHelp() {
{
aliases.add("help"); aliases.add("help");
aliases.add("?"); aliases.add("?");
@ -80,20 +78,17 @@ public class CommandHelp extends BaseCommand
} }
public void perform() public void perform() {
{
int page; int page;
if (parameters.size() > 0) { if (parameters.size() > 0) {
try { try {
page = Integer.parseInt(parameters.get(0)); page = Integer.parseInt(parameters.get(0));
} } catch (NumberFormatException e) {
catch (NumberFormatException e) {
// wasn't an integer // wasn't an integer
page = 1; page = 1;
} }
} } else {
else {
page = 1; page = 1;
} }

View File

@ -24,10 +24,9 @@ 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");
@ -43,14 +42,12 @@ public class CommandHide extends BaseCommand
} }
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,11 +23,9 @@ 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() {
{
public CommandInfo()
{
aliases.add("info"); aliases.add("info");
aliases.add("i"); aliases.add("i");
@ -45,8 +42,7 @@ public class CommandInfo extends BaseCommand
} }
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))) {
@ -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) {

View File

@ -28,20 +28,17 @@ 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; /* this is actually not true. the font used by Minecraft is not
protected static final int charactersPerLine = 52; /* this is actually no true. the monospaced. but there seems to be no (easy) way to calculate
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. the drawing-size of a string.
*/ */
private static final int charactersPerLine = 52;
public CommandList() public CommandList() {
{
aliases.add("list"); aliases.add("list");
aliases.add("ls"); aliases.add("ls");
@ -56,27 +53,43 @@ public class CommandList extends BaseCommand
senderMustBePlayer = false; senderMustBePlayer = false;
} }
public void perform() {
int page = this.getPageParameter();
List<String> allPages = this.pagedGateIds();
protected static List<String> linesOfGateIds(List<String> gates) 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<String> linesOfGateIds(List<String> gates) {
List<String> lines = new ArrayList<String>(); List<String> lines = new ArrayList<String>();
int index = 0; int index = 0;
List<String> gateIdsForCurrentLine = new ArrayList<String>(); List<String> gateIdsForCurrentLine = new ArrayList<String>();
int numCharactersInCurrentLine = 0; int numCharactersInCurrentLine = 0;
while (index < gates.size()) { while (index < gates.size()) {
String gateId = gates.get(index); String gateId = gates.get(index);
int gateIdLength = gateId.length() + 2; // actual length + comma + whitespace int gateIdLength = gateId.length() + 2; // actual length + comma + whitespace
// special case: very long gate id if (gateIdLength > charactersPerLine && numCharactersInCurrentLine == 0) { // special case: very long gate id
if (gateIdLength > charactersPerLine && numCharactersInCurrentLine == 0) {
gateIdsForCurrentLine = new ArrayList<String>(); gateIdsForCurrentLine = new ArrayList<String>();
numCharactersInCurrentLine = 0; numCharactersInCurrentLine = 0;
while ((gateId.length() + 2) > charactersPerLine) { while ((gateId.length() + 2) > charactersPerLine) {
int cutPos = charactersPerLine; int cutPos = charactersPerLine;
// is the id too long to add comma and whitespace but not longer than the line? // is the id too long to add comma and whitespace but not longer than the line?
@ -86,25 +99,18 @@ public class CommandList extends BaseCommand
lines.add(gateId.substring(0, cutPos)); lines.add(gateId.substring(0, cutPos));
gateId = gateId.substring(cutPos, gateId.length()); gateId = gateId.substring(cutPos, gateId.length());
} }
gateIdsForCurrentLine.add(gateId); gateIdsForCurrentLine.add(gateId);
numCharactersInCurrentLine += gateId.length(); numCharactersInCurrentLine += gateId.length();
index++; index++;
} } else if ((numCharactersInCurrentLine + gateIdLength) <= charactersPerLine) { // gate fits into current line
// gate fits into current line
else if ((numCharactersInCurrentLine + gateIdLength) <= charactersPerLine) {
gateIdsForCurrentLine.add(gateId); gateIdsForCurrentLine.add(gateId);
numCharactersInCurrentLine += gateIdLength; numCharactersInCurrentLine += gateIdLength;
index++; index++;
} } else { // the current gate does not fit on the
// the current gate does not fit on the
else {
lines.add(TextUtil.implode(gateIdsForCurrentLine, ", ") + ", "); lines.add(TextUtil.implode(gateIdsForCurrentLine, ", ") + ", ");
gateIdsForCurrentLine = new ArrayList<String>(); gateIdsForCurrentLine = new ArrayList<String>();
@ -116,40 +122,32 @@ public class CommandList extends BaseCommand
return lines; return lines;
} }
private static String intToTitleString(int i, boolean addPreviousPageNote, boolean addNextPageNote) {
protected static String intToTitleString(int i, boolean addPreviousPageNote, boolean addNextPageNote)
{
String retVal = ChatColor.DARK_AQUA + ""; String retVal = ChatColor.DARK_AQUA + "";
if (i < 26) { if (i < 26) {
retVal += (char) (i + 65); retVal += (char) (i + 65);
} } else if (i == 26) {
else if ( i == 26 ) {
retVal += "0-9"; retVal += "0-9";
} } else {
else {
retVal += "!@#$"; retVal += "!@#$";
} }
if (addPreviousPageNote && addNextPageNote) { if (addPreviousPageNote && addNextPageNote) {
retVal += " (more on previous and next page)"; retVal += " (more on previous and next page)";
} } else if (addPreviousPageNote) {
else if (addPreviousPageNote) {
retVal += " (more on previous page)"; retVal += " (more on previous page)";
} } else if (addNextPageNote) {
else if (addNextPageNote) {
retVal += " (more on next page)"; retVal += " (more on next page)";
} }
return retVal + "\n"; return retVal + "\n";
} }
/** /**
* Method for getting a collection of gates the player is allowed to see. * Method for getting a collection of gates the player is allowed to see.
*/ */
protected Collection<Gate> getAllGates() private Collection<Gate> getAllGates() {
{
Collection<Gate> gates = Plugin.getPlugin().getGatesManager().allGates(); Collection<Gate> gates = Plugin.getPlugin().getGatesManager().allGates();
if (this.sender instanceof Player && Plugin.getPermission() != null) { if (this.sender instanceof Player && Plugin.getPermission() != null) {
@ -159,7 +157,6 @@ public class CommandList extends BaseCommand
Collection<Gate> gatesCopy = new ArrayList<Gate>(gates); Collection<Gate> gatesCopy = new ArrayList<Gate>(gates);
for (Gate gate : gatesCopy) { for (Gate gate : gatesCopy) {
if (gate.getLocation() != null) { if (gate.getLocation() != null) {
boolean permissionAtGateLocation = Plugin.getPermission().has(gate.getLocation().getWorld(), p.getName(), this.requiredPermission); boolean permissionAtGateLocation = Plugin.getPermission().has(gate.getLocation().getWorld(), p.getName(), this.requiredPermission);
if (!permissionAtGateLocation) { if (!permissionAtGateLocation) {
@ -169,7 +166,6 @@ public class CommandList extends BaseCommand
} }
if (gate.getExit() != null) { if (gate.getExit() != null) {
boolean permissionAtGateExit = Plugin.getPermission().has(gate.getExit().getWorld(), p.getName(), this.requiredPermission); boolean permissionAtGateExit = Plugin.getPermission().has(gate.getExit().getWorld(), p.getName(), this.requiredPermission);
if (!permissionAtGateExit) { if (!permissionAtGateExit) {
gates.remove(gate); gates.remove(gate);
@ -181,7 +177,6 @@ public class CommandList extends BaseCommand
return gates; return gates;
} }
/** /**
* Sorts all gates by there first character. * Sorts all gates by there first character.
* Puts gates in corresponding Lists: (all returned lists will be sorted alphabetically) * Puts gates in corresponding Lists: (all returned lists will be sorted alphabetically)
@ -189,8 +184,7 @@ public class CommandList extends BaseCommand
* list 26: 0-9 * list 26: 0-9
* list 27: other * list 27: other
*/ */
protected static List<List<String>> gatesSortedByName(Collection<Gate> allGates) private static List<List<String>> gatesSortedByName(Collection<Gate> allGates) {
{
// create the lists // create the lists
List<List<String>> ids = new ArrayList<List<String>>(); List<List<String>> ids = new ArrayList<List<String>>();
@ -205,14 +199,11 @@ public class CommandList extends BaseCommand
if (first > 96 && first < 123) { // convert lower case chars if (first > 96 && first < 123) { // convert lower case chars
first -= 97; first -= 97;
} } else if (first > 64 && first < 91) { // convert upper case chars
else if (first > 64 && first < 91) { // convert upper case chars
first -= 65; first -= 65;
} } else if (first > 47 && first < 58) { // convert numbers
else if (first > 47 && first < 58) { // convert numbers
first = 26; first = 26;
} } else { // everything else
else { // everything else
first = 27; first = 27;
} }
@ -227,15 +218,13 @@ public class CommandList extends BaseCommand
return ids; return ids;
} }
/** /**
* Returns a list of strings. * Returns a list of strings.
* Each string is the text for a page. * Each string is the text for a page.
* The maximum number of lines per page is 'linesPerPage' minus 1. * The maximum number of lines per page is 'linesPerPage' minus 1.
* Will return an empty list if no gates are availible. * Will return an empty list if no gates are availible.
*/ */
protected List<String> pagedGateIds() private List<String> pagedGateIds() {
{
Collection<Gate> gates = this.getAllGates(); Collection<Gate> gates = this.getAllGates();
if (gates.size() == 0) { if (gates.size() == 0) {
@ -276,8 +265,7 @@ public class CommandList extends BaseCommand
if (linesNecessaryForCurrentGates < linesLeftOnPage) { if (linesNecessaryForCurrentGates < linesLeftOnPage) {
linesToFill = linesNecessaryForCurrentGates; linesToFill = linesNecessaryForCurrentGates;
moreGatesOnNextPage = false; moreGatesOnNextPage = false;
} } else {
else {
linesToFill = linesLeftOnPage - 1; linesToFill = linesLeftOnPage - 1;
moreGatesOnNextPage = true; moreGatesOnNextPage = true;
} }
@ -313,39 +301,14 @@ public class CommandList extends BaseCommand
} }
protected int getPageParameter() private int getPageParameter() {
{
int page = 1; int page = 1;
try { try {
page = new Integer(parameters.get(0)); page = new Integer(parameters.get(0));
} catch (Exception ignored) {
} }
catch (Exception ignored) { }
return page; return page;
} }
public void perform()
{
int page = this.getPageParameter();
List<String> 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);
}
}

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,18 +25,14 @@ 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() {
{
public CommandLocation()
{
aliases.add("location"); aliases.add("location");
aliases.add("lo"); aliases.add("lo");
requiredParameters.add("id"); requiredParameters.add("id");
helpDescription = "Set the entrance of the gate to your current location."; helpDescription = "Set the entrance of the gate to your current location.";
requiredPermission = Plugin.permissionManage; requiredPermission = Plugin.permissionManage;
needsPermissionAtCurrentLocation = true; needsPermissionAtCurrentLocation = true;
@ -45,13 +40,10 @@ public class CommandLocation extends BaseLocationCommand
senderMustBePlayer = true; senderMustBePlayer = true;
} }
public void perform() {
public void perform()
{
Location playerLocation = getValidPlayerLocation(); Location playerLocation = getValidPlayerLocation();
if (playerLocation == null) if (playerLocation == null) {
{
sendMessage("There is not enough room for a gate to open here"); sendMessage("There is not enough room for a gate to open here");
return; return;
} }
@ -60,25 +52,19 @@ public class CommandLocation extends BaseLocationCommand
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,11 +23,9 @@ 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() {
{
public CommandNew()
{
aliases.add("new"); aliases.add("new");
aliases.add("n"); aliases.add("n");
@ -35,20 +33,14 @@ public class CommandNew extends BaseLocationCommand
senderMustBePlayer = true; senderMustBePlayer = true;
hasGateParam = false; hasGateParam = false;
helpDescription = "Create a gate at your current location."; helpDescription = "Create a gate at your current location.";
requiredPermission = Plugin.permissionManage; requiredPermission = Plugin.permissionManage;
needsPermissionAtCurrentLocation = true; needsPermissionAtCurrentLocation = true;
shouldPersistToDisk = true; shouldPersistToDisk = true;
senderMustBePlayer = true; senderMustBePlayer = true;
} }
public void perform() {
public void perform()
{
String id = parameters.get(0); String id = parameters.get(0);
GatesManager gatesManager = Plugin.getPlugin().getGatesManager(); GatesManager gatesManager = Plugin.getPlugin().getGatesManager();
@ -60,19 +52,15 @@ public class CommandNew extends BaseLocationCommand
gate = new Gate(id); gate = new Gate(id);
sendMessage(ChatColor.GREEN + "Gate with id '" + id + "' was created."); sendMessage(ChatColor.GREEN + "Gate with id '" + id + "' was created.");
Location playerLocation = getValidPlayerLocation(); Location playerLocation = getValidPlayerLocation();
if (playerLocation != null) { if (playerLocation != null) {
try { try {
gate.setLocation(playerLocation); gate.setLocation(playerLocation);
sendMessage(ChatColor.AQUA + "The gates location has been set to your current location."); sendMessage(ChatColor.AQUA + "The gates location has been set to your current location.");
} catch (Exception ignored) {
} }
catch (Exception ignored) {} } else {
}
else
{
sendMessage(ChatColor.RED + "Your location is invalid!" + ChatColor.AQUA + "Go somewhere else and execute:"); sendMessage(ChatColor.RED + "Your location is invalid!" + ChatColor.AQUA + "Go somewhere else and execute:");
sendMessage(new CommandLocation().getUsageTemplate(true, true)); sendMessage(new CommandLocation().getUsageTemplate(true, true));
} }
@ -80,4 +68,3 @@ public class CommandNew extends BaseLocationCommand
gatesManager.handleNewGate(gate); gatesManager.handleNewGate(gate);
} }
} }

View File

@ -16,25 +16,19 @@
*/ */
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() {
{
public CommandOpen()
{
aliases.add("open"); aliases.add("open");
aliases.add("o"); aliases.add("o");
requiredParameters.add("id"); requiredParameters.add("id");
helpDescription = "Open a gate so players can use it."; helpDescription = "Open a gate so players can use it.";
requiredPermission = Plugin.permissionManage; requiredPermission = Plugin.permissionManage;
needsPermissionAtCurrentLocation = false; needsPermissionAtCurrentLocation = false;
@ -42,9 +36,7 @@ public class CommandOpen extends BaseCommand
senderMustBePlayer = false; senderMustBePlayer = false;
} }
public void perform() {
public void perform()
{
try { try {
boolean needsGateManagerUpdate = false; boolean needsGateManagerUpdate = false;
@ -61,10 +53,8 @@ public class CommandOpen 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());
} }
} }
} }

View File

@ -21,11 +21,9 @@ 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() {
{
public CommandRemove()
{
aliases.add("delete"); aliases.add("delete");
aliases.add("del"); aliases.add("del");
aliases.add("remove"); aliases.add("remove");
@ -43,9 +41,7 @@ public class CommandRemove extends BaseCommand
senderMustBePlayer = false; senderMustBePlayer = false;
} }
public void perform() {
public void perform()
{
Plugin.getPlugin().getGatesManager().handleDeletion(gate); 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,11 +21,9 @@ 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() {
{
public CommandRename()
{
aliases.add("rename"); aliases.add("rename");
aliases.add("rn"); aliases.add("rn");
@ -45,15 +43,13 @@ public class CommandRename extends BaseCommand
} }
public void perform() public void perform() {
{
String newId = parameters.get(1); String newId = parameters.get(1);
GatesManager gatesManager = Plugin.getPlugin().getGatesManager(); GatesManager gatesManager = Plugin.getPlugin().getGatesManager();
if (gatesManager.gateExists(newId)) { if (gatesManager.gateExists(newId)) {
sendMessage(ChatColor.RED + "Cannot rename " + gate.getId() + ". There is already a gate named " + newId + "."); sendMessage(ChatColor.RED + "Cannot rename " + gate.getId() + ". There is already a gate named " + newId + ".");
} } else {
else {
String oldId = gate.getId(); String oldId = gate.getId();
gate.setId(newId); gate.setId(newId);
@ -62,5 +58,4 @@ public class CommandRename extends BaseCommand
sendMessage(ChatColor.GREEN + "Gate " + gate.getId() + " is now known as " + newId + "."); 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() {
{
public CommandUnhide()
{
aliases.add("unhide"); aliases.add("unhide");
aliases.add("u"); aliases.add("u");
requiredParameters.add("id"); requiredParameters.add("id");
helpDescription = "Make that gate visible"; helpDescription = "Make that gate visible";
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(false); 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());
} }
} }
} }