✨ 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:
parent
65684d0036
commit
013d2d61f4
11 changed files with 273 additions and 155 deletions
|
|
@ -30,6 +30,9 @@ import cloud.commandframework.meta.CommandMeta;
|
|||
import cloud.commandframework.meta.SimpleCommandMeta;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.cloudburstmc.server.command.CommandSender;
|
||||
import org.cloudburstmc.server.event.EventPriority;
|
||||
import org.cloudburstmc.server.event.Listener;
|
||||
import org.cloudburstmc.server.event.server.RegistriesClosedEvent;
|
||||
import org.cloudburstmc.server.plugin.Plugin;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
|
@ -67,6 +70,15 @@ public class CloudburstCommandManager<C> extends CommandManager<C> {
|
|||
this.commandSenderMapper = commandSenderMapper;
|
||||
this.backwardsCommandSenderMapper = backwardsCommandSenderMapper;
|
||||
this.owningPlugin = owningPlugin;
|
||||
|
||||
// Prevent commands from being registered when the server would reject them anyways
|
||||
this.owningPlugin.getServer().getPluginManager().registerEvent(
|
||||
RegistriesClosedEvent.class,
|
||||
CloudListener.INSTANCE,
|
||||
EventPriority.NORMAL,
|
||||
(listener, event) -> this.lock(),
|
||||
this.owningPlugin
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -77,11 +89,20 @@ public class CloudburstCommandManager<C> extends CommandManager<C> {
|
|||
return this.backwardsCommandSenderMapper.apply(sender).hasPermission(permission);
|
||||
}
|
||||
|
||||
final void lock() {
|
||||
this.transitionOrThrow(RegistrationState.REGISTERING, RegistrationState.AFTER_REGISTRATION);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final @NonNull CommandMeta createDefaultCommandMeta() {
|
||||
return SimpleCommandMeta.builder().build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean isCommandRegistrationAllowed() {
|
||||
return this.getRegistrationState() != RegistrationState.AFTER_REGISTRATION;
|
||||
}
|
||||
|
||||
final @NonNull Function<@NonNull CommandSender, @NonNull C> getCommandSenderMapper() {
|
||||
return this.commandSenderMapper;
|
||||
}
|
||||
|
|
@ -95,4 +116,11 @@ public class CloudburstCommandManager<C> extends CommandManager<C> {
|
|||
return this.owningPlugin;
|
||||
}
|
||||
|
||||
static final class CloudListener implements Listener {
|
||||
static final CloudListener INSTANCE = new CloudListener();
|
||||
|
||||
private CloudListener() {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue