0

Added the ability to unload using the fallback.*

* Unfortunately it still won't remove the commands from the TabComplete
* Also, changed the error msg from an unregistered command to help
lessen the confusion on the previous issue.
This commit is contained in:
Minenash
2019-01-13 00:32:55 -05:00
parent 8f2200619e
commit ff281241b5
3 changed files with 51 additions and 8 deletions

View File

@@ -11,6 +11,7 @@ import java.lang.reflect.Method;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
@@ -154,6 +155,7 @@ public class CommandManager {
} }
reader.close(); reader.close();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace();
plugin.getLogger().log(Level.WARNING, "Error while loading command file. (" + sourceFile.getAbsolutePath() + ")"); plugin.getLogger().log(Level.WARNING, "Error while loading command file. (" + sourceFile.getAbsolutePath() + ")");
plugin.getLogger().log(Level.WARNING, e.getCause().toString()); plugin.getLogger().log(Level.WARNING, e.getCause().toString());
errors = true; errors = true;
@@ -325,6 +327,41 @@ public class CommandManager {
} }
} }
public static void unregisterAllWithFallback(String fallback) {
try {
final Field cmdMap = Bukkit.getServer().getClass().getDeclaredField("commandMap");
cmdMap.setAccessible(true);
CommandMap map = (CommandMap) cmdMap.get(Bukkit.getServer());
final Field knownCommandsField = map.getClass().getSuperclass().getDeclaredField("knownCommands");
knownCommandsField.setAccessible(true);
@SuppressWarnings("unchecked")
Map<String, org.bukkit.command.Command> knownCommands = (Map<String, org.bukkit.command.Command>) knownCommandsField.get(map);
fallback = fallback.toLowerCase();
System.out.println(fallback);
List<String> toRemove = new ArrayList<>();
for (String key: knownCommands.keySet()) {
org.bukkit.command.Command value = knownCommands.get(key);
if ((value instanceof Executable) && ((Executable)value).getMethodContainerName().equals(fallback)) {
toRemove.add(key);
value.unregister(map);
}
}
for (String key : toRemove) {
//EmptyCommand emptyCommand = new EmptyCommand(key);
knownCommands.remove(key);
//map.register(key, emptyCommand);
System.out.println("Unregestered " + key);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/** /**
* Parses the source code into an abstract command syntax * Parses the source code into an abstract command syntax
* *
@@ -719,7 +756,7 @@ public class CommandManager {
components.help = null; components.help = null;
components.permission = null; components.permission = null;
components.type = null; components.type = null;
Executable cmd = new Executable(cmdName, constructHelpPages(cmdName, components)); Executable cmd = new Executable(cmdName, constructHelpPages(cmdName, components), methodContainer.getClass().getSimpleName());
cmd.register(methods, plugin, methodContainer, components.getAliases()); cmd.register(methods, plugin, methodContainer, components.getAliases());
} }

View File

@@ -8,13 +8,13 @@ public class EmptyCommand extends Executable
{ {
public EmptyCommand(String name) public EmptyCommand(String name)
{ {
super(name, null); super(name, null, "");
} }
@Override @Override
public boolean execute(CommandSender sender, String name, String[] args_) public boolean execute(CommandSender sender, String name, String[] args_)
{ {
sender.sendMessage("§cUnknown command. Use §e/help§c, §e/plugins§c or ask a mod."); sender.sendMessage("§cCommand no longer Exists. Use §e/help§c, §e/plugins§c or ask a mod.");
return true; return true;
} }
} }

View File

@@ -35,17 +35,23 @@ public class Executable extends org.bukkit.command.Command {
private ArrayList<ExecutableDefinition> commands; private ArrayList<ExecutableDefinition> commands;
private ArrayList<HelpPageCommand[]> help; private ArrayList<HelpPageCommand[]> help;
private String name; private String name;
private String methodContainerName;
private JavaPlugin plugin; private JavaPlugin plugin;
public Executable(String name, ArrayList<HelpPageCommand[]> help) { public Executable(String name, ArrayList<HelpPageCommand[]> help, String methodContainerName) {
super(name); super(name);
this.help = help; this.help = help;
this.name = name; this.name = name;
this.commands = new ArrayList<ExecutableDefinition>(); this.commands = new ArrayList<ExecutableDefinition>();
this.methodContainerName = methodContainerName;
}
public String getMethodContainerName() {
return methodContainerName;
} }
public void register(ArrayList<Method> methods, JavaPlugin plugin, Object methodContainer, ArrayList<String> aliases) { public void register(ArrayList<Method> methods, JavaPlugin plugin, Object methodContainer, ArrayList<String> aliases) {
String moduleName = methodContainer.getClass().getSimpleName(); methodContainerName = methodContainer.getClass().getSimpleName().toLowerCase();
for (HelpPageCommand[] page : help) { for (HelpPageCommand[] page : help) {
for (HelpPageCommand cmd : page) { for (HelpPageCommand cmd : page) {
if (cmd != null) { if (cmd != null) {
@@ -95,13 +101,13 @@ public class Executable extends org.bukkit.command.Command {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Command> knownCommands = (Map<String, Command>) knownCommandsField.get(map); Map<String, Command> knownCommands = (Map<String, Command>) knownCommandsField.get(map);
knownCommands.remove(name); knownCommands.remove(name);
map.register(moduleName, this); map.register(methodContainerName, this);
for (String alias : aliases) { for (String alias : aliases) {
Executable cmd = new Executable(alias, this.help); Executable cmd = new Executable(alias, this.help, methodContainerName);
cmd.commands = this.commands; cmd.commands = this.commands;
cmd.plugin = this.plugin; cmd.plugin = this.plugin;
knownCommands.remove(alias); knownCommands.remove(alias);
map.register(moduleName, cmd); map.register(methodContainerName, cmd);
} }
} catch (Exception e) { } catch (Exception e) {
plugin.getLogger().log(Level.SEVERE, "Failed to register command '" + name + "'!"); plugin.getLogger().log(Level.SEVERE, "Failed to register command '" + name + "'!");