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>
<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$/ButtonProcessor/ButtonProcessor.iml" filepath="$PROJECT_DIR$/ButtonProcessor/ButtonProcessor.iml" />
</modules>
</component>

View file

@ -21,41 +21,27 @@ public class CommandCaller implements CommandExecutor {
public static void RegisterCommand(TBMCCommandBase cmd) throws Exception {
if (instance == null)
instance = new CommandCaller();
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);
{
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);
String[] helptext = cmd.GetHelpText(topcmd);
if (helptext == null || helptext.length == 0)
throw new Exception("Command " + cmd.GetCommandPath() + " has no help text!");
pc.setUsage(helptext.length > 1 ? helptext[1] : helptext[0]);
}
}
String[] topcmd = new String[1]; //Holds out param
PluginCommand pc = getPluginCommand(cmd, topcmd);
pc.setExecutor(instance);
String[] helptext = cmd.GetHelpText(topcmd[0]);
if (helptext == null || helptext.length == 0)
throw new Exception("Command " + cmd.GetCommandPath() + " has no help text!");
pc.setUsage(helptext.length > 1 ? helptext[1] : helptext[0]);
}
@Override
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)
path += " " + arg;
TBMCCommandBase cmd = TBMCChatAPI.GetCommands().get(path);
path.append(" ").append(arg);
TBMCCommandBase cmd = TBMCChatAPI.GetCommands().get(path.toString());
int argc = 0;
String[] subcmds = null;
while (cmd == null && (subcmds = TBMCChatAPI.GetSubCommands(path, sender)).length == 0 && path.contains(" ")) {
path = path.substring(0, path.lastIndexOf(' '));
while (cmd == null && (subcmds = TBMCChatAPI.GetSubCommands(path.toString(), sender)).length == 0 && path.toString().contains(" ")) {
path = new StringBuilder(path.substring(0, path.toString().lastIndexOf(' ')));
argc++;
cmd = TBMCChatAPI.GetCommands().get(path);
cmd = TBMCChatAPI.GetCommands().get(path.toString());
}
if (cmd == null) {
if (subcmds.length > 0) //Subcmds will always have value here (see assignment above)
@ -85,4 +71,37 @@ public class CommandCaller implements CommandExecutor {
}
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 org.bukkit.Bukkit;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginDescriptionFile;
@ -114,4 +116,10 @@ public class MainPlugin extends JavaPlugin {
}
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;
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);
}

View file

@ -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);
}
}

View file

@ -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,21 +154,19 @@ 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)
/**
* <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>
* 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>
* <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
*/
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;
/**
* <p>
* This method adds a plugin's command to help and sets its executor.
* </p>
* <p>
* 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>
* <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);
}
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
}
});
}
/**

View file

@ -1,6 +1,8 @@
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;
@ -103,4 +105,7 @@ public abstract class TBMCCommandBase {
}
return modOnly;
}
@Getter
Component component; //May be null
}