Skip to content

Commit

Permalink
Merge pull request #527 from bitpredator/develop
Browse files Browse the repository at this point in the history
refactor: esx_basicneeds
  • Loading branch information
bitpredator authored Sep 21, 2023
2 parents 8236ae4 + a158b01 commit 0fa0abc
Show file tree
Hide file tree
Showing 17 changed files with 90 additions and 52 deletions.
68 changes: 26 additions & 42 deletions server-data/resources/[esx_addons]/esx_basicneeds/client/main.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
ESX = nil
local IsDead = false
local IsAnimated = false

Citizen.CreateThread(function()
ESX = exports["es_extended"]:getSharedObject()
end)

AddEventHandler('esx_basicneeds:resetStatus', function()
TriggerEvent('esx_status:set', 'hunger', 500000)
TriggerEvent('esx_status:set', 'thirst', 500000)
Expand All @@ -26,63 +21,52 @@ AddEventHandler('esx:onPlayerDeath', function()
IsDead = true
end)

AddEventHandler('esx:onPlayerSpawn', function(spawn)
AddEventHandler('esx:onPlayerSpawn', function()
if IsDead then
TriggerEvent('esx_basicneeds:resetStatus')
end

IsDead = false
end)

AddEventHandler('esx_status:loaded', function(status)

TriggerEvent('esx_status:registerStatus', 'hunger', 1000000, '#CFAD0F', function(status)
AddEventHandler('esx_status:loaded', function()
TriggerEvent('esx_status:registerStatus', 'hunger', 1000000, '#CFAD0F', function()
return Config.Visible
end, function(status)
status.remove(100)
end)

TriggerEvent('esx_status:registerStatus', 'thirst', 1000000, '#0C98F1', function(status)
TriggerEvent('esx_status:registerStatus', 'thirst', 1000000, '#0C98F1', function()
return Config.Visible
end, function(status)
status.remove(75)
end)
end)

Citizen.CreateThread(function()
while true do
Citizen.Wait(1000)

local playerPed = PlayerPedId()
local prevHealth = GetEntityHealth(playerPed)
local health = prevHealth

TriggerEvent('esx_status:getStatus', 'hunger', function(status)
if status.val == 0 then
if prevHealth <= 150 then
health = health - 5
else
health = health - 1
end
end
end)

TriggerEvent('esx_status:getStatus', 'thirst', function(status)
if status.val == 0 then
if prevHealth <= 150 then
health = health - 5
else
health = health - 1
end
end
end)

if health ~= prevHealth then
SetEntityHealth(playerPed, health)
AddEventHandler('esx_status:onTick', function(data)
local playerPed = PlayerPedId()
local prevHealth = GetEntityHealth(playerPed)
local health = prevHealth

for _, v in pairs(data) do
if v.name == 'hunger' and v.percent == 0 then
if prevHealth <= 150 then
health = health - 5
else
health = health - 1
end
elseif v.name == 'thirst' and v.percent == 0 then
if prevHealth <= 150 then
health = health - 5
else
health = health - 1
end
end
end)
end

if health ~= prevHealth then SetEntityHealth(playerPed, health) end
end)

AddEventHandler('esx_basicneeds:isEating', function(cb)
cb(IsAnimated)
end)
end)
3 changes: 2 additions & 1 deletion server-data/resources/[esx_addons]/esx_basicneeds/config.lua
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Config = {}
Config.Visible = false
Config.Locale = 'en'
Config.Visible = false
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@ fx_version 'adamant'

game 'gta5'

description 'ESX Basic Needs'
description 'Basic Needs'
lua54 'yes'
version '0.0.5'

version '0.0.4'
shared_script '@es_extended/imports.lua'

server_scripts {
'@es_extended/locale.lua',
'locales/*.lua',
'config.lua',
'server/main.lua'
}

client_scripts {
'@es_extended/locale.lua',
'locales/*.lua',
'config.lua',
'client/main.lua'
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Locales['da'] = {
['got_healed'] = 'Du er blevet helbredt.'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Locales['de'] = {
['got_healed'] = 'Du wurdest geheilt.'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Locales['en'] = {
['got_healed'] = 'You have been healed.'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Locales['es'] = {
['used_bread'] = 'Has usado 1x pan',
['used_water'] = 'Has usado 1x agua',
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Locales['fi'] = {
['used_bread'] = 'sinä söit 1x leipä',
['used_water'] = 'sinä joit 1x vesi',
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Locales['fr'] = {
['got_healed'] = 'Vous avez été soigné.'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Locales['hu'] = {
['used_bread'] = 'Megettél egy kenyeret.',
['used_water'] = 'Megittál egy vizet.',
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Locales['it'] = {
['used_bread'] = 'hai usato 1x panino',
['used_water'] = 'hai usato 1x acqua',
['got_healed'] = 'Sei stato curato.'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Locales['nl'] = {
['used_eat'] = 'je hebt een %s gegeten',
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Locales['pl'] = {
['used_bread'] = 'zjadłeś/aś 1x chleb',
['used_water'] = 'wypiłeś/aś 1x woda',
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Locales['sl'] = {
['got_healed'] = 'Vi ste bili Pozdravljeni.'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Locales['sr'] = {
['got_healed'] = 'Vi ste izlečeni.'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Locales['zh-cn'] = {
['got_healed'] = '已重置饥饿+饥渴双值.'
}
18 changes: 11 additions & 7 deletions server-data/resources/[esx_addons]/esx_basicneeds/server/main.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
ESX = nil

ESX = exports["es_extended"]:getSharedObject()

ESX.RegisterCommand('heal', 'admin', function(xPlayer, args, showError)
ESX.RegisterCommand('heal', 'admin', function(_, args)
args.playerId.triggerEvent('esx_basicneeds:healPlayer')
args.playerId.triggerEvent('chat:addMessage', {args = {'^5HEAL', 'You have been healed.'}})
args.playerId.showNotification(_U('got_healed'))
end, true, {help = 'Heal a player, or yourself - restores thirst, hunger and health.', validate = true, arguments = {
{name = 'playerId', help = 'the player id', type = 'player'}
}})
}})

AddEventHandler('txAdmin:events:healedPlayer', function(eventData)
if GetInvokingResource() ~= "monitor" or type(eventData) ~= "table" or type(eventData.id) ~= "number" then
return
end

TriggerClientEvent('esx_basicneeds:healPlayer', eventData.id)
end)

0 comments on commit 0fa0abc

Please sign in to comment.