Made modOnly optional and inherited

Also accidentally fixed an issue, probably (getClass instead of
getValue.getClass)
Also testing in production
This commit is contained in:
Norbi Peti 2017-05-22 20:46:08 +02:00
parent c0e4a9e065
commit 25f770849f
4 changed files with 35 additions and 8 deletions

View file

@ -61,8 +61,7 @@ public class CommandCaller implements CommandExecutor {
} }
return true; return true;
} }
if (cmd.getClass().getAnnotation(CommandClass.class).modOnly() if (cmd.isModOnly() && !MainPlugin.permission.has(sender, "tbmc.admin")) {
&& !MainPlugin.permission.has(sender, "tbmc.admin")) {
sender.sendMessage("§cYou need to be a mod to use this command."); sender.sendMessage("§cYou need to be a mod to use this command.");
return true; return true;
} }

View file

@ -8,7 +8,8 @@ import java.lang.annotation.Target;
/** /**
* <b>Abstract classes with no {@link CommandClass} annotations will be ignored.</b> Classes that are not abstract or have the annotation will be included in the command path unless * <b>Abstract classes with no {@link CommandClass} annotations will be ignored.</b> Classes that are not abstract or have the annotation will be included in the command path unless
* {@link #excludeFromPath()} is true. * {@link #excludeFromPath()} is true.<br>
* <i>All commands with no modOnly set will <u>not be mod only</u></i>
* *
* @author NorbiPeti * @author NorbiPeti
* *
@ -18,11 +19,12 @@ import java.lang.annotation.Target;
@Inherited @Inherited
public @interface CommandClass { public @interface CommandClass {
/** /**
* Determines whether the command can only be used by mods and above or regular players can use it as well. * Determines whether the command can only be used by mods and above or regular players can use it as well.<br>
* <b>If not set, the command will <u>not</u> be mod only</b>
* *
* @return If the command is mod only * @return If the command is mod only
*/ */
public boolean modOnly(); public boolean modOnly() default false;
/** /**
* The command's path, or name if top-level command.<br> * The command's path, or name if top-level command.<br>

View file

@ -66,8 +66,7 @@ public class TBMCChatAPI {
if (cmd.getKey().startsWith(command + " ")) { if (cmd.getKey().startsWith(command + " ")) {
if (cmd.getValue().isPlayerOnly() && !(sender instanceof Player)) if (cmd.getValue().isPlayerOnly() && !(sender instanceof Player))
continue; continue;
if (cmd.getClass().getAnnotation(CommandClass.class).modOnly() if (cmd.getValue().isModOnly() && !MainPlugin.permission.has(sender, "tbmc.admin"))
&& !MainPlugin.permission.has(sender, "tbmc.admin"))
continue; continue;
int ind = cmd.getKey().indexOf(' ', command.length() + 2); int ind = cmd.getKey().indexOf(' ', command.length() + 2);
if (ind >= 0) { if (ind >= 0) {

View file

@ -18,13 +18,14 @@ public abstract class TBMCCommandBase {
public TBMCCommandBase() { public TBMCCommandBase() {
path = getcmdpath(); path = getcmdpath();
modonly = ismodonly();
} }
public abstract boolean OnCommand(CommandSender sender, String alias, String[] args); public abstract boolean OnCommand(CommandSender sender, String alias, String[] args);
public abstract String[] GetHelpText(String alias); public abstract String[] GetHelpText(String alias);
private String path = null; private final String path;
/** /**
* The command's path, or name if top-level command.<br> * The command's path, or name if top-level command.<br>
@ -77,4 +78,30 @@ public abstract class TBMCCommandBase {
? getClass().getAnnotation(OptionallyPlayerCommandClass.class).playerOnly() : true ? getClass().getAnnotation(OptionallyPlayerCommandClass.class).playerOnly() : true
: false; : false;
} }
private final boolean modonly;
/**
* Returns true if this class' or any superclass' modOnly property is set to true.
*/
public final boolean isModOnly() {
return modonly;
}
private final boolean ismodonly() {
if (!getClass().isAnnotationPresent(CommandClass.class))
throw new RuntimeException(
"No @CommandClass annotation on command class " + getClass().getSimpleName() + "!");
boolean modOnly = getClass().getAnnotation(CommandClass.class).modOnly();
for (Class<?> cl = getClass().getSuperclass(); cl != null
&& !cl.getPackage().getName().equals(TBMCCommandBase.class.getPackage().getName()); cl = cl
.getSuperclass()) { //
if (cl.isAnnotationPresent(CommandClass.class) && !modOnly
&& cl.getAnnotation(CommandClass.class).modOnly()) {
modOnly = true;
break;
}
}
return modOnly;
}
} }