Events
Custom events that to work across mod loaders
General
The Events are registered via a custom Event Factory. You can register listeners for the Event by using Event.register(handler);
, where the handler
is an instance of the Event Type. Events can be invoked, by using Event.invoke()
. this returns an Event Type, that combines every registered listener Event Type.
Event Types
These are Functional Interfaces, that will be used for listener.
For example, the following is a valid Event Type:
@FunctionalInterface
public interface PlayerJoin {
void join(ServerPlayer player);
}
You can create an Event using the following:
Depending on your Return Type, you need to use a different method of the EventFactory!
public static final Event<PlayerJoin> PLAYER_JOIN = EventFactory.createWithVoid();
A Listener can then be registered:
PLAYER_JOIN.register(player -> LOGGER.debug("player joined"));
And then the Event can be invoked:
PLAYER_JOIN.invoke().join(player);
Event Return Types
These are returned by the Event Type.
Void
Listeners for events with this type can’t return anything.
For example:
public static final Event<PlayerJoin> PLAYER_JOIN = EventFactory.createWithVoid();
InteractionResult
Listeners for events with this type must return an InteractionResult.
When the InteractionResult
is not InteractionResult.PASS
, the event is cancelled and so is the underlying Forge/Fabric/NeoForge event. If the listener returns an InteractionResult.FAIL
, the task the event was running on, is also cancelled. (For Example, when returning an InteractionResult.FAIL
for an RenderEvents.RENDER_MOUNT_HEALTH
, the Mount Health will not be rendered).
For example:
public static final Event<AllowSleepTime> ALLOW_SLEEP_TIME = EventFactory.createWithInteractionResult();
Custom Callback
These events can require any return argument, and therefore, the usage and type is defined per event in the respective event section. When registering such an event, you also have to define some method to handle the different return values.
For example:
public static final Event<SleepFinishedTime> SLEEP_FINISHED_TIME = EventFactory.createWithCallback(callbacks -> (level, newTime) -> {
long newNewTime = 0;
for (SleepFinishedTime callback : callbacks) {
long newTimeIn = callback.setTimeAddition(level, newTime);
if (level.getDayTime() <= newTimeIn) {
newNewTime = newTimeIn;
}
}
return newNewTime;
});
Client Events
These Events only run on the Client-Side. Therefore, the bytecode is only present for clients.
Client Player Events
World Join
The following example prints the text “client player joined” in the client log, once the player joined a World. Return Type: Void
ClientPlayerEvents.CLIENT_PLAYER_JOIN.register(player -> LOGGER.debug("client player joined"));
Quit World
The following example prints the text “client player quit” in the client log, once the player leaves the world. Return Type: Void
ClientPlayerEvents.CLIENT_PLAYER_QUIT.register(player -> LOGGER.debug("client player quit"));
Respawn
The following example prints the text “client player respawned” in the client log while the player is recreated. Return Type: Void
ClientPlayerEvents.CLIENT_PLAYER_RESPAWN.register((oldPlayer, newPlayer) -> LOGGER.debug("client player respawned"));
Render Events
HUD Rendering
This Event is called while the HUD is rendered. Return Type: Void
RenderEvents.HUD_RENDERING.register((graphics, tickDelta) -> LOGGER.debug("Rendering HUD"));
On Health Bar Rendering
This Event is called when the Health bar is rendered. Return Type: InteractionResult
Depending on the Minecraft version and the mod-loader, the graphics argument might be null
!
RenderEvents.RENDER_MOUNT_HEALTH.register((player, graphics) -> LOGGER.debug("Rendering Health Bar"));
On Food Bar Rendering
This Event is called when the Food bar is rendered. Return Type: InteractionResult
Depending on the Minecraft version and the mod-loader, the graphics argument might be null
!
RenderEvents.RENDER_FOOD.register((player, graphics) -> LOGGER.debug("Rendering Food Bar"));
On Breath Bar Rendering
This Event is called when the Breath bar is rendered. Return Type: InteractionResult
Depending on the Minecraft version and the mod-loader, the graphics argument might be null
!
RenderEvents.RENDER_BREATH.register((player, graphics) -> LOGGER.debug("Rendering Breath Bar"));
On Mount Health Bar Rendering
This Event is called when the Mount Health bar is rendered. Return Type: InteractionResult
Depending on the Minecraft version and the mod-loader, the graphics argument might be null
!
RenderEvents.RENDER_MOUNT_HEALTH.register((player, graphics) -> LOGGER.debug("Rendering Mount Health Bar"));
Client Tick Events
Pre Tick
This Event is called before the Minecraft Client ticks. Return Type: Void
ClientTickEvents.CLIENT_PRE.register(mc -> LOGGER.debug("pre tick"));
Post Tick
This Event is called after the Minecraft Client ticks. Return Type: Void
ClientTickEvents.CLIENT_POST.register(mc -> LOGGER.debug("post tick"));
Common Events
Events, that will run on the server-side, but can also run on the client-side, depending on the Event.
Command Events
Events, that have something to do with Commands
Register Commands
This Event is called when the commands are registered, so you can register custom commands. Return Type: Void
CommandEvents.REGISTRATION.register((dispatcher, registry, selection) -> LOGGER.debug("registering commands"));
Entity Events
On Interact with Player
Triggered, when the player interacts with an Entity Return Type: InteractionResult
EntityEvents.INTERACT_WITH_PLAYER.register((player, entity, interactionHand) -> {
LOGGER.debug("player interacted with an entity");
return InteractionResult.PASS;
});
On Death
Triggered, when an Entity is about to die. Return Type: InteractionResult
EntityEvents.LIVING_DEATH.register((player, entity, interactionHand) -> {
LOGGER.debug("an entity is about to die");
return InteractionResult.PASS;
});
On Breathe
Triggered, when an Entity is breathing. Return Type: InteractionResult
EntityEvents.LIVING_BREATHE.register((entity, canBreathe) -> {
LOGGER.debug("some entity can " + (canBreathe ? "" : "not ") + breathe);
return InteractionResult.PASS;
});
Player Events
World Join
The following example prints the text “player joined” in server log, once the player joined a World. Return Type: Void
PlayerEvents.PLAYER_JOIN.register(player -> LOGGER.debug("player joined"));
Quit World
The following example prints the text “player quit” in server log, once the player leaves the world. Return Type: Void
PlayerEvents.PLAYER_JOIN.register(player -> LOGGER.debug("player quit"));
Respawn
The following example prints the text “player respawned” in the server log while the player is recreated. Return Type: Void
PlayerEvents.PLAYER_RESPAWN.register((oldPlayer, newPlayer) -> LOGGER.debug("player respawned"));
Award Advancement
Triggered, when the player is granted a new advancement Return Type: Void
PlayerEvents.AWARD_ADVANCEMENT.register((player, advancement, criterionKey) -> LOGGER.debug("player was an advancement granted"));
Revoke Advancement
Triggered, when the player is loses an advancement Return Type: Void
PlayerEvents.REVOKE_ADVANCEMENT.register((player, advancement, criterionKey) -> LOGGER.debug("player lost am advancement"));
Allow Sleep Time
Triggered, when the game checks, whether the player can sleep that this time Return Type: Void
PlayerEvents.ALLOW_SLEEP_TIME.register((player, sleepingPos, vanillaResult) -> {
LOGGER.debug("checking sleep time");
return InteractionResult.PASS;
});
Finished Sleep Time
Triggered, when the game calculates the new time after the players slept. It returns the in game time, the players are waking up to. Return Type: Custom Callback, Returns: long
PlayerEvents.SLEEP_FINISHED_TIME.register((level, newTime) -> {
LOGGER.debug("players are waking up at " + newTime);
return newTime;
});
Destroy Speed
Triggered, when the game calculates the block breaking speed. It returns the block breaking speed after optional custom processing. Return Type: Custom Callback, Returns: float
PlayerEvents.DESTROY_SPEED.register((player, newSpeed) -> {
LOGGER.debug("player can mine as fast as " + newSpeed);
return newTime;
});
Resource Events
On Datapack Sync
Called, when the game sync the datapacks Return Type: Void
ResourceEvents.DATA_PACK_SYNC.register(player -> LOGGER.debug("syncing Datapacks to some player"));
Server Level Events
Level Load
Called, when the game loads the server level Return Type: Void
ServerLevelEvents.LEVEL_LOAD.register(serverLevel -> LOGGER.debug("loading serverlevel"));
Level Unload
Called, when the game unloads the server level Return Type: Void
ServerLevelEvents.LEVEL_UNLOAD.register(serverLevel -> LOGGER.debug("unloading serverlevel"));
Continue with Configuration