From 835abf73b4b3f474e1b1302e4247d6124a9b888f Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Sat, 24 Feb 2024 13:13:41 +1100 Subject: [PATCH] fix(lib): load server/vehicle.lua, various fixes and tweaks --- lib/client/player.lua | 18 ++++++++++++------ lib/client/player.ts | 40 ++++++++++++++++++++++++++-------------- lib/server/init.lua | 1 + lib/server/player.lua | 2 +- lib/server/vehicle.lua | 4 ++-- 5 files changed, 42 insertions(+), 23 deletions(-) diff --git a/lib/client/player.lua b/lib/client/player.lua index a3c96c33..0263de63 100644 --- a/lib/client/player.lua +++ b/lib/client/player.lua @@ -1,6 +1,7 @@ local OxPlayer = lib.class('OxPlayer') local groups = {} +-- Support for `player.method` rather than self (:) syntax function OxPlayer:__index(index) local value = OxPlayer[index] --[[@as any]] @@ -56,12 +57,6 @@ function OxPlayer:getState() return LocalPlayer.state; end -for method in pairs(exports.ox_core:GetPlayerCalls() or {}) do - if not OxPlayer[method] then OxPlayer[method] = OxPlayer.__call end -end - -local player = OxPlayer:new(exports.ox_core.GetPlayer()) - function OxPlayer:getGroups() return groups end function OxPlayer:getGroup(filter) @@ -99,11 +94,22 @@ end ---@class OxClient local Ox = Ox +local ok, resp = pcall(function() return exports.ox_core.GetPlayer() end) +local player = OxPlayer:new(ok and resp or {}) function Ox.GetPlayer() return player end +local function getMethods() + for method in pairs(exports.ox_core:GetPlayerCalls()) do + if not rawget(OxPlayer, method) then OxPlayer[method] = OxPlayer.__call end + end +end + +-- Prevent errors if resource starts before ox_core (generally during development) +if not pcall(getMethods) then CreateThread(getMethods) end + AddEventHandler('ox:playerLoaded', function(data) if player.charId then return end diff --git a/lib/client/player.ts b/lib/client/player.ts index ddd6943b..27b4ba25 100644 --- a/lib/client/player.ts +++ b/lib/client/player.ts @@ -5,25 +5,30 @@ import type { Dict } from 'types'; const groups: Dict = {}; class PlayerInterface { - userId: number; - charId?: number; - stateId?: string; [key: string]: any; - constructor() { - const player = exports.ox_core.GetPlayer(); - this.userId = player.userId; - this.charId = player.charId; - this.stateId = player.stateId; + constructor( + public userId: number, + public charId?: number, + public stateId?: string + ) { + this.userId = userId; + this.charId = charId; + this.stateId = stateId; this.constructor.prototype.toString = () => { return JSON.stringify(this, null, 2); }; - Object.keys(exports.ox_core.GetPlayerCalls()).forEach((method: string) => { - if (!this.constructor.prototype[method]) - this.constructor.prototype[method] = (...args: any[]) => exports.ox_core.CallPlayer(method, ...args); - }); + const getMethods = async () => { + Object.keys(exports.ox_core.GetPlayerCalls()).forEach((method: string) => { + if (!this.constructor.prototype[method]) + this.constructor.prototype[method] = (...args: any[]) => exports.ox_core.CallPlayer(method, ...args); + }); + }; + + // Prevent errors if resource starts before ox_core (generally during development) + getMethods().catch(() => setImmediate(getMethods)); } get(key: string) { @@ -82,8 +87,15 @@ class PlayerInterface { } } -//@ts-ignore -const player: typeof OxPlayer & PlayerInterface = new PlayerInterface(); +const { userId, charId, stateId } = ((): { userId: number; charId?: number; stateId?: string } => { + try { + return exports.ox_core.GetPlayer(); + } catch (e) { + return {} as any; + } +})(); + +const player = new PlayerInterface(userId, charId, stateId) as typeof OxPlayer & PlayerInterface; export function GetPlayer() { return player; diff --git a/lib/server/init.lua b/lib/server/init.lua index 205b32de..d677f59b 100644 --- a/lib/server/init.lua +++ b/lib/server/init.lua @@ -10,5 +10,6 @@ Ox = setmetatable({}, { }) require 'lib.server.player' +require 'lib.server.vehicle' return Ox diff --git a/lib/server/player.lua b/lib/server/player.lua index d76a6bf4..0e1e50ce 100644 --- a/lib/server/player.lua +++ b/lib/server/player.lua @@ -34,7 +34,7 @@ function OxPlayer:getState() end for method in pairs(exports.ox_core:GetPlayerCalls() or {}) do - if not OxPlayer[method] then OxPlayer[method] = OxPlayer.__call end + if not rawget(OxPlayer, method) then OxPlayer[method] = OxPlayer.__call end end local function CreatePlayerInstance(player) diff --git a/lib/server/vehicle.lua b/lib/server/vehicle.lua index a222032b..a15da682 100644 --- a/lib/server/vehicle.lua +++ b/lib/server/vehicle.lua @@ -21,7 +21,7 @@ function OxVehicle:__call(...) end function OxVehicle:__tostring() - return json.encode(self) + return json.encode(self, { indent = true}) end function OxVehicle:getCoords() @@ -33,7 +33,7 @@ function OxVehicle:getState() end for method in pairs(exports.ox_core:GetVehicleCalls() or {}) do - if not OxVehicle[method] then OxVehicle[method] = OxVehicle.__call end + if not rawget(OxVehicle, method) then OxVehicle[method] = OxVehicle.__call end end local function CreateVehicleInstance(vehicle)