Skip to content

Commit

Permalink
fix(lib): load server/vehicle.lua, various fixes and tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
thelindat committed Feb 24, 2024
1 parent 7a0569d commit 835abf7
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 23 deletions.
18 changes: 12 additions & 6 deletions lib/client/player.lua
Original file line number Diff line number Diff line change
@@ -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]]

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down
40 changes: 26 additions & 14 deletions lib/client/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,30 @@ import type { Dict } from 'types';
const groups: Dict<number> = {};

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) {
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions lib/server/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ Ox = setmetatable({}, {
})

require 'lib.server.player'
require 'lib.server.vehicle'

return Ox
2 changes: 1 addition & 1 deletion lib/server/player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions lib/server/vehicle.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand Down

0 comments on commit 835abf7

Please sign in to comment.