Skip to content

Commit e93bb58

Browse files
committed
fix: fix onEffectRemoved not triggered when effects are removed manually
1 parent 6dd3dcd commit e93bb58

File tree

5 files changed

+55
-56
lines changed

5 files changed

+55
-56
lines changed

src/legacy/api/EventAPI.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -492,10 +492,10 @@ void EnableEventListener(int eventId) {
492492
break;
493493

494494
case EVENT_TYPES::onEffectAdded:
495-
lse::events::entity::EffectApplyEvent();
495+
lse::events::player::AddEffectEvent();
496496
break;
497497
case EVENT_TYPES::onEffectRemoved:
498-
lse::events::entity::EffectExpiredEvent();
498+
lse::events::player::RemoveEffectEvent();
499499
break;
500500
case EVENT_TYPES::onEffectUpdated:
501501
lse::events::entity::EffectUpdateEvent();

src/lse/events/EntityEvents.cpp

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -312,54 +312,6 @@ LL_TYPE_INSTANCE_HOOK(
312312
origin(owner, sourcePlayer, actionIndex, sceneName);
313313
}
314314

315-
LL_TYPE_INSTANCE_HOOK(
316-
EffectApplyHook,
317-
HookPriority::Normal,
318-
MobEffectInstance,
319-
&MobEffectInstance::applyEffects,
320-
void,
321-
::Actor& mob
322-
) {
323-
IF_LISTENED(EVENT_TYPES::onEffectAdded) {
324-
if (mob.isPlayer()) {
325-
if (!CallEvent(
326-
EVENT_TYPES::onEffectAdded,
327-
PlayerClass::newPlayer(&static_cast<Player&>(mob)),
328-
String::newString(getComponentName().getString()),
329-
Number::newNumber(getAmplifier()),
330-
Number::newNumber(getDuration().getValueForSerialization())
331-
)) {
332-
return;
333-
}
334-
}
335-
}
336-
IF_LISTENED_END(EVENT_TYPES::onEffectAdded);
337-
origin(mob);
338-
}
339-
340-
LL_TYPE_INSTANCE_HOOK(
341-
EffectExpiredHook,
342-
HookPriority::Normal,
343-
MobEffectInstance,
344-
&MobEffectInstance::onEffectsExpired,
345-
void,
346-
::Actor& mob
347-
) {
348-
IF_LISTENED(EVENT_TYPES::onEffectRemoved) {
349-
if (mob.isPlayer()) {
350-
if (!CallEvent(
351-
EVENT_TYPES::onEffectRemoved,
352-
PlayerClass::newPlayer(&static_cast<Player&>(mob)),
353-
String::newString(getComponentName().getString())
354-
)) {
355-
return;
356-
}
357-
}
358-
}
359-
IF_LISTENED_END(EVENT_TYPES::onEffectRemoved);
360-
origin(mob);
361-
}
362-
363315
LL_TYPE_INSTANCE_HOOK(
364316
EffectUpdateHook,
365317
HookPriority::Normal,
@@ -424,8 +376,6 @@ void MobHurtEvent() {
424376
MobHurtEffectHook::hook();
425377
}
426378
void NpcCommandEvent() { NpcCommandHook::hook(); }
427-
void EffectApplyEvent() { EffectApplyHook::hook(); }
428-
void EffectExpiredEvent() { EffectExpiredHook::hook(); }
429379
void EffectUpdateEvent() { EffectUpdateHook::hook(); }
430380
void TransformationEvent() { TransformationHook::hook(); }
431381
} // namespace lse::events::entity

src/lse/events/EntityEvents.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ void ProjectileHitEntityEvent();
99
void ProjectileHitBlockEvent();
1010
void MobHurtEvent();
1111
void NpcCommandEvent();
12-
void EffectApplyEvent();
13-
void EffectExpiredEvent();
1412
void EffectUpdateEvent();
1513
void TransformationEvent();
16-
}
14+
} // namespace lse::events::entity

src/lse/events/PlayerEvents.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "ll/api/memory/Memory.h"
99
#include "ll/api/service/Bedrock.h"
1010
#include "mc/common/ActorUniqueID.h"
11+
#include "mc/deps/core/string/HashedString.h"
1112
#include "mc/deps/ecs/WeakEntityRef.h"
1213
#include "mc/server/ServerPlayer.h"
1314
#include "mc/server/module/VanillaServerGameplayEventListener.h"
@@ -19,6 +20,8 @@
1920
#include "mc/world/actor/player/Player.h"
2021
#include "mc/world/actor/player/PlayerItemInUse.h"
2122
#include "mc/world/containers/models/LevelContainerModel.h"
23+
#include "mc/world/effect/EffectDuration.h"
24+
#include "mc/world/effect/MobEffectInstance.h"
2225
#include "mc/world/events/BlockEventCoordinator.h"
2326
#include "mc/world/events/EventResult.h"
2427
#include "mc/world/events/PlayerOpenContainerEvent.h"
@@ -596,6 +599,50 @@ LL_TYPE_INSTANCE_HOOK(
596599
return origin(actor, location);
597600
}
598601

602+
LL_TYPE_INSTANCE_HOOK(
603+
AddEffectHook,
604+
HookPriority::Normal,
605+
Player,
606+
&Player::addEffect,
607+
void,
608+
::MobEffectInstance const& effect
609+
) {
610+
IF_LISTENED(EVENT_TYPES::onEffectAdded) {
611+
if (!CallEvent(
612+
EVENT_TYPES::onEffectAdded,
613+
PlayerClass::newPlayer(this),
614+
String::newString(effect.getComponentName().getString()),
615+
Number::newNumber(effect.getAmplifier()),
616+
Number::newNumber(effect.getDuration().getValueForSerialization())
617+
)) {
618+
return;
619+
}
620+
}
621+
IF_LISTENED_END(EVENT_TYPES::onEffectAdded);
622+
origin(effect);
623+
}
624+
625+
LL_TYPE_INSTANCE_HOOK(
626+
RemoveEffectHook,
627+
HookPriority::Normal,
628+
Player,
629+
&Player::$onEffectRemoved,
630+
void,
631+
::MobEffectInstance& effect
632+
) {
633+
IF_LISTENED(EVENT_TYPES::onEffectRemoved) {
634+
if (!CallEvent(
635+
EVENT_TYPES::onEffectRemoved,
636+
PlayerClass::newPlayer(this),
637+
String::newString(effect.getComponentName().getString())
638+
)) {
639+
return;
640+
}
641+
}
642+
IF_LISTENED_END(EVENT_TYPES::onEffectRemoved);
643+
origin(effect);
644+
}
645+
599646
void StartDestroyBlock() { StartDestroyHook::hook(); }
600647
void DropItem() {
601648
DropItemHook1::hook();
@@ -630,4 +677,6 @@ void UseBucketTakeEvent() {
630677
void ConsumeTotemEvent() { ConsumeTotemHook::hook(); }
631678
void SetArmorEvent() { SetArmorHook::hook(); }
632679
void InteractEntityEvent() { InteractEntityHook::hook(); }
680+
void AddEffectEvent() { AddEffectHook::hook(); }
681+
void RemoveEffectEvent() { RemoveEffectHook::hook(); }
633682
} // namespace lse::events::player

src/lse/events/PlayerEvents.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ void UseBucketTakeEvent();
2020
void ConsumeTotemEvent();
2121
void SetArmorEvent();
2222
void InteractEntityEvent();
23-
}
23+
void AddEffectEvent();
24+
void RemoveEffectEvent();
25+
} // namespace lse::events::player

0 commit comments

Comments
 (0)