nodecore-cd2025/mods/nc_api/util_phealth.lua
Aaron Suen 7216927d00 Fix falling ent displacing hot item crash
To repro: place a sand node at least 2 nodes high above
glowing lode prills and let the sand fall onto the prills and
try to displace them.

Introduced in 01252653-5da0918.

For some reason it seems like a non-player object is being
passed as the digger (I thought it would be nil for non-
players?) and this object apparently imitates a player but
is missing some critical thing such as metadata.

To work around the problem, just add guards to the
phealth functions, since only players are intended to have
"health" anyway.
2021-03-23 15:29:27 -04:00

41 lines
1.3 KiB
Lua

-- LUALOCALS < ---------------------------------------------------------
local math, nodecore, type
= math, nodecore, type
local math_ceil
= math.ceil
-- LUALOCALS > ---------------------------------------------------------
local function getphealth(player)
if not (player and player.is_player and player:is_player()) then return 0 end
return player:get_hp() + player:get_meta():get_float("dhp")
end
nodecore.getphealth = getphealth
local function setphealth(player, hp, reason, minwhole)
if not (player and player.is_player and player:is_player()) then return end
local hpmax = player:get_properties().hp_max
if hp > hpmax then hp = hpmax end
if hp < 0 then hp = 0 end
local whole = math_ceil(hp)
if whole == 0 then whole = 1 end
if minwhole and whole < minwhole then whole = minwhole end
local dhp = hp - whole
player:get_meta():set_float("dhp", dhp)
local old = player:get_hp()
if type(reason) ~= "table" then reason = {nc_type = reason} end
reason = reason or {}
reason.from = "mod"
reason.type = "set_hp"
player:set_hp(whole, reason)
return old ~= whole
end
nodecore.setphealth = setphealth
local function addphealth(player, hp, reason)
return setphealth(player,
getphealth(player) + hp,
reason,
hp >= 0 and player:get_hp())
end
nodecore.addphealth = addphealth