Skip to content

Commit

Permalink
Split Event::MobSpawnEvent (LiteLDev#1003)
Browse files Browse the repository at this point in the history
MobSpawnEvent -> MobTrySpawnEvent & MobSpawnedEvent
LLSE:
Remove onMobSpawn
Add onMobTrySpawn & onMobSpawned
  • Loading branch information
KobeBryant114514 authored Jan 13, 2023
1 parent 39a1906 commit 2aab7c1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 25 deletions.
14 changes: 14 additions & 0 deletions LiteLoader/include/llapi/EventAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -686,12 +686,26 @@ class EntityTransformEvent : public EventTemplate<EntityTransformEvent> {
Actor* mAfterEntity = nullptr;
};

// This Event is outdated, please use MobTrySpawnEvent or MobSpawnedEvent instead.
class MobSpawnEvent : public EventTemplate<MobSpawnEvent> {
public:
string mTypeName;
Vec3 mPos;
int mDimensionId = -1;
};

class MobTrySpawnEvent : public EventTemplate<MobTrySpawnEvent> {
public:
string mTypeName;
Vec3 mPos;
int mDimensionId = -1;
};

class MobSpawnedEvent : public EventTemplate<MobSpawnedEvent> {
public:
Mob* mMob;
Vec3 mPos;
int mDimensionId = -1;
};

/* endregion */
Expand Down
43 changes: 24 additions & 19 deletions LiteLoader/src/llapi/EventAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,8 @@ DECLARE_EVENT_DATA(ServerStoppedEvent);
DECLARE_EVENT_DATA(RegCmdEvent);
DECLARE_EVENT_DATA(PlayerBedEnterEvent);
DECLARE_EVENT_DATA(ScriptPluginManagerEvent);
DECLARE_EVENT_DATA(MobSpawnEvent);
DECLARE_EVENT_DATA(MobTrySpawnEvent);
DECLARE_EVENT_DATA(MobSpawnedEvent);
DECLARE_EVENT_DATA(FormResponsePacketEvent);
DECLARE_EVENT_DATA(ResourcePackInitEvent);
DECLARE_EVENT_DATA(PlayerOpenInventoryEvent);
Expand Down Expand Up @@ -2002,23 +2003,29 @@ TInstanceHook(int, "?startSleepInBed@Player@@UEAA?AW4BedSleepingResult@@AEBVBloc
////////////// MobSpawn //////////////
TInstanceHook(Mob*, "?spawnMob@Spawner@@QEAAPEAVMob@@AEAVBlockSource@@AEBUActorDefinitionIdentifier@@PEAVActor@@AEBVVec3@@_N44@Z",
Spawner, BlockSource* a2, ActorDefinitionIdentifier* a3, Actor* a4, Vec3& a5, bool a6, bool a7, bool a8) {
IF_LISTENED(MobTrySpawnEvent) {
MobTrySpawnEvent ev{};
ev.mTypeName = a3->getCanonicalName();
ev.mPos = a5;
ev.mDimensionId = a2->getDimensionId();
if (!ev.call()) {
return nullptr;
}
}
IF_LISTENED_END(MobTrySpawnEvent);
auto en = original(this, a2, a3, a4, a5, a6, a7, a8);
if (en == nullptr) {
return en;
}
else {
IF_LISTENED(MobSpawnEvent) {
MobSpawnEvent ev{};
ev.mTypeName = a3->getCanonicalName();
IF_LISTENED(MobSpawnedEvent) {
MobSpawnedEvent ev{};
ev.mMob = en;
ev.mPos = a5;
ev.mDimensionId = a2->getDimensionId();
ev.mMob = en;
if (!ev.call()) {
en->despawn();
return nullptr;
}
ev.call();
}
IF_LISTENED_END(MobSpawnEvent)
IF_LISTENED_END(MobSpawnedEvent)
return en;
}
}
Expand All @@ -2029,32 +2036,30 @@ TClasslessInstanceHook(std::optional<class BlockPos>, "?_findValidSpawnPosUnder@
auto spawn = original(this, pos, bs);
if (spawn)
{
IF_LISTENED(MobSpawnEvent) {
MobSpawnEvent ev{};
IF_LISTENED(MobTrySpawnEvent) {
MobTrySpawnEvent ev{};
ev.mTypeName = "minecraft:wandering_trader";
ev.mPos = spawn->toVec3();
ev.mDimensionId = bs->getDimensionId();
ev.mMob = nullptr;
if (!ev.call())
return std::nullopt;
}
IF_LISTENED_END(MobSpawnEvent)
IF_LISTENED_END(MobTrySpawnEvent)
}
return spawn;
}

TClasslessInstanceHook(void, "?_setRespawnStage@EndDragonFight@@AEAAXW4RespawnAnimation@@@Z",
int a1) {
IF_LISTENED(MobSpawnEvent) {
MobSpawnEvent ev{};
IF_LISTENED(MobTrySpawnEvent) {
MobTrySpawnEvent ev{};
ev.mTypeName = "minecraft:ender_dragon";
ev.mPos = Vec3::ZERO;
ev.mDimensionId = 2;
ev.mMob = nullptr;
if (!ev.call())
return;
}
IF_LISTENED_END(MobSpawnEvent);
IF_LISTENED_END(MobTrySpawnEvent);
return original(this, a1);
}

Expand Down Expand Up @@ -2121,4 +2126,4 @@ TInstanceHook(void, "?openInventory@ServerPlayer@@UEAAXXZ", ServerPlayer) {
}
IF_LISTENED_END(PlayerOpenInventoryEvent)
original(this);
}
}
22 changes: 16 additions & 6 deletions ScriptEngine/src/api/EventAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ enum class EVENT_TYPES : int {
onAttack,
onExplode,
onBedExplode,
onMobSpawn,
onMobTrySpawn,
onMobSpawned,
onContainerChangeSlot,
EVENT_COUNT
};
Expand Down Expand Up @@ -1040,12 +1041,21 @@ void EnableEventListener(int eventId) {
});
break;

case EVENT_TYPES::onMobSpawn:
Event::MobSpawnEvent::subscribe([](const MobSpawnEvent& ev) {
IF_LISTENED(EVENT_TYPES::onMobSpawn) {
CallEvent(EVENT_TYPES::onMobSpawn, String::newString(ev.mTypeName), FloatPos::newPos(ev.mPos, ev.mDimensionId), EntityClass::newEntity((Actor*)(ev.mMob)));
case EVENT_TYPES::onMobTrySpawn:
Event::MobTrySpawnEvent::subscribe([](const MobTrySpawnEvent& ev) {
IF_LISTENED(EVENT_TYPES::onMobTrySpawn) {
CallEvent(EVENT_TYPES::onMobTrySpawn, String::newString(ev.mTypeName), FloatPos::newPos(ev.mPos, ev.mDimensionId));
}
IF_LISTENED_END(EVENT_TYPES::onMobSpawn);
IF_LISTENED_END(EVENT_TYPES::onMobTrySpawn);
});
break;

case EVENT_TYPES::onMobSpawned:
Event::MobSpawnedEvent::subscribe([](const MobSpawnedEvent& ev) {
IF_LISTENED(EVENT_TYPES::onMobSpawned) {
CallEvent(EVENT_TYPES::onMobSpawned, EntityClass::newEntity((Actor*)(ev.mMob)), FloatPos::newPos(ev.mPos, ev.mDimensionId));
}
IF_LISTENED_END(EVENT_TYPES::onMobSpawned);
});
break;

Expand Down

0 comments on commit 2aab7c1

Please sign in to comment.