Add a permission denied format event.

This commit is contained in:
Olof Larsson 2013-03-26 09:43:23 +01:00
parent 6fc7723466
commit 2544fa0a6b
7 changed files with 76 additions and 24 deletions

View File

@ -2,12 +2,12 @@ package com.massivecraft.mcore;
public class Lang public class Lang
{ {
public static final String permForbidden = "<b>You don't have permission to %s."; public static final String PERM_DEFAULT_DENIED_FORMAT = "<b>You don't have permission to %s.";
public static final String permDoThat = "do that"; public static final String PERM_DEFAULT_DESCRIPTION = "do that";
public static final String commandSenderMustBePlayer = "<b>This command can only be used by ingame players."; public static final String COMMAND_SENDER_MUST_BE_PLAYER = "<b>This command can only be used by ingame players.";
public static final String commandSenderMusntBePlayer = "<b>This command can not be used by ingame players."; public static final String COMMAND_SENDER_MUSNT_BE_PLAYER = "<b>This command can not be used by ingame players.";
public static final String commandToFewArgs = "<b>To few arguments. <i>Use like this:"; public static final String COMMAND_TO_FEW_ARGS = "<b>To few arguments. <i>Use like this:";
public static final String commandToManyArgs = "<b>Strange arguments %s<b>."; public static final String COMMAND_TO_MANY_ARGS = "<b>Strange arguments %s<b>.";
public static final String commandToManyArgs2 = "<i>Use the command like this:"; public static final String COMMAND_TO_MANY_ARGS2 = "<i>Use the command like this:";
} }

View File

@ -91,7 +91,7 @@ public abstract class MCommand
String perm = this.getDescPermission(); String perm = this.getDescPermission();
if (perm != null) if (perm != null)
{ {
String pdesc = PermUtil.getPermissionDescription(this.getDescPermission()); String pdesc = PermUtil.getDescription(this.getDescPermission());
if (pdesc != null) if (pdesc != null)
{ {
return pdesc; return pdesc;
@ -320,7 +320,7 @@ public abstract class MCommand
{ {
if (sender != null) if (sender != null)
{ {
msg(Lang.commandToFewArgs); msg(Lang.COMMAND_TO_FEW_ARGS);
sender.sendMessage(this.getUseageTemplate()); sender.sendMessage(this.getUseageTemplate());
} }
return false; return false;
@ -332,8 +332,8 @@ public abstract class MCommand
{ {
// Get the to many string slice // Get the to many string slice
List<String> theToMany = args.subList(this.requiredArgs.size() + this.optionalArgs.size(), args.size()); List<String> theToMany = args.subList(this.requiredArgs.size() + this.optionalArgs.size(), args.size());
msg(Lang.commandToManyArgs, Txt.implodeCommaAndDot(theToMany, Txt.parse("<aqua>%s"), Txt.parse("<b>, "), Txt.parse("<b> and "), "")); msg(Lang.COMMAND_TO_MANY_ARGS, Txt.implodeCommaAndDot(theToMany, Txt.parse("<aqua>%s"), Txt.parse("<b>, "), Txt.parse("<b> and "), ""));
msg(Lang.commandToManyArgs2); msg(Lang.COMMAND_TO_MANY_ARGS2);
sender.sendMessage(this.getUseageTemplate()); sender.sendMessage(this.getUseageTemplate());
} }
return false; return false;

View File

@ -36,6 +36,6 @@ public class ReqHasPerm extends ReqAbstract
@Override @Override
public String createErrorMessage(CommandSender sender, MCommand command) public String createErrorMessage(CommandSender sender, MCommand command)
{ {
return PermUtil.getForbiddenMessage(this.perm); return PermUtil.getDeniedMessage(this.perm);
} }
} }

View File

@ -30,7 +30,7 @@ public class ReqIsPlayer extends ReqAbstract
@Override @Override
public String createErrorMessage(CommandSender sender, MCommand command) public String createErrorMessage(CommandSender sender, MCommand command)
{ {
return Lang.commandSenderMustBePlayer; return Lang.COMMAND_SENDER_MUST_BE_PLAYER;
} }
} }

View File

@ -30,7 +30,7 @@ public class ReqIsntPlayer extends ReqAbstract
@Override @Override
public String createErrorMessage(CommandSender sender, MCommand command) public String createErrorMessage(CommandSender sender, MCommand command)
{ {
return Lang.commandSenderMusntBePlayer; return Lang.COMMAND_SENDER_MUSNT_BE_PLAYER;
} }
} }

View File

@ -0,0 +1,35 @@
package com.massivecraft.mcore.event;
import org.bukkit.event.HandlerList;
public class MCorePermissionDeniedFormatEvent extends MCoreEvent
{
// -------------------------------------------- //
// REQUIRED EVENT CODE
// -------------------------------------------- //
private static final HandlerList handlers = new HandlerList();
@Override public HandlerList getHandlers() { return handlers; }
public static HandlerList getHandlerList() { return handlers; }
// -------------------------------------------- //
// FIELD
// -------------------------------------------- //
private final String permissionName;
public String getPermissionName() { return this.permissionName; }
private String format;
public String getFormat() { return this.format; }
public void setFormat(String format) { this.format = format; }
// -------------------------------------------- //
// CONSTRUCT
// -------------------------------------------- //
public MCorePermissionDeniedFormatEvent(String permissionName)
{
this.permissionName = permissionName;
this.format = null;
}
}

View File

@ -11,6 +11,7 @@ import org.bukkit.permissions.PermissionDefault;
import com.massivecraft.mcore.Lang; import com.massivecraft.mcore.Lang;
import com.massivecraft.mcore.MCore; import com.massivecraft.mcore.MCore;
import com.massivecraft.mcore.event.MCorePermissionDeniedFormatEvent;
public class PermUtil public class PermUtil
{ {
@ -43,7 +44,7 @@ public class PermUtil
if (permissible instanceof CommandSender) if (permissible instanceof CommandSender)
{ {
CommandSender sender = (CommandSender)permissible; CommandSender sender = (CommandSender)permissible;
sender.sendMessage(getForbiddenMessage(perm)); sender.sendMessage(getDeniedMessage(perm));
} }
} }
return false; return false;
@ -53,24 +54,40 @@ public class PermUtil
// DESCRIPTIONS AND MESSAGES // DESCRIPTIONS AND MESSAGES
// -------------------------------------------- // // -------------------------------------------- //
public static String getPermissionDescription(String perm) public static String getDescription(String perm)
{ {
if (perm == null) return Lang.permDoThat; if (perm == null) return Lang.PERM_DEFAULT_DESCRIPTION;
Permission permission = Bukkit.getPluginManager().getPermission(perm); Permission permission = Bukkit.getPluginManager().getPermission(perm);
return getPermissionDescription(permission); return getDescription(permission);
} }
public static String getDescription(Permission perm)
public static String getPermissionDescription(Permission perm)
{ {
if (perm == null) return Lang.permDoThat; if (perm == null) return Lang.PERM_DEFAULT_DESCRIPTION;
String desc = perm.getDescription(); String desc = perm.getDescription();
if (desc == null || desc.length() == 0) return Lang.permDoThat; if (desc == null || desc.length() == 0) return Lang.PERM_DEFAULT_DESCRIPTION;
return desc; return desc;
} }
public static String getForbiddenMessage(String perm) public static String getDeniedFormat(String perm)
{ {
return Txt.parse(Lang.permForbidden, getPermissionDescription(perm)); MCorePermissionDeniedFormatEvent event = new MCorePermissionDeniedFormatEvent(perm);
event.run();
String ret = event.getFormat();
if (ret == null) ret = Lang.PERM_DEFAULT_DENIED_FORMAT;
return ret;
}
public static String getDeniedFormat(Permission perm)
{
return getDeniedFormat(perm == null ? null : perm.getName());
}
public static String getDeniedMessage(String perm)
{
return Txt.parse(getDeniedFormat(perm), getDescription(perm));
}
public static String getDeniedMessage(Permission perm)
{
return Txt.parse(getDeniedFormat(perm), getDescription(perm));
} }
// -------------------------------------------- // // -------------------------------------------- //