Some command improvements

Removing commands on component/plugin disable
This commit is contained in:
Norbi Peti 2019-01-11 00:41:26 +01:00
parent 245f6bbf59
commit a6d1122f7f
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
7 changed files with 361 additions and 278 deletions

View file

@ -4,7 +4,6 @@
<modules> <modules>
<module fileurl="file://$PROJECT_DIR$/BuildConfigUpdater/BuildConfigUpdater.iml" filepath="$PROJECT_DIR$/BuildConfigUpdater/BuildConfigUpdater.iml" /> <module fileurl="file://$PROJECT_DIR$/BuildConfigUpdater/BuildConfigUpdater.iml" filepath="$PROJECT_DIR$/BuildConfigUpdater/BuildConfigUpdater.iml" />
<module fileurl="file://$PROJECT_DIR$/ButtonCore/ButtonCore (1) (com.github.TBMCPlugins.ButtonCore).iml" filepath="$PROJECT_DIR$/ButtonCore/ButtonCore (1) (com.github.TBMCPlugins.ButtonCore).iml" /> <module fileurl="file://$PROJECT_DIR$/ButtonCore/ButtonCore (1) (com.github.TBMCPlugins.ButtonCore).iml" filepath="$PROJECT_DIR$/ButtonCore/ButtonCore (1) (com.github.TBMCPlugins.ButtonCore).iml" />
<module fileurl="file://$PROJECT_DIR$/ButtonCore/ButtonCore (1) (com.github.TBMCPlugins.ButtonCore).iml" filepath="$PROJECT_DIR$/ButtonCore/ButtonCore (1) (com.github.TBMCPlugins.ButtonCore).iml" />
<module fileurl="file://$PROJECT_DIR$/ButtonProcessor/ButtonProcessor.iml" filepath="$PROJECT_DIR$/ButtonProcessor/ButtonProcessor.iml" /> <module fileurl="file://$PROJECT_DIR$/ButtonProcessor/ButtonProcessor.iml" filepath="$PROJECT_DIR$/ButtonProcessor/ButtonProcessor.iml" />
</modules> </modules>
</component> </component>

View file

@ -21,41 +21,27 @@ public class CommandCaller implements CommandExecutor {
public static void RegisterCommand(TBMCCommandBase cmd) throws Exception { public static void RegisterCommand(TBMCCommandBase cmd) throws Exception {
if (instance == null) if (instance == null)
instance = new CommandCaller(); instance = new CommandCaller();
String topcmd = cmd.GetCommandPath(); String[] topcmd = new String[1]; //Holds out param
if (topcmd == null) PluginCommand pc = getPluginCommand(cmd, topcmd);
throw new Exception("Command " + cmd.getClass().getSimpleName() + " has no command path!");
if (cmd.getPlugin() == null)
throw new Exception("Command " + cmd.GetCommandPath() + " has no plugin!");
int i;
if ((i = topcmd.indexOf(' ')) != -1) // Get top-level command
topcmd = topcmd.substring(0, i);
{
PluginCommand pc = ((JavaPlugin) cmd.getPlugin()).getCommand(topcmd);
if (pc == null)
throw new Exception("Top level command " + topcmd + " not registered in plugin.yml for plugin: "
+ cmd.getPlugin().getName());
else {
pc.setExecutor(instance); pc.setExecutor(instance);
String[] helptext = cmd.GetHelpText(topcmd); String[] helptext = cmd.GetHelpText(topcmd[0]);
if (helptext == null || helptext.length == 0) if (helptext == null || helptext.length == 0)
throw new Exception("Command " + cmd.GetCommandPath() + " has no help text!"); throw new Exception("Command " + cmd.GetCommandPath() + " has no help text!");
pc.setUsage(helptext.length > 1 ? helptext[1] : helptext[0]); pc.setUsage(helptext.length > 1 ? helptext[1] : helptext[0]);
} }
}
}
@Override @Override
public boolean onCommand(CommandSender sender, Command command, String alias, String[] args) { public boolean onCommand(CommandSender sender, Command command, String alias, String[] args) {
String path = command.getName().toLowerCase(); StringBuilder path = new StringBuilder(command.getName().toLowerCase());
for (String arg : args) for (String arg : args)
path += " " + arg; path.append(" ").append(arg);
TBMCCommandBase cmd = TBMCChatAPI.GetCommands().get(path); TBMCCommandBase cmd = TBMCChatAPI.GetCommands().get(path.toString());
int argc = 0; int argc = 0;
String[] subcmds = null; String[] subcmds = null;
while (cmd == null && (subcmds = TBMCChatAPI.GetSubCommands(path, sender)).length == 0 && path.contains(" ")) { while (cmd == null && (subcmds = TBMCChatAPI.GetSubCommands(path.toString(), sender)).length == 0 && path.toString().contains(" ")) {
path = path.substring(0, path.lastIndexOf(' ')); path = new StringBuilder(path.substring(0, path.toString().lastIndexOf(' ')));
argc++; argc++;
cmd = TBMCChatAPI.GetCommands().get(path); cmd = TBMCChatAPI.GetCommands().get(path.toString());
} }
if (cmd == null) { if (cmd == null) {
if (subcmds.length > 0) //Subcmds will always have value here (see assignment above) if (subcmds.length > 0) //Subcmds will always have value here (see assignment above)
@ -85,4 +71,37 @@ public class CommandCaller implements CommandExecutor {
} }
return true; return true;
} }
public static void UnregisterCommand(TBMCCommandBase cmd) throws Exception {
PluginCommand pc = getPluginCommand(cmd, null);
pc.setExecutor(null); //Sets the executor to this plugin
}
/**
* Gets the plugin command from the TBMC command.
*
* @param cmd The TBMC command
* @param out_topcmd An array with at least 1 elements or null
* @return The Bukkit plugin command - an exception is generated if null
* @throws Exception If the command isn't set up properly (or a different error)
*/
public static PluginCommand getPluginCommand(TBMCCommandBase cmd, String[] out_topcmd) throws Exception {
String topcmd = cmd.GetCommandPath();
if (topcmd == null)
throw new Exception("Command " + cmd.getClass().getSimpleName() + " has no command path!");
if (cmd.getPlugin() == null)
throw new Exception("Command " + cmd.GetCommandPath() + " has no plugin!");
int i;
if ((i = topcmd.indexOf(' ')) != -1) // Get top-level command
topcmd = topcmd.substring(0, i);
if (out_topcmd != null && out_topcmd.length > 0)
out_topcmd[0] = topcmd;
{
PluginCommand pc = ((JavaPlugin) cmd.getPlugin()).getCommand(topcmd);
if (pc == null)
throw new Exception("Top level command " + topcmd + " not registered in plugin.yml for plugin: "
+ cmd.getPlugin().getName());
return pc;
}
}
} }

View file

@ -16,6 +16,8 @@ import com.earth2me.essentials.Essentials;
import net.milkbowl.vault.permission.Permission; import net.milkbowl.vault.permission.Permission;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.BlockCommandSender; import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender; import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.PluginDescriptionFile;
@ -114,4 +116,10 @@ public class MainPlugin extends JavaPlugin {
} }
return (permission != null); 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;
}
} }

View file

@ -1,6 +1,7 @@
package buttondevteam.lib.architecture; package buttondevteam.lib.architecture;
import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.chat.TBMCChatAPI;
import lombok.AccessLevel; import lombok.AccessLevel;
import lombok.Getter; import lombok.Getter;
import lombok.experimental.var; import lombok.experimental.var;
@ -32,6 +33,7 @@ public abstract class ButtonPlugin extends JavaPlugin {
pluginDisable(); pluginDisable();
saveConfig(); saveConfig();
iConfig.resetConfigurationCache(); iConfig.resetConfigurationCache();
TBMCChatAPI.RemoveCommands(this);
} catch (Exception e) { } catch (Exception e) {
TBMCCoreAPI.SendException("Error while disabling plugin " + getName() + "!", e); TBMCCoreAPI.SendException("Error while disabling plugin " + getName() + "!", e);
} }

View file

@ -5,7 +5,6 @@ import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.architecture.exceptions.UnregisteredComponentException; import buttondevteam.lib.architecture.exceptions.UnregisteredComponentException;
import buttondevteam.lib.chat.TBMCChatAPI; import buttondevteam.lib.chat.TBMCChatAPI;
import buttondevteam.lib.chat.TBMCCommandBase; import buttondevteam.lib.chat.TBMCCommandBase;
import lombok.AccessLevel;
import lombok.Getter; import lombok.Getter;
import lombok.NonNull; import lombok.NonNull;
import lombok.experimental.var; import lombok.experimental.var;
@ -26,7 +25,7 @@ public abstract class Component {
@Getter @Getter
private boolean enabled = false; private boolean enabled = false;
@Getter(value = AccessLevel.PROTECTED) @Getter
@NonNull @NonNull
private JavaPlugin plugin; private JavaPlugin plugin;
@NonNull @NonNull
@ -131,6 +130,7 @@ public abstract class Component {
component.disable(); component.disable();
component.plugin.saveConfig(); component.plugin.saveConfig();
component.config.resetConfigurationCache(); component.config.resetConfigurationCache();
TBMCChatAPI.RemoveCommands(component);
} }
} }

View file

@ -6,6 +6,7 @@ import buttondevteam.lib.TBMCChatEvent;
import buttondevteam.lib.TBMCChatPreprocessEvent; import buttondevteam.lib.TBMCChatPreprocessEvent;
import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.TBMCSystemChatEvent; import buttondevteam.lib.TBMCSystemChatEvent;
import buttondevteam.lib.architecture.Component;
import buttondevteam.lib.chat.Channel.RecipientTestResult; import buttondevteam.lib.chat.Channel.RecipientTestResult;
import lombok.val; import lombok.val;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -119,10 +120,8 @@ public class TBMCChatAPI {
continue; continue;
TBMCCommandBase c = cmd.newInstance(); TBMCCommandBase c = cmd.newInstance();
c.plugin = plugin; c.plugin = plugin;
if (HasNulls(plugin, c)) CommandCaller.RegisterCommand(c); //Will check for nulls
continue;
commands.put(c.GetCommandPath(), c); commands.put(c.GetCommandPath(), c);
CommandCaller.RegisterCommand(c);
} catch (Exception e) { } catch (Exception e) {
TBMCCoreAPI.SendException("An error occured while registering command " + cmd.getName(), e); TBMCCoreAPI.SendException("An error occured while registering command " + cmd.getName(), e);
} }
@ -155,21 +154,19 @@ public class TBMCChatAPI {
else else
c = thecmdclass.newInstance(); c = thecmdclass.newInstance();
c.plugin = plugin; c.plugin = plugin;
if (HasNulls(plugin, c)) CommandCaller.RegisterCommand(c); //Will check for nulls
return;
commands.put(c.GetCommandPath(), c); commands.put(c.GetCommandPath(), c);
CommandCaller.RegisterCommand(c);
} catch (Exception e) { } catch (Exception e) {
TBMCCoreAPI.SendException("An error occured while registering command " + thecmdclass.getSimpleName(), 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)
/** /**
* <p> * <p>
* 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.
* </p> * </p>
* <p> * <p>
* The <u>command must be registered</u> in the caller plugin's plugin.yml. Otherwise the plugin will output a messsage to console. * The <u>command must be registered</u> in the caller plugin's plugin.yml. Otherwise the plugin will output a message to console.
* </p> * </p>
* <p> * <p>
* <i>Using this method after the server is done loading will have no effect.</i> * <i>Using this method after the server is done loading will have no effect.</i>
@ -181,29 +178,82 @@ public class TBMCChatAPI {
* The command to add * The command to add
*/ */
public static void AddCommand(JavaPlugin plugin, TBMCCommandBase cmd) { public static void AddCommand(JavaPlugin plugin, TBMCCommandBase cmd) {
if (HasNulls(plugin, cmd))
return;
// plugin.getLogger().info("Registering command /" + cmd.GetCommandPath() + " for " + plugin.getName());
try { try {
if (plugin == null) throw new IllegalArgumentException("The plugin is null!");
if (cmd == null) throw new IllegalArgumentException("The command is null!");
cmd.plugin = plugin; cmd.plugin = plugin;
CommandCaller.RegisterCommand(cmd); //Checks for other nulls
commands.put(cmd.GetCommandPath(), cmd); commands.put(cmd.GetCommandPath(), cmd);
CommandCaller.RegisterCommand(cmd);
} catch (Exception e) { } 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) { * <p>
TBMCCoreAPI.SendException("An error occured while registering a command for plugin " + plugin.getName(), * This method adds a plugin's command to help and sets its executor.
new Exception("The command is null!")); * </p>
return true; * <p>
} else if (cmd.GetCommandPath() == null) { * The <u>command must be registered</u> in the caller plugin's plugin.yml. Otherwise the plugin will output a message to console.
TBMCCoreAPI.SendException("An error occured while registering command " + cmd.getClass().getSimpleName() * </p>
+ " for plugin " + plugin.getName(), new Exception("The command path is null!")); * <p>
return true; * <i>Using this method after the server is done loading will have no effect.</i>
* </p>
*
* @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);
}
}
/**
* 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; 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
}
});
} }
/** /**

View file

@ -1,6 +1,8 @@
package buttondevteam.lib.chat; package buttondevteam.lib.chat;
import buttondevteam.lib.architecture.Component;
import javassist.Modifier; import javassist.Modifier;
import lombok.Getter;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
@ -103,4 +105,7 @@ public abstract class TBMCCommandBase {
} }
return modOnly; return modOnly;
} }
@Getter
Component component; //May be null
} }