User Tools

Site Tools


tutorial:callbacks

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
Next revisionBoth sides next revision
tutorial:callbacks [2020/02/17 09:29] – created mkpolitutorial:callbacks [2021/07/14 23:31] – Flesh out draft into something more useful daomephsta
Line 1: Line 1:
-====== Register a Callback Listener for Events (DRAFT) ======+====== Listening to Events ====== 
 +In this tutorial you will learn to: 
 +  - Understand Events and Callbacks 
 +  - Register a callback for an existing Event
  
-===== Callbacks Classes ===== +===== Events ===== 
-Callbacks Classes are a series of Classes which named [Eventname]Callback is able to register a callback listener function to the specific event.+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 in Fabric API ==== +===== Callbacks ===== 
-Event Callbacks provided by Fabric API can be found in ''net.fabricmc.fabric.api.event'' package.+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.
  
-A list of existing callbacks+==== Callback Interfaces in Fabric API ==== 
 +All event callback interfaces provided by Fabric API can be found in the ''net.fabricmc.fabric.api.event'' package. 
 +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 don't drop when hand-mined. It returns ''<yarn class_1269.field_5811>'' 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.
  
-=== Player Interactive Events ===+<yarncode java [enable_line_numbers="true"]> 
 +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; 
 +        }); 
 +    } 
 +
 +</yarncode> 
 + 
 +=== 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: [[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]]
  
Line 16: Line 50:
  
 === Registry Events === === Registry Events ===
-<!-- TODOAdd 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]]
  
-=== Looting Events === +[[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]]
-<!-- TODOAdd Events -->+
  
 === Looting Events === === Looting Events ===
-<!-- TODO: Add 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]]
  
-=== Render Events === +There is an example of using ''LootTableLoadingCallback'' [[tutorial:adding_to_loot_tables|here]].
-<!-- TODOAdd Events -->+
  
-There is an example using ''LootTableLoadingCallback'' you can find [[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/1.15/fabric-events-lifecycle-v0/src/main/java/net/fabricmc/fabric/api/event/server/ServerStartCallback.java|ServerStartCallback]] / [[https://github.com/FabricMC/fabric/blob/1.15/fabric-events-lifecycle-v0/src/main/java/net/fabricmc/fabric/api/event/server/ServerStopCallback.java|ServerStopCallback]] / [[https://github.com/FabricMC/fabric/blob/1.15/fabric-events-lifecycle-v0/src/main/java/net/fabricmc/fabric/api/event/server/ServerTickCallback.java|ServerTickCallback]]
  
- +=== Network Events === 
-==== Custom Callbacks ==== +[[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]]
-Although there is plenty of Callbacks provided already by Fabric API, you can still make your own API for your goal. Please refer to [[tutorial: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 toIf you want to make a ; +
- +
-You can interrupt and stop continuing by sending ActionResult.SUCCESS; +
- +
- +
-As stated in javadoc of ''AttackBlockCallback'' +
- +
-<code> +
-/** +
- * 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. +
- */ +
-</code> +
- +
-<code java [enable_line_numbers="true"]+
-public class ExampleMod implements ModInitializer +
-+
-    [...] +
-     +
-    @Override +
-    public void onInitialize() { +
-        AttackBlockCallback.EVENT.register((player, world, hand, pos, direction) -> { +
-            // Do sth... +
-            if ([condition]) { +
-                return ActionResult.SUCCESS; +
-            } else { +
-                return ActionResult.PASS; +
-            } +
-             +
-        }) +
-    } +
-+
-</code> +
tutorial/callbacks.txt · Last modified: 2023/05/04 11:01 by solidblock