Skip to content

Commit 4c8cb70

Browse files
authored
feat: add onEndermanTakeBlock event [#233] (#283)
1 parent 51fa3a2 commit 4c8cb70

File tree

7 files changed

+82
-0
lines changed

7 files changed

+82
-0
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Added onEndermanTakeBlock event [#233] @zimuya4153
13+
1014
## [0.11.2] - 2025-06-01
1115

1216
### Fixed
@@ -906,6 +910,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
906910
[#226]: https://github.com/LiteLDev/LegacyScriptEngine/issues/226
907911
[#227]: https://github.com/LiteLDev/LegacyScriptEngine/issues/227
908912
[#231]: https://github.com/LiteLDev/LegacyScriptEngine/issues/231
913+
[#233]: https://github.com/LiteLDev/LegacyScriptEngine/issues/233
909914
[#236]: https://github.com/LiteLDev/LegacyScriptEngine/issues/236
910915
[#240]: https://github.com/LiteLDev/LegacyScriptEngine/issues/240
911916
[#242]: https://github.com/LiteLDev/LegacyScriptEngine/issues/242

docs/apis/EventAPI/EntityEvents.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,3 +246,19 @@ hooks).
246246
Note: This event is triggered when the `TransformationComponent` of the entity in `Addons` is activated, and is mostly
247247
used for the interaction between the engine and the Addon. Only `UniqueId` is provided since the entity pointer before
248248
the transition is destroyed quickly.
249+
250+
#### `"onEndermanTakeBlock"` - Enderman Take Block Event
251+
252+
!!! warning
253+
This event is only available in 0.11.3 and later versions.
254+
255+
- Listener function prototype
256+
`function(entity, block, pos)`
257+
- Parameters
258+
- entity : `Entity`
259+
Enderman entity.
260+
- block : `Block`
261+
Pick up the Block
262+
- pos : `BlockPos`
263+
The coordinates of the block.
264+
- Intercept events: function returns `false`

docs/apis/EventAPI/EntityEvents.zh.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,20 @@ ActorDamageCause 为伤害原因枚举,枚举值如下,有问号的待验证
255255

256256
注:此事件为 `Addons` 中实体的 `TransformationComponent` 激活时触发,多用于引擎与Addon交互。由于转变前的实体指针很快被销毁,因此只提供
257257
`UniqueId`
258+
259+
260+
#### `"onEndermanTakeBlock"` - 末影人搬运方块
261+
262+
!!! warning
263+
此事件仅在0.11.3及更高版本中可用。
264+
265+
- 监听函数原型
266+
`function(entity, block, pos)`
267+
- 参数:
268+
- entity : `Entity`
269+
搬运方块的末影人
270+
- block : `Block`
271+
被搬运的方块
272+
- pos : `BlockPos`
273+
被搬运的方块坐标
274+
- 拦截事件:函数返回`false`

src/legacy/api/EventAPI.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,9 @@ void EnableEventListener(int eventId) {
738738
case EVENT_TYPES::onNpcCmd:
739739
lse::events::entity::NpcCommandEvent();
740740
break;
741+
case EVENT_TYPES::onEndermanTakeBlock:
742+
lse::events::entity::EndermanTakeBlockEvent();
743+
break;
741744
default:
742745
break;
743746
}

src/legacy/api/EventAPI.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ enum class EVENT_TYPES : int {
7373
onMobTrySpawn,
7474
onMobSpawned,
7575
onNpcCmd,
76+
onEndermanTakeBlock,
7677
/* Block Events */
7778
onBlockInteracted,
7879
onBlockChanged,

src/lse/events/EntityEvents.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,44 @@ LL_TYPE_INSTANCE_HOOK(
370370
origin(originalActor, transformed, transformation, ownerID, level);
371371
}
372372

373+
LL_TYPE_INSTANCE_HOOK(
374+
ActorDestroyBlockEventHook,
375+
HookPriority::Normal,
376+
ActorEventCoordinator,
377+
&ActorEventCoordinator::sendEvent,
378+
CoordinatorResult,
379+
EventRef<ActorGameplayEvent<CoordinatorResult>> const& event
380+
)
381+
try {
382+
return event.get().visit([&](auto&& arg) {
383+
if constexpr (std::is_same_v<std::decay_t<decltype(arg)>, Details::ValueOrRef<ActorGriefingBlockEvent const>>) {
384+
auto& griefingEvent = arg.value();
385+
386+
if (auto entity = griefingEvent.mActorContext->tryUnwrap(); entity && entity->isType(ActorType::EnderMan)) {
387+
IF_LISTENED(EVENT_TYPES::onEndermanTakeBlock) {
388+
if (!CallEvent(
389+
EVENT_TYPES::onEndermanTakeBlock,
390+
EntityClass::newEntity(entity.as_ptr()),
391+
BlockClass::newBlock(
392+
*griefingEvent.mBlock,
393+
BlockPos(griefingEvent.mPos),
394+
entity->getDimensionId()
395+
),
396+
IntPos::newPos(BlockPos(griefingEvent.mPos), entity->getDimensionId())
397+
)) {
398+
return CoordinatorResult::Cancel;
399+
}
400+
}
401+
IF_LISTENED_END(EVENT_TYPES::onEndermanTakeBlock);
402+
}
403+
return CoordinatorResult::Continue;
404+
}
405+
return origin(event);
406+
});
407+
} catch (...) {
408+
return origin(event);
409+
}
410+
373411
void ProjectileSpawnEvent() {
374412
ProjectileSpawnHook1::hook();
375413
ProjectileSpawnHook2::hook();
@@ -385,6 +423,7 @@ void MobHurtEvent() {
385423
MobHurtEffectHook::hook();
386424
}
387425
void NpcCommandEvent() { NpcCommandHook::hook(); }
426+
void EndermanTakeBlockEvent() { ActorDestroyBlockEventHook::hook(); }
388427
void EffectUpdateEvent() { EffectUpdateHook::hook(); }
389428
void TransformationEvent() { TransformationHook::hook(); }
390429
} // namespace lse::events::entity

src/lse/events/EntityEvents.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ void ProjectileHitEntityEvent();
99
void ProjectileHitBlockEvent();
1010
void MobHurtEvent();
1111
void NpcCommandEvent();
12+
void EndermanTakeBlockEvent();
1213
void EffectUpdateEvent();
1314
void TransformationEvent();
1415
} // namespace lse::events::entity

0 commit comments

Comments
 (0)