From b8ad39269234b13ef9ea0372a974c8e3d4227e58 Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Thu, 30 Dec 2021 02:25:00 +1100 Subject: [PATCH] feat(commands): add a RegisterCommand wrapper --- fxmanifest.lua | 2 +- imports/commands/server.lua | 48 +++++++++++++++++++++++++++++++++++++ resource/main.lua | 3 ++- version.lua | 16 ++----------- 4 files changed, 53 insertions(+), 16 deletions(-) create mode 100644 imports/commands/server.lua diff --git a/fxmanifest.lua b/fxmanifest.lua index ec44131..e85407c 100644 --- a/fxmanifest.lua +++ b/fxmanifest.lua @@ -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.' diff --git a/imports/commands/server.lua b/imports/commands/server.lua new file mode 100644 index 0000000..ca0ffe5 --- /dev/null +++ b/imports/commands/server.lua @@ -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 +]] \ No newline at end of file diff --git a/resource/main.lua b/resource/main.lua index ea0651d..5342930 100644 --- a/resource/main.lua +++ b/resource/main.lua @@ -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 }) \ No newline at end of file diff --git a/version.lua b/version.lua index 4ea227a..e34142c 100644 --- a/version.lua +++ b/version.lua @@ -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')