Skip to content

Commit

Permalink
Merge pull request #23 from ghosty2004/bugfix/getVariable
Browse files Browse the repository at this point in the history
refactor: getVariable method to return null instead of undefined
  • Loading branch information
Jengas authored Apr 20, 2024
2 parents 72e30a2 + 9a2afee commit 42ac396
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 30 deletions.
2 changes: 1 addition & 1 deletion bindings/src/client/entities/Colshape.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ function getServerColshape(id, position, dimension, type, meta) {
remoteId: id,
shapeType: type,
getVariable(key) {
return meta[key];
return meta[key] ?? null;
},
hasVariable(key) {
return key in meta;
Expand Down
8 changes: 4 additions & 4 deletions bindings/src/client/entities/Object.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ export class _Object extends _Entity {
type = 'object';

getVariable(key) {
if (!this.hasVariable(key)) return undefined;
return toMp(this.alt.getMeta(key));
if (!this.hasVariable(key)) return null;
return toMp(this.alt.getMeta(key)) ?? null;
}

hasVariable(key) {
Expand Down Expand Up @@ -209,8 +209,8 @@ export class _NetworkObject extends _Object {
}

getVariable(key) {
if (!this.hasVariable(key)) return undefined;
return toMp(this.alt.getStreamSyncedMeta(key));
if (!this.hasVariable(key)) return null;
return toMp(this.alt.getStreamSyncedMeta(key)) ?? null;
}

hasVariable(key) {
Expand Down
4 changes: 2 additions & 2 deletions bindings/src/client/entities/Ped.js
Original file line number Diff line number Diff line change
Expand Up @@ -821,8 +821,8 @@ export class _LocalPed extends _Ped {
}

getVariable(key) {
if (!this.hasVariable(key)) return undefined;
return toMp(this.alt.getMeta(key));
if (!this.hasVariable(key)) return null;
return toMp(this.alt.getMeta(key)) ?? null;
}

setVariable(key, value) {
Expand Down
4 changes: 2 additions & 2 deletions bindings/src/client/entities/Player.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export class _Player extends _Entity {
}

getVariable(key) {
if (!this.alt.valid) return undefined;
if (this.alt === alt.Player.local && alt.hasLocalMeta(key)) return toMp(alt.getLocalMeta(key));
if (!this.alt.valid) return null;
if (this.alt === alt.Player.local && alt.hasLocalMeta(key)) return toMp(alt.getLocalMeta(key)) ?? null;
return super.getVariable(key);
}

Expand Down
4 changes: 2 additions & 2 deletions bindings/src/client/entities/Vehicle.js
Original file line number Diff line number Diff line change
Expand Up @@ -524,8 +524,8 @@ export class _LocalVehicle extends _Vehicle {
// TODO: override natives to use pos setter

getVariable(key) {
if (!this.hasVariable(key)) return undefined;
return toMp(this.alt.getMeta(key));
if (!this.hasVariable(key)) return null;
return toMp(this.alt.getMeta(key)) ?? null;
}

setVariable(key, value) {
Expand Down
8 changes: 4 additions & 4 deletions bindings/src/client/entities/WorldObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ export class _WorldObject extends _BaseObject {
}

getVariable(key) {
if (!this.alt.valid) return undefined;
if (this.#alt.getStreamSyncedMeta && this.#alt.hasStreamSyncedMeta(key)) return toMp(this.#alt.getStreamSyncedMeta(key));
if (this.#alt.hasSyncedMeta(key)) return toMp(this.#alt.getSyncedMeta(key));
return undefined;
if (!this.alt.valid) return null;
if (this.#alt.getStreamSyncedMeta && this.#alt.hasStreamSyncedMeta(key)) return toMp(this.#alt.getStreamSyncedMeta(key)) ?? null;
if (this.#alt.hasSyncedMeta(key)) return toMp(this.#alt.getSyncedMeta(key)) ?? null;
return null;
}

hasVariable(key) {
Expand Down
8 changes: 4 additions & 4 deletions bindings/src/client/entities/label/Label.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ export class _Label extends _VirtualEntityBase {

getVariable(key) {
if (this.alt.isRemote) {
if (!this.alt.hasStreamSyncedMeta(key)) return undefined;
return toMp(this.alt.getStreamSyncedMeta(key));
if (!this.alt.hasStreamSyncedMeta(key)) return null;
return toMp(this.alt.getStreamSyncedMeta(key)) ?? null;
}

if (!this.alt.hasMeta(key)) return undefined;
return toMp(this.alt.getMeta(key));
if (!this.alt.hasMeta(key)) return null;
return toMp(this.alt.getMeta(key)) ?? null;
}

setVariable(key, value) {
Expand Down
4 changes: 2 additions & 2 deletions bindings/src/server/entities/Colshape.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ export class _Colshape extends _WorldObject {
}

getVariable(key) {
if (!this.hasVariable(key)) return undefined;
return toMp(this.alt.getMeta(key));
if (!this.hasVariable(key)) return null;
return toMp(this.alt.getMeta(key)) ?? null;
}

destroy() {
Expand Down
2 changes: 1 addition & 1 deletion bindings/src/server/entities/Dummy.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class _MpEntity {
}

getVariable(key) {
return toMp(this.#meta[key]);
return toMp(this.#meta[key]) ?? null;
}

hasVariable(key) {
Expand Down
6 changes: 3 additions & 3 deletions bindings/src/server/entities/Entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ export class _Entity extends _WorldObject {
}

getStreamVariable(key) {
if (!mp._shareVariablesBetweenResources) return this.#streamVariableCache.get(key);
if (!this.hasStreamVariable(key)) return undefined;
return toMp(this.#alt.getStreamSyncedMeta(key));
if (!mp._shareVariablesBetweenResources) return this.#streamVariableCache.get(key) ?? null;
if (!this.hasStreamVariable(key)) return null;
return toMp(this.#alt.getStreamSyncedMeta(key)) ?? null;
}

hasStreamVariable(key) {
Expand Down
10 changes: 5 additions & 5 deletions bindings/src/server/entities/WorldObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@ export class _WorldObject extends _BaseObject {

getVariable(key) {
if (!mp._shareVariablesBetweenResources)
return this.#variableCache.get(key);
return this.#variableCache.get(key) ?? null;

if (!this.hasVariable(key)) return undefined;
if (!this.hasVariable(key)) return null;

if (this.#alt.hasLocalMeta && this.#alt.hasLocalMeta(key))
return toMp(this.#alt.getLocalMeta(key));
return toMp(this.#alt.getLocalMeta(key)) ?? null;

if (!mp._syncedMeta && this.#alt.getStreamSyncedMeta)
return toMp(this.#alt.getStreamSyncedMeta(key));
return toMp(this.#alt.getStreamSyncedMeta(key)) ?? null;

return toMp(this.#alt.getSyncedMeta(key));
return toMp(this.#alt.getSyncedMeta(key)) ?? null;
}

hasVariable(key) {
Expand Down

0 comments on commit 42ac396

Please sign in to comment.