Give CommandManager a registration state (#148)

* Make CommandManager track its availability for registration

This prevents situations where changes to the manager
would result in undefined state in other places.

* Add unsafe registration capability

* Very minor formatting + `@since` tags

* Add changes to changelog

Co-authored-by: Alexander Söderberg <sauilitired@gmail.com>
This commit is contained in:
zml 2020-11-29 06:29:41 -08:00 committed by Alexander Söderberg
parent 65684d0036
commit 013d2d61f4
11 changed files with 273 additions and 155 deletions

View file

@ -182,9 +182,9 @@ public class BukkitCommandManager<C> extends CommandManager<C> implements Brigad
this.getParserRegistry().registerParserSupplier(TypeToken.get(MultiplePlayerSelector.class), parserParameters ->
new MultiplePlayerSelectorArgument.MultiplePlayerSelectorParser<>());
/* Register suggestion listener */
/* Register suggestion and state listener */
this.owningPlugin.getServer().getPluginManager().registerEvents(
new CommandSuggestionsListener<>(this),
new CloudBukkitListener<>(this),
this.owningPlugin
);
@ -241,6 +241,7 @@ public class BukkitCommandManager<C> extends CommandManager<C> implements Brigad
}
protected final void setSplitAliases(final boolean value) {
this.requireState(RegistrationState.BEFORE_REGISTRATION);
this.splitAliases = value;
}
@ -311,6 +312,7 @@ public class BukkitCommandManager<C> extends CommandManager<C> implements Brigad
* supported by the platform
*/
public void registerBrigadier() throws BrigadierFailureException {
this.requireState(RegistrationState.BEFORE_REGISTRATION);
this.checkBrigadierCompatibility();
try {
final CloudCommodoreManager<C> cloudCommodoreManager = new CloudCommodoreManager<>(this);
@ -369,6 +371,12 @@ public class BukkitCommandManager<C> extends CommandManager<C> implements Brigad
return this.backwardsCommandSenderMapper;
}
final void lockIfBrigadierCapable() {
if (this.minecraftVersion >= BRIGADIER_MINIMUM_VERSION) {
this.transitionOrThrow(RegistrationState.REGISTERING, RegistrationState.AFTER_REGISTRATION);
}
}
/**
* Reasons to explain why Brigadier failed to initialize
*/

View file

@ -25,18 +25,20 @@ package cloud.commandframework.bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.event.server.TabCompleteEvent;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.ArrayList;
import java.util.List;
final class CommandSuggestionsListener<C> implements Listener {
final class CloudBukkitListener<C> implements Listener {
private final BukkitCommandManager<C> bukkitCommandManager;
CommandSuggestionsListener(final @NonNull BukkitCommandManager<C> bukkitCommandManager) {
CloudBukkitListener(final @NonNull BukkitCommandManager<C> bukkitCommandManager) {
this.bukkitCommandManager = bukkitCommandManager;
}
@ -67,4 +69,13 @@ final class CommandSuggestionsListener<C> implements Listener {
event.setCompletions(suggestions);
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
void onPlayerLogin(final @NonNull PlayerLoginEvent event) {
/* If the server is brigadier-capable, any registration after players
have joined (and been sent a command tree) is unsafe.
Bukkit's PlayerJoinEvent is called just after the command tree is sent,
so we have to perform this state change at PlayerLoginEvent to lock before that happens. */
this.bukkitCommandManager.lockIfBrigadierCapable();
}
}