3 min

Plugin Lifecycle

Understanding the three phases of plugin execution: setup, start, and shutdown.

Lifecycle Overview

Every Hytale plugin extends JavaPlugin and follows a three-phase lifecycle:

phasemethodwhen calleduse for
Setupsetup()Plugin loadingConfiguration, dependency checks
Startstart()Server readyRegister commands, events, start tasks
Shutdownshutdown()Server stoppingCleanup, 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 @Override
10 protected void setup() {
11 // Phase 1: Setup
12 // - Load configuration files
13 // - Initialize connections
14 // - Validate dependencies
15
16 getLogger().info("Loading configuration...");
17 database = new DatabaseConnection(getConfig());
18 }
19
20 @Override
21 protected void start() {
22 // Phase 2: Start
23 // - Register commands
24 // - Register event listeners
25 // - Start scheduled tasks
26 // - Players can join after this
27
28 getEventRegistry().register(
29 PlayerConnectEvent.class,
30 this::onPlayerConnect
31 );
32
33 getLogger().info("Plugin ready!");
34 }
35
36 @Override
37 protected void shutdown() {
38 // Phase 3: Shutdown
39 // - Save player data
40 // - Close connections
41 // - Cancel running tasks
42
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// CORRECT
2public MyPlugin(JavaPluginInit init) {
3 super(init);
4}
5
6// WRONG - Missing super call
7public MyPlugin(JavaPluginInit init) {
8 // Missing super(init)!
9}
10
11// WRONG - Wrong constructor signature
12public MyPlugin() {
13 // Wrong! Must accept JavaPluginInit
14}

Available Services

The JavaPlugin base class provides access to core services:

methodreturnsdescription
getLogger()LoggerPlugin-scoped logging
getEventRegistry()EventRegistryEvent registration
getUniverse()UniverseAccess to worlds/players
getName()StringPlugin name from manifest
getVersion()StringPlugin 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.