permissionProvider = getServer().getServicesManager()
+ .getRegistration(Permission.class);
+ if (permissionProvider != null) {
+ permission = permissionProvider.getProvider();
+ }
+ return (permission != null);
+ }
+
+ @Override
+ public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
+ sender.sendMessage("§cThis command isn't available."); //In theory, unregistered commands use this method
+ return true;
+ }
+}
diff --git a/ButtonCore/src/main/java/buttondevteam/lib/architecture/ButtonPlugin.java b/ButtonCore/src/main/java/buttondevteam/lib/architecture/ButtonPlugin.java
index 292ac46..56e580d 100644
--- a/ButtonCore/src/main/java/buttondevteam/lib/architecture/ButtonPlugin.java
+++ b/ButtonCore/src/main/java/buttondevteam/lib/architecture/ButtonPlugin.java
@@ -1,6 +1,7 @@
package buttondevteam.lib.architecture;
import buttondevteam.lib.TBMCCoreAPI;
+import buttondevteam.lib.chat.TBMCChatAPI;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.experimental.var;
@@ -32,6 +33,7 @@ public abstract class ButtonPlugin extends JavaPlugin {
pluginDisable();
saveConfig();
iConfig.resetConfigurationCache();
+ TBMCChatAPI.RemoveCommands(this);
} catch (Exception e) {
TBMCCoreAPI.SendException("Error while disabling plugin " + getName() + "!", e);
}
diff --git a/ButtonCore/src/main/java/buttondevteam/lib/architecture/Component.java b/ButtonCore/src/main/java/buttondevteam/lib/architecture/Component.java
index 98e38d1..9a09483 100644
--- a/ButtonCore/src/main/java/buttondevteam/lib/architecture/Component.java
+++ b/ButtonCore/src/main/java/buttondevteam/lib/architecture/Component.java
@@ -5,7 +5,6 @@ import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.architecture.exceptions.UnregisteredComponentException;
import buttondevteam.lib.chat.TBMCChatAPI;
import buttondevteam.lib.chat.TBMCCommandBase;
-import lombok.AccessLevel;
import lombok.Getter;
import lombok.NonNull;
import lombok.experimental.var;
@@ -26,7 +25,7 @@ public abstract class Component {
@Getter
private boolean enabled = false;
- @Getter(value = AccessLevel.PROTECTED)
+ @Getter
@NonNull
private JavaPlugin plugin;
@NonNull
@@ -131,6 +130,7 @@ public abstract class Component {
component.disable();
component.plugin.saveConfig();
component.config.resetConfigurationCache();
+ TBMCChatAPI.RemoveCommands(component);
}
}
diff --git a/ButtonCore/src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java b/ButtonCore/src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java
index b08a64f..ed61f53 100755
--- a/ButtonCore/src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java
+++ b/ButtonCore/src/main/java/buttondevteam/lib/chat/TBMCChatAPI.java
@@ -6,6 +6,7 @@ import buttondevteam.lib.TBMCChatEvent;
import buttondevteam.lib.TBMCChatPreprocessEvent;
import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.TBMCSystemChatEvent;
+import buttondevteam.lib.architecture.Component;
import buttondevteam.lib.chat.Channel.RecipientTestResult;
import lombok.val;
import org.bukkit.Bukkit;
@@ -119,10 +120,8 @@ public class TBMCChatAPI {
continue;
TBMCCommandBase c = cmd.newInstance();
c.plugin = plugin;
- if (HasNulls(plugin, c))
- continue;
+ CommandCaller.RegisterCommand(c); //Will check for nulls
commands.put(c.GetCommandPath(), c);
- CommandCaller.RegisterCommand(c);
} catch (Exception e) {
TBMCCoreAPI.SendException("An error occured while registering command " + cmd.getName(), e);
}
@@ -155,55 +154,106 @@ public class TBMCChatAPI {
else
c = thecmdclass.newInstance();
c.plugin = plugin;
- if (HasNulls(plugin, c))
- return;
+ CommandCaller.RegisterCommand(c); //Will check for nulls
commands.put(c.GetCommandPath(), c);
- CommandCaller.RegisterCommand(c);
} catch (Exception e) {
TBMCCoreAPI.SendException("An error occured while registering command " + thecmdclass.getSimpleName(), e);
}
- }
+ } //TODO: onCommand(CommandSender sender, String alias, int arg1, String arg2) (planned for a while)
/**
*
- * This method adds a plugin's command to help and sets it's executor.
+ * This method adds a plugin's command to help and sets its executor.
*
*
- * The command must be registered in the caller plugin's plugin.yml. Otherwise the plugin will output a messsage to console.
+ * The command must be registered in the caller plugin's plugin.yml. Otherwise the plugin will output a message to console.
*
*
* Using this method after the server is done loading will have no effect.
*
- *
+ *
* @param plugin
* The caller plugin
* @param cmd
* The command to add
*/
public static void AddCommand(JavaPlugin plugin, TBMCCommandBase cmd) {
- if (HasNulls(plugin, cmd))
- return;
- // plugin.getLogger().info("Registering command /" + cmd.GetCommandPath() + " for " + plugin.getName());
try {
+ if (plugin == null) throw new IllegalArgumentException("The plugin is null!");
+ if (cmd == null) throw new IllegalArgumentException("The command is null!");
cmd.plugin = plugin;
+ CommandCaller.RegisterCommand(cmd); //Checks for other nulls
commands.put(cmd.GetCommandPath(), cmd);
- CommandCaller.RegisterCommand(cmd);
} catch (Exception e) {
- TBMCCoreAPI.SendException("An error occured while registering command " + cmd.GetCommandPath(), e);
+ TBMCCoreAPI.SendException("An error occured while registering command " + (cmd == null ? "n u l l" : cmd.GetCommandPath()), e);
}
}
- private static boolean HasNulls(JavaPlugin plugin, TBMCCommandBase cmd) {
- if (cmd == null) {
- TBMCCoreAPI.SendException("An error occured while registering a command for plugin " + plugin.getName(),
- new Exception("The command is null!"));
- return true;
- } else if (cmd.GetCommandPath() == null) {
- TBMCCoreAPI.SendException("An error occured while registering command " + cmd.getClass().getSimpleName()
- + " for plugin " + plugin.getName(), new Exception("The command path is null!"));
- return true;
+ /**
+ *
+ * This method adds a plugin's command to help and sets its executor.
+ *
+ *
+ * The command must be registered in the caller plugin's plugin.yml. Otherwise the plugin will output a message to console.
+ *
+ *
+ * Using this method after the server is done loading will have no effect.
+ *
+ *
+ * @param component The caller component
+ * @param cmd The command to add
+ */
+ public static void AddCommand(Component component, TBMCCommandBase cmd) {
+ try {
+ if (component == null) throw new IllegalArgumentException("The component is null!");
+ if (cmd == null) throw new IllegalArgumentException("The command is null!");
+ cmd.plugin = component.getPlugin();
+ cmd.component = component;
+ CommandCaller.RegisterCommand(cmd); //Checks for other nulls
+ commands.put(cmd.GetCommandPath(), cmd);
+ } catch (Exception e) {
+ TBMCCoreAPI.SendException("An error occured while registering command " + (cmd == null ? "n u l l" : cmd.GetCommandPath()), e);
}
- return false;
+ }
+
+ /**
+ * Removes all commands from the plugin
+ *
+ * @param plugin The plugin to remove commands from
+ */
+ public static void RemoveCommands(JavaPlugin plugin) {
+ commands.values().removeIf(c -> {
+ try {
+ if (c.plugin == plugin) {
+ CommandCaller.UnregisterCommand(c);
+ return true; //Remove
+ }
+ return false;
+ } catch (Exception e) {
+ TBMCCoreAPI.SendException("An error occured while unregistering commands for " + plugin.getName(), e);
+ return true; //Remove if it couldn't get the plugin command
+ }
+ });
+ }
+
+ /**
+ * Removes all commands from the component
+ *
+ * @param component The component to remove commands from
+ */
+ public static void RemoveCommands(Component component) {
+ commands.values().removeIf(c -> {
+ try {
+ if (c.component == component) {
+ CommandCaller.UnregisterCommand(c);
+ return true; //Remove
+ }
+ return false;
+ } catch (Exception e) {
+ TBMCCoreAPI.SendException("An error occured while unregistering commands for " + component.getClass().getSimpleName(), e);
+ return true; //Remove if it couldn't get the plugin command
+ }
+ });
}
/**
diff --git a/ButtonCore/src/main/java/buttondevteam/lib/chat/TBMCCommandBase.java b/ButtonCore/src/main/java/buttondevteam/lib/chat/TBMCCommandBase.java
index de5fb14..dbd3b9f 100755
--- a/ButtonCore/src/main/java/buttondevteam/lib/chat/TBMCCommandBase.java
+++ b/ButtonCore/src/main/java/buttondevteam/lib/chat/TBMCCommandBase.java
@@ -1,106 +1,111 @@
-package buttondevteam.lib.chat;
-
-import javassist.Modifier;
-import org.bukkit.command.CommandSender;
-import org.bukkit.plugin.Plugin;
-
-import java.util.function.Function;
-
-/**
- * Extend this class to create new TBMCCommand and use {@link TBMCChatAPI#AddCommand(org.bukkit.plugin.java.JavaPlugin, TBMCCommandBase)} to add it. Note: The command path (command name
- * and subcommand arguments) will be the class name by default, removing any "command" from it. To change it (especially for subcommands), use the path field in the {@link CommandClass} annotation.
- *
- * @author Norbi
- *
- */
-@TBMCCommandEnforcer
-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 final String path;
-
- /**
- * The command's path, or name if top-level command.
- * For example:
- * "u admin updateplugin" or "u" for the top level one
- * The path must be lowercase!
- * Abstract classes with no {@link CommandClass} annotations will be ignored.
- *
- * @return The command path, which is the command class name by default (removing any "command" from it) - Change via the {@link CommandClass} annotation
- */
- public final String GetCommandPath() {
- return path;
- }
-
- private String getcmdpath() {
- if (!getClass().isAnnotationPresent(CommandClass.class))
- throw new RuntimeException(
- "No @CommandClass annotation on command class " + getClass().getSimpleName() + "!");
- Function, String> getFromClass = cl -> cl.getSimpleName().toLowerCase().replace("commandbase", "") // <-- ...
- .replace("command", "");
- String path = getClass().getAnnotation(CommandClass.class).path(),
- prevpath = path = path.length() == 0 ? getFromClass.apply(getClass()) : path;
- for (Class> cl = getClass().getSuperclass(); cl != null
- && !cl.getPackage().getName().equals(TBMCCommandBase.class.getPackage().getName()); cl = cl
- .getSuperclass()) { //
- String newpath;
- if (!cl.isAnnotationPresent(CommandClass.class)
- || (newpath = cl.getAnnotation(CommandClass.class).path()).length() == 0
- || newpath.equals(prevpath)) {
- if ((Modifier.isAbstract(cl.getModifiers()) && !cl.isAnnotationPresent(CommandClass.class))
- || cl.getAnnotation(CommandClass.class).excludeFromPath()) // <--
- continue;
- newpath = getFromClass.apply(cl);
- }
- path = (prevpath = newpath) + " " + path;
- }
- return path;
- }
-
- Plugin plugin; // Used By TBMCChatAPI
-
- public final Plugin getPlugin() { // Used by CommandCaller (ButtonChat)
- return plugin;
- }
-
- public final boolean isPlayerOnly() {
- return this instanceof PlayerCommandBase ||
- (this instanceof OptionallyPlayerCommandBase &&
- (!getClass().isAnnotationPresent(OptionallyPlayerCommandClass.class)
- || getClass().getAnnotation(OptionallyPlayerCommandClass.class).playerOnly()));
- }
-
- 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 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;
- }
-}
+package buttondevteam.lib.chat;
+
+import buttondevteam.lib.architecture.Component;
+import javassist.Modifier;
+import lombok.Getter;
+import org.bukkit.command.CommandSender;
+import org.bukkit.plugin.Plugin;
+
+import java.util.function.Function;
+
+/**
+ * Extend this class to create new TBMCCommand and use {@link TBMCChatAPI#AddCommand(org.bukkit.plugin.java.JavaPlugin, TBMCCommandBase)} to add it. Note: The command path (command name
+ * and subcommand arguments) will be the class name by default, removing any "command" from it. To change it (especially for subcommands), use the path field in the {@link CommandClass} annotation.
+ *
+ * @author Norbi
+ *
+ */
+@TBMCCommandEnforcer
+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 final String path;
+
+ /**
+ * The command's path, or name if top-level command.
+ * For example:
+ * "u admin updateplugin" or "u" for the top level one
+ * The path must be lowercase!
+ * Abstract classes with no {@link CommandClass} annotations will be ignored.
+ *
+ * @return The command path, which is the command class name by default (removing any "command" from it) - Change via the {@link CommandClass} annotation
+ */
+ public final String GetCommandPath() {
+ return path;
+ }
+
+ private String getcmdpath() {
+ if (!getClass().isAnnotationPresent(CommandClass.class))
+ throw new RuntimeException(
+ "No @CommandClass annotation on command class " + getClass().getSimpleName() + "!");
+ Function, String> getFromClass = cl -> cl.getSimpleName().toLowerCase().replace("commandbase", "") // <-- ...
+ .replace("command", "");
+ String path = getClass().getAnnotation(CommandClass.class).path(),
+ prevpath = path = path.length() == 0 ? getFromClass.apply(getClass()) : path;
+ for (Class> cl = getClass().getSuperclass(); cl != null
+ && !cl.getPackage().getName().equals(TBMCCommandBase.class.getPackage().getName()); cl = cl
+ .getSuperclass()) { //
+ String newpath;
+ if (!cl.isAnnotationPresent(CommandClass.class)
+ || (newpath = cl.getAnnotation(CommandClass.class).path()).length() == 0
+ || newpath.equals(prevpath)) {
+ if ((Modifier.isAbstract(cl.getModifiers()) && !cl.isAnnotationPresent(CommandClass.class))
+ || cl.getAnnotation(CommandClass.class).excludeFromPath()) // <--
+ continue;
+ newpath = getFromClass.apply(cl);
+ }
+ path = (prevpath = newpath) + " " + path;
+ }
+ return path;
+ }
+
+ Plugin plugin; // Used By TBMCChatAPI
+
+ public final Plugin getPlugin() { // Used by CommandCaller (ButtonChat)
+ return plugin;
+ }
+
+ public final boolean isPlayerOnly() {
+ return this instanceof PlayerCommandBase ||
+ (this instanceof OptionallyPlayerCommandBase &&
+ (!getClass().isAnnotationPresent(OptionallyPlayerCommandClass.class)
+ || getClass().getAnnotation(OptionallyPlayerCommandClass.class).playerOnly()));
+ }
+
+ 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 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;
+ }
+
+ @Getter
+ Component component; //May be null
+}