diff --git a/client/functions/callbacks.lua b/client/functions/callbacks.lua index f6092b3..3e61e99 100644 --- a/client/functions/callbacks.lua +++ b/client/functions/callbacks.lua @@ -4,11 +4,7 @@ local CallbackHandler = {} local GenerateCallbackHandlerKey = function() local requestId = math.random(1, 999999999) - if not CallbackHandler[requestId] then - return tostring(requestId) - else - GenerateCallbackHandlerKey() - end + return not CallbackHandler[requestId] and tostring(requestId) or GenerateCallbackHandlerKey() end RegisterNetEvent("msk_core:client:callbackResponse", function(requestId, ...) diff --git a/client/functions/player.lua b/client/functions/player.lua index a883c40..1e2a87b 100644 --- a/client/functions/player.lua +++ b/client/functions/player.lua @@ -1,10 +1,14 @@ local PlayerState = Player local Player = {} +Player.Get = function(playerId, key) + return MSK.Trigger('msk_core:player', playerId, key) +end + function Player:set(key, value) if self[key] ~= value then TriggerEvent('msk_core:onPlayer', key, value, self[key]) - TriggerServerEvent('msk_core:onPlayer', key, key == 'vehicle' and NetworkGetNetworkIdFromEntity(value) or value, self[key]) + TriggerServerEvent('msk_core:onPlayer', key, key == 'vehicle' and DoesEntityExist(value) and NetworkGetNetworkIdFromEntity(value) or value, self[key]) self[key] = value return true @@ -24,12 +28,18 @@ local GetPlayerDeath = function() local isDead = IsPlayerDead(Player.clientId) or IsEntityDead(Player.ped) or IsPedFatallyInjured(Player.ped) if GetResourceState("visn_are") == "started" then - local healthBuffer = exports.visn_are:GetHealthBuffer() + local healthBuffer = MSK.Call(function() + return exports.visn_are:GetHealthBuffer() + end) + isDead = healthBuffer.unconscious end if GetResourceState("osp_ambulance") == "started" then - local data = exports.osp_ambulance:GetAmbulanceData(Player.serverId) + local data = MSK.Call(function() + return exports.osp_ambulance:GetAmbulanceData(Player.serverId) + end) + isDead = data.isDead or data.inLastStand end @@ -37,7 +47,7 @@ local GetPlayerDeath = function() end setmetatable(Player, { - __index = function(self, key, ...) + __index = function(self, key) if key == 'coords' then return GetEntityCoords(self.ped) elseif key == 'heading' then @@ -45,6 +55,10 @@ setmetatable(Player, { elseif key == 'state' then return PlayerState(self.serverId).state end + + if tonumber(key) then + return MSK.Trigger('msk_core:player', key) + end end }) diff --git a/client/main.lua b/client/main.lua index df79483..53b822b 100644 --- a/client/main.lua +++ b/client/main.lua @@ -14,6 +14,8 @@ if Config.Framework == 'AUTO' then Config.Framework = 'ESX' elseif GetResourceState('qb-core') ~= 'missing' then Config.Framework = 'QBCore' + elseif GetResourceState('ox_core') ~= 'missing' then + Config.Framework = 'OXCore' else Config.Framework = 'STANDALONE' MSK.Bridge.Framework.Type = 'STANDALONE' @@ -30,6 +32,9 @@ elseif Config.Framework == 'QBCore' then QBCore = exports['qb-core']:GetCoreObject() MSK.Bridge.Framework.Type = 'QBCore' MSK.Bridge.Framework.Core = QBCore +elseif Config.Framework == 'OXCore' then + MSK.Bridge.Framework.Type = 'OXCore' + MSK.Bridge.Framework.Core = Ox or Citizen.Trace("^1SCRIPT ERROR: Please add '@ox_core/lib/init.lua' to the fxmanifest.lua^0\n") end MSK.Bridge.Inventory = Config.Inventory diff --git a/fxmanifest.lua b/fxmanifest.lua index ed6c755..7ff5917 100644 --- a/fxmanifest.lua +++ b/fxmanifest.lua @@ -4,11 +4,12 @@ games { 'gta5' } author 'Musiker15 - MSK Scripts' name 'msk_core' description 'Functions for MSK Scripts' -version '2.7.1' +version '2.7.2' lua54 'yes' shared_scripts { + -- '@ox_core/lib/init.lua', 'config.lua' } diff --git a/import.lua b/import.lua index da2adee..e4021ca 100644 --- a/import.lua +++ b/import.lua @@ -94,6 +94,10 @@ if context == 'client' then elseif key == 'state' then return PlayerState(self.serverId).state end + + if tonumber(key) then + return MSK.Trigger('msk_core:player', key) + end end }) diff --git a/server/functions/callbacks.lua b/server/functions/callbacks.lua index 17d8a33..1fa6f4c 100644 --- a/server/functions/callbacks.lua +++ b/server/functions/callbacks.lua @@ -8,6 +8,7 @@ MSK.Register = function(eventName, cb) Callbacks[eventName] = cb end MSK.RegisterCallback = MSK.Register -- Backwards compatibility +MSK.RegisterServerCallback = MSK.Register -- Backwards compatibility exports('Register', MSK.Register) RegisterNetEvent('msk_core:server:triggerCallback', function(eventName, requestId, cb, ...) @@ -35,11 +36,7 @@ end) local GenerateCallbackHandlerKey = function() local requestId = math.random(1, 999999999) - if not CallbackHandler[requestId] then - return tostring(requestId) - else - GenerateCallbackHandlerKey() - end + return not CallbackHandler[requestId] and tostring(requestId) or GenerateCallbackHandlerKey() end MSK.Trigger = function(eventName, playerId, ...) @@ -63,7 +60,6 @@ MSK.Trigger = function(eventName, playerId, ...) local result = Citizen.Await(p) return table.unpack(result) end -MSK.TriggerCallback = MSK.Trigger -- Backwards compatibility exports('Trigger', MSK.Trigger) RegisterNetEvent("msk_core:server:callbackResponse", function(requestId, ...) @@ -79,10 +75,6 @@ end) ---------------------------------------------------------------- -- Server Callbacks with Method [return] ---------------------------------------------------------------- -MSK.Register('msk_core:hasItem', function(source, itemName, metadata) - return MSK.HasItem(source, itemName, metadata) -end) - MSK.Register('msk_core:isAceAllowed', function(source, command) return MSK.IsAceAllowed(source, command) end) @@ -91,16 +83,6 @@ MSK.Register('msk_core:isPrincipalAceAllowed', function(source, principal, ace) return MSK.IsPrincipalAceAllowed(principal, ace) end) --- For clientside MSK.RegisterCommand -MSK.Register('msk_core:doesPlayerExist', function(source, targetId) - return DoesPlayerExist(targetId) -end) - --- For clientside MSK.RegisterCommand -MSK.Register('msk_core:getPlayerData', function(source, targetId) - return MSK.GetPlayer({source = targetId}) -end) - ---------------------------------------------------------------- -- Server Callbacks with Method [cb] ---------------------------------------------------------------- diff --git a/server/functions/commands.lua b/server/functions/commands.lua index 006fbf0..cfc2a07 100644 --- a/server/functions/commands.lua +++ b/server/functions/commands.lua @@ -1,5 +1,15 @@ local RegisteredCommands = {} +-- For clientside MSK.RegisterCommand +MSK.Register('msk_core:doesPlayerExist', function(source, targetId) + return DoesPlayerExist(targetId) +end) + +-- For clientside MSK.RegisterCommand +MSK.Register('msk_core:getPlayerData', function(source, targetId) + return MSK.GetPlayer({source = targetId}) +end) + AddEventHandler('playerJoining', function() local playerId = source diff --git a/server/functions/main.lua b/server/functions/main.lua index 8261e72..c55fa8e 100644 --- a/server/functions/main.lua +++ b/server/functions/main.lua @@ -135,7 +135,21 @@ MSK.HasItem = function(playerId, itemName, metadata) end local Player = MSK.GetPlayer({source = playerId}) + + if type(itemName) ~= 'table' then + return Player.HasItem(itemName, metadata) + end + + for i = 1, #itemName do + local item = itemName[i] + local hasItem = Player.HasItem(item, metadata) + + if hasItem then + return hasItem + end + end - return Player.HasItem(itemName, metadata) + return false end -exports('HasItem', MSK.HasItem) \ No newline at end of file +exports('HasItem', MSK.HasItem) +MSK.Register('msk_core:hasItem', MSK.HasItem) \ No newline at end of file diff --git a/server/functions/player.lua b/server/functions/player.lua index 429d006..fc81c3f 100644 --- a/server/functions/player.lua +++ b/server/functions/player.lua @@ -49,4 +49,15 @@ local onPlayer = function(key, value, oldValue) end RegisterNetEvent('msk_core:onPlayer', onPlayer) -MSK.Player = Player \ No newline at end of file +MSK.Player = Player + +-- For clientside MSK.Player[targetId] and MSK.Player.Get(targetId, key) +MSK.Register('msk_core:player', function(source, targetId, key) + local targetId = tonumber(targetId) + + if DoesPlayerExist(targetId) then + return key and MSK.Player[targetId][key] or MSK.Player[targetId] + end + + return false +end) \ No newline at end of file diff --git a/server/main.lua b/server/main.lua index ec355f7..bc29888 100644 --- a/server/main.lua +++ b/server/main.lua @@ -13,6 +13,8 @@ if Config.Framework == 'AUTO' then Config.Framework = 'ESX' elseif GetResourceState('qb-core') ~= 'missing' then Config.Framework = 'QBCore' + elseif GetResourceState('ox_core') ~= 'missing' then + Config.Framework = 'OXCore' else Config.Framework = 'STANDALONE' MSK.Bridge.Framework.Type = 'STANDALONE' @@ -29,6 +31,9 @@ elseif Config.Framework == 'QBCore' then QBCore = exports['qb-core']:GetCoreObject() MSK.Bridge.Framework.Type = 'QBCore' MSK.Bridge.Framework.Core = QBCore +elseif Config.Framework == 'OXCore' then + MSK.Bridge.Framework.Type = 'OXCore' + MSK.Bridge.Framework.Core = Ox or Citizen.Trace("^1SCRIPT ERROR: Please add '@ox_core/lib/init.lua' to the fxmanifest.lua^0\n") end MSK.Bridge.Inventory = Config.Inventory diff --git a/shared/functions.lua b/shared/functions.lua index 7bf20e4..b990702 100644 --- a/shared/functions.lua +++ b/shared/functions.lua @@ -6,6 +6,16 @@ end exports('Config', MSK.GetConfig) exports('GetConfig', MSK.GetConfig) +MSK.Call = function(fn, timeout) + return MSK.Timeout.Await(timeout or 1000, function() + local success, result = pcall(fn) + + if success then + return result + end + end) +end + MSK.Logging = function(code, ...) assert(code and type(code) == 'string', 'Parameter "code" has to be a string on function MSK.Logging') print(('[^2%s^0] %s'):format(GetInvokingResource() or 'msk_core', Config.LoggingTypes[code] or Config.LoggingTypes['debug']), ..., '^0') diff --git a/shared/math.lua b/shared/math.lua index fa3f486..ee3d744 100644 --- a/shared/math.lua +++ b/shared/math.lua @@ -3,14 +3,15 @@ local Numbers = {} for i = 48, 57 do table.insert(Numbers, string.char(i)) end -MSK.Math.Number = function(length) - assert(length, 'Parameter "length" is nil on function MSK.Math.Number') +MSK.Math.Random = function(length) + assert(length, 'Parameter "length" is nil on function MSK.Math.Random') math.randomseed(GetGameTimer()) - return length > 0 and MSK.Math.Number(length - 1) .. Numbers[math.random(1, #Numbers)] or '' + return length > 0 and MSK.Math.Random(length - 1) .. Numbers[math.random(1, #Numbers)] or '' end -MSK.GetRandomNumber = MSK.Math.Number -- Backwards compatibility -exports('GetRandomNumber', MSK.Math.Number) +MSK.Math.Number = MSK.Math.Random -- Backwards compatibility +MSK.GetRandomNumber = MSK.Math.Random -- Backwards compatibility +exports('GetRandomNumber', MSK.Math.Random) MSK.Math.Round = function(num, decimal) assert(num and tonumber(num), 'Parameter "num" has to be a number on function MSK.Math.Round') diff --git a/shared/timeout.lua b/shared/timeout.lua index d6ed1c5..6d61077 100644 --- a/shared/timeout.lua +++ b/shared/timeout.lua @@ -39,8 +39,7 @@ MSK.DelTimeout = MSK.Timeout.Clear -- Backwards compatibility exports('ClearTimeout', MSK.Timeout.Clear) -- Credits to ox_lib (https://overextended.dev/ox_lib/Modules/WaitFor/Shared) -MSK.Timeout.Await = function(ms, cb, errMessage) - assert(ms and tonumber(ms), 'Parameter "ms" has to be a number on function MSK.Timeout.Await') +MSK.Timeout.Await = function(timeout, cb, errMessage) local value = cb() if value ~= nil then return value end