Skip to content

Commit 86cc588

Browse files
committed
fix: add valid check for Entity
1 parent 0e7974d commit 86cc588

File tree

6 files changed

+37
-7
lines changed

6 files changed

+37
-7
lines changed

src/legacy/api/DeviceAPI.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,20 @@ void DeviceClass::setPlayer(Player* player) {
4444
try {
4545
if (player) {
4646
mWeakEntity = player->getWeakEntity();
47+
mValid = true;
4748
}
48-
} catch (...) {}
49+
} catch (...) {
50+
mValid = false;
51+
}
4952
}
5053

51-
Player* DeviceClass::getPlayer() { return mWeakEntity.tryUnwrap<Player>().as_ptr(); }
54+
Player* DeviceClass::getPlayer() {
55+
if (mValid) {
56+
return mWeakEntity.tryUnwrap<Player>().as_ptr();
57+
} else {
58+
return nullptr;
59+
}
60+
}
5261

5362
Local<Value> DeviceClass::getIP() {
5463
try {

src/legacy/api/DeviceAPI.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class Player;
88
class DeviceClass : public ScriptClass {
99
private:
1010
WeakRef<EntityContext> mWeakEntity;
11+
bool mValid;
1112

1213
public:
1314
explicit DeviceClass(Player* player) : ScriptClass(ScriptClass::ConstructFromCpp<DeviceClass>{}) {

src/legacy/api/EntityAPI.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,23 @@ void EntityClass::set(Actor* actor) {
209209
try {
210210
if (actor) {
211211
mWeakEntity = actor->getWeakEntity();
212+
mValid = true;
212213
}
213-
} catch (...) {}
214+
} catch (...) {
215+
mValid = false;
216+
}
214217
}
215218

216219
WeakStorageEntity& WeakStorageEntity::operator=(WeakStorageEntity const&) = default;
217220
WeakStorageEntity::WeakStorageEntity(WeakStorageEntity const&) = default;
218221

219-
Actor* EntityClass::get() { return mWeakEntity.tryUnwrap<Actor>().as_ptr(); }
222+
Actor* EntityClass::get() {
223+
if (mValid) {
224+
return mWeakEntity.tryUnwrap<Actor>().as_ptr();
225+
} else {
226+
return nullptr;
227+
}
228+
}
220229

221230
Local<Value> EntityClass::asPointer(const Arguments& args) {
222231
try {

src/legacy/api/EntityAPI.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
#pragma once
22
#include "api/APIHelp.h"
3-
#include "mc/world/ActorRuntimeID.h"
43
#include "mc/entity/WeakEntityRef.h"
4+
#include "mc/world/ActorRuntimeID.h"
55

66
//////////////////// Classes ////////////////////
77
class Actor;
88
class EntityClass : public ScriptClass {
99
private:
1010
WeakRef<EntityContext> mWeakEntity;
11+
bool mValid;
1112

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

src/legacy/api/PlayerAPI.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -723,11 +723,20 @@ void PlayerClass::set(Player* player) {
723723
try {
724724
if (player) {
725725
mWeakEntity = player->getWeakEntity();
726+
mValid = true;
726727
}
727-
} catch (...) {}
728+
} catch (...) {
729+
mValid = false;
730+
}
728731
}
729732

730-
Player* PlayerClass::get() { return mWeakEntity.tryUnwrap<Player>().as_ptr(); }
733+
Player* PlayerClass::get() {
734+
if (mValid) {
735+
return mWeakEntity.tryUnwrap<Player>().as_ptr();
736+
} else {
737+
return nullptr;
738+
}
739+
}
731740

732741
Local<Value> PlayerClass::getName() {
733742
try {

src/legacy/api/PlayerAPI.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Player;
1010
class PlayerClass : public ScriptClass {
1111
private:
1212
WeakRef<EntityContext> mWeakEntity;
13+
bool mValid;
1314

1415
public:
1516
explicit PlayerClass(Player* player);

0 commit comments

Comments
 (0)