====== Listening to Events ====== In this tutorial you will learn to: - Understand Events and Callbacks - Register a callback for an existing Event ===== Events ===== 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 [[https://github.com/FabricMC/fabric/blob/1.17/fabric-lifecycle-events-v1/src/main/java/net/fabricmc/fabric/api/client/event/lifecycle/v1/ClientTickEvents.java|ClientTickEvents]] groups several related events together. ===== Callbacks ===== 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. ==== Callback Interfaces in Fabric API ==== 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. ==== Custom Callbacks ==== Although there are plenty of events already provided by Fabric API, you can still make your own events. Please refer to [[tutorial:events]]. ===== Practice ====== This example registers an ''AttackBlockCallback'' to damage players when they hit blocks that don't drop when hand-mined. It returns '''' as other callbacks should still be called. See the [[https://github.com/FabricMC/fabric/blob/12865e786ce2102d344304a679b70084be84d166/fabric-events-interaction-v0/src/main/java/net/fabricmc/fabric/api/event/player/AttackBlockCallback.java#L29-L39|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) -> { class_2680 state = world.method_8320(pos); /* Manual spectator check is necessary because AttackBlockCallbacks fire before the spectator check */ if (state.method_29291() && !player.method_7325() && player.method_6047().method_7960()) { player.method_5643(class_1282.field_5869, 1.0F); } return class_1269.field_5811; }); } } ===== Fabric API Events ====== === Player Interaction Events === Player: [[https://github.com/FabricMC/fabric/blob/1.15/fabric-events-interaction-v0/src/main/java/net/fabricmc/fabric/api/event/player/AttackBlockCallback.java|AttackBlockCallback]] / [[https://github.com/FabricMC/fabric/blob/1.15/fabric-events-interaction-v0/src/main/java/net/fabricmc/fabric/api/event/player/AttackEntityCallback.java|AttackEntityCallback]] / [[https://github.com/FabricMC/fabric/blob/1.15/fabric-events-interaction-v0/src/main/java/net/fabricmc/fabric/api/event/player/UseBlockCallback.java|UseBlockCallback]] / [[https://github.com/FabricMC/fabric/blob/1.15/fabric-events-interaction-v0/src/main/java/net/fabricmc/fabric/api/event/player/UseEntityCallback.java|UseEntityCallback]] / [[https://github.com/FabricMC/fabric/blob/1.15/fabric-events-interaction-v0/src/main/java/net/fabricmc/fabric/api/event/player/UseItemCallback.java|UseItemCallback]] Player (Client): [[https://github.com/FabricMC/fabric/blob/1.15/fabric-events-interaction-v0/src/main/java/net/fabricmc/fabric/api/event/client/player/ClientPickBlockApplyCallback.java|ClientPickBlockApplyCallback]] / [[https://github.com/FabricMC/fabric/blob/1.15/fabric-events-interaction-v0/src/main/java/net/fabricmc/fabric/api/event/client/player/ClientPickBlockCallback.java|ClientPickBlockCallback]] / [[https://github.com/FabricMC/fabric/blob/1.15/fabric-events-interaction-v0/src/main/java/net/fabricmc/fabric/api/event/client/player/ClientPickBlockGatherCallback.java|ClientPickBlockGatherCallback]] === Registry Events === [[https://github.com/FabricMC/fabric/blob/1.15/fabric-object-builders-v0/src/main/java/net/fabricmc/fabric/api/event/registry/BlockConstructedCallback.java|BlockConstructedCallback]] / [[https://github.com/FabricMC/fabric/blob/1.15/fabric-object-builders-v0/src/main/java/net/fabricmc/fabric/api/event/registry/ItemConstructedCallback.java|ItemConstructedCallback]] [[https://github.com/FabricMC/fabric/blob/1.15/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/api/event/registry/RegistryEntryAddedCallback.java|RegistryEntryAddedCallback]] / [[https://github.com/FabricMC/fabric/blob/1.15/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/api/event/registry/RegistryEntryRemovedCallback.java|RegistryEntryRemovedCallback]] / [[https://github.com/FabricMC/fabric/blob/1.15/fabric-registry-sync-v0/src/main/java/net/fabricmc/fabric/api/event/registry/RegistryIdRemapCallback.java|RegistryIdRemapCallback]] === Looting Events === [[https://github.com/FabricMC/fabric/blob/1.15/fabric-loot-tables-v1/src/main/java/net/fabricmc/fabric/api/loot/v1/event/LootTableLoadingCallback.java|LootTableLoadingCallback]] There is an example of using ''LootTableLoadingCallback'' [[tutorial:adding_to_loot_tables|here]]. === World Events === [[https://github.com/FabricMC/fabric/blob/1.15/fabric-events-lifecycle-v0/src/main/java/net/fabricmc/fabric/api/event/world/WorldTickCallback.java|WorldTickCallback]] === Server Events === [[https://github.com/FabricMC/fabric/blob/d70d2c06bb8fafdb72c6778b29fb050618015ab3/fabric-lifecycle-events-v1/src/main/java/net/fabricmc/fabric/api/event/lifecycle/v1/ServerLifecycleEvents.java#L134|ServerLifecycleEvents.ServerStarted]] / [[https://github.com/FabricMC/fabric/blob/d70d2c06bb8fafdb72c6778b29fb050618015ab3/fabric-lifecycle-events-v1/src/main/java/net/fabricmc/fabric/api/event/lifecycle/v1/ServerLifecycleEvents.java#L144|ServerLifecycleEvents.ServerStopped]] / [[https://github.com/FabricMC/fabric/blob/3c70d5c76fbb04541b48b7e7fb8c8b7da88e9618/fabric-lifecycle-events-v1/src/main/java/net/fabricmc/fabric/api/event/lifecycle/v1/ServerTickEvents.java#L68|ServerTickEvents.StartTick]] / [[https://github.com/FabricMC/fabric/blob/3c70d5c76fbb04541b48b7e7fb8c8b7da88e9618/fabric-lifecycle-events-v1/src/main/java/net/fabricmc/fabric/api/event/lifecycle/v1/ServerTickEvents.java#L73|ServerTickEvents.EndTick]] === Network Events === [[https://github.com/FabricMC/fabric/blob/1.15/fabric-networking-v0/src/main/java/net/fabricmc/fabric/api/event/network/C2SPacketTypeCallback.java|C2SPacketTypeCallback]] / [[https://github.com/FabricMC/fabric/blob/1.15/fabric-networking-v0/src/main/java/net/fabricmc/fabric/api/event/network/S2CPacketTypeCallback.java|S2CPacketTypeCallback]] See [[tutorial:event_index|Event Index]] for a more complete and updated list.