Skip to content

Commit

Permalink
Update v2.7.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Musiker15 committed Nov 3, 2024
1 parent 16c289c commit b929451
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 40 deletions.
6 changes: 1 addition & 5 deletions client/functions/callbacks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...)
Expand Down
22 changes: 18 additions & 4 deletions client/functions/player.lua
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -24,27 +28,37 @@ 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

return isDead
end

setmetatable(Player, {
__index = function(self, key, ...)
__index = function(self, key)
if key == 'coords' then
return GetEntityCoords(self.ped)
elseif key == 'heading' then
return GetEntityHeading(self.ped)
elseif key == 'state' then
return PlayerState(self.serverId).state
end

if tonumber(key) then
return MSK.Trigger('msk_core:player', key)
end
end
})

Expand Down
5 changes: 5 additions & 0 deletions client/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}

Expand Down
4 changes: 4 additions & 0 deletions import.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
})

Expand Down
22 changes: 2 additions & 20 deletions server/functions/callbacks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...)
Expand Down Expand Up @@ -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, ...)
Expand All @@ -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, ...)
Expand All @@ -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)
Expand All @@ -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]
----------------------------------------------------------------
Expand Down
10 changes: 10 additions & 0 deletions server/functions/commands.lua
Original file line number Diff line number Diff line change
@@ -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

Expand Down
18 changes: 16 additions & 2 deletions server/functions/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
exports('HasItem', MSK.HasItem)
MSK.Register('msk_core:hasItem', MSK.HasItem)
13 changes: 12 additions & 1 deletion server/functions/player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,15 @@ local onPlayer = function(key, value, oldValue)
end
RegisterNetEvent('msk_core:onPlayer', onPlayer)

MSK.Player = Player
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)
5 changes: 5 additions & 0 deletions server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions shared/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
11 changes: 6 additions & 5 deletions shared/math.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
3 changes: 1 addition & 2 deletions shared/timeout.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b929451

Please sign in to comment.