瓦解核心:视界 | Brea:Horiz
概述
Horiz Lib 提供了一个易于使用的事件转发功能,允许与游戏中实体相关的事件被转发到实体持有的事件处理器中,从而避免繁琐的事件注册以及动态添加和移除操作。
其逻辑为:使用 AttachmentType 存储特定事件的处理器列表;监听与实体相关的事件,从事件中获取 data attachment(数据附加物),并执行其中包含的相应处理器,例如:
public static final Consumer<EntityEvent> consumer = event -> event.getEntity().getExistingData(Horiz.EVENT_DISTRIBUTE).ifPresent(d -> d.post(event));
@SubscribeEvent
public static void hurtIncome(LivingIncomingDamageEvent event) {
consumer.accept(event);
}
此过程使得单个实体的可变事件处理行为更加灵活和便捷。你可以利用此过程实现诸如“Tinkers' Construct 装备特性”、“饰品效果”以及“实体技能系统”等功能。
如何使用此系统?
首先,我们需要获取相应的服务端 Entity(实体)。在获得目标实体后,你可以使用:
public <T extends Event> void initEntity() {
Entity entity; // 你获取到的实体
Class<T> eventClazz; // 要监听的事件类
Consumer<T> eventConsumer; // 事件的处理器
// IdentEvent 包含事件类及其对应的处理器,用于管理已添加的监听器
IdentEvent<T> ident = new IdentEvent(eventClazz, eventConsumer);
EntityEventDistribute distribute = entity.getData(Horiz.EVENT_DISTRIBUTE);
distribute.add(ident); // 使用 add 方法添加新的监听器
distribute.remove(ident); // 使用 remove 方法清除现有的监听器
distribute.removeByClass(eventClazz); // 或使用 removeByClass 清除特定事件的所有监听器
// 注意:作用范围仅限于实体分发器内的监听器,这不会影响游戏事件总线和其他实体分发器中的注册
}
此外,你还可以在需要时通过给事件添加标签来快速移除它们。
想象一个文件夹——你可以向其中添加文件,也可以添加其他文件夹。标签的设计与此类似。在 EntityEventDistribute 的 add 方法中,它允许在 IdentEvent 之后添加一组 ResourceLocation 作为监听器的标签路径,而在 removeMarked 方法中,你可以根据此路径快速移除监听器。
让我们通过一个示例来演示:
static {
ResourceLocation loc1, loc2, loc3, loc4;
IdentEvent<?> i1, i2, i3, i4, i5, i6, i7, i8, i9;
EntityEventDistribute distribute;
distribute.add(i1);
distribute.add(i2);
distribute.add(i3, loc1);
distribute.add(i4, loc1, loc2);
distribute.add(i5, loc1, loc2);
distribute.add(i6, loc1, loc3);
distribute.add(i7, loc1, loc3, loc4);
distribute.add(i8, loc2);
distribute.add(i9, loc2, loc3);
// 存储的结构大致如下:
// root
// |-(i1),(i2)
// |-loc1
// | |-(i3)
// | |-loc2
// | | \(i4),(i5)
// | \loc3
// | |-(i6)
// | \loc4
// | \(i7)
// \loc2
// |-(i8)
// \loc3
// \(i9)
// 假设执行以下方法且 distribute 自动恢复(实际上并不会)
distribute.removeMarked(loc1, loc3, loc4); // 移除 i7
distribute.removeMarked(loc1, loc3); // 移除 i6, i7
distribute.removeMarked(loc1, loc2); // 移除 i4, i5
distribute.removeMarked(loc1); // 移除 i3, i4, i5, i6, i7
distribute.removeMarkedSelf(loc1); // 移除 i3,不移除子路径下的内容
distribute.removeMarked(loc2); // 移除 i8, i9
}
添加更多事件转发
正如我们在开头提到的,事件转发的逻辑是在 EventConsumer#consumer 常量中完成的。你只需要监听现有事件并使用 Consumer#accept 方法来执行它们。此过程简化了获取实体数据附加物的步骤。如果你的事件不是 EntityEvent,你可能需要自行完成相应的逻辑。
其他常见问题解答
我应该何时初始化用于转发的监听器?
你可以在实体创建时直接进行初始化。如果无法这样做或需要全局初始化各种实体,你可以监听
GatherEntityDistributeEvent 事件,该事件
由实体加入世界的最低优先级处转发而来。
注意:由于此数据附加物没有配置 Codec,因此转发中的事件不会被保存——这意味着当实体重新加载或传送至另一个维度时,你需要重新初始化这些事件。
因此,我们建议使用 GatherEntityDistributeEvent。
我想处理实体对目标造成伤害/实体击杀目标的情况,该怎么办?
库提供了伤害事件和击杀事件的转发——你可以在 EventConsumer 类的底部找到相应的转发事件。
模组已经提供了哪些事件的转发?
你可以在 EventConsumer#bootstrapConsumer() 下查看模组提供的转发事件。
如果同一事件由不同模组转发,监听器会被执行多次吗?
不会。在 EntityEventDistribute 类中,有一个表记录了事件的哈希值。如果同一事件的相同实例被多次转发,第一次之后的转发将被过滤掉。
贡献
欢迎为 Horiz 模组做出贡献。你可以提交 Issue 以报告问题,或 Fork 本项目以提交你的改进。
开发者
瓦解核心:视界 | Brea:Horiz
Overview
Horiz Lib provides an easy-to-use event forwarding feature that allows events related to entities in the game to be
forwarded to the event handlers held by the entities, avoiding cumbersome event registration and dynamic addition and
removal.
The logic is: use AttachmentType to store a list of handlers for a specific event; listen for events related to the
entity, get the data attachment from the event, and execute the corresponding handlers contained in the event, for
example:
public static final Consumer<EntityEvent> consumer = event -> event.getEntity().getExistingData(Horiz.EVENT_DISTRIBUTE).ifPresent(d -> d.post(event));
@SubscribeEvent
public static void hurtIncome(LivingIncomingDamageEvent event) {
consumer.accept(event);
}
This process makes the variable event handling behavior for a single entity more flexible and convenient. You can use
this process to implement features such as "Tinkers' Construct equipment traits," "accessory effects," and "entity skill
systems."
How to use this system?
First, we need to get the corresponding server-side Entity. After obtaining the target entity, you can use:
public <T extends Event> void initEntity() {
Entity entity; // the entity you obtained
Class<T> eventClazz; // the class of the event to listen to
Consumer<T> eventConsumer; // the handler for the event
// IdentEvent contains the class of the event and its corresponding handler, used to manage added listeners
IdentEvent<T> ident = new IdentEvent(eventClazz, eventConsumer);
EntityEventDistribute distribute = entity.getData(Horiz.EVENT_DISTRIBUTE);
distribute.add(ident); // use the add method to add a new listener
distribute.remove(ident); // use the remove method to clear an existing listener
distribute.removeByClass(eventClazz); // or use removeByClass to clear all listeners for a specific event
// Note: The scope is the listeners within the entity distributor, which does not affect the registration of the game event bus and other entity distributors
}
Additionally, you can quickly remove events by adding tags to them when needed.
Imagine a folder—you can add files to it, or you can add other folders. The design of tags is similar. In the add
method of EntityEventDistribute, it allows
adding a set of ResourceLocation as the tag path of the listener after IdentEvent, and in the removeMarked method,
you can quickly remove listeners based on this path.
Let's demonstrate with an example:
static {
ResourceLocation loc1, loc2, loc3, loc4;
IdentEvent<?> i1, i2, i3, i4, i5, i6, i7, i8, i9;
EntityEventDistribute distribute;
distribute.add(i1);
distribute.add(i2);
distribute.add(i3, loc1);
distribute.add(i4, loc1, loc2);
distribute.add(i5, loc1, loc2);
distribute.add(i6, loc1, loc3);
distribute.add(i7, loc1, loc3, loc4);
distribute.add(i8, loc2);
distribute.add(i9, loc2, loc3);
// The stored structure is roughly like this:
// root
// |-(i1),(i2)
// |-loc1
// | |-(i3)
// | |-loc2
// | | \(i4),(i5)
// | \loc3
// | |-(i6)
// | \loc4
// | \(i7)
// \loc2
// |-(i8)
// \loc3
// \(i9)
// Assuming the following method is executed and distribute is automatically restored (it actually won't)
distribute.removeMarked(loc1, loc3, loc4); // remove i7
distribute.removeMarked(loc1, loc3); // remove i6, i7
distribute.removeMarked(loc1, loc2); // remove i4, i5
distribute.removeMarked(loc1); // remove i3, i4, i5, i6, i7
distribute.removeMarkedSelf(loc1); // remove i3, do not remove contents under sub-paths
distribute.removeMarked(loc2); // remove i8, i9
}
Adding more event forwarding
As we mentioned at the beginning, the logic for forwarding events is completed in the EventConsumer#consumer constant.
You only need to listen to existing events and use the Consumer#accept method to execute them. This process simplifies
the process of obtaining entity data attachments. If your event is not an EntityEvent, you may need to complete the
corresponding logic yourself.
Other Q&A
When should I initialize the event listeners for forwarding?
You can initialize directly when the entity is created. If you cannot do this or need to globally initialize various
entities, you can listen to
the GatherEntityDistributeEvent event, which is
forwarded from the lowest priority of the entity joining the world.
Note: Since this data attachment does not have a configured Codec, the events in the forwarding will not be saved—this
means that when the entity is reloaded or teleported to another dimension, you need to reinitialize the events.
Therefore, we recommend using GatherEntityDistributeEvent.
I want to handle situations where the entity causes damage to the target/entity kills the target, what should I do?
The library provides forwarding for damage events and kill events—you can find the corresponding events forwarded at
the bottom of the EventConsumer class.
Which events does the mod already provide forwarding for?
You can see the forwarding provided by the mod
under EventConsumer#bootstrapConsumer().
If the same event is forwarded by different mods, will the listener be executed multiple times?
No. In the EntityEventDistribute class, there
is a table that records the hash values of events. If the same instance of the same event is forwarded multiple times,
the forwarding after the first time will be filtered out.
Contribution
Contributions to the Horiz mod are welcome. You can submit issues to report problems or fork this project to submit
your improvements.
Developers
- Mon-landis: Provides technical support and development.
评论 0
登录后参与评分
还没有评论,来抢沙发吧