Skip to content

Commit 1183a33

Browse files
committed
fix: fix onEntityTransformation
1 parent c1c0925 commit 1183a33

1 file changed

Lines changed: 51 additions & 69 deletions

File tree

src/lse/events/EntityEvents.cpp

Lines changed: 51 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "mc/entity/components_json_legacy/NpcComponent.h"
1313
#include "mc/entity/components_json_legacy/ProjectileComponent.h"
1414
#include "mc/entity/components_json_legacy/TransformationComponent.h"
15+
#include "mc/events/MinecraftEventing.h"
1516
#include "mc/legacy/ActorUniqueID.h"
1617
#include "mc/world/actor/ActorDamageSource.h"
1718
#include "mc/world/actor/ActorDefinitionIdentifier.h"
@@ -42,6 +43,7 @@
4243
#include "mc/world/phys/AABB.h"
4344
#include "mc/world/phys/HitResult.h"
4445

46+
#include <optional>
4547
#include <utility>
4648

4749
namespace lse::events::entity {
@@ -334,46 +336,6 @@ LL_TYPE_INSTANCE_HOOK(
334336
return origin(source, damage, hurtParameters);
335337
}
336338

337-
LL_TYPE_INSTANCE_HOOK(
338-
MobHurtEffectHook,
339-
HookPriority::Normal,
340-
Mob,
341-
&Mob::getDamageAfterResistanceEffect,
342-
float,
343-
::ActorDamageSource const& source,
344-
float damage
345-
) {
346-
IF_LISTENED(EVENT_TYPES::onMobHurt) {
347-
if (isServerThread()) {
348-
// Mob is still hurt after hook Mob::$hurtEffects, and all hurt events are handled by this function, but
349-
// we just need magic damage.
350-
if (source.mCause == SharedTypes::Legacy::ActorDamageCause::Magic
351-
|| source.mCause == SharedTypes::Legacy::ActorDamageCause::Wither) {
352-
Actor* damageSource = nullptr;
353-
if (source.isEntitySource()) {
354-
if (source.isChildEntitySource()) {
355-
damageSource = ll::service::getLevel()->fetchEntity(source.getEntityUniqueID(), false);
356-
} else {
357-
damageSource = ll::service::getLevel()->fetchEntity(source.getDamagingEntityUniqueID(), false);
358-
}
359-
}
360-
361-
if (!CallEvent(
362-
EVENT_TYPES::onMobHurt,
363-
EntityClass::newEntity(this),
364-
damageSource ? EntityClass::newEntity(damageSource) : Local<Value>(),
365-
Number::newNumber(damage < 0.0f ? -damage : damage),
366-
Number::newNumber(static_cast<int>(source.mCause))
367-
)) {
368-
return 0.0f;
369-
}
370-
}
371-
}
372-
}
373-
IF_LISTENED_END(EVENT_TYPES::onMobHurt)
374-
return origin(source, damage);
375-
}
376-
377339
LL_TYPE_INSTANCE_HOOK(
378340
NpcCommandHook,
379341
HookPriority::Normal,
@@ -413,56 +375,74 @@ LL_TYPE_INSTANCE_HOOK(
413375
origin(owner, sourcePlayer, actionIndex, sceneName);
414376
}
415377

416-
LL_TYPE_INSTANCE_HOOK(
378+
LL_TYPE_STATIC_HOOK(
417379
EffectUpdateHook,
418380
HookPriority::Normal,
419-
Actor,
420-
&Actor::onEffectUpdated,
381+
MinecraftEventing,
382+
&MinecraftEventing::fireEventMobEffectChanged,
421383
void,
422-
MobEffectInstance& effect
384+
::Mob& mob,
385+
::MobEffectInstance const& effectInstance,
386+
::MinecraftEventing::ChangeType change
423387
) {
424388
IF_LISTENED(EVENT_TYPES::onEffectUpdated) {
425-
if (isServerThread() && isPlayer()) {
389+
if (isServerThread() && mob.isPlayer()) {
426390
if (!CallEvent(
427391
EVENT_TYPES::onEffectUpdated,
428-
PlayerClass::newPlayer(reinterpret_cast<Player*>(this)),
429-
String::newString(MobEffect::mMobEffects()[effect.mId]->mComponentName->getString()),
430-
Number::newNumber(effect.mAmplifier),
431-
Number::newNumber(effect.mDuration->mValue)
392+
PlayerClass::newPlayer(&reinterpret_cast<Player&>(mob)),
393+
String::newString(MobEffect::mMobEffects()[effectInstance.mId]->mComponentName->getString()),
394+
Number::newNumber(effectInstance.mAmplifier),
395+
Number::newNumber(effectInstance.mDuration->mValue)
432396
)) {
433397
return;
434398
}
435399
}
436400
}
437401
IF_LISTENED_END(EVENT_TYPES::onEffectUpdated);
438-
origin(effect);
402+
origin(mob, effectInstance, change);
439403
}
440404

405+
namespace Transformation {
406+
std::pair<::OwnerPtr<::EntityContext>, Actor*> currentActor = {{}, nullptr};
441407
LL_TYPE_INSTANCE_HOOK(
442-
TransformationHook,
408+
TransformedActorHook,
443409
HookPriority::Normal,
444-
TransformationComponent,
445-
&TransformationComponent::maintainOldData,
446-
void,
447-
::Actor& originalActor,
448-
::Actor& transformed,
449-
::TransformationDescription const& transformation,
450-
::ActorUniqueID const& ownerID,
451-
::Level const& level
410+
ActorFactory,
411+
&ActorFactory::createTransformedActor,
412+
::OwnerPtr<::EntityContext>,
413+
::ActorDefinitionIdentifier const& identifier,
414+
::Actor* from
452415
) {
416+
auto context = origin(identifier, from);
417+
currentActor = {context, from};
418+
return context;
419+
}
420+
421+
LL_TYPE_INSTANCE_HOOK(
422+
addEntityHook,
423+
HookPriority::Normal,
424+
Level,
425+
&Level::$addEntity,
426+
::Actor*,
427+
::BlockSource& region,
428+
::OwnerPtr<::EntityContext> entity
429+
) {
430+
auto newActor = origin(region, entity);
453431
IF_LISTENED(EVENT_TYPES::onEntityTransformation) {
454-
if (isServerThread()) {
455-
CallEvent(
456-
EVENT_TYPES::onEntityTransformation,
457-
String::newString(std::to_string(originalActor.getOrCreateUniqueID().rawID)),
458-
EntityClass::newEntity(&transformed)
459-
);
432+
if (isServerThread() && currentActor.first) {
433+
if (currentActor.first->mEntity == entity->mEntity && currentActor.second) {
434+
CallEvent(
435+
EVENT_TYPES::onEntityTransformation,
436+
String::newString(std::to_string(currentActor.second->getOrCreateUniqueID().rawID)),
437+
EntityClass::newEntity(newActor)
438+
);
439+
}
460440
}
461441
}
462442
IF_LISTENED_END(EVENT_TYPES::onEntityTransformation);
463-
464-
origin(originalActor, transformed, transformation, ownerID, level);
443+
return newActor;
465444
}
445+
} // namespace Transformation
466446

467447
LL_TYPE_INSTANCE_HOOK(
468448
EndermanTakeBlockHook,
@@ -523,9 +503,11 @@ void ActorRideEvent() { static ll::memory::HookRegistrar<ActorRideHook> reg; }
523503
void WitherDestroyEvent() { static ll::memory::HookRegistrar<WitherDestroyHook> reg; }
524504
void ProjectileHitEntityEvent() { static ll::memory::HookRegistrar<ProjectileHitEntityHook> reg; }
525505
void ProjectileHitBlockEvent() { static ll::memory::HookRegistrar<ProjectileHitBlockHook> reg; }
526-
void MobHurtEvent() { static ll::memory::HookRegistrar<MobHurtHook, MobHurtEffectHook> reg; }
506+
void MobHurtEvent() { static ll::memory::HookRegistrar<MobHurtHook> reg; }
527507
void NpcCommandEvent() { static ll::memory::HookRegistrar<NpcCommandHook> reg; }
528508
void EndermanTakeBlockEvent() { static ll::memory::HookRegistrar<EndermanTakeBlockHook> reg; }
529509
void EffectUpdateEvent() { static ll::memory::HookRegistrar<EffectUpdateHook> reg; }
530-
void TransformationEvent() { static ll::memory::HookRegistrar<TransformationHook> reg; }
510+
void TransformationEvent() {
511+
static ll::memory::HookRegistrar<Transformation::addEntityHook, Transformation::TransformedActorHook> reg;
512+
}
531513
} // namespace lse::events::entity

0 commit comments

Comments
 (0)