Skip to content

Commit

Permalink
Use new player metadata API when available (#29)
Browse files Browse the repository at this point in the history
Make use of existing minetest.is_player() method

Don't store the stamina HUD ID as player metadata
  • Loading branch information
fluxionary committed Mar 19, 2020
1 parent 6ee95f9 commit 228f5d3
Showing 1 changed file with 52 additions and 26 deletions.
78 changes: 52 additions & 26 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ function stamina.log(level, message, ...)
end

local function get_setting(key, default)
local setting = minetest.settings:get("stamina." .. key)
if setting and not tonumber(setting) then
stamina.log("warning", "Invalid value for setting %s: %q. Using default %q.", key, setting, default)
local value = minetest.settings:get("stamina." .. key)
local num_value = tonumber(value)
if value and not num_value then
stamina.log("warning", "Invalid value for setting %s: %q. Using default %q.", key, value, default)
end
return tonumber(setting) or default
return num_value or default
end

stamina.settings = {
Expand Down Expand Up @@ -51,38 +52,56 @@ local settings = stamina.settings

local attribute = {
saturation = "stamina:level",
hud_id = "stamina:hud_id",
poisoned = "stamina:poisoned",
exhaustion = "stamina:exhaustion",
}

local function is_player(player)
return (
player and
not player.is_fake_player and
player.get_attribute and -- check for pipeworks fake player
player.is_player and
player:is_player()
minetest.is_player(player) and
not player.is_fake_player
)
end

local function get_int_attribute(player, key)
local level = player:get_attribute(key)
if level then
return tonumber(level)
local function set_player_attribute(player, key, value)
if player.get_meta then
if value == nil then
player:get_meta():set_string(key, "")
else
player:get_meta():set_string(key, tostring(value))
end
else
return nil
player:set_attribute(key, value)
end
end

local function get_player_attribute(player, key)
if player.get_meta then
return player:get_meta():get_string(key)
else
return player:get_attribute(key)
end
end

local hud_ids_by_player_name = {}

local function get_hud_id(player)
return hud_ids_by_player_name[player:get_player_name()]
end

local function set_hud_id(player, hud_id)
hud_ids_by_player_name[player:get_player_name()] = hud_id
end

--- SATURATION API ---
function stamina.get_saturation(player)
return get_int_attribute(player, attribute.saturation)
return tonumber(get_player_attribute(player, attribute.saturation))
end

function stamina.set_saturation(player, level)
player:set_attribute(attribute.saturation, level)
set_player_attribute(player, attribute.saturation, level)
player:hud_change(
player:get_attribute(attribute.hud_id),
get_hud_id(player),
"number",
math.min(settings.visual_max, level)
)
Expand Down Expand Up @@ -130,16 +149,17 @@ stamina.change = stamina.change_saturation -- for backwards compatablity
--- END SATURATION API ---
--- POISON API ---
function stamina.is_poisoned(player)
return player:get_attribute(attribute.poisoned) == "yes"
return get_player_attribute(player, attribute.poisoned) == "yes"
end

function stamina.set_poisoned(player, poisoned)
local hud_id = get_hud_id(player)
if poisoned then
player:hud_change(player:get_attribute(attribute.hud_id), "text", "stamina_hud_poison.png")
player:set_attribute(attribute.poisoned, "yes")
player:hud_change(hud_id, "text", "stamina_hud_poison.png")
set_player_attribute(player, attribute.poisoned, "yes")
else
player:hud_change(player:get_attribute(attribute.hud_id), "text", "stamina_hud_fg.png")
player:set_attribute(attribute.poisoned, "no")
player:hud_change(hud_id, "text", "stamina_hud_fg.png")
set_player_attribute(player, attribute.poisoned, "no")
end
end

Expand Down Expand Up @@ -189,11 +209,11 @@ stamina.exhaustion_reasons = {
}

function stamina.get_exhaustion(player)
return get_int_attribute(player, attribute.exhaustion)
return tonumber(get_player_attribute(player, attribute.exhaustion))
end

function stamina.set_exhaustion(player, exhaustion)
player:set_attribute(attribute.exhaustion, exhaustion)
set_player_attribute(player, attribute.exhaustion, exhaustion)
end

stamina.registered_on_exhaust_players = {}
Expand Down Expand Up @@ -498,9 +518,15 @@ minetest.register_on_joinplayer(function(player)
max = 0,
})
stamina.set_saturation(player, level)
player:set_attribute(attribute.hud_id, id)
set_hud_id(player, id)
-- reset poisoned
stamina.set_poisoned(player, false)
-- remove legacy hud_id from player metadata
set_player_attribute(player, "stamina:hud_id", nil)
end)

minetest.register_on_leaveplayer(function(player)
set_hud_id(player, nil)
end)

minetest.register_globalstep(stamina_globaltimer)
Expand Down

0 comments on commit 228f5d3

Please sign in to comment.