Skip to content

Commit

Permalink
Sync registered commands once when one or more commands are added or …
Browse files Browse the repository at this point in the history
…removed
  • Loading branch information
PseudoKnight committed Aug 26, 2018
1 parent 88dbe5f commit 87fdb9a
Showing 1 changed file with 24 additions and 8 deletions.
Expand Up @@ -15,6 +15,8 @@

public class BukkitMCCommandMap implements MCCommandMap {

private static boolean synced = true;

SimpleCommandMap scm;

public BukkitMCCommandMap(SimpleCommandMap invokeMethod) {
Expand All @@ -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
Expand Down Expand Up @@ -57,20 +78,14 @@ public List<MCCommand> getCommands() {
@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;
}

Expand All @@ -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;
Expand Down

0 comments on commit 87fdb9a

Please sign in to comment.