From e8084ad9313c612ab1082fc30ca6ff3719d97953 Mon Sep 17 00:00:00 2001 From: ghosty Date: Wed, 24 Apr 2024 13:40:17 +0300 Subject: [PATCH 1/8] fix: getVariable return type in client-side (#24) * fix: getVariable return type in client-side A few days ago I made a commit (9a2afee7544ef0359dd1cfe5bad7d062454e92ce) where I explained that the `getVariable` method returns null if it is not defined, but in client-side this will return undefined. This commit is just a reverse of client src * refactor: removed unnecesary undefined returnes --- bindings/src/client/entities/Colshape.js | 2 +- bindings/src/client/entities/Object.js | 8 ++++---- bindings/src/client/entities/Ped.js | 4 ++-- bindings/src/client/entities/Player.js | 4 ++-- bindings/src/client/entities/Vehicle.js | 4 ++-- bindings/src/client/entities/WorldObject.js | 7 +++---- bindings/src/client/entities/label/Label.js | 8 ++++---- 7 files changed, 18 insertions(+), 19 deletions(-) diff --git a/bindings/src/client/entities/Colshape.js b/bindings/src/client/entities/Colshape.js index 1a300a0..9d42a20 100644 --- a/bindings/src/client/entities/Colshape.js +++ b/bindings/src/client/entities/Colshape.js @@ -131,7 +131,7 @@ function getServerColshape(id, position, dimension, type, meta) { remoteId: id, shapeType: type, getVariable(key) { - return meta[key] ?? null; + return meta[key]; }, hasVariable(key) { return key in meta; diff --git a/bindings/src/client/entities/Object.js b/bindings/src/client/entities/Object.js index ad41d38..3809319 100644 --- a/bindings/src/client/entities/Object.js +++ b/bindings/src/client/entities/Object.js @@ -36,8 +36,8 @@ export class _Object extends _Entity { type = 'object'; getVariable(key) { - if (!this.hasVariable(key)) return null; - return toMp(this.alt.getMeta(key)) ?? null; + if (!this.hasVariable(key)) return; + return toMp(this.alt.getMeta(key)); } hasVariable(key) { @@ -209,8 +209,8 @@ export class _NetworkObject extends _Object { } getVariable(key) { - if (!this.hasVariable(key)) return null; - return toMp(this.alt.getStreamSyncedMeta(key)) ?? null; + if (!this.hasVariable(key)) return; + return toMp(this.alt.getStreamSyncedMeta(key)); } hasVariable(key) { diff --git a/bindings/src/client/entities/Ped.js b/bindings/src/client/entities/Ped.js index 4b1e90b..6abd9fe 100644 --- a/bindings/src/client/entities/Ped.js +++ b/bindings/src/client/entities/Ped.js @@ -821,8 +821,8 @@ export class _LocalPed extends _Ped { } getVariable(key) { - if (!this.hasVariable(key)) return null; - return toMp(this.alt.getMeta(key)) ?? null; + if (!this.hasVariable(key)) return; + return toMp(this.alt.getMeta(key)); } setVariable(key, value) { diff --git a/bindings/src/client/entities/Player.js b/bindings/src/client/entities/Player.js index f4428c6..4d278ac 100644 --- a/bindings/src/client/entities/Player.js +++ b/bindings/src/client/entities/Player.js @@ -19,8 +19,8 @@ export class _Player extends _Entity { } getVariable(key) { - if (!this.alt.valid) return null; - if (this.alt === alt.Player.local && alt.hasLocalMeta(key)) return toMp(alt.getLocalMeta(key)) ?? null; + if (!this.alt.valid) return; + if (this.alt === alt.Player.local && alt.hasLocalMeta(key)) return toMp(alt.getLocalMeta(key)); return super.getVariable(key); } diff --git a/bindings/src/client/entities/Vehicle.js b/bindings/src/client/entities/Vehicle.js index 1ec07ba..d570c09 100644 --- a/bindings/src/client/entities/Vehicle.js +++ b/bindings/src/client/entities/Vehicle.js @@ -524,8 +524,8 @@ export class _LocalVehicle extends _Vehicle { // TODO: override natives to use pos setter getVariable(key) { - if (!this.hasVariable(key)) return null; - return toMp(this.alt.getMeta(key)) ?? null; + if (!this.hasVariable(key)) return; + return toMp(this.alt.getMeta(key)); } setVariable(key, value) { diff --git a/bindings/src/client/entities/WorldObject.js b/bindings/src/client/entities/WorldObject.js index 71cb45c..bf8e605 100644 --- a/bindings/src/client/entities/WorldObject.js +++ b/bindings/src/client/entities/WorldObject.js @@ -14,10 +14,9 @@ export class _WorldObject extends _BaseObject { } getVariable(key) { - 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; + if (!this.alt.valid) return; + 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)); } hasVariable(key) { diff --git a/bindings/src/client/entities/label/Label.js b/bindings/src/client/entities/label/Label.js index 75c3ad1..b3626b7 100644 --- a/bindings/src/client/entities/label/Label.js +++ b/bindings/src/client/entities/label/Label.js @@ -48,12 +48,12 @@ export class _Label extends _VirtualEntityBase { getVariable(key) { if (this.alt.isRemote) { - if (!this.alt.hasStreamSyncedMeta(key)) return null; - return toMp(this.alt.getStreamSyncedMeta(key)) ?? null; + if (!this.alt.hasStreamSyncedMeta(key)) return; + return toMp(this.alt.getStreamSyncedMeta(key)); } - if (!this.alt.hasMeta(key)) return null; - return toMp(this.alt.getMeta(key)) ?? null; + if (!this.alt.hasMeta(key)) return undefined; + return toMp(this.alt.getMeta(key)); } setVariable(key, value) { From 7763d3f013f16d378f736695b706099e31755185 Mon Sep 17 00:00:00 2001 From: Jengas Date: Wed, 24 Apr 2024 22:05:59 +0200 Subject: [PATCH 2/8] chore: bump version to 1.0.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 181bd6a..92eb51b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ragemp-altv-bridge", - "version": "1.0.1", + "version": "1.0.2", "description": "RAGE Multiplayer alt:V Bridge. This package provides a bridge between RAGE Multiplayer and alt:V. It allows you to use RAGEMP code in alt:V.", "keywords": [ "ragemp", From 86f9e0a04a7a95785c6e38b6992c6825fb9d459a Mon Sep 17 00:00:00 2001 From: Jengas Date: Sun, 19 May 2024 10:32:17 +0200 Subject: [PATCH 3/8] fix: missing default p2 bool --- bindings/src/client/entities/Vehicle.js | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bindings/src/client/entities/Vehicle.js b/bindings/src/client/entities/Vehicle.js index d570c09..fe7b20f 100644 --- a/bindings/src/client/entities/Vehicle.js +++ b/bindings/src/client/entities/Vehicle.js @@ -335,7 +335,7 @@ export class _Vehicle extends _Entity { return this.setTaskGotoPlaneMinHeightAboveTerrain; } - getPedInSeat(seatIndex, p2) { + getPedInSeat(seatIndex, p2 = false) { return natives.getPedInVehicleSeat(this.handle, seatIndex, p2); } diff --git a/package.json b/package.json index 92eb51b..c2a4cef 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ragemp-altv-bridge", - "version": "1.0.2", + "version": "1.0.3", "description": "RAGE Multiplayer alt:V Bridge. This package provides a bridge between RAGE Multiplayer and alt:V. It allows you to use RAGEMP code in alt:V.", "keywords": [ "ragemp", From 895cea6911f26067c587b7f3e29989b039e261a5 Mon Sep 17 00:00:00 2001 From: Jengas Date: Sun, 19 May 2024 11:18:23 +0200 Subject: [PATCH 4/8] update: better enter/leave vehicle events handler fix: seat index on client-side --- bindings/src/client/entities/Player.js | 51 ++++++++++---------------- bindings/src/shared/utils.js | 4 ++ 2 files changed, 23 insertions(+), 32 deletions(-) diff --git a/bindings/src/client/entities/Player.js b/bindings/src/client/entities/Player.js index 4d278ac..5533145 100644 --- a/bindings/src/client/entities/Player.js +++ b/bindings/src/client/entities/Player.js @@ -3,7 +3,7 @@ import * as natives from 'natives'; import mp from '../../shared/mp.js'; import {ClientPool} from '../pools/ClientPool.js'; import {_Entity} from './Entity.js'; -import {altSeatToMp, getOverlayColorType, internalName, toMp} from '../../shared/utils'; +import {altSeatToClientMp, getOverlayColorType, internalName, toMp} from '../../shared/utils'; import {EntityGetterView} from '../../shared/pools/EntityGetterView'; import {emitServerInternal} from '../clientUtils'; import {BaseObjectType} from '../../shared/BaseObjectType'; @@ -1068,37 +1068,6 @@ function getSeat() { } } -if (mp._main) { - let lastVehicle = mp.players.local.vehicle; - let lastSeat = getSeat(); - setInterval(() => { - const newVehicle = mp.players.local.vehicle; - if (newVehicle !== lastVehicle) { - console.log('Changed vehicle from ' + lastVehicle?.id + ' to ' + newVehicle?.id); - mp.notifyTrace('player', 'player changed vehicle from', lastVehicle, 'to', newVehicle); - if (lastVehicle) { - mp.events.dispatchLocal('playerLeaveVehicle', lastVehicle?.alt?.valid ? lastVehicle : null, lastSeat); - } - - if (newVehicle) { - const newSeat = getSeat(); - mp.events.dispatchLocal('playerStartEnterVehicle', newVehicle, newSeat); - mp.events.dispatchLocal('playerEnterVehicle', newVehicle, newSeat); - lastSeat = newSeat; - } - - lastVehicle = newVehicle; - } else if (lastVehicle) { - const newSeat = getSeat(); - if (newSeat !== lastSeat) { - console.log('Changed vehicle seat from ' + lastSeat + ' to ' + newSeat); - mp.events.dispatchLocal('playerEnterVehicle', newVehicle, newSeat); - lastSeat = newSeat; - } - } - }, 500); -} - alt.on('netOwnerChange', (ent, newOwner, oldOwner) => { mp.events.dispatchLocal('entityControllerChange', ent, toMp(newOwner)); }); @@ -1127,3 +1096,21 @@ alt.on('playerStartTalking', (player) => { alt.on('playerStopTalking', (player) => { mp.events.dispatchLocal('playerStopTalking', player.mp); }); + + +alt.on('enteringVehicle', (vehicle, seat) => { + mp.events.dispatchLocal('playerStartEnterVehicle', vehicle?.mp, altSeatToClientMp(seat)); +}); + +alt.on('enteredVehicle', (vehicle, seat) => { + mp.events.dispatchLocal('playerEnterVehicle', vehicle?.mp, altSeatToClientMp(seat)); +}); + +alt.on('changedVehicleSeat', (vehicle, oldSeat, seat) => { + mp.events.dispatchLocal('playerEnterVehicle', vehicle?.mp, altSeatToClientMp(seat)); +}); + +alt.on('leftVehicle', (vehicle, seat) => { + // mp.events.dispatchLocal('playerStartExitVehicle', vehicle?.mp, altSeatToClientMp(seat) - 1); + mp.events.dispatchLocal('playerLeaveVehicle', vehicle?.mp, altSeatToClientMp(seat)); +}); diff --git a/bindings/src/shared/utils.js b/bindings/src/shared/utils.js index 8a5f9f3..0afc83d 100644 --- a/bindings/src/shared/utils.js +++ b/bindings/src/shared/utils.js @@ -135,6 +135,10 @@ export const altSeatToMp = (altSeat) => { return altSeat - 1; }; +export const altSeatToClientMp = (altSeat) => { + return altSeat - 2; +}; + export const mpSeatToAlt = (mpSeat) => { return mpSeat + 1; }; From 5a18d2d07bae2cefc37f4bcbcf661bfa89f7212e Mon Sep 17 00:00:00 2001 From: Jengas Date: Sun, 19 May 2024 11:36:37 +0200 Subject: [PATCH 5/8] chore: bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c2a4cef..883b0dc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ragemp-altv-bridge", - "version": "1.0.3", + "version": "1.0.4", "description": "RAGE Multiplayer alt:V Bridge. This package provides a bridge between RAGE Multiplayer and alt:V. It allows you to use RAGEMP code in alt:V.", "keywords": [ "ragemp", From 5968c73bb5518acb09f09af359e187c0bfa1f65c Mon Sep 17 00:00:00 2001 From: Jengas Date: Sun, 19 May 2024 12:01:03 +0200 Subject: [PATCH 6/8] fix: possible fix for objects that couldn't be created by natives --- bindings/src/client/entities/Object.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/bindings/src/client/entities/Object.js b/bindings/src/client/entities/Object.js index 3809319..11da4d6 100644 --- a/bindings/src/client/entities/Object.js +++ b/bindings/src/client/entities/Object.js @@ -246,6 +246,10 @@ export class _NetworkObject extends _Object { if (shouldCancel()) return; this.#handle = natives.createObject(model, this.alt.pos.x, this.alt.pos.y, this.alt.pos.z, false, false, false); + if (!this.#handle) { + console.error('Failed to create object using native:', model); + return; + } natives.setEntityCoordsNoOffset(this.#handle, this.alt.pos.x, this.alt.pos.y, this.alt.pos.z, false, false, false); store.add(this, undefined, this.#handle, undefined); From 6ee4e1576321fc1f41ca773b760f3ca2e379c321 Mon Sep 17 00:00:00 2001 From: Jengas Date: Sun, 19 May 2024 15:42:04 +0200 Subject: [PATCH 7/8] revert: package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 883b0dc..c2a4cef 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ragemp-altv-bridge", - "version": "1.0.4", + "version": "1.0.3", "description": "RAGE Multiplayer alt:V Bridge. This package provides a bridge between RAGE Multiplayer and alt:V. It allows you to use RAGEMP code in alt:V.", "keywords": [ "ragemp", From df0b64363a256838bc70e4f90c73e52500629222 Mon Sep 17 00:00:00 2001 From: Jengas Date: Sun, 19 May 2024 16:07:48 +0200 Subject: [PATCH 8/8] update: use new Error instead of console.error Co-authored-by: xshady <54737754+xxshady@users.noreply.github.com> --- bindings/src/client/entities/Object.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bindings/src/client/entities/Object.js b/bindings/src/client/entities/Object.js index 11da4d6..bded2ba 100644 --- a/bindings/src/client/entities/Object.js +++ b/bindings/src/client/entities/Object.js @@ -247,8 +247,7 @@ export class _NetworkObject extends _Object { this.#handle = natives.createObject(model, this.alt.pos.x, this.alt.pos.y, this.alt.pos.z, false, false, false); if (!this.#handle) { - console.error('Failed to create object using native:', model); - return; + throw new Error('Failed to create object using native:', model); } natives.setEntityCoordsNoOffset(this.#handle, this.alt.pos.x, this.alt.pos.y, this.alt.pos.z, false, false, false); store.add(this, undefined, this.#handle, undefined);