Skip to content

Commit

Permalink
fix: add valid check for Entity
Browse files Browse the repository at this point in the history
  • Loading branch information
ShrBox committed Aug 10, 2024
1 parent 0e7974d commit 86cc588
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 7 deletions.
13 changes: 11 additions & 2 deletions src/legacy/api/DeviceAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,20 @@ void DeviceClass::setPlayer(Player* player) {
try {
if (player) {
mWeakEntity = player->getWeakEntity();
mValid = true;
}
} catch (...) {}
} catch (...) {
mValid = false;
}
}

Player* DeviceClass::getPlayer() { return mWeakEntity.tryUnwrap<Player>().as_ptr(); }
Player* DeviceClass::getPlayer() {
if (mValid) {
return mWeakEntity.tryUnwrap<Player>().as_ptr();
} else {
return nullptr;
}
}

Local<Value> DeviceClass::getIP() {
try {
Expand Down
1 change: 1 addition & 0 deletions src/legacy/api/DeviceAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class Player;
class DeviceClass : public ScriptClass {
private:
WeakRef<EntityContext> mWeakEntity;
bool mValid;

public:
explicit DeviceClass(Player* player) : ScriptClass(ScriptClass::ConstructFromCpp<DeviceClass>{}) {
Expand Down
13 changes: 11 additions & 2 deletions src/legacy/api/EntityAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,23 @@ void EntityClass::set(Actor* actor) {
try {
if (actor) {
mWeakEntity = actor->getWeakEntity();
mValid = true;
}
} catch (...) {}
} catch (...) {
mValid = false;
}
}

WeakStorageEntity& WeakStorageEntity::operator=(WeakStorageEntity const&) = default;
WeakStorageEntity::WeakStorageEntity(WeakStorageEntity const&) = default;

Actor* EntityClass::get() { return mWeakEntity.tryUnwrap<Actor>().as_ptr(); }
Actor* EntityClass::get() {
if (mValid) {
return mWeakEntity.tryUnwrap<Actor>().as_ptr();
} else {
return nullptr;
}
}

Local<Value> EntityClass::asPointer(const Arguments& args) {
try {
Expand Down
3 changes: 2 additions & 1 deletion src/legacy/api/EntityAPI.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#pragma once
#include "api/APIHelp.h"
#include "mc/world/ActorRuntimeID.h"
#include "mc/entity/WeakEntityRef.h"
#include "mc/world/ActorRuntimeID.h"

//////////////////// Classes ////////////////////
class Actor;
class EntityClass : public ScriptClass {
private:
WeakRef<EntityContext> mWeakEntity;
bool mValid;

public:
explicit EntityClass(Actor* actor) : ScriptClass(ScriptClass::ConstructFromCpp<EntityClass>{}) { set(actor); }
Expand Down
13 changes: 11 additions & 2 deletions src/legacy/api/PlayerAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -723,11 +723,20 @@ void PlayerClass::set(Player* player) {
try {
if (player) {
mWeakEntity = player->getWeakEntity();
mValid = true;
}
} catch (...) {}
} catch (...) {
mValid = false;
}
}

Player* PlayerClass::get() { return mWeakEntity.tryUnwrap<Player>().as_ptr(); }
Player* PlayerClass::get() {
if (mValid) {
return mWeakEntity.tryUnwrap<Player>().as_ptr();
} else {
return nullptr;
}
}

Local<Value> PlayerClass::getName() {
try {
Expand Down
1 change: 1 addition & 0 deletions src/legacy/api/PlayerAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Player;
class PlayerClass : public ScriptClass {
private:
WeakRef<EntityContext> mWeakEntity;
bool mValid;

public:
explicit PlayerClass(Player* player);
Expand Down

0 comments on commit 86cc588

Please sign in to comment.