Skip to content

Commit bdfc060

Browse files
committed
Update v2.6.2
1 parent 94da3bb commit bdfc060

File tree

12 files changed

+255
-99
lines changed

12 files changed

+255
-99
lines changed

client/functions/ace.lua

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
MSK.IsAceAllowed = function(command)
2+
return MSK.Trigger('msk_core:isAceAllowed', ('command.%s'):format(command))
3+
end
4+
exports('IsAceAllowed', MSK.IsAceAllowed)
5+
6+
MSK.IsPrincipalAceAllowed = function(restricted, ace)
7+
return MSK.Trigger('msk_core:isPrincipalAceAllowed', restricted, ace)
8+
end
9+
exports('IsPrincipalAceAllowed', MSK.IsPrincipalAceAllowed)

client/functions/callbacks.lua

+1-7
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,7 @@ end)
2727
MSK.Register = function(eventName, cb)
2828
Callbacks[eventName] = cb
2929
end
30-
MSK.RegisterCallback = MSK.Register -- Support for old Scripts
31-
MSK.RegisterClientCallback = MSK.Register -- Support for old Scripts
3230
exports('Register', MSK.Register)
33-
exports('RegisterCallback', MSK.Register) -- Support for old Scripts
34-
exports('RegisterServerCallback', MSK.Register) -- Support for old Scripts
3531

3632
RegisterNetEvent('msk_core:client:triggerClientCallback', function(playerId, eventName, requestId, ...)
3733
if not Callbacks[eventName] then
@@ -92,6 +88,4 @@ MSK.TriggerCallback = function(eventName, ...)
9288
local result = Citizen.Await(p)
9389
return table.unpack(result)
9490
end
95-
MSK.TriggerServerCallback = MSK.TriggerCallback -- Support for old Scripts
96-
exports('TriggerCallback', MSK.TriggerCallback)
97-
exports('TriggerServerCallback', MSK.TriggerCallback) -- Support for old Scripts
91+
exports('TriggerCallback', MSK.TriggerCallback)

client/functions/command.lua

+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
local RegisteredCommands, RegisteredHotkeys = {}, {}
2+
3+
local RegisterHotkey = function(commandName, properties)
4+
if RegisteredCommands[commandName].properties.params then
5+
if RegisteredHotkeys[commandName] then
6+
MSK.Logging('warn', ("Command '%s' found a Registered Hotkey! You'll need a server restart to delete it."):format(commandName))
7+
end
8+
9+
return MSK.Logging('warn', ("Command '%s' failed to RegisterHotkey! You cannot use params and register a hotkey together!"):format(commandName))
10+
end
11+
12+
if type(properties) ~= 'table' then
13+
return MSK.Logging('error', ('expected "table" for parameter "hotkey", received %s'):format(type(properties)))
14+
end
15+
16+
if properties.text and properties.key then
17+
RegisteredHotkeys[commandName] = properties
18+
RegisterKeyMapping(commandName, properties.text, properties.type or 'keyboard', properties.key)
19+
else
20+
MSK.Logging('error', ('expected "text" and "key" for parameter "hotkey", received (text: %s, key: %s)'):format(properties.text, properties.key))
21+
end
22+
end
23+
24+
local parseArgs = function(source, args, raw, params)
25+
if not params then return args end
26+
27+
local DoesPlayerExist = function(playerId)
28+
return MSK.Trigger('msk_core:doesPlayerExist', playerId)
29+
end
30+
31+
local GetPlayerData = function(playerId)
32+
return MSK.Trigger('msk_core:getPlayerData', playerId)
33+
end
34+
35+
for i=1, #params do
36+
local arg, param = args[i], params[i]
37+
local value
38+
39+
if param.type == 'number' then
40+
value = tonumber(arg)
41+
elseif param.type == 'string' then
42+
value = not tonumber(arg) and arg
43+
elseif param.type == 'playerId' then
44+
value = arg == 'me' and source or tonumber(arg)
45+
46+
if not value or not DoesPlayerExist(value) then
47+
value = false
48+
end
49+
elseif param.type == 'player' then
50+
value = arg == 'me' and source or tonumber(arg)
51+
52+
if not value or not DoesPlayerExist(value) then
53+
value = false
54+
end
55+
56+
if value then
57+
value = GetPlayerData(value)
58+
end
59+
else
60+
value = arg
61+
end
62+
63+
if not value and (not param.optional or param.optional and arg) then
64+
MSK.Logging('error', ("Command '%s' received an invalid %s for argument %s (%s), received '%s'^0"):format(MSK.String.Split(raw, ' ')[1] or raw, param.type, i, param.name, arg))
65+
MSK.Notification('Command Error', ("Command '%s' received an invalid %s for argument %s (%s), received '%s'"):format(MSK.String.Split(raw, ' ')[1] or raw, param.type, i, param.name, arg), 'error')
66+
return
67+
end
68+
69+
args[param.name] = value
70+
args[i] = nil
71+
end
72+
73+
return args
74+
end
75+
76+
MSK.RegisterCommand = function(commandName, callback, restricted, properties)
77+
if type(commandName) == 'table' then
78+
for k, v in ipairs(commandName) do
79+
MSK.RegisterCommand(v, callback, restricted, properties)
80+
end
81+
return
82+
end
83+
84+
if RegisteredCommands[commandName] then
85+
MSK.Logging('info', ('Command ^3%s^0 is already registerd. Overriding Command...'):format(commandName))
86+
end
87+
88+
RegisteredCommands[commandName] = {commandName = commandName, callback = callback, properties = properties}
89+
local params, showSuggestion, hotkey
90+
91+
if properties then
92+
params = properties.params
93+
showSuggestion = properties.showSuggestion == nil or properties.showSuggestion
94+
hotkey = properties.hotkey
95+
96+
RegisteredCommands[commandName].showSuggestion = showSuggestion
97+
end
98+
99+
if params then
100+
for i = 1, #params do
101+
local param = params[i]
102+
103+
if param.type then
104+
param.help = param.help and ('%s (type: %s)'):format(param.help, param.type) or ('(type: %s)'):format(param.type)
105+
end
106+
end
107+
end
108+
109+
local checkPlayerAce = function()
110+
if not restricted then
111+
return true
112+
end
113+
114+
return MSK.IsAceAllowed(commandName)
115+
end
116+
117+
local commandHandler = function(source, args, raw)
118+
local aceAllowed = checkPlayerAce()
119+
if not aceAllowed then return end
120+
121+
args = parseArgs(source, args, raw, params)
122+
if not args then return end
123+
124+
local success, response = pcall(callback, source, args, raw)
125+
126+
if not success then
127+
MSK.Logging('error', ("Command '%s' failed to execute! (response: %s)"):format(MSK.String.Split(raw, ' ')[1] or raw, response))
128+
end
129+
end
130+
131+
RegisterCommand(commandName, commandHandler)
132+
133+
if properties and showSuggestion then
134+
properties.name = ('/%s'):format(commandName)
135+
properties.showSuggestion = nil
136+
properties.hotkey = nil
137+
138+
RegisteredCommands[commandName].properties = properties
139+
140+
TriggerEvent('chat:addSuggestion', properties.name, properties.help, properties.params)
141+
end
142+
143+
if hotkey then
144+
RegisterHotkey(commandName, hotkey)
145+
end
146+
147+
return RegisteredCommands[commandName]
148+
end
149+
exports('RegisterCommand', MSK.RegisterCommand)

client/functions/main.lua

+1-15
Original file line numberDiff line numberDiff line change
@@ -201,18 +201,4 @@ exports('GetClosestPlayer', MSK.GetClosestPlayer)
201201
MSK.GetClosestPlayers = function(coords, distance)
202202
return GetClosestEntities(true, coords, distance)
203203
end
204-
exports('GetClosestPlayers', MSK.GetClosestPlayers)
205-
206-
MSK.IsAceAllowed = function(command)
207-
return MSK.Trigger('msk_core:isAceAllowed', ('command.%s'):format(command))
208-
end
209-
exports('IsAceAllowed', MSK.IsAceAllowed)
210-
211-
MSK.RegisterCommand = function(command, cb, restricted)
212-
RegisterCommand(command, function(source, args, raw)
213-
if not restricted or MSK.IsAceAllowed(command) then
214-
cb(source, args, raw)
215-
end
216-
end)
217-
end
218-
exports('RegisterCommand', MSK.RegisterCommand)
204+
exports('GetClosestPlayers', MSK.GetClosestPlayers)

config.lua

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ Config = {}
33
Config.Debug = false
44
Config.VersionChecker = true
55
----------------------------------------------------------------
6-
-- Supported Frameworks: AUTO, ESX, QBCore
6+
-- Supported Frameworks: AUTO, ESX, QBCore, STANDALONE
77
-- AUTO will search for your framework
88
Config.Framework = 'AUTO'
99

1010
-- Supported Inventories: default, custom, ox_inventory, qs-inventory
11+
-- For ESX Default Inventory or Chezza Inventory, set to 'default'
1112
-- Set to 'custom' if you use another inventory
1213
-- You can add your own inventory in: server/inventories/custom.lua
1314
Config.Inventory = 'default'
@@ -43,9 +44,10 @@ end
4344
Config.progressColor = "#5eb131" -- Default Color for ProgressBar
4445
----------------------------------------------------------------
4546
Config.LoggingTypes = {
46-
['info'] = '[^4Info^0]',
4747
['debug'] = '[^3DEBUG^0]',
48-
['error'] = '[^1ERROR^0]',
48+
['info'] = '[^4Info^0]',
49+
['warn'] = '[^3Warning^0]^3',
50+
['error'] = '[^1ERROR^0]^1',
4951
}
5052
----------------------------------------------------------------
5153
-- If enabled it will display a 3D Text on the position the player disconnected

fxmanifest.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ games { 'gta5' }
44
author 'Musiker15 - MSK Scripts'
55
name 'msk_core'
66
description 'Functions for MSK Scripts'
7-
version '2.6.1'
7+
version '2.6.2'
88

99
lua54 'yes'
1010

server/functions/ace.lua

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ MSK.IsAceAllowed = function(playerId, command)
33
end
44
exports('IsAceAllowed', MSK.IsAceAllowed)
55

6+
MSK.IsPrincipalAceAllowed = function(restricted, ace)
7+
return IsPrincipalAceAllowed(restricted, ace)
8+
end
9+
exports('IsPrincipalAceAllowed', MSK.IsPrincipalAceAllowed)
10+
611
local allowAce = function(allow)
712
return allow == false and 'deny' or 'allow'
813
end

server/functions/callbacks.lua

+15-7
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ MSK.Register = function(eventName, cb)
88
Callbacks[eventName] = cb
99
end
1010
MSK.RegisterCallback = MSK.Register -- Support for old Scripts
11-
MSK.RegisterServerCallback = MSK.Register -- Support for old Scripts
1211
exports('Register', MSK.Register)
1312
exports('RegisterCallback', MSK.Register) -- Support for old Scripts
14-
exports('RegisterServerCallback', MSK.Register) -- Support for old Scripts
1513

1614
RegisterNetEvent('msk_core:server:triggerCallback', function(eventName, requestId, cb, ...)
1715
local playerId = source
@@ -67,10 +65,8 @@ MSK.Trigger = function(eventName, playerId, ...)
6765
return table.unpack(result)
6866
end
6967
MSK.TriggerCallback = MSK.Trigger -- Support for old Scripts
70-
MSK.TriggerClientCallback = MSK.Trigger -- Support for old Scripts
7168
exports('Trigger', MSK.Trigger)
7269
exports('TriggerCallback', MSK.Trigger) -- Support for old Scripts
73-
exports('TriggerClientCallback', MSK.Trigger) -- Support for old Scripts
7470

7571
RegisterNetEvent("msk_core:server:callbackResponse", function(requestId, ...)
7672
if not CallbackHandler[requestId] then return end
@@ -87,11 +83,23 @@ end)
8783
----------------------------------------------------------------
8884
MSK.Register('msk_core:hasItem', MSK.HasItem)
8985
MSK.Register('msk_core:isAceAllowed', MSK.IsAceAllowed)
86+
MSK.Register('msk_core:isPrincipalAceAllowed', MSK.IsPrincipalAceAllowed)
87+
88+
-- For clientside MSK.RegisterCommand
89+
MSK.Register('msk_core:doesPlayerExist', function(source, targetId)
90+
return DoesPlayerExist(targetId)
91+
end)
92+
93+
-- For clientside MSK.RegisterCommand
94+
MSK.Register('msk_core:getPlayerData', function(source, targetId)
95+
return MSK.GetPlayer({source = targetId})
96+
end)
9097

9198
----------------------------------------------------------------
9299
-- Server Callbacks with Method [cb]
93100
----------------------------------------------------------------
94-
MSK.Register('msk_core:ThisIsATest', function(source, cb, param1, param2, param3)
95-
cb(param1, param2, param3)
96-
print(param1, param2, param3)
101+
MSK.Register('msk_core:ThisIsATest', function(source, cb, params, ...)
102+
cb(params, ...)
103+
104+
-- Do something here with params, ...
97105
end)

server/functions/commands.lua

+11-10
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ local parseArgs = function(source, args, raw, params)
5757

5858
if not value and (not param.optional or param.optional and arg) then
5959
if source == 0 then
60-
return MSK.Logging('error', ("^1Command '%s' received an invalid %s for argument %s (%s), received '%s'^0"):format(MSK.String.Split(raw, ' ')[1] or raw, param.type, i, param.name, arg))
60+
return MSK.Logging('error', ("Command '%s' received an invalid %s for argument %s (%s), received '%s'^0"):format(MSK.String.Split(raw, ' ')[1] or raw, param.type, i, param.name, arg))
6161
else
6262
return MSK.Notification(source, 'Command Error', ("Command '%s' received an invalid %s for argument %s (%s), received '%s'"):format(MSK.String.Split(raw, ' ')[1] or raw, param.type, i, param.name, arg), 'error')
6363
end
@@ -73,7 +73,7 @@ end
7373
MSK.RegisterCommand = function(commandName, callback, properties, ...)
7474
if ... ~= nil then
7575
-- Backwards compatibility
76-
warn(('Command "%s" is using deprecated syntax for MSK.RegisterCommand. Please update to to new syntax.'):format(commandName))
76+
MSK.Logging('warn', ('Command "%s" is using deprecated syntax for MSK.RegisterCommand. Please update to to new syntax.'):format(commandName))
7777
return MSK._RegisterCommand(commandName, callback, properties, ...)
7878
end
7979

@@ -89,14 +89,14 @@ MSK.RegisterCommand = function(commandName, callback, properties, ...)
8989
end
9090

9191
RegisteredCommands[commandName] = {commandName = commandName, callback = callback, properties = properties}
92-
local params, restricted, showSuggestion, allowConsole, framework
92+
local params, restricted, showSuggestion, allowConsole, returnPlayer
9393

9494
if properties then
9595
params = properties.params
9696
restricted = properties.restricted
9797
showSuggestion = properties.showSuggestion == nil or properties.showSuggestion
9898
allowConsole = properties.allowConsole == nil or properties.allowConsole
99-
framework = properties.framework
99+
returnPlayer = properties.returnPlayer
100100

101101
RegisteredCommands[commandName].showSuggestion = showSuggestion
102102
RegisteredCommands[commandName].allowConsole = allowConsole
@@ -114,24 +114,23 @@ MSK.RegisterCommand = function(commandName, callback, properties, ...)
114114

115115
local commandHandler = function(source, args, raw)
116116
if source == 0 and not allowConsole then
117-
return MSK.Logging('error', ('^1You cannot run Command ^3%s^1 in Console!^0'):format(commandName))
117+
return MSK.Logging('error', ('You cannot run Command ^3%s^1 in Console!^0'):format(commandName))
118118
end
119119

120120
args = parseArgs(source, args, raw, params)
121121
if not args then return end
122122

123123
local success, response
124124

125-
-- Backwards compatibility
126-
if framework and (MSK.Bridge.Framework.Type == 'ESX' or MSK.Bridge.Framework.Type == 'QBCore') then
125+
if returnPlayer and (MSK.Bridge.Framework.Type == 'ESX' or MSK.Bridge.Framework.Type == 'QBCore') then
127126
local Player = MSK.GetPlayer({source = source})
128127
success, response = pcall(callback, Player, args, raw)
129128
else
130129
success, response = pcall(callback, source, args, raw)
131130
end
132131

133132
if not success then
134-
MSK.Logging('error', ("^1Command '%s' failed to execute! (response: %s)"):format(MSK.String.Split(raw, ' ')[1] or raw, response))
133+
MSK.Logging('error', ("Command '%s' failed to execute! (response: %s)"):format(MSK.String.Split(raw, ' ')[1] or raw, response))
135134
end
136135
end
137136

@@ -158,12 +157,14 @@ MSK.RegisterCommand = function(commandName, callback, properties, ...)
158157
properties.restricted = nil
159158
properties.showSuggestion = nil
160159
properties.allowConsole = nil
161-
properties.framework = nil -- Backwards compatibility
160+
properties.returnPlayer = nil
162161

163162
RegisteredCommands[commandName].properties = properties
164163

165164
TriggerClientEvent('chat:addSuggestion', -1, properties.name, properties.help, properties.params)
166165
end
166+
167+
return RegisteredCommands[commandName]
167168
end
168169
exports('RegisterCommand', MSK.RegisterCommand)
169170

@@ -180,7 +181,7 @@ MSK._RegisterCommand = function(commandName, group, cb, console, framework, sugg
180181
restricted = group,
181182
showSuggestion = true,
182183
allowConsole = console,
183-
framework = framework
184+
returnPlayer = framework
184185
}
185186
end
186187

0 commit comments

Comments
 (0)