Registration
Registration
Section titled “Registration”Package: com.hypixel.hytale.registry
public class RegistrationA handle representing a single item registered with a Registry. Each Registration tracks whether it is still active and provides an unregister() method to remove itself. Registrations are lifecycle-aware: they become inactive either when explicitly unregistered or when the owning registry shuts down.
Fields
Section titled “Fields”@Nonnullprotected final BooleanSupplier isEnabledA supplier that returns true when the owning registry is still active. This is the registry’s enabled state, passed in during construction. When the registry calls shutdown(), this supplier starts returning false, causing isRegistered() to return false even if unregister() was never called.
@Nonnullprotected final Runnable unregisterA callback that performs the actual removal of this registration from the underlying system (e.g., removing an event listener from the EventBus, or removing a command from the dispatcher). Invoked by unregister() if the registration is still active.
private boolean registered = trueInternal flag tracking whether unregister() has been called. Starts as true and is set to false when unregister() executes.
Constructor
Section titled “Constructor”public Registration(@Nonnull BooleanSupplier isEnabled, @Nonnull Runnable unregister)Creates a registration with the given enabled supplier and unregister callback.
- isEnabled — typically the owning Registry’s
isEnabled()method reference. Used to determine whether the registration is still meaningful. - unregister — the callback that performs actual cleanup when
unregister()is called (e.g., removing the listener, deregistering the command).
The registered flag is initially true.
Methods
Section titled “Methods”unregister
Section titled “unregister”public void unregister()Removes this registration. The method checks two conditions before executing the unregister callback:
- The
registeredflag must betrue(i.e.,unregister()has not already been called). - The
isEnabledsupplier must returntrue(i.e., the owning registry has not been shut down).
If both conditions are met, the unregister callback is invoked and the registered flag is set to false. If either condition fails, the method is a no-op. This design ensures:
- Idempotent: calling
unregister()multiple times is safe — the callback runs at most once. - Shutdown-safe: calling
unregister()after the registry has shut down is a no-op rather than an error, because the registry’s shutdown already handled cleanup.
isRegistered
Section titled “isRegistered”public boolean isRegistered()Returns true only if both of the following are true:
- The
registeredflag istrue(the registration has not been explicitly unregistered). - The
isEnabledsupplier returnstrue(the owning registry is still active).
This means a registration can become inactive in two ways:
- Explicit unregister — the plugin calls
registration.unregister(), settingregisteredtofalse. - Registry shutdown — the owning registry shuts down, causing
isEnabledto returnfalse.
In both cases, isRegistered() returns false.
Lifecycle
Section titled “Lifecycle”A Registration handle progresses through these states:
register() | v +-------------------+ | registered = true | | isEnabled = true | | isRegistered() | | returns true | +-------------------+ | | unregister() registry.shutdown() | | v v +-------------------+ | registered = false| (or isEnabled = false) | isRegistered() | | returns false | +-------------------+-
Active — created by Registry.register(). Both
isRegistered()returnstrueand the underlying server-level registration is live. -
Manually unregistered — the plugin calls
unregister(). The unregister callback runs, removing the registration from the server-level manager.isRegistered()returnsfalse. -
Auto-unregistered on shutdown — when the plugin shuts down,
PluginBase.cleanup()iterates the shared shutdown task list in reverse order (LIFO). Each task invokes the unregister callback. TheisEnabledsupplier then returnsfalse(since the registry is shut down), andisRegistered()returnsfalse.
In all cases, the unregister callback runs at most once, preventing double-removal.
Known Subclasses
Section titled “Known Subclasses”| Class | Package | Additional Context |
|---|---|---|
EventRegistration | com.hypixel.hytale.event | Carries the event class this registration listens to |
CommandRegistration | com.hypixel.hytale.server.core.command.system | Holds the AbstractCommand instance that was registered |
These subclasses inherit the lifecycle behavior from Registration and add domain-specific fields.
Usage Example
Section titled “Usage Example”@Overrideprotected void setup() { // Store the handle to optionally unregister later EventRegistration<Void, BootEvent> bootReg = getEventRegistry().register(BootEvent.class, event -> { getLogger().info("Server booted!"); });
// Check if still active if (bootReg.isRegistered()) { getLogger().info("Boot listener is registered."); }
// Manually unregister (optional -- auto-cleaned on shutdown) bootReg.unregister();
// Now returns false assert !bootReg.isRegistered();
// Safe to call again -- no-op bootReg.unregister();}Related Types
Section titled “Related Types”- Registry — the abstract registry that creates and manages registrations
- Registry.RegistrationWrapFunction — wraps registrations with lifecycle tracking
- PluginBase — owns the registries; shutdown triggers cascading cleanup
- EventRegistry — concrete registry whose
register()returns event-specific registrations - CommandRegistry — concrete registry whose registration methods return command-specific registrations
- Registry System Overview — architectural overview of the registry system