fix confused code and redundant checks for fake players, backguard compatibility
* level its not property set on join player due inherit code * real check player for fake players provided in ch_player function * do not check fake players in callbacks due performance impack * the level of the attribute its never set property, was backguard compatibilty part * closes and fixed https://notabug.org/TenPlus1/stamina/issues/15 * this mod now works with older engines 0.4 and also newer 5.X
This commit is contained in:
parent
41bd87405d
commit
26ae940ec9
78
init.lua
78
init.lua
@ -24,6 +24,7 @@ STAMINA_STARVE_LVL = 3 -- level of staturation that causes starving
|
|||||||
|
|
||||||
STAMINA_VISUAL_MAX = 20 -- hud bar extends only to 20
|
STAMINA_VISUAL_MAX = 20 -- hud bar extends only to 20
|
||||||
|
|
||||||
|
local is_50 = minetest.has_feature("object_use_texture_alpha")
|
||||||
|
|
||||||
local function clamp(val, minval, maxval)
|
local function clamp(val, minval, maxval)
|
||||||
return math.max(math.min(val, maxval), minval)
|
return math.max(math.min(val, maxval), minval)
|
||||||
@ -37,34 +38,36 @@ SPRINT_JUMP = clamp(tonumber(minetest.settings:get("stamina_sprint_jump")) or 0
|
|||||||
SPRINT_DRAIN = clamp(tonumber(minetest.settings:get("stamina_sprint_drain")) or 0.35, 0.0, 1.0)
|
SPRINT_DRAIN = clamp(tonumber(minetest.settings:get("stamina_sprint_drain")) or 0.35, 0.0, 1.0)
|
||||||
|
|
||||||
|
|
||||||
|
local function ck_player(player)
|
||||||
|
-- pipeworks fake player check its not necesary with real player check:
|
||||||
|
if player then if minetest.is_player(player) then return true end end
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
local function get_int_attribute(player)
|
local function get_int_attribute(player)
|
||||||
|
|
||||||
-- pipeworks fake player check
|
if not ck_player(player) then return nil end
|
||||||
if not player or not player.get_attribute then
|
|
||||||
return nil
|
local level
|
||||||
|
if is_50 then
|
||||||
|
local meta = player:get_meta()
|
||||||
|
level = meta and meta:get_string("stamina:level")
|
||||||
|
else
|
||||||
|
level = player and player:get_attribute("stamina:level")
|
||||||
end
|
end
|
||||||
|
|
||||||
local meta = player:get_meta()
|
if level then return tonumber(level) end -- true its converted to number, even false
|
||||||
local level = meta and meta:get_string("stamina:level")
|
|
||||||
|
|
||||||
if level then
|
|
||||||
return tonumber(level)
|
|
||||||
end
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local stamina_enabled = minetest.settings:get_bool("enable_stamina") ~= false
|
local stamina_enabled = minetest.settings:get_bool("enable_stamina") ~= false
|
||||||
local damage_enabled = minetest.settings:get_bool("enable_damage")
|
local damage_enabled = minetest.settings:get_bool("enable_damage")
|
||||||
|
|
||||||
|
|
||||||
local function stamina_update_level(player, level)
|
local function stamina_update_level(player, level)
|
||||||
|
|
||||||
-- pipeworks fake player check
|
if not ck_player(player) then return end
|
||||||
if not player.get_attribute or not stamina_enabled then
|
|
||||||
return nil
|
|
||||||
end
|
|
||||||
|
|
||||||
local old = get_int_attribute(player)
|
local old = get_int_attribute(player)
|
||||||
|
|
||||||
@ -77,9 +80,12 @@ local function stamina_update_level(player, level)
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local meta = player and player:get_meta() ; if not meta then return end
|
if is_50 then
|
||||||
|
local meta = player and player:get_meta() ; if not meta then return end
|
||||||
meta:set_string("stamina:level", level)
|
meta:set_string("stamina:level", level)
|
||||||
|
else
|
||||||
|
player:set_attribute("stamina:level", level)
|
||||||
|
end
|
||||||
|
|
||||||
player:hud_change(
|
player:hud_change(
|
||||||
stamina.players[player:get_player_name()].hud_id,
|
stamina.players[player:get_player_name()].hud_id,
|
||||||
@ -113,8 +119,7 @@ end
|
|||||||
local function exhaust_player(player, v)
|
local function exhaust_player(player, v)
|
||||||
|
|
||||||
if not player
|
if not player
|
||||||
or not player.is_player
|
or not ck_player(player) -- player.is_player can return nill in rare cases, and real check of player here
|
||||||
or not player:is_player()
|
|
||||||
or not player.set_attribute then
|
or not player.set_attribute then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
@ -151,7 +156,7 @@ local pova_mod = minetest.get_modpath("pova")
|
|||||||
|
|
||||||
local function set_sprinting(name, sprinting)
|
local function set_sprinting(name, sprinting)
|
||||||
|
|
||||||
local player = minetest.get_player_by_name(name) ; if not player then return end
|
local player = minetest.get_player_by_name(name) ; if not ck_player(player) then return end
|
||||||
|
|
||||||
-- get player physics
|
-- get player physics
|
||||||
local def = player:get_physics_override()
|
local def = player:get_physics_override()
|
||||||
@ -217,7 +222,7 @@ end
|
|||||||
|
|
||||||
local function head_particle(player, texture)
|
local function head_particle(player, texture)
|
||||||
|
|
||||||
if not player then return end
|
if not ck_player(player) then return end
|
||||||
|
|
||||||
local prop = player:get_properties()
|
local prop = player:get_properties()
|
||||||
local eye_height = prop.eye_height or 1.23
|
local eye_height = prop.eye_height or 1.23
|
||||||
@ -246,7 +251,7 @@ local function drunk_tick()
|
|||||||
|
|
||||||
for _,player in ipairs(minetest.get_connected_players()) do
|
for _,player in ipairs(minetest.get_connected_players()) do
|
||||||
|
|
||||||
if not player then return end
|
if not ck_player(player) then return end
|
||||||
|
|
||||||
local name = player:get_player_name()
|
local name = player:get_player_name()
|
||||||
|
|
||||||
@ -295,7 +300,7 @@ local function health_tick()
|
|||||||
|
|
||||||
for _,player in ipairs(minetest.get_connected_players()) do
|
for _,player in ipairs(minetest.get_connected_players()) do
|
||||||
|
|
||||||
if not player then return end
|
if not ck_player(player) then return end
|
||||||
|
|
||||||
local name = player:get_player_name()
|
local name = player:get_player_name()
|
||||||
|
|
||||||
@ -331,7 +336,7 @@ local function action_tick()
|
|||||||
|
|
||||||
for _,player in ipairs(minetest.get_connected_players()) do
|
for _,player in ipairs(minetest.get_connected_players()) do
|
||||||
|
|
||||||
if not player then return end
|
if not ck_player(player) then return end
|
||||||
local controls = player:get_player_control()
|
local controls = player:get_player_control()
|
||||||
|
|
||||||
-- Determine if the player is walking or jumping
|
-- Determine if the player is walking or jumping
|
||||||
@ -351,7 +356,7 @@ local function action_tick()
|
|||||||
--- START sprint
|
--- START sprint
|
||||||
if enable_sprint then
|
if enable_sprint then
|
||||||
|
|
||||||
if not player then return end
|
if not ck_player(player) then return end
|
||||||
local name = player:get_player_name()
|
local name = player:get_player_name()
|
||||||
|
|
||||||
-- check if player can sprint (stamina must be over 6 points)
|
-- check if player can sprint (stamina must be over 6 points)
|
||||||
@ -417,7 +422,7 @@ local function poison_tick()
|
|||||||
|
|
||||||
for _,player in ipairs(minetest.get_connected_players()) do
|
for _,player in ipairs(minetest.get_connected_players()) do
|
||||||
|
|
||||||
if not player then return end
|
if not ck_player(player) then return end
|
||||||
|
|
||||||
local name = player:get_player_name()
|
local name = player:get_player_name()
|
||||||
|
|
||||||
@ -457,7 +462,7 @@ local function stamina_tick()
|
|||||||
|
|
||||||
for _,player in ipairs(minetest.get_connected_players()) do
|
for _,player in ipairs(minetest.get_connected_players()) do
|
||||||
|
|
||||||
if not player then return end
|
if not ck_player(player) then return end
|
||||||
|
|
||||||
local h = get_int_attribute(player)
|
local h = get_int_attribute(player)
|
||||||
|
|
||||||
@ -512,9 +517,7 @@ if damage_enabled and minetest.settings:get_bool("enable_stamina") ~= false then
|
|||||||
-- override core.do_item_eat() so we can redirect hp_change to stamina
|
-- override core.do_item_eat() so we can redirect hp_change to stamina
|
||||||
core.do_item_eat = function(hp_change, replace_with_item, itemstack, user, pointed_thing)
|
core.do_item_eat = function(hp_change, replace_with_item, itemstack, user, pointed_thing)
|
||||||
|
|
||||||
if user.is_fake_player then
|
if not ck_player(user) then return end -- abort if called by fake player (eg. pipeworks-wielder)
|
||||||
return -- abort if called by fake player (eg. pipeworks-wielder)
|
|
||||||
end
|
|
||||||
|
|
||||||
local old_itemstack = itemstack
|
local old_itemstack = itemstack
|
||||||
|
|
||||||
@ -639,17 +642,22 @@ if damage_enabled and minetest.settings:get_bool("enable_stamina") ~= false then
|
|||||||
|
|
||||||
local level = STAMINA_VISUAL_MAX -- TODO
|
local level = STAMINA_VISUAL_MAX -- TODO
|
||||||
|
|
||||||
|
-- the code here was redundant cos get_int_attribute already call the level
|
||||||
|
-- but with backguard compat, tenplus1 just inherint the code but with removed backguard compatibility
|
||||||
if get_int_attribute(player) then
|
if get_int_attribute(player) then
|
||||||
|
|
||||||
level = math.min(get_int_attribute(player), STAMINA_VISUAL_MAX)
|
level = math.min(get_int_attribute(player), STAMINA_VISUAL_MAX)
|
||||||
else
|
end
|
||||||
|
if is_50 then
|
||||||
local meta = player:get_meta()
|
local meta = player:get_meta()
|
||||||
|
if meta then meta:set_string("stamina:level", level) end
|
||||||
meta:set_string("stamina:level", level)
|
else
|
||||||
|
player:set_attribute("stamina:level", level)
|
||||||
end
|
end
|
||||||
|
|
||||||
local name = player:get_player_name()
|
local name = player:get_player_name()
|
||||||
|
|
||||||
|
if not name then return end
|
||||||
|
|
||||||
local id = player:hud_add({
|
local id = player:hud_add({
|
||||||
name = "stamina",
|
name = "stamina",
|
||||||
hud_elem_type = "statbar",
|
hud_elem_type = "statbar",
|
||||||
@ -673,6 +681,7 @@ if damage_enabled and minetest.settings:get_bool("enable_stamina") ~= false then
|
|||||||
|
|
||||||
minetest.register_on_respawnplayer(function(player)
|
minetest.register_on_respawnplayer(function(player)
|
||||||
|
|
||||||
|
if not player then return end
|
||||||
local name = player:get_player_name() ; if not name then return end
|
local name = player:get_player_name() ; if not name then return end
|
||||||
|
|
||||||
if stamina.players[name].poisoned
|
if stamina.players[name].poisoned
|
||||||
@ -691,6 +700,7 @@ if damage_enabled and minetest.settings:get_bool("enable_stamina") ~= false then
|
|||||||
minetest.register_globalstep(stamina_globaltimer)
|
minetest.register_globalstep(stamina_globaltimer)
|
||||||
|
|
||||||
minetest.register_on_placenode(function(pos, oldnode, player, ext)
|
minetest.register_on_placenode(function(pos, oldnode, player, ext)
|
||||||
|
if not player then return end
|
||||||
exhaust_player(player, STAMINA_EXHAUST_PLACE)
|
exhaust_player(player, STAMINA_EXHAUST_PLACE)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user