====== 监听事件 ====== 在本教程中,你会学到: - 理解事件和回调 - 对已有的事件注册回调 ===== 事件 ===== 事件是用存储并调用回调的 ''net.fabricmc.fabric.api.event.Event'' 的实例代表的,通常回调有单个事件实例,存储在回调接口的 ''EVENT'' 静态字段中,但也有其他的形式。例如,[[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]] 将几个有关的事件分组在一起。 ===== 回调 ===== 每个事件都有对应的回调接口,通常命名为 ''EventNameCallback''。回调是通过在事件接口调用 ''register()'' 来注册的,调用时需要一个回调接口实例作为参数。 ==== Fabric API 中的回调接口 ==== Fabric API 提供的所有事件回调接口可以在 ''net.fabricmc.fabric.api.event'' 包中找到。本教程底部提供了部分存在的回调的列表。 ==== 自定义回调 ==== 虽然 Fabric API 已经提供了许多事件,你还是可以制作你自己的事件,请参考[[zh_cn:tutorial:events]]。 ===== 实例 ====== 此例注册一个 ''AttackBlockCallback'',玩家击打不能徒手挖掘掉落的方块时,会受到伤害。会返回 '''' 因为还需要调用其他的回调。关于其他值的含义,请参考你的 IDE 中 [[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。 public class ExampleMod implements ModInitializer { [...] @Override public void onInitialize() { AttackBlockCallback.EVENT.register((player, world, hand, pos, direction) -> { class_2680 state = world.method_8320(pos); /* 手动的旁观者检查是必要的,因为 AttackBlockCallbacks 会在旁观者检查之前应用 */ 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; }); } } ===== Fabric API 事件 ====== === 玩家交互事件 === 玩家:[[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]] 玩家(客户端):[[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]] === 注册表事件 === [[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]] === 战利品事件 === [[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]] 关于使用 ''LootTableLoadingCallback'' 的例子,请参考[[zh_cn:tutorial:adding_to_loot_tables|此教程]]。 === 世界事件 === [[https://github.com/FabricMC/fabric/blob/1.15/fabric-events-lifecycle-v0/src/main/java/net/fabricmc/fabric/api/event/world/WorldTickCallback.java|WorldTickCallback]] === 服务器事件 === [[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]] === 网络事件 === [[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]]