From 25f770849f2bc4f8c4c2ce77edab88e7c435da1e Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Mon, 22 May 2017 20:46:08 +0200 Subject: [PATCH] Made modOnly optional and inherited Also accidentally fixed an issue, probably (getClass instead of getValue.getClass) Also testing in production --- .../buttondevteam/core/CommandCaller.java | 3 +- .../buttondevteam/lib/chat/CommandClass.java | 8 +++-- .../buttondevteam/lib/chat/TBMCChatAPI.java | 3 +- .../lib/chat/TBMCCommandBase.java | 29 ++++++++++++++++++- 4 files changed, 35 insertions(+), 8 deletions(-) diff --git a/src/main/java/buttondevteam/core/CommandCaller.java b/src/main/java/buttondevteam/core/CommandCaller.java index 2b27f9e..688dd0d 100644 --- a/src/main/java/buttondevteam/core/CommandCaller.java +++ b/src/main/java/buttondevteam/core/CommandCaller.java @@ -61,8 +61,7 @@ public class CommandCaller implements CommandExecutor { } return true; } - if (cmd.getClass().getAnnotation(CommandClass.class).modOnly() - && !MainPlugin.permission.has(sender, "tbmc.admin")) { + if (cmd.isModOnly() && !MainPlugin.permission.has(sender, "tbmc.admin")) { sender.sendMessage("§cYou need to be a mod to use this command."); return true; } diff --git a/src/main/java/buttondevteam/lib/chat/CommandClass.java b/src/main/java/buttondevteam/lib/chat/CommandClass.java index 1a49d87..74e70d7 100644 --- a/src/main/java/buttondevteam/lib/chat/CommandClass.java +++ b/src/main/java/buttondevteam/lib/chat/CommandClass.java @@ -8,7 +8,8 @@ import java.lang.annotation.Target; /** * Abstract classes with no {@link CommandClass} annotations will be ignored. 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.
+ * All commands with no modOnly set will not be mod only * * @author NorbiPeti * @@ -18,11 +19,12 @@ import java.lang.annotation.Target; @Inherited 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.
+ * If not set, the command will not be 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.
diff --git a/src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java b/src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java index 943de8d..359e7e7 100644 --- a/src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java +++ b/src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java @@ -66,8 +66,7 @@ public class TBMCChatAPI { if (cmd.getKey().startsWith(command + " ")) { if (cmd.getValue().isPlayerOnly() && !(sender instanceof Player)) continue; - if (cmd.getClass().getAnnotation(CommandClass.class).modOnly() - && !MainPlugin.permission.has(sender, "tbmc.admin")) + if (cmd.getValue().isModOnly() && !MainPlugin.permission.has(sender, "tbmc.admin")) continue; int ind = cmd.getKey().indexOf(' ', command.length() + 2); if (ind >= 0) { diff --git a/src/main/java/buttondevteam/lib/chat/TBMCCommandBase.java b/src/main/java/buttondevteam/lib/chat/TBMCCommandBase.java index 086f15d..2f7cb2d 100644 --- a/src/main/java/buttondevteam/lib/chat/TBMCCommandBase.java +++ b/src/main/java/buttondevteam/lib/chat/TBMCCommandBase.java @@ -18,13 +18,14 @@ public abstract class TBMCCommandBase { public TBMCCommandBase() { path = getcmdpath(); + modonly = ismodonly(); } public abstract boolean OnCommand(CommandSender sender, String alias, String[] args); public abstract String[] GetHelpText(String alias); - private String path = null; + private final String path; /** * The command's path, or name if top-level command.
@@ -77,4 +78,30 @@ public abstract class TBMCCommandBase { ? getClass().getAnnotation(OptionallyPlayerCommandClass.class).playerOnly() : true : 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; + } }