In this tutorial you will learn to:
Events are represented by instances of net.fabricmc.fabric.api.event.Event
which store and call callbacks. Often there is a single event instance for a callback, which is stored in a static field EVENT
of the callback interface, but there are other patterns. For example ClientTickEvents groups several related events together.
Each event has a corresponding callback interface, conventionally named EventNameCallback
. Callbacks are registered by calling register()
on an event instance with an instance of the callback interface as the argument.
All event callback interfaces provided by Fabric API can be found in the net.fabricmc.fabric.api.event
package.
A partial list of existing callbacks is provided at the bottom of this tutorial.
Although there are plenty of events already provided by Fabric API, you can still make your own events. Please refer to events.
This example registers an AttackBlockCallback
to damage players when they hit blocks that don't drop when hand-mined. It returns ActionResult.PASS
as other callbacks should still be called. See the AttackBlockCallback JavaDoc in your IDE for the meaning of other values.
public class ExampleMod implements ModInitializer { [...] @Override public void onInitialize() { AttackBlockCallback.EVENT.register((player, world, hand, pos, direction) -> { BlockState state = world.getBlockState(pos); /* Manual spectator check is necessary because AttackBlockCallbacks fire before the spectator check */ if (state.isToolRequired() && !player.isSpectator() && player.getMainHandStack().isEmpty()) { player.damage(DamageSource.field_5869, 1.0F); } return ActionResult.PASS; }); } }
Player: AttackBlockCallback / AttackEntityCallback / UseBlockCallback / UseEntityCallback / UseItemCallback
Player (Client): ClientPickBlockApplyCallback / ClientPickBlockCallback / ClientPickBlockGatherCallback
BlockConstructedCallback / ItemConstructedCallback
RegistryEntryAddedCallback / RegistryEntryRemovedCallback / RegistryIdRemapCallback
There is an example of using LootTableLoadingCallback
here.
ServerLifecycleEvents.ServerStarted / ServerLifecycleEvents.ServerStopped / ServerTickEvents.StartTick / ServerTickEvents.EndTick
C2SPacketTypeCallback / S2CPacketTypeCallback
See Event Index for a more complete and updated list.