Skip to content

Instantly share code, notes, and snippets.

@thegalaxydev
Created January 17, 2026 05:18
Show Gist options
  • Select an option

  • Save thegalaxydev/46be0c8fc24f2d50eee2674392fd5f63 to your computer and use it in GitHub Desktop.

Select an option

Save thegalaxydev/46be0c8fc24f2d50eee2674392fd5f63 to your computer and use it in GitHub Desktop.

To hook into events like block breaking in Hytale, you use the EventRegistry provided by the PluginBase (the class your JavaPlugin extends). Unlike some other platforms that use annotations, Hytale uses a functional registration pattern.

1. The Core Pattern

You register a listener by calling getEventRegistry().register(EventClass.class, consumer).

// Inside your plugin or a listener class
getEventRegistry().register(BreakBlockEvent.class, event -> {
    // Your logic here
    System.out.println("Block broken at: " + event.getTargetBlock());
});

2. Implementation Example

Here is a complete example of how to implement and register event listeners.

Create a Listener Class (EventListener.java)

This class contains the logic for the events you want to monitor.

package org.bh.plugin;

import com.hypixel.hytale.logger.HytaleLogger;
import com.hypixel.hytale.server.core.event.events.ecs.BreakBlockEvent;
import com.hypixel.hytale.server.core.event.events.player.PlayerConnectEvent;

public class EventListener {
    private static final HytaleLogger LOGGER = HytaleLogger.forEnclosingClass();

    public static void register(BHPartnerPlugin plugin) {
        // Hook into block breaking events
        plugin.getEventRegistry().register(BreakBlockEvent.class, event -> {
            LOGGER.atInfo().log("Block broken at: " + event.getTargetBlock().toString());
            
            // Example: To cancel the event (if supported by the specific event)
            // event.setCancelled(true);
        });

        // Hook into player connection events
        plugin.getEventRegistry().register(PlayerConnectEvent.class, event -> {
            // Use getPlayerRef() to get the player's reference and username
            LOGGER.atInfo().log("Player connected: " + event.getPlayerRef().getUsername());
        });
    }
}

Register the Listeners in your Plugin (BHPartnerPlugin.java)

The best place to register these is during the setup() phase of your plugin.

@Override
protected void setup() {
    // ... other registrations (commands, etc.)
    
    // Register your event listeners
    EventListener.register(this);
}

3. Key Event Classes

Common events you might want to use:

  • BreakBlockEvent: Triggered when a block is broken. Found in com.hypixel.hytale.server.core.event.events.ecs.
  • PlaceBlockEvent: Triggered when a block is placed. Found in com.hypixel.hytale.server.core.event.events.ecs.
  • PlayerConnectEvent: Triggered when a player joins. Found in com.hypixel.hytale.server.core.event.events.player.
  • PlayerDisconnectEvent: Triggered when a player leaves.

4. Advanced: Event Priority

If you need your listener to run before or after others, you can pass an EventPriority or a numeric value to the register method:

import com.hypixel.hytale.event.EventPriority;

getEventRegistry().register(EventPriority.HIGHEST, BreakBlockEvent.class, event -> {
    // This will run early in the event chain
});

This functional approach makes event handling very clean and easy to manage within your plugin's lifecycle.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment