Skip to content
This repository was archived by the owner on Apr 26, 2025. It is now read-only.

Commit d55979d

Browse files
committed
Rework the command system
1 parent e91da04 commit d55979d

File tree

4 files changed

+93
-29
lines changed

4 files changed

+93
-29
lines changed

src/main/java/net/tonimatasdev/perworldplugins/PerWorldPlugins.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ public void onEnable() {
5252
// Convert listeners and set worlds to commands.
5353
ListenerManager.convert();
5454
CommandManager.setWorldsToCommands();
55-
CommandManager.clear();
5655

5756
// Send a message on completion of conversion.
5857
Bukkit.getConsoleSender().sendMessage("[PerWorldPlugins] " + ChatColor.GREEN + "Converted all Listeners correctly.");

src/main/java/net/tonimatasdev/perworldplugins/api/PerWorldCommand.java

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
11
package net.tonimatasdev.perworldplugins.api;
22

33
import net.tonimatasdev.perworldplugins.util.PerWorldUtils;
4+
import org.bukkit.command.Command;
5+
import org.bukkit.command.CommandSender;
46
import org.bukkit.plugin.Plugin;
57

68
import java.util.ArrayList;
79
import java.util.List;
810

9-
public class PerWorldCommand {
10-
private final Plugin plugin;
11-
private List<String> disabledWorlds;
11+
public abstract class PerWorldCommand extends Command {
12+
protected Plugin plugin;
13+
protected List<String> disabledWorlds;
1214

13-
public PerWorldCommand(Plugin plugin) {
15+
public PerWorldCommand(Command command, Plugin plugin) {
16+
// Add all command information.
17+
super(command.getName());
18+
setAliases(command.getAliases());
19+
setDescription(command.getDescription());
20+
setLabel(command.getLabel());
21+
setName(command.getName());
22+
setPermission(command.getPermission());
23+
setPermissionMessage(command.getPermissionMessage());
24+
setUsage(command.getUsage());
25+
// Add plugin and disabled worlds.
1426
this.plugin = plugin;
1527
this.disabledWorlds = new ArrayList<>();
1628
}
@@ -26,4 +38,20 @@ public List<String> getDisabledWorlds() {
2638
public void setDisabledWorlds() {
2739
this.disabledWorlds = PerWorldUtils.getDisabledWorlds(plugin);
2840
}
41+
42+
public void setPlugin(Plugin plugin) {
43+
this.plugin = plugin;
44+
}
45+
46+
public static PerWorldCommand get(Command command, Plugin plugin) {
47+
// Create PerWorldCommand.
48+
return new PerWorldCommand(command, plugin) {
49+
// Use the normal command execute.
50+
@Override
51+
@SuppressWarnings("NullableProblems")
52+
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
53+
return command.execute(sender, commandLabel, args);
54+
}
55+
};
56+
}
2957
}

src/main/java/net/tonimatasdev/perworldplugins/listener/Listeners.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import net.tonimatasdev.perworldplugins.api.PerWorldCommand;
55
import net.tonimatasdev.perworldplugins.manager.CommandManager;
66
import org.bukkit.ChatColor;
7+
import org.bukkit.command.Command;
78
import org.bukkit.event.EventHandler;
89
import org.bukkit.event.EventPriority;
910
import org.bukkit.event.Listener;
@@ -32,16 +33,23 @@ public void enablePlugin(PluginEnableEvent event) {
3233

3334
@EventHandler(priority = EventPriority.MONITOR)
3435
public void onCommandPreProcess(PlayerCommandPreprocessEvent event) {
35-
// Get command.
36+
// Get message.
3637
String message = event.getMessage().split(" ")[0].replaceFirst("/", "");
37-
PerWorldCommand perWorldCommand = CommandManager.commands.get(message.split(":").length > 1 ? message.split(":")[1] : message);
38-
38+
// Get command string.
39+
String commandString = message.split(":").length > 1 ? message.split(":")[1] : message;
40+
// Get command.
41+
Command command = CommandManager.getCommandMap().getCommand(commandString);
3942
// Detects if the command is null.
40-
if (perWorldCommand == null) return;
41-
42-
// Get PerWorldCommand and check if the player is in the disabled world.
43+
if (command == null) return;
44+
// Detects if the command is PerWorldCommand.
45+
if (!(command instanceof PerWorldCommand)) return;
46+
// Get PerWorldCommand.
47+
PerWorldCommand perWorldCommand = (PerWorldCommand) command;
48+
// Check if the player is in the disabled world
4349
if (perWorldCommand.getDisabledWorlds().contains(event.getPlayer().getWorld().getName())) {
50+
// Send block message to the player.
4451
event.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', Objects.requireNonNull(PerWorldPlugins.getInstance().getConfig().getString("disabledCommandMessage"))));
52+
// Cancel the event.
4553
event.setCancelled(true);
4654
}
4755
}

src/main/java/net/tonimatasdev/perworldplugins/manager/CommandManager.java

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,66 @@
99
import org.bukkit.plugin.SimplePluginManager;
1010

1111
import 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

1417
public 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

Comments
 (0)