Files
ND_Core/server/main.lua

165 lines
5.6 KiB
Lua
Raw Normal View History

2023-07-09 21:19:35 +02:00
NDCore = {}
NDCore.players = {}
2023-07-11 01:05:13 +02:00
PlayersInfo = {}
local resourceName = GetCurrentResourceName()
2023-07-11 01:05:13 +02:00
local tempPlayersInfo = {}
2023-07-12 04:51:50 +02:00
Config = {
serverName = GetConvar("core:serverName", "Unconfigured ND-Core Server"),
discordInvite = GetConvar("core:discordInvite", "https://discord.gg/Z9Mxu72zZ6"),
discordMemeberRequired = GetConvarInt("core:discordMemeberRequired", 1) == 1,
2023-07-12 04:51:50 +02:00
discordAppId = GetConvar("core:discordAppId", "858146067018416128"),
discordAsset = GetConvar("core:discordAsset", "andyyy"),
discordAssetSmall = GetConvar("core:discordAssetSmall", "andyyy"),
discordActionText = GetConvar("core:discordActionText", "DISCORD"),
discordActionLink = GetConvar("discordActionLink", "https://discord.gg/Z9Mxu72zZ6"),
discordActionText2 = GetConvar("core:discordActionText2", "STORE"),
discordActionLink2 = GetConvar("core:discordActionLink2", "https://andyyy.tebex.io/category/fivem-scripts"),
characterIdentifier = GetConvar("core:characterIdentifier", "license"),
2023-12-03 08:38:35 -07:00
discordGuildId = GetConvar("core:discordGuildId", "false"),
discordBotToken = GetConvar("core:discordBotToken", "false"),
2023-07-12 04:51:50 +02:00
randomUnlockedVehicleChance = GetConvarInt("core:randomUnlockedVehicleChance", 30),
disableVehicleAirControl = GetConvarInt("core:disableVehicleAirControl", 1) == 1,
useInventoryForKeys = GetConvarInt("core:useInventoryForKeys", 1) == 1,
2023-08-28 03:04:25 +02:00
groups = json.decode(GetConvar("core:groups", "[]")),
admins = json.decode(GetConvar("core:admins", "[]")),
2023-10-21 21:44:17 +02:00
adminDiscordRoles = json.decode(GetConvar("core:adminDiscordRoles", "[]")),
groupRoles = json.decode(GetConvar("core:groupRoles", "[]")),
2023-11-11 03:02:08 +01:00
multiCharacter = false,
compatibility = json.decode(GetConvar("core:compatibility", "[]"))
2023-07-12 04:51:50 +02:00
}
2022-11-30 03:32:52 +01:00
SetConvarServerInfo("Discord", Config.discordInvite)
2023-08-28 03:04:25 +02:00
SetConvarServerInfo("NDCore", GetResourceMetadata(resourceName, "version", 0) or "invalid")
2023-07-11 01:05:13 +02:00
SetConvarReplicated("inventory:framework", "nd")
2023-07-10 02:31:42 +02:00
2023-07-11 01:05:13 +02:00
local function getIdentifierList(src)
local list = {}
for i=0, GetNumPlayerIdentifiers(src) do
local identifier = GetPlayerIdentifier(src, i)
if identifier then
local colon = identifier:find(":")
local identifierType = identifier:sub(1, colon-1)
list[identifierType] = identifier
2023-07-11 01:05:13 +02:00
end
end
return list
end
AddEventHandler("playerJoining", function(oldId)
local src = source
2023-12-16 19:46:35 +01:00
local oldTempId = tonumber(oldId)
PlayersInfo[src] = tempPlayersInfo[oldTempId]
tempPlayersInfo[oldTempId] = nil
if Config.multiCharacter then return end
Wait(3000)
local characters = NDCore.fetchAllCharacters(src)
local id = next(characters)
if id then
return NDCore.setActiveCharacter(src, id)
end
local player = NDCore.newCharacter(src, {
firstname = GetPlayerName(src),
lastname = "",
dob = "",
gender = ""
})
NDCore.setActiveCharacter(src, player.id)
2023-07-11 01:05:13 +02:00
end)
local function checkDiscordIdentifier(identifiers)
2023-12-03 08:41:46 -07:00
if Config.discordBotToken == "false" or Config.discordGuildId == "false" then return end
local discordIdentifier = identifiers["discord"]
if not discordIdentifier then return end
return NDCore.getDiscordInfo(discordIdentifier:gsub("discord:", ""))
end
AddEventHandler("onResourceStart", function(name)
if name ~= resourceName then return end
for _, playerId in ipairs(GetPlayers()) do
local src = tonumber(playerId)
local identifiers = getIdentifierList(src)
PlayersInfo[src] = {
identifiers = identifiers,
discord = checkDiscordIdentifier(identifiers) or {}
}
Wait(65)
end
end)
2023-07-11 01:05:13 +02:00
AddEventHandler("playerConnecting", function(name, setKickReason, deferrals)
local tempSrc = source
local identifiers = getIdentifierList(tempSrc)
local mainIdentifier = identifiers[Config.characterIdentifier]
2023-12-20 13:02:13 +01:00
local discordInfo = nil
2023-07-11 01:05:13 +02:00
deferrals.defer()
Wait(0)
2023-12-20 13:02:13 +01:00
if mainIdentifier and Config.discordBotToken ~= "false" and Config.discordGuildId ~= "false" then
discordInfo = checkDiscordIdentifier(identifiers)
if not discordInfo and Config.discordMemeberRequired then
2023-07-11 01:05:13 +02:00
deferrals.done(("Your discord was not found, join our discord here: %s."):format(Config.discordInvite))
Wait(0)
end
end
deferrals.update("Connecting...")
Wait(0)
if mainIdentifier then
tempPlayersInfo[tempSrc] = {
identifiers = identifiers,
discord = discordInfo
}
deferrals.done()
else
deferrals.done(("Your %s was not found."):format(Config.characterIdentifier))
Wait(0)
end
end)
AddEventHandler("playerDropped", function()
local src = source
local char = NDCore.players[src]
if char then char.unload() end
PlayersInfo[src] = nil
end)
2023-08-23 03:29:36 +02:00
AddEventHandler("onResourceStop", function(name)
if name ~= resourceName then return end
for _, player in pairs(NDCore.players) do
player.unload()
2023-08-23 03:29:36 +02:00
Wait(10)
end
end)
2023-07-16 12:31:02 +02:00
SetTimeout(500, function()
NDCore.loadSQL({
"database/characters.sql",
"database/vehicles.sql"
}, resourceName)
end)
2023-09-24 11:19:36 +02:00
RegisterNetEvent("ND:playerEliminated", function(info)
local src = source
local player = NDCore.getPlayer(src)
if not player then return end
player.setMetadata({
dead = true,
deathInfo = info
})
end)
2024-03-16 19:12:34 +01:00
RegisterNetEvent("ND:updateClothing", function(clothing)
local src = source
local player = NDCore.getPlayer(src)
if not player or not clothing or type(clothing) ~= "table" then return end
player.setMetadata("clothing", clothing)
end)