Skip to content

Commit e6fe003

Browse files
committed
Add VaxCommandRegistry class to manage command registration
This class provides methods for registering and unregistering commands, utilizing a thread-safe map and server command map integration. It ensures that command updates are reflected for online players in real-time.
1 parent 276a7f4 commit e6fe003

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package de.varilx.command.registry;
2+
3+
4+
import de.varilx.BaseAPI;
5+
import de.varilx.command.VaxCommand;
6+
import lombok.AccessLevel;
7+
import lombok.SneakyThrows;
8+
import lombok.experimental.FieldDefaults;
9+
import org.bukkit.Bukkit;
10+
import org.bukkit.command.CommandMap;
11+
import org.bukkit.entity.Player;
12+
import org.bukkit.plugin.Plugin;
13+
14+
import java.util.Map;
15+
import java.util.concurrent.ConcurrentHashMap;
16+
17+
@FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true)
18+
public class VaxCommandRegistry {
19+
20+
Plugin plugin;
21+
Map<String, VaxCommand> commands;
22+
23+
public VaxCommandRegistry() {
24+
this.plugin = BaseAPI.getBaseAPI().getPlugin();
25+
this.commands = new ConcurrentHashMap<>();
26+
}
27+
28+
public void registerCommand(VaxCommand command) {
29+
commands.put(command.getName(), command);
30+
}
31+
32+
public void unregisterCommand(String command) {
33+
CommandMap commandMap = Bukkit.getServer().getCommandMap();
34+
35+
commandMap.getKnownCommands().remove(command);
36+
Bukkit.getOnlinePlayers().forEach(Player::updateCommands);
37+
}
38+
39+
@SneakyThrows
40+
private void registerServerCommand(VaxCommand vaxCommand) {
41+
CommandMap commandMap = Bukkit.getServer().getCommandMap();
42+
commandMap.register("", vaxCommand);
43+
commandMap.getKnownCommands().put(vaxCommand.getName(), vaxCommand);
44+
Bukkit.getOnlinePlayers().forEach(Player::updateCommands);
45+
}
46+
47+
48+
}

0 commit comments

Comments
 (0)