Lifecycle Overview
Every Hytale plugin extends JavaPlugin and follows a three-phase lifecycle:
| phase | method | when called | use for |
|---|---|---|---|
| Setup | setup() | Plugin loading | Configuration, dependency checks |
| Start | start() | Server ready | Register commands, events, start tasks |
| Shutdown | shutdown() | Server stopping | Cleanup, save data, cancel tasks |
Complete Example
MyPlugin.javajava
1public class MyPlugin extends JavaPlugin {2 3 private DatabaseConnection database;4 5 public MyPlugin(JavaPluginInit init) {6 super(init);7 }8 9 @Override10 protected void setup() {11 // Phase 1: Setup12 // - Load configuration files13 // - Initialize connections14 // - Validate dependencies15 16 getLogger().info("Loading configuration...");17 database = new DatabaseConnection(getConfig());18 }19 20 @Override21 protected void start() {22 // Phase 2: Start23 // - Register commands24 // - Register event listeners25 // - Start scheduled tasks26 // - Players can join after this27 28 getEventRegistry().register(29 PlayerConnectEvent.class, 30 this::onPlayerConnect31 );32 33 getLogger().info("Plugin ready!");34 }35 36 @Override37 protected void shutdown() {38 // Phase 3: Shutdown39 // - Save player data40 // - Close connections41 // - Cancel running tasks42 43 if (database != null) {44 database.close();45 }46 47 getLogger().info("Plugin stopped.");48 }49 50 private void onPlayerConnect(PlayerConnectEvent event) {51 Player player = event.getPlayer();52 getLogger().info("Player joined: " + player.getName());53 }54}Constructor Requirements
critical
The constructor MUST call
super(init) and accept a JavaPluginInit parameter. Failure to do this will crash the server on startup.1// CORRECT2public MyPlugin(JavaPluginInit init) {3 super(init);4}56// WRONG - Missing super call7public MyPlugin(JavaPluginInit init) {8 // Missing super(init)!9}1011// WRONG - Wrong constructor signature12public MyPlugin() {13 // Wrong! Must accept JavaPluginInit14}Available Services
The JavaPlugin base class provides access to core services:
| method | returns | description |
|---|---|---|
| getLogger() | Logger | Plugin-scoped logging |
| getEventRegistry() | EventRegistry | Event registration |
| getUniverse() | Universe | Access to worlds/players |
| getName() | String | Plugin name from manifest |
| getVersion() | String | Plugin version from manifest |
Best Practices
do
Keep setup() fast - only load configuration and validate.
do
Register all commands and events in start(), not setup().
do
Always cleanup resources in shutdown() to prevent memory leaks.
avoid
Don't access players in setup() - they aren't connected yet.