User Tools

Site Tools


zh_cn:tutorial:callbacks

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
zh_cn:tutorial:callbacks [2021/09/17 00:38] – [Fabric API 事件] solidblockzh_cn:tutorial:callbacks [2023/05/04 11:01] (current) – [实例] solidblock
Line 5: Line 5:
  
 ===== 事件 ===== ===== 事件 =====
-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. +事件是用存储并调用回调的 ''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]] 将几个有关的事件分组在一起。
 ===== 回调 ===== ===== 回调 =====
-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. +每个事件都有对应的回调接口,通常命名为 ''EventNameCallback''。回调是通过在事件接口调用 ''register()'' 来注册的,调用时需要一个回调接口实例作为参数。
 ==== Fabric API 中的回调接口 ==== ==== Fabric API 中的回调接口 ====
-All event callback interfaces provided by Fabric API can be found in the ''net.fabricmc.fabric.api.event'' package. +Fabric API 提供的所有事件回调接口可以在 ''net.fabricmc.fabric.api.event'' 包中找到。本教程底部提供了部分存在的回调的列表。
-A partial list of existing callbacks is provided at the bottom of this tutorial.+
  
 ==== 自定义回调 ==== ==== 自定义回调 ====
-Although there are plenty of events already provided by Fabric API, you can still make your own events. Please refer to [[tutorial:events]].+虽然 Fabric API 已经提供了许多事件,你还是可以制作你自己的事件,请参考[[zh_cn:tutorial:events]]
  
 ===== 实例 ====== ===== 实例 ======
-This example registers an ''AttackBlockCallback'' to damage players when they hit blocks that 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. +此例注册一个 ''AttackBlockCallback'',玩家击打不能徒手挖掘掉落的方块时,会受到伤害。会返回 ''<yarn class_1269.field_5811>'' 因为还需要调用其他的回调。关于其他值的含义,请参考你的 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
 <yarncode java [enable_line_numbers="true"]> <yarncode java [enable_line_numbers="true"]>
-public class ExampleMod implements ModInitializer +public class ExampleMod implements ModInitializer {
-{+
     [...]     [...]
          
     @Override     @Override
-    public void onInitialize()  +    public void onInitialize() { 
-    +        AttackBlockCallback.EVENT.register((player, world, hand, pos, direction) -> {
-        AttackBlockCallback.EVENT.register((player, world, hand, pos, direction) ->  +
-        {+
             class_2680 state = world.method_8320(pos);             class_2680 state = world.method_8320(pos);
-            /* Manual spectator check is necessary because AttackBlockCallbacks +            /* 手动的旁观者检查是必要的,因为 AttackBlockCallbacks 会在旁观者检查之前应用 */
-               fire before the spectator check */+
             if (state.method_29291() && !player.method_7325() &&              if (state.method_29291() && !player.method_7325() && 
-                player.method_6047().method_7960())  +                player.method_6047().method_7960()) {
-            {+
                 player.method_5643(class_1282.field_5869, 1.0F);                 player.method_5643(class_1282.field_5869, 1.0F);
             }              } 
zh_cn/tutorial/callbacks.1631839117.txt.gz · Last modified: 2021/09/17 00:38 by solidblock