Skip to content

Commit 603e54a

Browse files
committed
fix: fix EntityAPI completely #157
1 parent 13ff641 commit 603e54a

File tree

6 files changed

+93
-49
lines changed

6 files changed

+93
-49
lines changed

src/legacy/api/DeviceAPI.cpp

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,36 @@ ClassDefine<DeviceClass> DeviceClassBuilder = defineClass<DeviceClass>("LLSE_Dev
3333

3434
//////////////////// Classes ////////////////////
3535

36+
namespace PlayerAPIPatch {
37+
extern std::list<Player*> validPlayers;
38+
}
39+
3640
// 生成函数
37-
Local<Object> DeviceClass::newDevice(ActorRuntimeID& id) {
38-
auto newp = new DeviceClass(id);
41+
Local<Object> DeviceClass::newDevice(Player* player) {
42+
auto newp = new DeviceClass(player);
3943
return newp->getScriptObject();
4044
}
4145

4246
// 成员函数
43-
void DeviceClass::setPlayer(ActorRuntimeID& id) {
44-
if (id) {
45-
runtimeId = id;
47+
void DeviceClass::setPlayer(Player* player) {
48+
if (player) {
49+
mPlayer = player;
50+
PlayerAPIPatch::validPlayers.emplace_back(player);
51+
} else {
52+
mValid = false;
4653
}
4754
}
4855

4956
Player* DeviceClass::getPlayer() {
50-
if (runtimeId) {
51-
ll::service::getLevel()->getRuntimePlayer(runtimeId);
57+
mValid = false;
58+
for (Player* player : PlayerAPIPatch::validPlayers) {
59+
if (player == mPlayer) {
60+
mValid = true;
61+
break;
62+
}
63+
}
64+
if (mPlayer && mValid) {
65+
return mPlayer;
5266
}
5367
return nullptr;
5468
}

src/legacy/api/DeviceAPI.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
#pragma once
22
#include "api/APIHelp.h"
3-
#include "mc/deps/core/mce/UUID.h"
43
#include "mc/world/ActorRuntimeID.h"
5-
#include "mc/world/ActorUniqueID.h"
64

75
//////////////////// Classes ////////////////////
86
class Player;
97
class DeviceClass : public ScriptClass {
108
private:
11-
ActorRuntimeID runtimeId;
9+
Player* mPlayer;
10+
bool mValid = true;
1211

1312
public:
14-
explicit DeviceClass(ActorRuntimeID& id) : ScriptClass(ScriptClass::ConstructFromCpp<DeviceClass>{}) {
15-
setPlayer(id);
13+
explicit DeviceClass(Player* player) : ScriptClass(ScriptClass::ConstructFromCpp<DeviceClass>{}) {
14+
setPlayer(player);
1615
}
1716

18-
void setPlayer(ActorRuntimeID& id);
17+
void setPlayer(Player* player);
1918
Player* getPlayer();
2019

21-
static Local<Object> newDevice(ActorRuntimeID& id);
20+
static Local<Object> newDevice(Player* player);
2221

2322
Local<Value> getIP();
2423
Local<Value> getAvgPing();

src/legacy/api/EntityAPI.cpp

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,42 +9,38 @@
99
#include "api/NativeAPI.h"
1010
#include "api/NbtAPI.h"
1111
#include "api/PlayerAPI.h"
12+
#include "ll/api/memory/Hook.h"
1213
#include "ll/api/memory/Memory.h"
1314
#include "ll/api/service/Bedrock.h"
1415
#include "lse/api/MoreGlobal.h"
1516
#include "mc/common/HitDetection.h"
16-
#include "mc/dataloadhelper/DefaultDataLoadHelper.h"
1717
#include "mc/deps/core/string/HashedString.h"
1818
#include "mc/entity/utilities/ActorDamageCause.h"
19+
#include "mc/entity/utilities/ActorEquipment.h"
20+
#include "mc/entity/utilities/ActorMobilityUtils.h"
1921
#include "mc/entity/utilities/ActorType.h"
22+
#include "mc/math/Vec2.h"
23+
#include "mc/nbt/CompoundTag.h"
2024
#include "mc/world/SimpleContainer.h"
2125
#include "mc/world/actor/ActorDefinitionIdentifier.h"
26+
#include "mc/world/actor/Mob.h"
27+
#include "mc/world/actor/components/SynchedActorDataAccess.h"
28+
#include "mc/world/actor/item/ItemActor.h"
29+
#include "mc/world/actor/player/Player.h"
30+
#include "mc/world/attribute/AttributeInstance.h"
31+
#include "mc/world/attribute/SharedAttributes.h"
2232
#include "mc/world/effect/MobEffectInstance.h"
33+
#include "mc/world/level/BlockSource.h"
34+
#include "mc/world/level/IConstBlockSource.h"
35+
#include "mc/world/level/Level.h"
2336
#include "mc/world/level/Spawner.h"
2437
#include "mc/world/level/block/Block.h"
38+
#include "mc/world/level/material/Material.h"
39+
#include "mc/world/phys/AABB.h"
2540

2641
#include <climits>
42+
#include <entt/entt.hpp>
2743
#include <magic_enum.hpp>
28-
#include <mc/deps/core/string/HashedString.h>
29-
#include <mc/entity/EntityContext.h>
30-
#include <mc/entity/utilities/ActorEquipment.h>
31-
#include <mc/entity/utilities/ActorMobilityUtils.h>
32-
#include <mc/nbt/CompoundTag.h>
33-
#include <mc/server/ServerPlayer.h>
34-
#include <mc/world/actor/Mob.h>
35-
#include <mc/world/actor/SynchedActorData.h>
36-
#include <mc/world/actor/SynchedActorDataEntityWrapper.h>
37-
#include <mc/world/actor/components/SynchedActorDataAccess.h>
38-
#include <mc/world/actor/item/ItemActor.h>
39-
#include <mc/world/attribute/Attribute.h>
40-
#include <mc/world/attribute/AttributeInstance.h>
41-
#include <mc/world/attribute/SharedAttributes.h>
42-
#include <mc/world/level/BlockSource.h>
43-
#include <mc/world/level/IConstBlockSource.h>
44-
#include <mc/world/level/Level.h>
45-
#include <mc/world/level/biome/Biome.h>
46-
#include <mc/world/level/material/Material.h>
47-
#include <mc/world/phys/AABB.h>
4844
#include <memory>
4945
#include <vector>
5046

@@ -189,6 +185,12 @@ ClassDefine<void> ActorDamageCauseBuilder =
189185
.build();
190186

191187
// clang-format on
188+
namespace EntityAPIPatch {
189+
std::list<Actor*> validActors;
190+
LL_AUTO_TYPE_INSTANCE_HOOK(ActorDestructorHook, HookPriority::Highest, Actor, "??1Actor@@UEAA@XZ", void) {
191+
validActors.remove(this);
192+
}
193+
} // namespace EntityAPIPatch
192194

193195
// 生成函数
194196
Local<Object> EntityClass::newEntity(Actor* actor) {
@@ -211,13 +213,23 @@ std::optional<Actor*> EntityClass::tryExtractActor(Local<Value> v) {
211213
// 成员函数
212214
void EntityClass::set(Actor* actor) {
213215
if (actor) {
214-
runtimeId = actor->getRuntimeID();
216+
mActor = actor;
217+
EntityAPIPatch::validActors.emplace_back(actor);
218+
} else {
219+
mValid = false;
215220
}
216221
}
217222

218223
Actor* EntityClass::get() {
219-
if (runtimeId) {
220-
return ll::service::getLevel()->getRuntimeEntity(runtimeId);
224+
mValid = false;
225+
for (Actor* actor : EntityAPIPatch::validActors) {
226+
if (actor == mActor) {
227+
mValid = true;
228+
break;
229+
}
230+
}
231+
if (mActor && mValid) {
232+
return mActor;
221233
}
222234
return nullptr;
223235
}
@@ -901,11 +913,10 @@ Local<Value> EntityClass::isPlayer(const Arguments& args) {
901913

902914
Local<Value> EntityClass::toPlayer(const Arguments& args) {
903915
try {
904-
Player* player = ll::service::getLevel()->getRuntimePlayer(runtimeId);
905-
if (!player) {
916+
if (!mActor || !mValid) {
906917
return Local<Value>();
907918
}
908-
return PlayerClass::newPlayer(player);
919+
return PlayerClass::newPlayer(static_cast<Player*>(mActor));
909920
}
910921
CATCH("Fail in toPlayer!");
911922
}

src/legacy/api/EntityAPI.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#pragma once
22
#include "api/APIHelp.h"
33
#include "mc/world/ActorRuntimeID.h"
4-
#include "mc/world/ActorUniqueID.h"
54

65
//////////////////// Classes ////////////////////
76
class Actor;
87
class EntityClass : public ScriptClass {
98
private:
10-
ActorRuntimeID runtimeId;
9+
Actor* mActor;
10+
bool mValid = true;
1111

1212
public:
1313
explicit EntityClass(Actor* actor) : ScriptClass(ScriptClass::ConstructFromCpp<EntityClass>{}) { set(actor); }

src/legacy/api/PlayerAPI.cpp

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "legacyapi/form/FormPacketHelper.h"
2121
#include "legacyapi/form/FormUI.h"
2222
#include "ll/api/form/CustomForm.h"
23+
#include "ll/api/memory/Hook.h"
2324
#include "ll/api/memory/Memory.h"
2425
#include "ll/api/service/Bedrock.h"
2526
#include "ll/api/service/PlayerInfo.h"
@@ -76,6 +77,7 @@
7677

7778
#include <algorithm>
7879
#include <climits>
80+
#include <list>
7981
#include <mc/entity/EntityContext.h>
8082
#include <mc/entity/utilities/ActorEquipment.h>
8183
#include <mc/entity/utilities/ActorMobilityUtils.h>
@@ -716,16 +718,33 @@ Local<Value> McClass::broadcast(const Arguments& args) {
716718
CATCH("Fail in Broadcast!")
717719
}
718720

721+
namespace PlayerAPIPatch {
722+
std::list<Player*> validPlayers;
723+
LL_AUTO_TYPE_INSTANCE_HOOK(PlayerDestructorHook, HookPriority::Highest, Player, "??1Player@@UEAA@XZ", void) {
724+
validPlayers.remove(this);
725+
}
726+
} // namespace PlayerAPIPatch
727+
719728
// 成员函数
720729
void PlayerClass::set(Player* player) {
721730
if (player) {
722-
runtimeId = player->getRuntimeID();
731+
mPlayer = player;
732+
PlayerAPIPatch::validPlayers.emplace_back(player);
733+
} else {
734+
mValid = false;
723735
}
724736
}
725737

726738
Player* PlayerClass::get() {
727-
if (runtimeId) {
728-
return ll::service::getLevel()->getRuntimePlayer(runtimeId);
739+
mValid = false;
740+
for (Player* player : PlayerAPIPatch::validPlayers) {
741+
if (player == mPlayer) {
742+
mValid = true;
743+
break;
744+
}
745+
}
746+
if (mPlayer && mValid) {
747+
return mPlayer;
729748
}
730749
return nullptr;
731750
}
@@ -2041,7 +2060,7 @@ Local<Value> PlayerClass::getBlockStandingOn(const Arguments& args) {
20412060

20422061
Local<Value> PlayerClass::getDevice(const Arguments& args) {
20432062
try {
2044-
return DeviceClass::newDevice(runtimeId);
2063+
return DeviceClass::newDevice(mPlayer);
20452064
}
20462065
CATCH("Fail in getDevice!");
20472066
}

src/legacy/api/PlayerAPI.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ class SimulatedPlayer;
88
class Player;
99
class PlayerClass : public ScriptClass {
1010
private:
11-
ActorRuntimeID runtimeId;
11+
Player* mPlayer;
12+
bool mValid = true;
1213

1314
public:
14-
explicit PlayerClass(Player* p);
15+
explicit PlayerClass(Player* player);
1516

1617
void set(Player* player);
1718
Player* get();

0 commit comments

Comments
 (0)