99import org .bukkit .plugin .SimplePluginManager ;
1010
1111import java .lang .reflect .Field ;
12- import java .util .*;
12+ import java .util .ArrayList ;
13+ import java .util .Arrays ;
14+ import java .util .List ;
15+ import java .util .Map ;
1316
1417public class CommandManager implements Listener {
15- public static final Map <String , PerWorldCommand > commands = new HashMap <>();
1618 private static final List <String > defaultCommands = Arrays .asList ("version" , "timings" , "reload" , "plugins" , "tps" , "mspt" , "paper" , "spigot" , "restart" , "perworldplugins" );
17- private static final List <Command > registered = new ArrayList <>();
1819
1920 public static void addPluginCommands (Plugin plugin ) {
20- // Get all commands of server command map.
21- for (Command command : getCommandMap ().getCommands ()) {
22- // If the command is default, registered or PluginCommand, skip it.
23- if (defaultCommands .contains (command .getName ()) || registered .contains (command )) continue ;
24- // Put the command to the command map.
25- registered .add (command );
26- commands .put (command .getName (), new PerWorldCommand (plugin ));
21+ // Create PerWorldCommand list.
22+ List <PerWorldCommand > perWorldCommands = new ArrayList <>();
23+ // Create key list.
24+ List <String > registeredKeys = new ArrayList <>();
25+
26+ // Get all keys.
27+ for (String commandKey : getCommands ().keySet ()) {
28+ // Get the command from the key.
29+ Command command = getCommands ().get (commandKey );
30+ // Check if it is default command, PerWorldCommand or is a registered key.
31+ if (defaultCommands .contains (command .getName ()) || command instanceof PerWorldCommand || registeredKeys .contains (commandKey )) continue ;
32+ // Get and add a PerWorldCommand to perWorldCommands list.
33+ perWorldCommands .add (PerWorldCommand .get (command , plugin ));
34+
35+ // Add key to registeredKeys list.
36+ registeredKeys .add (commandKey );
2737 }
38+
39+ // Remove all commands from registeredKeys list.
40+ registeredKeys .forEach (key -> getCommands ().remove (key ));
41+ // Register all commands from perWorldCommands list.
42+ perWorldCommands .forEach (perWorldCommand -> getCommandMap ().register (perWorldCommand .getPlugin ().getName (), perWorldCommand ));
2843 }
2944
3045 public static void setWorldsToCommands () {
3146 // Get all command map values.
32- for (PerWorldCommand command : commands .values ()) {
33- // Set disabled worlds to the command.
34- command .setDisabledWorlds ();
47+ for (Command command : getCommandMap ().getCommands ()) {
48+ // Check if is a PerWorldCommand.
49+ if (command instanceof PerWorldCommand ) {
50+ // Set disabled worlds to the command.
51+ ((PerWorldCommand ) command ).setDisabledWorlds ();
52+ }
3553 }
3654 }
3755
38- private static SimpleCommandMap getCommandMap () {
56+ @ SuppressWarnings ("unchecked" )
57+ public static Map <String , Command > getCommands () {
58+ try {
59+ // Get "knownCommands" field.
60+ Field knownCommandsField = SimpleCommandMap .class .getDeclaredField ("knownCommands" );
61+ // Set accessible.
62+ knownCommandsField .setAccessible (true );
63+
64+ // Return knownCommands field from command map.
65+ return (Map <String , Command >) knownCommandsField .get (getCommandMap ());
66+ } catch (NoSuchFieldException | IllegalAccessException e ) {
67+ throw new RuntimeException (e );
68+ }
69+ }
70+
71+ public static SimpleCommandMap getCommandMap () {
3972 try {
4073 // Get "commandMap" field.
4174 Field commandMapField = SimplePluginManager .class .getDeclaredField ("commandMap" );
@@ -48,8 +81,4 @@ private static SimpleCommandMap getCommandMap() {
4881 throw new RuntimeException (e );
4982 }
5083 }
51-
52- public static void clear () {
53- registered .clear ();
54- }
5584}
0 commit comments