|
12 | 12 | #include "mc/entity/components_json_legacy/NpcComponent.h" |
13 | 13 | #include "mc/entity/components_json_legacy/ProjectileComponent.h" |
14 | 14 | #include "mc/entity/components_json_legacy/TransformationComponent.h" |
| 15 | +#include "mc/events/MinecraftEventing.h" |
15 | 16 | #include "mc/legacy/ActorUniqueID.h" |
16 | 17 | #include "mc/world/actor/ActorDamageSource.h" |
17 | 18 | #include "mc/world/actor/ActorDefinitionIdentifier.h" |
|
42 | 43 | #include "mc/world/phys/AABB.h" |
43 | 44 | #include "mc/world/phys/HitResult.h" |
44 | 45 |
|
| 46 | +#include <optional> |
45 | 47 | #include <utility> |
46 | 48 |
|
47 | 49 | namespace lse::events::entity { |
@@ -334,46 +336,6 @@ LL_TYPE_INSTANCE_HOOK( |
334 | 336 | return origin(source, damage, hurtParameters); |
335 | 337 | } |
336 | 338 |
|
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 | | - |
377 | 339 | LL_TYPE_INSTANCE_HOOK( |
378 | 340 | NpcCommandHook, |
379 | 341 | HookPriority::Normal, |
@@ -413,56 +375,74 @@ LL_TYPE_INSTANCE_HOOK( |
413 | 375 | origin(owner, sourcePlayer, actionIndex, sceneName); |
414 | 376 | } |
415 | 377 |
|
416 | | -LL_TYPE_INSTANCE_HOOK( |
| 378 | +LL_TYPE_STATIC_HOOK( |
417 | 379 | EffectUpdateHook, |
418 | 380 | HookPriority::Normal, |
419 | | - Actor, |
420 | | - &Actor::onEffectUpdated, |
| 381 | + MinecraftEventing, |
| 382 | + &MinecraftEventing::fireEventMobEffectChanged, |
421 | 383 | void, |
422 | | - MobEffectInstance& effect |
| 384 | + ::Mob& mob, |
| 385 | + ::MobEffectInstance const& effectInstance, |
| 386 | + ::MinecraftEventing::ChangeType change |
423 | 387 | ) { |
424 | 388 | IF_LISTENED(EVENT_TYPES::onEffectUpdated) { |
425 | | - if (isServerThread() && isPlayer()) { |
| 389 | + if (isServerThread() && mob.isPlayer()) { |
426 | 390 | if (!CallEvent( |
427 | 391 | 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) |
432 | 396 | )) { |
433 | 397 | return; |
434 | 398 | } |
435 | 399 | } |
436 | 400 | } |
437 | 401 | IF_LISTENED_END(EVENT_TYPES::onEffectUpdated); |
438 | | - origin(effect); |
| 402 | + origin(mob, effectInstance, change); |
439 | 403 | } |
440 | 404 |
|
| 405 | +namespace Transformation { |
| 406 | +std::pair<::OwnerPtr<::EntityContext>, Actor*> currentActor = {{}, nullptr}; |
441 | 407 | LL_TYPE_INSTANCE_HOOK( |
442 | | - TransformationHook, |
| 408 | + TransformedActorHook, |
443 | 409 | 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 |
452 | 415 | ) { |
| 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); |
453 | 431 | 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 | + } |
460 | 440 | } |
461 | 441 | } |
462 | 442 | IF_LISTENED_END(EVENT_TYPES::onEntityTransformation); |
463 | | - |
464 | | - origin(originalActor, transformed, transformation, ownerID, level); |
| 443 | + return newActor; |
465 | 444 | } |
| 445 | +} // namespace Transformation |
466 | 446 |
|
467 | 447 | LL_TYPE_INSTANCE_HOOK( |
468 | 448 | EndermanTakeBlockHook, |
@@ -523,9 +503,11 @@ void ActorRideEvent() { static ll::memory::HookRegistrar<ActorRideHook> reg; } |
523 | 503 | void WitherDestroyEvent() { static ll::memory::HookRegistrar<WitherDestroyHook> reg; } |
524 | 504 | void ProjectileHitEntityEvent() { static ll::memory::HookRegistrar<ProjectileHitEntityHook> reg; } |
525 | 505 | 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; } |
527 | 507 | void NpcCommandEvent() { static ll::memory::HookRegistrar<NpcCommandHook> reg; } |
528 | 508 | void EndermanTakeBlockEvent() { static ll::memory::HookRegistrar<EndermanTakeBlockHook> reg; } |
529 | 509 | 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 | +} |
531 | 513 | } // namespace lse::events::entity |
0 commit comments