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

  1. CommandPreRegistrationEvent is fired (cancellable)
  2. If not cancelled, command is registered to the dispatcher
  3. CommandPostRegistrationEvent is 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:
  • Constructor Details

    • CommandPreRegistrationEvent

      public CommandPreRegistrationEvent(Command<S> command)
      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:
      isCancelled in interface CancellableEvent
      Returns:
      true if the command registration should be prevented, false otherwise
    • 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 a CommandPostRegistrationEvent will be fired indicating the failure.

      Specified by:
      setCancelled in interface CancellableEvent
      Parameters:
      cancelled - true to prevent the command from being registered, false to allow it