| Both sides previous revisionPrevious revision | |
| drafts:callbacks [2023/09/13 18:47] – removed - external edit (Unknown date) 127.0.0.1 | drafts:callbacks [2023/09/13 18:47] (current) – ↷ Page moved from playground:callbacks to drafts:callbacks nebelnidas |
|---|
| | ====== Listening to Events (DRAFT) ====== |
| | By the end of this tutorial, you will: |
| | - Understand Events and Callbacks, |
| | - Learn how to register a Callback on an existing Event, |
| | - 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 === |
| | 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 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 === |
| | [[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]] |
| | |
| | ==== 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 ====== |
| | <!-- 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; |
| | |
| | <code java> |
| | /** |
| | * 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> |
| | |
| | <!-- TODO: Really do sth. --> |
| | |
| | <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> |
| | |
| | <!-- TODO: An image of the effect of something have done --> |
| |