Refactored the BaseCommand class.

This commit is contained in:
Tobias Ottenweller 2013-05-18 11:11:10 +02:00
parent 34b1c7e0ce
commit e9027900ce

View File

@ -80,45 +80,34 @@ public abstract class BaseCommand
boolean senderIsPlayer = this.sender instanceof Player; boolean senderIsPlayer = this.sender instanceof Player;
boolean hasGateParameter = false; boolean hasGateParameter = false;
if (this.hasGateParam == true && this.parameters.size() > 0 && this.setGateUsingParameter(this.parameters.get(0))) { if (this.hasGateParam && this.parameters.size() > 0 && this.setGateUsingParameter(this.parameters.get(0))) {
hasGateParameter = true; hasGateParameter = true;
} }
boolean senderHasPermission = this.hasPermission(); boolean senderHasPermission = this.hasPermission();
boolean valid = false; boolean valid = false;
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 if (!allParamtertersThere) else {
{ if (!allParamtertersThere) {
sendMessage(ChatColor.RED + "Some parameters are missing! " + ChatColor.AQUA + "Usage: " + this.getUseageTemplate(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) sendMessage(ChatColor.RED + "You either provided a invalid gate or do not have permission to " + this.helpDescription.toLowerCase());
{ valid = false;
sendMessage(ChatColor.RED + "You either provided a invalid gate or do not have permission to " + this.helpDescription.toLowerCase()); } else if (!senderHasPermission) {
valid = false; sendMessage(ChatColor.RED + "You lack the permissions to " + this.helpDescription.toLowerCase());
} valid = false;
else if (!senderHasPermission) } else if (this.hasGateParam && !hasGateParameter) {
{ sendMessage(ChatColor.RED + "There exists no gate with id " + this.parameters.get(0));
sendMessage(ChatColor.RED + "You lack the permissions to " + this.helpDescription.toLowerCase()); valid = false;
valid = false; } else {
} valid = true;
}
else if (this.hasGateParam && !hasGateParameter) }
{
sendMessage(ChatColor.RED + "There exists no gate with id " + this.parameters.get(0));
valid = false;
}
else
{
valid = true;
}
return valid; return valid;
} }
@ -143,55 +132,41 @@ public abstract class BaseCommand
*/ */
protected boolean hasPermission() protected boolean hasPermission()
{ {
if (Plugin.getPermission() == null) // fallback <EFBFBD> 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);
} }
if (!(this.sender instanceof Player)) {
Player p = null; // sender is no player - there is no information about the senders locations
return Plugin.getPermission().has(this.sender, this.requiredPermission);
if (this.sender instanceof Player)
{
p = (Player) this.sender;
}
else
{
// sender is no player <EFBFBD> there is no information about the senders locations
return Plugin.getPermission().has(this.sender, this.requiredPermission);
} }
Player p = (Player) this.sender;
boolean hasPermission = false; boolean hasPermission = false;
if (this.requiredPermission.equals(Plugin.permissionInfo)) if (this.requiredPermission.equals(Plugin.permissionInfo)) {
{
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);
} }
} }
@ -202,29 +177,12 @@ public abstract class BaseCommand
protected boolean hasPermissionAtGateLocationAndExit(Player p) protected boolean hasPermissionAtGateLocationAndExit(Player p)
{ {
if (this.gate == null || p == null) // make sure we don't run into a nullpointer exception if (this.gate == null || p == null) { // make sure we don't run into a nullpointer exception
{
return false; return false;
} }
boolean permAtLocation; boolean permAtLocation = this.gate.getLocation() == null || Plugin.getPermission().has(this.gate.getLocation().getWorld(), p.getName(), this.requiredPermission);
boolean permAtExit = this.gate.getExit() == null || Plugin.getPermission().has(this.gate.getExit().getWorld(), p.getName(), this.requiredPermission);
if (this.gate.getLocation() == null) {
permAtLocation = true;
}
else {
boolean permAtLocation = Plugin.getPermission().has(this.gate.getLocation().getWorld(), p.getName(), this.requiredPermission);
}
boolean permAtExit;
if (this.gate.getExit() == null) {
permAtExit = true;
}
else {
permAtExit = Plugin.getPermission().has(this.gate.getExit().getWorld(), p.getName(), this.requiredPermission);
}
return permAtLocation & permAtExit; return permAtLocation & permAtExit;
} }
@ -233,7 +191,8 @@ 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) {
@ -270,11 +229,13 @@ public abstract class BaseCommand
return ret; return ret;
} }
protected String getUseageTemplate(boolean withColor) { protected String getUsageTemplate(boolean withColor)
{
return getUsageTemplate(withColor, false); return getUsageTemplate(withColor, false);
} }
protected String getUseageTemplate() { protected String getUsageTemplate()
return getUseageTemplate(true); {
return getUsageTemplate(true);
} }
} }