Skip to content

Commit a434206

Browse files
committed
fix: fix onMobHurt #154
fix: fix Entity::hurt #153
1 parent 1996021 commit a434206

File tree

5 files changed

+83
-27
lines changed

5 files changed

+83
-27
lines changed

src/legacy/api/EntityAPI.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,10 +1012,10 @@ Local<Value> EntityClass::hurt(const Arguments& args) {
10121012
return Boolean::newBoolean(false);
10131013
}
10141014
ActorDamageByActorSource damageBySource = ActorDamageByActorSource(*source.value(), (ActorDamageCause)type);
1015-
return Boolean::newBoolean(entity->hurt(damageBySource, damage, true, false));
1015+
return Boolean::newBoolean(entity->_hurt(damageBySource, damage, true, false));
10161016
}
10171017
ActorDamageSource damageSource = ActorDamageSource((ActorDamageCause)type);
1018-
return Boolean::newBoolean(entity->hurt(damageSource, damage, true, false));
1018+
return Boolean::newBoolean(entity->_hurt(damageSource, damage, true, false));
10191019
}
10201020
CATCH("Fail in hurt!");
10211021
}

src/legacy/api/EventAPI.cpp

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -528,27 +528,7 @@ void EnableEventListener(int eventId) {
528528
break;
529529

530530
case EVENT_TYPES::onMobHurt:
531-
bus.emplaceListener<ActorHurtEvent>([](ActorHurtEvent& ev) {
532-
IF_LISTENED(EVENT_TYPES::onMobHurt) {
533-
Actor* source = nullptr;
534-
if (ev.source().isEntitySource()) {
535-
if (ev.source().isChildEntitySource()) {
536-
source = ll::service::getLevel()->fetchEntity(ev.source().getEntityUniqueID());
537-
} else {
538-
source = ll::service::getLevel()->fetchEntity(ev.source().getDamagingEntityUniqueID());
539-
}
540-
}
541-
542-
CallEvent(
543-
EVENT_TYPES::onMobHurt,
544-
EntityClass::newEntity(&ev.self()),
545-
source ? EntityClass::newEntity(source) : Local<Value>(),
546-
Number::newNumber(float(ev.damage())),
547-
Number::newNumber((int)ev.source().getCause())
548-
);
549-
}
550-
IF_LISTENED_END(EVENT_TYPES::onMobHurt)
551-
});
531+
lse::events::MobHurtEvent();
552532
break;
553533

554534
case EVENT_TYPES::onStepOnPressurePlate:

src/legacy/api/PlayerAPI.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@
6363
#include "mc/world/Container.h"
6464
#include "mc/world/Minecraft.h"
6565
#include "mc/world/SimpleContainer.h"
66+
#include "mc/world/actor/ActorDamageByActorSource.h"
6667
#include "mc/world/actor/player/EnderChestContainer.h"
6768
#include "mc/world/actor/player/PlayerScoreSetFunction.h"
68-
#include "mc/world/actor/player/PlayerUISlot.h"
6969
#include "mc/world/effect/MobEffectInstance.h"
7070
#include "mc/world/events/BossEventUpdateType.h"
7171
#include "mc/world/item/registry/ItemStack.h"
@@ -2623,9 +2623,11 @@ Local<Value> PlayerClass::hurt(const Arguments& args) {
26232623
if (!source) {
26242624
return Boolean::newBoolean(false);
26252625
}
2626-
return Boolean::newBoolean(player->hurtByCause(damage, (ActorDamageCause)type, source.value()));
2626+
ActorDamageByActorSource damageBySource = ActorDamageByActorSource(*source.value(), (ActorDamageCause)type);
2627+
return Boolean::newBoolean(player->_hurt(damageBySource, damage, true, false));
26272628
}
2628-
return Boolean::newBoolean(player->hurtByCause(damage, (ActorDamageCause)type));
2629+
ActorDamageSource damageSource = ActorDamageSource((ActorDamageCause)type);
2630+
return Boolean::newBoolean(player->_hurt(damageSource, damage, true, false));
26292631
}
26302632
CATCH("Fail in hurt!");
26312633
}

src/legacy/events/EventHooks.cpp

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include "mc/server/commands/CommandOrigin.h"
1212
#include "mc/server/commands/CommandOriginType.h"
1313
#include "mc/world/ActorUniqueID.h"
14+
#include "mc/world/actor/ActorDamageSource.h"
15+
#include "mc/world/actor/Mob.h"
1416
#include "mc/world/containers/ContainerID.h"
1517
#include "mc/world/inventory/transaction/InventorySource.h"
1618
#include "mc/world/scores/ScoreInfo.h"
@@ -61,7 +63,6 @@
6163
#include <mc/world/phys/AABB.h>
6264
#include <mc/world/scores/ServerScoreboard.h>
6365

64-
6566
namespace lse::events {
6667

6768
// NOLINTBEGIN(modernize-use-trailing-return-type)
@@ -1147,6 +1148,74 @@ LL_TYPE_INSTANCE_HOOK(
11471148
}
11481149
} // namespace HopperEvents
11491150

1151+
LL_TYPE_INSTANCE_HOOK(
1152+
MobHurtHook,
1153+
HookPriority::Normal,
1154+
Mob,
1155+
"?_hurt@Mob@@MEAA_NAEBVActorDamageSource@@M_N1@Z",
1156+
bool,
1157+
ActorDamageSource const& source,
1158+
float dmg,
1159+
bool knock,
1160+
bool ignite
1161+
) {
1162+
IF_LISTENED(EVENT_TYPES::onMobHurt) {
1163+
Actor* damageSource;
1164+
if (source.isEntitySource()) {
1165+
if (source.isChildEntitySource()) {
1166+
damageSource = ll::service::getLevel()->fetchEntity(source.getEntityUniqueID());
1167+
} else {
1168+
damageSource = ll::service::getLevel()->fetchEntity(source.getDamagingEntityUniqueID());
1169+
}
1170+
}
1171+
1172+
CallEventRtnValue(
1173+
EVENT_TYPES::onMobHurt,
1174+
false,
1175+
EntityClass::newEntity(this),
1176+
damageSource ? EntityClass::newEntity(damageSource) : Local<Value>(),
1177+
Number::newNumber(dmg),
1178+
Number::newNumber((int)source.getCause())
1179+
);
1180+
}
1181+
IF_LISTENED_END(EVENT_TYPES::onMobHurt)
1182+
return origin(source, dmg, knock, ignite);
1183+
}
1184+
1185+
LL_TYPE_INSTANCE_HOOK(
1186+
MobHurtEffectHook,
1187+
HookPriority::Normal,
1188+
Mob,
1189+
"?hurtEffects@Mob@@UEAAXAEBVActorDamageSource@@M_N1@Z",
1190+
bool,
1191+
ActorDamageSource const& source,
1192+
float damage,
1193+
bool knock,
1194+
bool ignite
1195+
) {
1196+
IF_LISTENED(EVENT_TYPES::onMobHurt) {
1197+
Actor* damageSource;
1198+
if (source.isEntitySource()) {
1199+
if (source.isChildEntitySource()) {
1200+
damageSource = ll::service::getLevel()->fetchEntity(source.getEntityUniqueID());
1201+
} else {
1202+
damageSource = ll::service::getLevel()->fetchEntity(source.getDamagingEntityUniqueID());
1203+
}
1204+
}
1205+
1206+
CallEventRtnValue(
1207+
EVENT_TYPES::onMobHurt,
1208+
false,
1209+
EntityClass::newEntity(this),
1210+
damageSource ? EntityClass::newEntity(damageSource) : Local<Value>(),
1211+
Number::newNumber(damage),
1212+
Number::newNumber((int)source.getCause())
1213+
);
1214+
}
1215+
IF_LISTENED_END(EVENT_TYPES::onMobHurt)
1216+
return origin(source, damage, knock, ignite);
1217+
}
1218+
11501219
void PlayerStartDestroyBlock() { PlayerStartDestroyHook::hook(); }
11511220
void PlayerDropItem() {
11521221
PlayerDropItemHook1::hook();
@@ -1212,6 +1281,10 @@ void HopperEvent(bool pullIn) {
12121281
HopperEvents::HopperPushOutHook::hook();
12131282
}
12141283
}
1284+
void MobHurtEvent() {
1285+
MobHurtHook::hook();
1286+
MobHurtEffectHook::hook();
1287+
}
12151288

12161289
// NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast)
12171290
// NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables)

src/legacy/events/EventHooks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,6 @@ void PlayerSetArmorEvent();
3939
void ProjectileHitEntityEvent();
4040
void ProjectileHitBlockEvent();
4141
void HopperEvent(bool pullIn);
42+
void MobHurtEvent();
4243

4344
} // namespace lse::events

0 commit comments

Comments
 (0)