User Tools

Site Tools


Sidebar

← Go back to the homepage

Fabric Tutorials

Setup

Basics

These pages are essential must-reads when modding with Fabric, and modding Minecraft in general, if you are new to modding, it is recommended you read the following.

Items

Blocks and Block Entities

Data Generation

World Generation

Commands

These pages will guide you through Mojang's Brigadier library which allows you to create commands with complex arguments and actions.

Events

These pages will guide you through using the many events included in Fabric API, and how to create your own events for you or other mods to use.

Entities

Fluids

Mixins & ASM

These pages will guide you through the usage of SpongePowered's Mixin library, which is a highly complex topic. We recommend you read these pages thoroughly.

Miscellaneous

Yarn

Contribute to Fabric

tutorial:callbacks

Listening to Events

In this tutorial you will learn to:

  1. Understand Events and Callbacks
  2. 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 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 events.

Practice

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.

  1. public class ExampleMod implements ModInitializer
  2. {
  3. [...]
  4.  
  5. @Override
  6. public void onInitialize()
  7. {
  8. AttackBlockCallback.EVENT.register((player, world, hand, pos, direction) ->
  9. {
  10. BlockState state = world.getBlockState(pos);
  11. /* Manual spectator check is necessary because AttackBlockCallbacks
  12.   fire before the spectator check */
  13. if (state.isToolRequired() && !player.isSpectator() &&
  14. player.getMainHandStack().isEmpty())
  15. {
  16. player.damage(DamageSource.GENERIC, 1.0F);
  17. }
  18. return ActionResult.PASS;
  19. });
  20. }
  21. }

Fabric API Events

Player Interaction Events

Registry Events

Looting Events

LootTableLoadingCallback

There is an example of using LootTableLoadingCallback here.

World Events

Server Events

Network Events

tutorial/callbacks.txt · Last modified: 2021/07/26 23:00 by daomephsta