Skip to content

Commit b73392d

Browse files
committed
Update v1.3
* Updated MSK.logging function * Added MSK.registerCommand function
1 parent bd767b0 commit b73392d

File tree

4 files changed

+141
-32
lines changed

4 files changed

+141
-32
lines changed

client.lua

+8-15
Original file line numberDiff line numberDiff line change
@@ -96,26 +96,19 @@ MSK.DelTimeout = function(i)
9696
MSK.Timeouts[i] = nil
9797
end
9898

99-
MSK.logging = function(script, code, msg, msg2, msg3)
99+
MSK.logging = function(script, code, ...)
100100
if code == 'error' then
101-
if msg3 then
102-
print(script, '[^1ERROR^0]', msg, msg2, msg3)
103-
elseif msg2 and not msg3 then
104-
print(script, '[^1ERROR^0]', msg, msg2)
105-
else
106-
print(script, '[^1ERROR^0]', msg)
107-
end
101+
print(script, '[^1ERROR^0]', ...)
108102
elseif code == 'debug' then
109-
if msg3 then
110-
print(script, '[^3DEBUG^0]', msg, msg2, msg3)
111-
elseif msg2 and not msg3 then
112-
print(script, '[^3DEBUG^0]', msg, msg2)
113-
else
114-
print(script, '[^3DEBUG^0]', msg)
115-
end
103+
print(script, '[^3DEBUG^0]', ...)
116104
end
117105
end
118106

107+
logging = function(code, ...)
108+
local script = "[^2"..GetCurrentResourceName().."^0]"
109+
MSK.logging(script, code, ...)
110+
end
111+
119112
GenerateRequestKey = function(tbl)
120113
local id = string.upper(MSK.GetRandomLetter(3)) .. math.random(000, 999) .. string.upper(MSK.GetRandomLetter(2)) .. math.random(00, 99)
121114

config.lua

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
Config = {}
22
----------------------------------------------------------------
3-
Config.VersionChecker = true
3+
Config.VersionChecker = true
4+
----------------------------------------------------------------
5+
-- This is only for MSK.RegisterCommand // View the Wiki to get more Information about this
6+
Config.Framework = 'standalone' -- Set to 'standalone', 'esx' or 'qbcore'
7+
----------------------------------------------------------------

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 'Core functions for MSK Scripts'
7-
version '1.2'
7+
version '1.3'
88

99
lua54 'yes'
1010

server.lua

+127-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
MSK = {}
2+
RegisteredCommands = {}
3+
4+
if Config.Framework:match('esx') then
5+
ESX = exports["es_extended"]:getSharedObject()
6+
elseif Config.Framework:match('qbcore') then
7+
QBCore = exports['qb-core']:GetCoreObject()
8+
end
29

310
local Callbacks = {}
411
local Letters = {}
@@ -15,6 +22,92 @@ MSK.GetRandomLetter = function(length)
1522
end
1623
end
1724

25+
MSK.RegisterCommand = function(name, group, cb, console, suggestion)
26+
if RegisteredCommands[name] then
27+
logging('debug', ('Command %s is already registerd. Overriding Command...'):format(name))
28+
end
29+
30+
if type(name) == 'table' then
31+
for k, v in ipairs(name) do
32+
MSK.RegisterCommand(v, group, cb, console, suggestion)
33+
end
34+
return
35+
end
36+
37+
local added = addChatSuggestions(name, suggestion)
38+
while not added do Wait(1) end
39+
40+
RegisteredCommands[name] = {group = group, cb = cb, console = console, suggestion = suggestion}
41+
42+
RegisterCommand(name, function(source, args, rawCommand)
43+
local source = source
44+
local Command, error = RegisteredCommands[name], nil
45+
46+
if not Command.console and source == 0 then
47+
logging('error', 'You can not run this Command in Server Console!')
48+
else
49+
if Command.suggestion and Command.suggestion.arguments then
50+
local newArgs = {}
51+
52+
for k, v in ipairs(Command.suggestion.arguments) do
53+
if v.action == 'number' then
54+
if args[k] then
55+
if tonumber(args[k]) then
56+
newArgs[v.name] = args[k]
57+
else
58+
error = ('Argument %s is not a number!'):format(v.name)
59+
end
60+
end
61+
elseif v.action == 'playerId' then
62+
if args[k] then
63+
if tonumber(args[k]) > 0 and doesPlayerIdExist(args[k]) then
64+
newArgs[v.name] = args[k]
65+
else
66+
error = ('PlayerId %s does not exist!'):format(args[k])
67+
end
68+
end
69+
else
70+
newArgs[v.name] = args[k]
71+
end
72+
73+
if not error and not newArgs[v.name] and v.val then
74+
error = ('Argument Mismatch with Argument %s'):format(v.name)
75+
end
76+
if error then break end
77+
end
78+
79+
args = newArgs
80+
end
81+
82+
if error then
83+
if source == 0 then
84+
logging('error', error)
85+
else
86+
MSK.Notification(source, error)
87+
end
88+
else
89+
if Config.Framework:match('standalone') then
90+
cb(source, args, rawCommand)
91+
elseif Config.Framework:match('esx') then
92+
local xPlayer = ESX.GetPlayerFromId(source)
93+
cb(xPlayer, args, rawCommand)
94+
elseif Config.Framework:match('qbcore') then
95+
local Player = QBCore.Functions.GetPlayer(source)
96+
cb(Player, args, rawCommand)
97+
end
98+
end
99+
end
100+
end, true)
101+
102+
if type(group) == 'table' then
103+
for k, v in ipairs(group) do
104+
ExecuteCommand(('add_ace group.%s command.%s allow'):format(v, name))
105+
end
106+
else
107+
ExecuteCommand(('add_ace group.%s command.%s allow'):format(group, name))
108+
end
109+
end
110+
18111
MSK.Notification = function(src, text)
19112
TriggerClientEvent('msk_core:notification', src, text)
20113
end
@@ -73,23 +166,11 @@ MSK.RegisterCallback = function(name, cb)
73166
Callbacks[name] = cb
74167
end
75168

76-
MSK.logging = function(script, code, msg, msg2, msg3)
169+
MSK.logging = function(script, code, ...)
77170
if code == 'error' then
78-
if msg3 then
79-
print(script, '[^1ERROR^0]', msg, msg2, msg3)
80-
elseif msg2 and not msg3 then
81-
print(script, '[^1ERROR^0]', msg, msg2)
82-
else
83-
print(script, '[^1ERROR^0]', msg)
84-
end
171+
print(script, '[^1ERROR^0]', ...)
85172
elseif code == 'debug' then
86-
if msg3 then
87-
print(script, '[^3DEBUG^0]', msg, msg2, msg3)
88-
elseif msg2 and not msg3 then
89-
print(script, '[^3DEBUG^0]', msg, msg2)
90-
else
91-
print(script, '[^3DEBUG^0]', msg)
92-
end
173+
print(script, '[^3DEBUG^0]', ...)
93174
end
94175
end
95176

@@ -103,6 +184,37 @@ AddEventHandler('msk_core:triggerCallback', function(name, requestId, ...)
103184
end
104185
end)
105186

187+
doesPlayerIdExist = function(playerId)
188+
for k, id in pairs(GetPlayers()) do
189+
if id == playerId then
190+
return true
191+
end
192+
end
193+
return false
194+
end
195+
196+
addChatSuggestions = function(name, suggestion)
197+
if RegisteredCommands[name] then
198+
if RegisteredCommands[name].suggestion then
199+
TriggerClientEvent('chat:removeSuggestion', -1, '/' .. name)
200+
end
201+
end
202+
203+
if suggestion then
204+
if not suggestion.arguments then suggestion.arguments = {} end
205+
if not suggestion.help then suggestion.help = '' end
206+
207+
TriggerClientEvent('chat:addSuggestion', -1, '/' .. name, suggestion.help, suggestion.arguments)
208+
end
209+
210+
return true
211+
end
212+
213+
logging = function(code, ...)
214+
local script = "[^2"..GetCurrentResourceName().."^0]"
215+
MSK.logging(script, code, ...)
216+
end
217+
106218
GithubUpdater = function()
107219
GetCurrentVersion = function()
108220
return GetResourceMetadata( GetCurrentResourceName(), "version" )

0 commit comments

Comments
 (0)