Skip to content

Commit 76c8a4d

Browse files
committed
feat: add onProjectileHitEntity & onProjectileHitBlock #87
1 parent bdce321 commit 76c8a4d

File tree

3 files changed

+52
-34
lines changed

3 files changed

+52
-34
lines changed

src/legacy/api/EventAPI.cpp

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -295,18 +295,6 @@ void EnableEventListener(int eventId) {
295295
IF_LISTENED_END(EVENT_TYPES::afterPlaceBlock);
296296
});
297297
break;
298-
// Deprecated
299-
/*case EVENT_TYPES::onMove:
300-
Event::PlayerMoveEvent::subscribe([](const PlayerMoveEvent& ev) {
301-
IF_LISTENED(EVENT_TYPES::onMove) {
302-
CallEvent(EVENT_TYPES::onMove,
303-
PlayerClass::newPlayer(ev.mPlayer), FloatPos::newPos(ev.mPos,
304-
ev.mPlayer->getDimensionId()));
305-
}
306-
IF_LISTENED_END(EVENT_TYPES::onMove);
307-
});
308-
break;*/
309-
310298
case EVENT_TYPES::onJump:
311299
bus.emplaceListener<PlayerJumpEvent>([](PlayerJumpEvent& ev) {
312300
IF_LISTENED(EVENT_TYPES::onJump) {
@@ -595,17 +583,9 @@ void EnableEventListener(int eventId) {
595583
lse::events::ProjectileCreatedEvent();
596584
break;
597585

598-
// case EVENT_TYPES::onProjectileHitEntity:
599-
// Event::ProjectileHitEntityEvent::subscribe(
600-
// [](const ProjectileHitEntityEvent &ev) {
601-
// IF_LISTENED(EVENT_TYPES::onProjectileHitEntity) {
602-
// CallEvent(EVENT_TYPES::onProjectileHitEntity,
603-
// EntityClass::newEntity(ev.mTarget),
604-
// EntityClass::newEntity(ev.mSource));
605-
// }
606-
// IF_LISTENED_END(EVENT_TYPES::onProjectileHitEntity);
607-
// });
608-
// break;
586+
case EVENT_TYPES::onProjectileHitEntity:
587+
lse::events::ProjectileHitEntityEvent();
588+
break;
609589

610590
// case EVENT_TYPES::onEntityTransformation:
611591
// Event::EntityTransformEvent::subscribe([](const EntityTransformEvent
@@ -619,17 +599,9 @@ void EnableEventListener(int eventId) {
619599
// });
620600
// break;
621601

622-
// case EVENT_TYPES::onProjectileHitBlock:
623-
// Event::ProjectileHitBlockEvent::subscribe(
624-
// [](const ProjectileHitBlockEvent &ev) {
625-
// IF_LISTENED(EVENT_TYPES::onProjectileHitBlock) {
626-
// CallEvent(EVENT_TYPES::onProjectileHitBlock,
627-
// BlockClass::newBlock(ev.mBlockInstance),
628-
// EntityClass::newEntity(ev.mSource));
629-
// }
630-
// IF_LISTENED_END(EVENT_TYPES::onProjectileHitBlock);
631-
// });
632-
// break;
602+
case EVENT_TYPES::onProjectileHitBlock:
603+
lse::events::ProjectileHitBlockEvent();
604+
break;
633605

634606
case EVENT_TYPES::onLiquidFlow:
635607
lse::events::LiquidFlowEvent();

src/legacy/events/EventHooks.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <ll/api/memory/Memory.h>
1818
#include <mc/common/wrapper/InteractionResult.h>
1919
#include <mc/entity/WeakEntityRef.h>
20+
#include <mc/entity/components/ProjectileComponent.h>
2021
#include <mc/entity/utilities/ActorType.h>
2122
#include <mc/server/module/VanillaServerGameplayEventListener.h>
2223
#include <mc/world/actor/ActorDefinitionIdentifier.h>
@@ -964,6 +965,47 @@ LL_TYPE_INSTANCE_HOOK(
964965
origin(std::move(armorSlot), item);
965966
}
966967

968+
LL_TYPE_INSTANCE_HOOK(
969+
ProjectileHitEntityHook,
970+
HookPriority::Normal,
971+
ProjectileComponent,
972+
&ProjectileComponent::onHit,
973+
void,
974+
Actor& owner,
975+
HitResult const& res
976+
) {
977+
IF_LISTENED(EVENT_TYPES::onProjectileHitEntity) {
978+
CallEventVoid(
979+
EVENT_TYPES::onProjectileHitEntity,
980+
EntityClass::newEntity(res.getEntity()),
981+
EntityClass::newEntity(&owner)
982+
);
983+
}
984+
IF_LISTENED_END(EVENT_TYPES::onProjectileHitEntity);
985+
origin(owner, res);
986+
}
987+
988+
LL_TYPE_INSTANCE_HOOK(
989+
ProjectileHitBlockHook,
990+
HookPriority::Normal,
991+
Block,
992+
&Block::onProjectileHit,
993+
void,
994+
BlockSource& region,
995+
BlockPos const& pos,
996+
Actor const& projectile
997+
) {
998+
IF_LISTENED(EVENT_TYPES::onProjectileHitBlock) {
999+
CallEventVoid(
1000+
EVENT_TYPES::onProjectileHitBlock,
1001+
BlockClass::newBlock(this, &pos, &region),
1002+
EntityClass::newEntity(&const_cast<Actor&>(projectile))
1003+
);
1004+
}
1005+
IF_LISTENED_END(EVENT_TYPES::onProjectileHitBlock);
1006+
origin(region, pos, projectile);
1007+
}
1008+
9671009
void PlayerStartDestroyBlock() { PlayerStartDestroyHook::hook(); }
9681010
void PlayerDropItem() { PlayerDropItemHook::hook(); }
9691011
void PlayerOpenContainerEvent() { PlayerOpenContainerHook::hook(); }
@@ -1015,6 +1057,8 @@ void PlayerUseBucketTakeEvent() {
10151057
}
10161058
void PlayerConsumeTotemEvent() { PlayerConsumeTotemHook::hook(); }
10171059
void PlayerSetArmorEvent() { PlayerSetArmorHook::hook(); }
1060+
void ProjectileHitEntityEvent() { ProjectileHitEntityHook::hook(); }
1061+
void ProjectileHitBlockEvent() { ProjectileHitBlockHook::hook(); }
10181062

10191063
// NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast)
10201064
// NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables)

src/legacy/events/EventHooks.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,7 @@ void PlayerUseBucketPlaceEvent();
3636
void PlayerUseBucketTakeEvent();
3737
void PlayerConsumeTotemEvent();
3838
void PlayerSetArmorEvent();
39+
void ProjectileHitEntityEvent();
40+
void ProjectileHitBlockEvent();
3941

4042
} // namespace lse::events

0 commit comments

Comments
 (0)