Permalink
Browse files

Sync registered commands once when one or more commands are added or …

…removed
  • Loading branch information...
PseudoKnight committed Aug 26, 2018
1 parent 88dbe5f commit 87fdb9afd5a8903c9b263e0739cb797c3494bf8f
Showing with 24 additions and 8 deletions.
  1. +24 −8 src/main/java/com/laytonsmith/abstraction/bukkit/BukkitMCCommandMap.java
@@ -15,6 +15,8 @@
public class BukkitMCCommandMap implements MCCommandMap {
private static boolean synced = true;
SimpleCommandMap scm;
public BukkitMCCommandMap(SimpleCommandMap invokeMethod) {
@@ -26,9 +28,28 @@ public Object getHandle() {
return scm;
}
/**
* Syncs the command list with players and other command senders that use the vanilla dispatcher.
*/
private static void SyncCommands() {
if(CommandHelperPlugin.self.isFirstLoad()) {
// Craftbukkit already syncs after plugins are enabled, so we don't have to.
return;
}
if(synced) {
Bukkit.getScheduler().runTask(CommandHelperPlugin.self, () -> {
Server s = Bukkit.getServer();
ReflectionUtils.invokeMethod(s.getClass(), s, "syncCommands");
synced = true;
});
}
synced = false;
}
@Override
public void clearCommands() {
scm.clearCommands();
SyncCommands();
}
@Override
@@ -57,20 +78,14 @@ public MCCommand getCommand(String name) {
@Override
public boolean register(String fallback, MCCommand cmd) {
boolean success = scm.register(fallback, ((BukkitMCCommand) cmd).cmd);
if(!CommandHelperPlugin.self.isFirstLoad()) {
Server s = Bukkit.getServer();
ReflectionUtils.invokeMethod(s.getClass(), s, "syncCommands");
}
SyncCommands();
return success;
}
@Override
public boolean register(String label, String fallback, MCCommand cmd) {
boolean success = scm.register(label, fallback, ((BukkitMCCommand) cmd).cmd);
if(!CommandHelperPlugin.self.isFirstLoad()) {
Server s = Bukkit.getServer();
ReflectionUtils.invokeMethod(s.getClass(), s, "syncCommands");
}
SyncCommands();
return success;
}
@@ -79,6 +94,7 @@ public boolean register(String label, String fallback, MCCommand cmd) {
public boolean unregister(MCCommand cmd) {
if(cmd.isRegistered()) {
((Map<String, Command>) ReflectionUtils.invokeMethod(scm.getClass(), scm, "getKnownCommands")).remove(cmd.getName());
SyncCommands();
return cmd.unregister(this);
} else {
return false;

0 comments on commit 87fdb9a

Please sign in to comment.