feat(commands): add a RegisterCommand wrapper

This commit is contained in:
Linden
2021-12-30 02:25:00 +11:00
parent d0c5f722db
commit b8ad392692
4 changed files with 53 additions and 16 deletions

View File

@@ -23,7 +23,7 @@ game 'gta5'
--[[ Resource Information ]]--
name 'pe-lualib'
author 'Linden'
version '1.1.0'
version '1.2.0'
repository 'https://github.com/project-error/pe-lualib'
description 'A library of shared functions to utilise in other resources.'

View File

@@ -0,0 +1,48 @@
---@param group string
---@param name string
---@param callback function
---@param arguments string
local function AddCommand(group, name, callback, arguments)
if type(name) == 'table' then
for i=1, #name do
AddCommand(group, name[i], callback, arguments)
end
else
if not group then group = 'builtin.everyone' end
RegisterCommand(name, function(source, args)
if arguments then
for i=1, #arguments do
local arg, argType = string.strsplit(':', arguments[i])
local value = args[i]
if arg == 'target' and value == 'me' then value = source end
if argType then
if argType == 'number' then value = tonumber(value) end
assert(type(value) == argType or argType:find('?'), ('%s expected <%s> for argument %s (%s), received %s'):format(name, argType, i, arg, type(value)))
end
args[arg] = value
args[i] = nil
end
end
callback(tonumber(source), args)
end, group and true)
name = ('command.%s'):format(name)
if not IsPrincipalAceAllowed(group, name) then ExecuteCommand(('add_ace %s %s allow'):format(group, name)) end
end
end
return AddCommand
--[[ Example
AddCommand('admin', {'additem', 'giveitem'}, function(source, args)
args.item = Items(args.item)
if args.item and args.count > 0 then
Inventory.AddItem(args.target, args.item.name, args.count, args.metatype)
end
end, {'target:number', 'item:string', 'count:number', 'metatype:?string'})
-- /additem 1 burger 1
]]

View File

@@ -3,7 +3,8 @@
--- Alias for `exports['pe-lualib']`
lib = setmetatable({}, {
__newindex = function(_, name, fn)
__newindex = function(self, name, fn)
exports(name, fn)
rawset(self, name, fn)
end
})

View File

@@ -5,22 +5,10 @@ CreateThread(function()
local version = GetResourceMetadata(resource, 'version', 0)
PerformHttpRequest(('%s/master/fxmanifest.lua'):format(url:gsub('github.com', 'raw.githubusercontent.com')), function(error, response)
if error == 200 then
if error == 200 and response then
local latest = response:match('%d%.%d+%.%d+')
if version < latest then
local curMajor, curMinor = string.strsplit('.', version)
local newMajor, newMinor = string.strsplit('.', response:match('%d%.%d+%.%d+'))
local link = ('%s/archive/refs/tags/%s.zip'):format(url, latest, latest)
if tonumber(curMajor) < tonumber(newMajor) then
latest = 'A major update'
elseif tonumber(curMinor) < tonumber(newMinor) then
latest = 'An update'
else
latest = 'A patch'
end
print(('^3%s is available for oxmysql - please download the latest release (current version: %s)\n ^3- %s^0'):format(latest, version, link))
print(('^3An update is available for pe-lualib - please download the latest release (current version: %s)\n ^3- %s^0'):format(latest, version, ('%s/archive/refs/tags/%s.zip'):format(url, latest, latest)))
end
end
end, 'GET')