Class CommandPreRegistrationEvent<S extends CommandSource>
java.lang.Object
studio.mevera.imperat.events.CommandEvent<S>
studio.mevera.imperat.events.types.CommandPreRegistrationEvent<S>
- Type Parameters:
S- the source type that this command supports
- All Implemented Interfaces:
CancellableEvent,Event
public final class CommandPreRegistrationEvent<S extends CommandSource>
extends CommandEvent<S>
implements CancellableEvent
Event fired before a command is registered to the command dispatcher.
This cancellable event allows listeners to intercept and potentially prevent command registration, enabling dynamic command filtering, validation, and conditional registration based on runtime conditions.
When this event is cancelled by setting setCancelled(boolean) to
true, the command will not be registered to the dispatcher, and a
CommandPostRegistrationEvent will still be fired with failure information.
Common Use Cases
- Preventing specific commands from being registered based on configuration
- Implementing permission-based command filtering at registration time
- Logging and monitoring command registrations for debugging
- Validating command metadata before allowing registration
- Dynamically enabling/disabling commands based on server state
- Preventing conflicting commands from being registered
Event Lifecycle
CommandPreRegistrationEventis fired (cancellable)- If not cancelled, command is registered to the dispatcher
CommandPostRegistrationEventis fired with success/failure status
Example: Blocking Specific Commands
eventBus.register(CommandPreRegistrationEvent.class, event -> {
RootCommand<?> command = event.getCommand();
// Prevent registration of dangerous commands
if (command.getName().equals("dangerous-command")) {
event.setCancelled(true);
logger.warn("Blocked registration of dangerous command");
}
});
Example: Configuration-Based Filtering
eventBus.register(CommandPreRegistrationEvent.class, event -> {
RootCommand<?> command = event.getCommand();
// Only allow commands listed in configuration
if (!config.isCommandEnabled(command.getName())) {
event.setCancelled(true);
logger.info("RootCommand '" + command.getName() + "' is disabled in config");
}
}, Priority.HIGH);
Example: Permission-Based Registration
eventBus.register(CommandPreRegistrationEvent.class, event -> {
RootCommand<?> command = event.getCommand();
// Check if the server has permission to register this command
if (command.getPermission() != null && !serverHasPermission(command.getPermission())) {
event.setCancelled(true);
logger.warn("Server lacks permission to register: " + command.getName());
}
});
Example: Logging and Monitoring
eventBus.register(CommandPreRegistrationEvent.class, event -> {
RootCommand<?> command = event.getCommand();
logger.info("Registering command: " + command.getName());
logger.debug(" Aliases: " + command.getAliases());
logger.debug(" Permission: " + command.getPermission());
// Track registration metrics
metrics.incrementCommandRegistrations();
}, Priority.LOWEST); // Run last to ensure other handlers run first
- Since:
- 1.0
- See Also:
-
Field Summary
Fields inherited from class studio.mevera.imperat.events.CommandEvent
command -
Constructor Summary
ConstructorsConstructorDescriptionCommandPreRegistrationEvent(Command<S> command) Constructs a new command pre-registration event. -
Method Summary
Modifier and TypeMethodDescriptionbooleanChecks whether the command registration has been canceled.voidsetCancelled(boolean cancelled) Sets whether the command registration should be cancelled.Methods inherited from class studio.mevera.imperat.events.CommandEvent
getCommand
-
Constructor Details
-
CommandPreRegistrationEvent
Constructs a new command pre-registration event.- Parameters:
command- the command that is about to be registered
-
-
Method Details
-
isCancelled
public boolean isCancelled()Checks whether the command registration has been canceled.- Specified by:
isCancelledin interfaceCancellableEvent- Returns:
trueif the command registration should be prevented,falseotherwise
-
setCancelled
public void setCancelled(boolean cancelled) Sets whether the command registration should be cancelled.If set to
true, the command will not be registered to the dispatcher, and aCommandPostRegistrationEventwill be fired indicating the failure.- Specified by:
setCancelledin interfaceCancellableEvent- Parameters:
cancelled-trueto prevent the command from being registered,falseto allow it
-