User Tools

Site Tools


drafts:callbacks

Listening to Events (DRAFT)

By the end of this tutorial, you will:

  1. Understand Events and Callbacks,
  2. Learn how to register a Callback on an existing Event,
  3. Make Wool blocks drop Bedrock when broken

What's a Callback?

Callback functions are pieces of code that you can register to run when certain events happen in Minecraft. (E.g. a block being broken, a Mods register functions to be called by the They more or less work as less-invasive, less-powerful alternative to Mixin code, and they cover a lot of the cases where you might assume a Mixin is required.

Callback Interfaces

There is a series of interfaces named [EventName]Callback. They will handle the events (get called by mixins), invoke callbacks which are registered on mod initialization.

Callback Interfaces in Fabric API

Event Callbacks provided by Fabric API can be found in net.fabricmc.fabric.api.event package.

Here is a partial list of existing callbacks.

Player Interactive Events

Registry Events

Looting Events

LootTableLoadingCallback

There is an example using LootTableLoadingCallback you can find here.

World Events

Server Events

Network Events

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

<!– TODO: Add explaination –>

Let's see Take AttackBlockCallback as an example for how register a listener

Basically, we are going to … (an event listener) callback to listen the event.

Since there is not more a method that is able to be called on a block clicked, you may want to. If you want to make a ;

As stated in javadoc of AttackBlockCallback, this event accepts ; You can interrupt and stop continuing by sending ActionResult.SUCCESS;

/**
 * Callback for left-clicking ("attacking") a block.
 * Is hooked in before the spectator check, so make sure to check for the player's game mode as well!
 *
 * <p>Upon return:
 * <ul><li>SUCCESS cancels further processing and, on the client, sends a packet to the server.
 * <li>PASS falls back to further processing.
 * <li>FAIL cancels further processing and does not send a packet to the server.</ul>
 *
 * <p>ATTACK_BLOCK does not let you control the packet sending process yet.
 */

<!– TODO: Really do sth. –>

  1. public class ExampleMod implements ModInitializer
  2. {
  3. [...]
  4.  
  5. @Override
  6. public void onInitialize() {
  7. AttackBlockCallback.EVENT.register((player, world, hand, pos, direction) -> {
  8. // Do sth...
  9. if ([condition]) {
  10. return ActionResult.SUCCESS;
  11. } else {
  12. return ActionResult.PASS;
  13. }
  14.  
  15. })
  16. }
  17. }

<!– TODO: An image of the effect of something have done –>

drafts/callbacks.txt · Last modified: 2023/09/13 18:47 by nebelnidas