User Tools

Site Tools


tutorial:callbacks

This is an old revision of the document!


Register a Callback Listener for Events (DRAFT)

Callbacks Classes

Callbacks Classes are a series of Classes which named [Eventname]Callback is able to register a callback listener function to the specific event.

Callbacks in Fabric API

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

A list of existing callbacks

Player Interactive Events

Registry Events

<!– TODO: Add Events –>

Looting Events

<!– TODO: Add Events –>

Looting Events

<!– TODO: Add Events –>

Render Events

<!– TODO: Add Events –>

There is an example using LootTableLoadingCallback you can find here.

Custom Callbacks

Although there is plenty of Callbacks provided already by Fabric API, you can still make your own API for your goal. Please refer to events.

Practice

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

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 ;

You can interrupt and stop continuing by sending ActionResult.SUCCESS;

As stated in javadoc of AttackBlockCallback

/**
 * 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.
 */
  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. }
tutorial/callbacks.1581931741.txt.gz · Last modified: 2020/02/17 09:29 by mkpoli