mirror of
https://github.com/esx-framework/esx_core.git
synced 2026-09-01 10:48:52 +00:00
feat(legacy/multichar): Added support for esx_multicharacter; applied minor changes to spawning
This commit is contained in:
+119
-30
@@ -3,47 +3,41 @@ Citizen.CreateThread(function()
|
||||
SetGameType('ESX Roleplay')
|
||||
end)
|
||||
|
||||
local awaitingRegistration = {}
|
||||
RegisterNetEvent('esx:onPlayerJoined')
|
||||
AddEventHandler('esx:onPlayerJoined', function()
|
||||
if not ESX.Players[source] then
|
||||
onPlayerJoined(source)
|
||||
end
|
||||
end)
|
||||
if Config.Multichar then
|
||||
AddEventHandler('esx_identity:completedRegistration', function(playerId, data)
|
||||
awaitingRegistration[playerId] = data
|
||||
end)
|
||||
AddEventHandler('esx:onPlayerJoined', function(src, char, isNew)
|
||||
if not ESX.Players[src] then
|
||||
onPlayerJoined(src, char, isNew)
|
||||
end
|
||||
end)
|
||||
else
|
||||
AddEventHandler('esx:onPlayerJoined', function()
|
||||
if not ESX.Players[source] then
|
||||
onPlayerJoined(source)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
function onPlayerJoined(playerId)
|
||||
function onPlayerJoined(playerId, char, isNew)
|
||||
local identifier = ESX.GetIdentifier(playerId)
|
||||
if char then identifier = 'char'..char..':'..identifier end
|
||||
|
||||
if identifier then
|
||||
if ESX.GetPlayerFromIdentifier(identifier) then
|
||||
DropPlayer(playerId, ('there was an error loading your character!\nError code: identifier-active-ingame\n\nThis error is caused by a player on this server who has the same identifier as you have. Make sure you are not playing on the same Rockstar account.\n\nYour Rockstar identifier: %s'):format(identifier))
|
||||
elseif Config.Multichar and isNew then
|
||||
createESXPlayer(identifier, playerId)
|
||||
else
|
||||
MySQL.Async.fetchScalar('SELECT 1 FROM users WHERE identifier = @identifier', {
|
||||
['@identifier'] = identifier
|
||||
}, function(result)
|
||||
if result then
|
||||
loadESXPlayer(identifier, playerId, false)
|
||||
else
|
||||
local accounts = {}
|
||||
|
||||
for account,money in pairs(Config.StartingAccountMoney) do
|
||||
accounts[account] = money
|
||||
end
|
||||
|
||||
if IsPlayerAceAllowed(playerId, "command") then
|
||||
print(('^2[INFO] ^0 Player ^5%s ^0Has been granted admin permissions via ^5Ace Perms.^7'):format(playerId))
|
||||
defaultGroup = "admin"
|
||||
else
|
||||
defaultGroup = "user"
|
||||
end
|
||||
|
||||
MySQL.Async.execute('INSERT INTO users (`accounts`, `identifier`, `group`) VALUES (@accounts, @identifier, @group)', {
|
||||
['@accounts'] = json.encode(accounts),
|
||||
['@identifier'] = identifier,
|
||||
['@group'] = defaultGroup
|
||||
}, function(rowsChanged)
|
||||
loadESXPlayer(identifier, playerId, true)
|
||||
end)
|
||||
end
|
||||
else createESXPlayer(identifier, playerId) end
|
||||
end)
|
||||
end
|
||||
else
|
||||
@@ -51,6 +45,51 @@ function onPlayerJoined(playerId)
|
||||
end
|
||||
end
|
||||
|
||||
function createESXPlayer(identifier, playerId)
|
||||
local accounts = {}
|
||||
|
||||
for account,money in pairs(Config.StartingAccountMoney) do
|
||||
accounts[account] = money
|
||||
end
|
||||
|
||||
if IsPlayerAceAllowed(playerId, "command") then
|
||||
print(('^2[INFO] ^0 Player ^5%s ^0Has been granted admin permissions via ^5Ace Perms.^7'):format(playerId))
|
||||
defaultGroup = "admin"
|
||||
else
|
||||
defaultGroup = "user"
|
||||
end
|
||||
|
||||
if not Config.Multichar then
|
||||
MySQL.Async.execute('INSERT INTO users (`accounts`, `identifier`, `group`) VALUES (@accounts, @identifier, @group)', {
|
||||
['@accounts'] = json.encode(accounts),
|
||||
['@identifier'] = identifier,
|
||||
['@group'] = defaultGroup
|
||||
}, function(rowsChanged)
|
||||
loadESXPlayer(identifier, playerId, true)
|
||||
end)
|
||||
else
|
||||
local data
|
||||
awaitingRegistration[playerId] = true
|
||||
while true do
|
||||
Citizen.Wait(250)
|
||||
if awaitingRegistration[playerId] ~= true then data = awaitingRegistration[playerId] break end
|
||||
end
|
||||
awaitingRegistration[playerId] = nil
|
||||
MySQL.Async.execute('INSERT INTO users (`accounts`, `identifier`, `group`, `firstname`, `lastname`, `dateofbirth`, `sex`, `height`) VALUES (@accounts, @identifier, @group, @firstname, @lastname, @dateofbirth, @sex, @height)', {
|
||||
['@accounts'] = json.encode(accounts),
|
||||
['@identifier'] = identifier,
|
||||
['@group'] = defaultGroup,
|
||||
['@firstname'] = data.firstname,
|
||||
['@lastname'] = data.lastname,
|
||||
['@dateofbirth'] = data.dateofbirth,
|
||||
['@sex'] = data.sex,
|
||||
['@height'] = data.height,
|
||||
}, function(rowsChanged)
|
||||
loadESXPlayer(identifier, playerId, true)
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
AddEventHandler('playerConnecting', function(name, setCallback, deferrals)
|
||||
deferrals.defer()
|
||||
local playerId = source
|
||||
@@ -81,7 +120,13 @@ function loadESXPlayer(identifier, playerId, isNew)
|
||||
}
|
||||
|
||||
table.insert(tasks, function(cb)
|
||||
MySQL.Async.fetchAll('SELECT accounts, job, job_grade, `group`, loadout, position, inventory FROM users WHERE identifier = @identifier', {
|
||||
local query
|
||||
if Config.Multichar then
|
||||
query = 'SELECT `accounts`, `job`, `job_grade`, `group`, `loadout`, `position`, `inventory`, `skin`, `firstname`, `lastname`, `dateofbirth`, `sex`, `height` FROM `users` WHERE identifier = @identifier'
|
||||
else
|
||||
query = 'SELECT `accounts`, `job`, `job_grade`, `group`, `loadout`, `position`, `inventory`, `skin` FROM `users` WHERE identifier = @identifier'
|
||||
end
|
||||
MySQL.Async.fetchAll(query, {
|
||||
['@identifier'] = identifier
|
||||
}, function(result)
|
||||
local job, grade, jobObject, gradeObject = result[1].job, tostring(result[1].job_grade)
|
||||
@@ -203,6 +248,25 @@ function loadESXPlayer(identifier, playerId, isNew)
|
||||
userData.coords = {x = -269.4, y = -955.3, z = 31.2, heading = 205.8}
|
||||
end
|
||||
|
||||
-- Skin
|
||||
if result[1].skin and result[1].skin ~= '' then
|
||||
userData.skin = json.decode(result[1].skin)
|
||||
elseif userData.sex then
|
||||
if userData.sex == 'f' then userData.skin = {sex=1} else userData.skin = {sex=0} end
|
||||
end
|
||||
|
||||
-- Identity
|
||||
if Config.Multichar then
|
||||
if result[1].firstname and result[1].firstname ~= '' then
|
||||
userData.firstname = result[1].firstname
|
||||
userData.lastname = result[1].lastname
|
||||
userData.playerName = userData.firstname..' '..userData.lastname
|
||||
if result[1].dateofbirth then userData.dateofbirth = result[1].dateofbirth end
|
||||
if result[1].sex then userData.sex = result[1].sex end
|
||||
if result[1].height then userData.height = result[1].height end
|
||||
end
|
||||
end
|
||||
|
||||
cb()
|
||||
end)
|
||||
end)
|
||||
@@ -210,6 +274,15 @@ function loadESXPlayer(identifier, playerId, isNew)
|
||||
Async.parallel(tasks, function(results)
|
||||
local xPlayer = CreateExtendedPlayer(playerId, identifier, userData.group, userData.accounts, userData.inventory, userData.weight, userData.job, userData.loadout, userData.playerName, userData.coords)
|
||||
ESX.Players[playerId] = xPlayer
|
||||
|
||||
if Config.Multichar and userData.firstname then
|
||||
xPlayer.set('firstName', userData.firstname)
|
||||
xPlayer.set('lastName', userData.lastname)
|
||||
if userData.dateofbirth then xPlayer.set('dateofbirth', userData.dateofbirth) end
|
||||
if userData.sex then xPlayer.set('sex', userData.sex) end
|
||||
if userData.height then xPlayer.set('height', userData.height) end
|
||||
end
|
||||
|
||||
TriggerEvent('esx:playerLoaded', playerId, xPlayer, isNew)
|
||||
|
||||
xPlayer.triggerEvent('esx:playerLoaded', {
|
||||
@@ -220,7 +293,8 @@ function loadESXPlayer(identifier, playerId, isNew)
|
||||
job = xPlayer.getJob(),
|
||||
loadout = xPlayer.getLoadout(),
|
||||
maxWeight = xPlayer.getMaxWeight(),
|
||||
money = xPlayer.getMoney()
|
||||
money = xPlayer.getMoney(),
|
||||
skin = userData.skin
|
||||
}, isNew)
|
||||
|
||||
xPlayer.triggerEvent('esx:createMissingPickups', ESX.Pickups)
|
||||
@@ -250,6 +324,21 @@ AddEventHandler('playerDropped', function(reason)
|
||||
end
|
||||
end)
|
||||
|
||||
if Config.Multichar then
|
||||
AddEventHandler('esx:playerLogout', function(playerId)
|
||||
local xPlayer = ESX.GetPlayerFromId(playerId)
|
||||
awaitingRegistration[playerId] = nil
|
||||
if xPlayer then
|
||||
TriggerEvent('esx:playerDropped', playerId, reason)
|
||||
|
||||
ESX.SavePlayer(xPlayer, function()
|
||||
ESX.Players[playerId] = nil
|
||||
end)
|
||||
end
|
||||
TriggerClientEvent("esx:onPlayerLogout", playerId)
|
||||
end)
|
||||
end
|
||||
|
||||
RegisterNetEvent('esx:updateCoords')
|
||||
AddEventHandler('esx:updateCoords', function(coords)
|
||||
local xPlayer = ESX.GetPlayerFromId(source)
|
||||
|
||||
Reference in New Issue
Block a user