From e70fd5348f389aa487796c6361ee32a465929c16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20F=C3=BClling?= Date: Sat, 9 May 2020 04:21:17 +0200 Subject: [PATCH] initial commit --- .gitignore | 3 ++ README.md | 24 ++++++++++ client/main.lua | 119 ++++++++++++++++++++++++++++++++++++++++++++++++ config.lua | 10 ++++ fxmanifest.lua | 24 ++++++++++ locales/en.lua | 5 ++ server/main.lua | 50 ++++++++++++++++++++ 7 files changed, 235 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 client/main.lua create mode 100644 config.lua create mode 100644 fxmanifest.lua create mode 100644 locales/en.lua create mode 100644 server/main.lua diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9d6e5df --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.DS_Store +.idea +*.iml diff --git a/README.md b/README.md new file mode 100644 index 0000000..82283b8 --- /dev/null +++ b/README.md @@ -0,0 +1,24 @@ +# esx_nicedeath + +ESX plugin that makes death a little bit more configurable. + +This plugin has the following features: + +- Enables the death screen and let's you see the ragdolling +- Configurable respawn cooldown +- Weapons, Items, Cash can be removed on death (but also kept) +- Configurable respawn location (mt. zonah by default) + +## Installation + +1. Download this repo into your `resources/[esx/]` folder +2. Add `start esx_nicedeath` to your `server.cfg` +3. Modify `spawnmanager` to disable death spawn: + 1. find `resources/[managers]/spawnmanager/spawnmanager.lua` + 2. replace line 345 + - Old: `if (diedAt and (math.abs(GetTimeDifference(GetGameTimer(), diedAt)) > 2000)) or respawnForced then` + - New: `if respawnForced then` + 3. Remember to redo this once you update the `server-data` repo! + + + diff --git a/client/main.lua b/client/main.lua new file mode 100644 index 0000000..aa3f141 --- /dev/null +++ b/client/main.lua @@ -0,0 +1,119 @@ +ESX = nil +local isDead = false + +--- Thread to get the ESX object +Citizen.CreateThread(function() + while ESX == nil do + TriggerEvent('esx:getSharedObject', function(obj) + ESX = obj + end) + Citizen.Wait(0) + end +end) + +--- Utility function that starts the respawn timer +function StartRespawnTimer() + local respawnTimer = ESX.Math.Round(Config.RespawnTimer / 1000) + Citizen.CreateThread(function() + -- respawn timer + while respawnTimer > 0 and isDead do + Citizen.Wait(1000) + + if respawnTimer > 0 then + respawnTimer = respawnTimer - 1 + end + end + end) + Citizen.CreateThread(function() + local text, timeHeld + while isDead do + Citizen.Wait(0) + if respawnTimer > 0 then + text = _U('respawn_available_in', formatTimer(respawnTimer)) + drawText(text) + else + text = _U('respawn_prompt') + if IsControlPressed(0, 38) and timeHeld > 60 then + doRespawn() + break + end + if IsControlPressed(0, 38) then + timeHeld = timeHeld + 1 + else + timeHeld = 0 + end + drawText(text) + end + end + end) +end + +--- Utility function that does the respawn logic +function doRespawn() + Citizen.CreateThread(function() + DoScreenFadeOut(800) + while not IsScreenFadedOut() do + Citizen.Wait(10) + end + ESX.TriggerServerCallback('esx_nicedeath:onDeath', function() + ESX.SetPlayerData('loadout', {}) + RespawnPed(PlayerPedId(), Config.RespawnPoint.coords, Config.RespawnPoint.heading) + StopScreenEffect('DeathFailOut') + DoScreenFadeIn(800) + end) + end) +end + +--- Utility function to respawn a ped +function RespawnPed(ped, coords, heading) + SetEntityCoordsNoOffset(ped, coords.x, coords.y, coords.z, false, false, false, true) + NetworkResurrectLocalPlayer(coords.x, coords.y, coords.z, heading, true, false) + SetPlayerInvincible(ped, false) + ClearPedBloodDamage(ped) + + TriggerServerEvent('esx:onPlayerSpawn') + TriggerEvent('esx:onPlayerSpawn') + TriggerEvent('playerSpawned') -- compatibility with old scripts, should be removed soon +end + +--- Utility function to draw text on the screen +function drawText(text) + SetTextFont(4) + SetTextScale(0.0, 0.5) + SetTextColour(255, 255, 255, 255) + SetTextDropshadow(0, 0, 0, 0, 255) + SetTextDropShadow() + SetTextOutline() + SetTextCentre(true) + SetTextEntry('STRING') + AddTextComponentString(text) + DrawText(0.5, 0.8) +end + +--- Utility function to format the countdown to mm:ss +function formatTimer(seconds) + local seconds = tonumber(seconds) + + if seconds <= 0 then + return 0, 0 + else + local hours = string.format('%02.f', math.floor(seconds / 3600)) + local mins = string.format('%02.f', math.floor(seconds / 60 - (hours * 60))) + local secs = string.format('%02.f', math.floor(seconds - hours * 3600 - mins * 60)) + + return mins, secs + end +end + +--- Respawn event handler +AddEventHandler('esx:onPlayerSpawn', function() + isDead = false +end) + +--- Death event handler +AddEventHandler('esx:onPlayerDeath', function(data) + isDead = true + ESX.UI.Menu.CloseAll() + StartRespawnTimer() + StartScreenEffect('DeathFailOut', 0, false) +end) \ No newline at end of file diff --git a/config.lua b/config.lua new file mode 100644 index 0000000..7099586 --- /dev/null +++ b/config.lua @@ -0,0 +1,10 @@ +Config = {} + +Config.Locale = 'en' + +Config.RespawnTimer = 10000 -- time in milliseconds (10s by default) +Config.RespawnPoint = {coords = vector3(-498.24, -335.85, 34.5), heading = 260.3} + +Config.KeepWeapons = true +Config.RemoveCash = false +Config.RemoveItems = false diff --git a/fxmanifest.lua b/fxmanifest.lua new file mode 100644 index 0000000..51cf2be --- /dev/null +++ b/fxmanifest.lua @@ -0,0 +1,24 @@ +fx_version 'adamant' + +game 'gta5' + +name 'esx_nicedeath' +description 'esx death fx and cooldown' + +version '1.0.0' + +server_scripts { + 'config.lua', + 'server/main.lua', +} + +client_scripts { + '@es_extended/locale.lua', + 'locales/en.lua', + 'config.lua', + 'client/main.lua', +} + +dependencies { + 'es_extended', +} diff --git a/locales/en.lua b/locales/en.lua new file mode 100644 index 0000000..5b90e01 --- /dev/null +++ b/locales/en.lua @@ -0,0 +1,5 @@ +Locales['en'] = { + ['respawn_available_in'] = 'respawn available in ~b~%s minutes %s seconds~s~', + ['respawn_prompt'] = 'hold [~b~E~s~] to respawn', +} + diff --git a/server/main.lua b/server/main.lua new file mode 100644 index 0000000..8606f1c --- /dev/null +++ b/server/main.lua @@ -0,0 +1,50 @@ +ESX = nil + +--- Event to get the ESX object +TriggerEvent('esx:getSharedObject', function(obj) + ESX = obj +end) + +--- Death event listener +ESX.RegisterServerCallback('esx_nicedeath:onDeath', function(source, cb) + local xPlayer = ESX.GetPlayerFromId(source) + + if Config.RemoveCash then + if xPlayer.getMoney() > 0 then + xPlayer.removeMoney(xPlayer.getMoney()) + end + end + + if Config.RemoveItems then + for i = 1, #xPlayer.inventory, 1 do + if xPlayer.inventory[i].count > 0 then + xPlayer.setInventoryItem(xPlayer.inventory[i].name, 0) + end + end + end + + local weapons = {} + if Config.KeepWeapons then + -- save weapons & restore 3s later to bypass spawnmanager + for i = 1, #xPlayer.getLoadout(), 1 do + table.insert(weapons, xPlayer.getLoadout()[i]) + end + Citizen.CreateThread(function() + Citizen.Wait(3000) + for i = 1, #weapons, 1 do + if weapons[i].label ~= nil then + xPlayer.addWeapon(weapons[i].name, weapons[i].ammo) + for j = 1, #weapons[i].components, 1 do + xPlayer.addWeaponComponent(weapons[i].name, weapons[i].components[j]) + end + end + end + end) + else + for i = 1, #xPlayer.loadout, 1 do + xPlayer.removeWeapon(xPlayer.getLoadout()[i].name) + end + end + + cb() +end)