From 52250d0d495cba7cc569786a1d874faf85e40d7e Mon Sep 17 00:00:00 2001 From: Demetrio <15928886+d3rp-jsx@users.noreply.github.com> Date: Tue, 10 Aug 2021 19:31:30 +0200 Subject: [PATCH] Dynamic slots (#36) * Removed slots configuration * feature: dynamic slots * fixes + improvements * edited core functionality * added commands for slots management * added commands english locale Co-authored-by: d3rp-jsx <15928886+derp-jsx@users.noreply.github.com> --- client/main.lua | 7 +++--- config.lua | 2 +- esx_multicharacter.sql | 7 ++++++ locales/en.lua | 7 ++++++ server/main.lua | 48 ++++++++++++++++++++++++++++++++++++++++-- 5 files changed, 65 insertions(+), 6 deletions(-) create mode 100644 esx_multicharacter.sql diff --git a/client/main.lua b/client/main.lua index 28e33a4d..15f6b391 100644 --- a/client/main.lua +++ b/client/main.lua @@ -142,9 +142,10 @@ if ESX.GetConfig().Multichar then end RegisterNetEvent('esx_multicharacter:SetupUI') - AddEventHandler('esx_multicharacter:SetupUI', function(data) + AddEventHandler('esx_multicharacter:SetupUI', function(data, slots) DoScreenFadeOut(0) Characters = data + local slots = slots local elements = {} local Character = next(Characters) exports.spawnmanager:forceRespawn() @@ -179,7 +180,7 @@ if ESX.GetConfig().Multichar then local label = v.firstname..' '..v.lastname elements[#elements+1] = {label = label, value = v.id} end - if #elements < Config.Slots then + if #elements < slots then elements[#elements+1] = {label = _('create_char'), value = (#elements+1), new = true} end ESX.UI.Menu.Open('default', GetCurrentResourceName(), 'selectchar', { @@ -228,7 +229,7 @@ if ESX.GetConfig().Multichar then else ESX.UI.Menu.CloseAll() local GetSlot = function() - for i=1, Config.Slots do + for i=1, slots do if not Characters[i] then return i end diff --git a/config.lua b/config.lua index f1874abf..bda8e554 100644 --- a/config.lua +++ b/config.lua @@ -1,7 +1,7 @@ Config = {} Config.Locale = 'en' -Config.Slots = 4 +Config.Slots = 1 Config.Spawn = vector4(-113.7, 565.3, 195.2, 0) -- Sets the location for character selection -- To set the spawn location for new characters, modify the default value in the `users` SQL table diff --git a/esx_multicharacter.sql b/esx_multicharacter.sql new file mode 100644 index 00000000..9495bb5a --- /dev/null +++ b/esx_multicharacter.sql @@ -0,0 +1,7 @@ +CREATE TABLE `multicharacter_slots` ( + `identifier` VARCHAR(60) NOT NULL DEFAULT '' COLLATE 'utf8mb4_unicode_ci', + `slots` INT(11) NOT NULL DEFAULT '1' +) +COLLATE='utf8mb4_unicode_ci' +ENGINE=InnoDB +; diff --git a/locales/en.lua b/locales/en.lua index f814d78a..e61481b0 100644 --- a/locales/en.lua +++ b/locales/en.lua @@ -8,4 +8,11 @@ Locales['en'] = { ['char_delete'] = "Delete this character", ['cancel'] = "Cancel", ['confirm'] = "Confirm", + ['command_setslots'] = "Set multicharacter slots number of a player", + ['command_remslots'] = "Remove multicharacter slots number of a player", + ['command_identifier'] = "Player identifier", + ['command_slots'] = "# of slots", + ['slotsadd'] = "You added %s slots to %s", + ['slotsedit'] = "You set %s slots to %s", + ['slotsrem'] = "You removed slots to %s", } diff --git a/server/main.lua b/server/main.lua index ea74dafa..d8abff13 100644 --- a/server/main.lua +++ b/server/main.lua @@ -12,9 +12,15 @@ elseif ESX.GetConfig().Multichar == true then local SetupCharacters = function(playerId) while Fetch == -1 do Citizen.Wait(500) end local identifier = Config.Prefix..'%:'..ESX.GetIdentifier(playerId) + local slots = MySQL.Sync.fetchScalar("SELECT `slots` FROM `multicharacter_slots` WHERE identifier = ?", { + ESX.GetIdentifier(playerId) + }) + if not slots then + slots = Config.Slots + end MySQL.Async.fetchAll(Fetch, { identifier, - Config.Slots + slots }, function(result) local characters = {} for i=1, #result, 1 do @@ -38,7 +44,7 @@ elseif ESX.GetConfig().Multichar == true then } if result[i].sex == 'm' then characters[id].sex = _('male') else characters[id].sex = _('female') end end - TriggerClientEvent('esx_multicharacter:SetupUI', playerId, characters) + TriggerClientEvent('esx_multicharacter:SetupUI', playerId, characters, slots) end) end @@ -123,6 +129,44 @@ elseif ESX.GetConfig().Multichar == true then TriggerEvent('esx:playerLogout', src) end) + ESX.RegisterCommand('setslots', 'admin', function(xPlayer, args, showError) + local slots = MySQL.Sync.fetchScalar('SELECT `slots` FROM `multicharacter_slots` WHERE identifier = ?', { + args.identifier + }) + + if slots == nil then + MySQL.Async.execute('INSERT INTO `multicharacter_slots` (`identifier`, `slots`) VALUES (?, ?)', { + args.identifier, + args.slots + }) + xPlayer.triggerEvent('esx:showNotification', _U('slotsadd', args.slots, args.identifier)) + else + MySQL.Async.execute('UPDATE `multicharacter_slots` SET `slots` = ? WHERE `identifier` = ?', { + args.slots, + args.identifier + }) + xPlayer.triggerEvent('esx:showNotification', _U('slotsedit', args.slots, args.identifier)) + end + end, true, {help = _U('command_setslots'), validate = true, arguments = { + {name = 'identifier', help = _U('command_identifier'), type = 'string'}, + {name = 'slots', help = _U('command_slots'), type = 'number'} + }}) + + ESX.RegisterCommand('remslots', 'admin', function(xPlayer, args, showError) + local slots = MySQL.Sync.fetchScalar('SELECT `slots` FROM `multicharacter_slots` WHERE identifier = ?', { + args.identifier + }) + + if slots ~= nil then + MySQL.Async.execute('DELETE FROM `multicharacter_slots` WHERE `identifier` = ?', { + args.identifier + }) + xPlayer.triggerEvent('esx:showNotification', _U('slotsrem', args.identifier)) + end + end, true, {help = _U('command_remslots'), validate = true, arguments = { + {name = 'identifier', help = _U('command_identifier'), type = 'string'} + }}) + RegisterCommand('forcelog', function(source, args, rawCommand) TriggerEvent('esx:playerLogout', source) end, true)