From f1b99dc117d91f21667f1ab2aab76ba8b465d502 Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Tue, 6 Jun 2017 18:04:36 +0200 Subject: [PATCH] Use new player attribute API for mcl_hunger --- mods/PLAYER/mcl_hunger/hunger.lua | 2 +- mods/PLAYER/mcl_hunger/init.lua | 75 +++++++++---------------------- 2 files changed, 23 insertions(+), 54 deletions(-) diff --git a/mods/PLAYER/mcl_hunger/hunger.lua b/mods/PLAYER/mcl_hunger/hunger.lua index 126be3d4..cab773d5 100644 --- a/mods/PLAYER/mcl_hunger/hunger.lua +++ b/mods/PLAYER/mcl_hunger/hunger.lua @@ -183,7 +183,7 @@ function mcl_hunger.item_eat(hunger_change, replace_with_item, poisontime, poiso if not _mcl_saturation then saturation = 0 else - saturation = math.floor(minetest.registered_items[itemname]._mcl_saturation * 10) + saturation = minetest.registered_items[itemname]._mcl_saturation end mcl_hunger.saturate(name, saturation, false) diff --git a/mods/PLAYER/mcl_hunger/init.lua b/mods/PLAYER/mcl_hunger/init.lua index ec4e3bf3..0f6b336d 100644 --- a/mods/PLAYER/mcl_hunger/init.lua +++ b/mods/PLAYER/mcl_hunger/init.lua @@ -22,7 +22,7 @@ mcl_hunger.EXHAUST_DAMAGE = 100 -- TODO (mostly done): taking damage (protected mcl_hunger.EXHAUST_REGEN = 6000 -- Regenerate 1 HP mcl_hunger.EXHAUST_LVL = 4000 -- at what exhaustion player saturation gets lowered -mcl_hunger.SATURATION_INIT = 50 -- Initial saturation for new/respawning players +mcl_hunger.SATURATION_INIT = 5 -- Initial saturation for new/respawning players mcl_hunger.active = false @@ -41,7 +41,7 @@ end Hunger values is identical to Minecraft's and ranges from 0 to 20. Exhaustion and saturation values are stored as integers, unlike in Minecraft. Exhaustion is Minecraft exhaustion times 1000 and ranges from 0 to 4000. - Saturation is Minecraft exhaustion times 10 and ranges from 0 to 200. + Saturation is Minecraft saturation and ranges from 0 to 20. Food saturation is stored in the custom item definition field _mcl_saturation. This field uses the original Minecraft value. @@ -59,7 +59,7 @@ local hunger_hud = {} local function init_hud(player) hb.init_hudbar(player, "hunger", mcl_hunger.get_hunger(player)) if mcl_hunger.debug then - hb.init_hudbar(player, "saturation", mcl_hunger.get_saturation(player), mcl_hunger.get_hunger(player)*10) + hb.init_hudbar(player, "saturation", mcl_hunger.get_saturation(player), mcl_hunger.get_hunger(player)) hb.init_hudbar(player, "exhaustion", mcl_hunger.get_exhaustion(player)) end end @@ -67,11 +67,7 @@ end -- HUD updating functions for Debug Mode. No-op if not in Debug Mode function mcl_hunger.update_saturation_hud(player, saturation, hunger) if mcl_hunger.debug then - local satulimit - if hunger then - satulimit = hunger * 10 - end - hb.change_hudbar(player, "saturation", saturation, satulimit) + hb.change_hudbar(player, "saturation", saturation, hunger) end end function mcl_hunger.update_exhaustion_hud(player, exhaustion) @@ -85,51 +81,26 @@ dofile(minetest.get_modpath("mcl_hunger").."/hunger.lua") -- register saturation hudbar hb.register_hudbar("hunger", 0xFFFFFF, S("Food"), { icon = "hbhunger_icon.png", bgicon = "hbhunger_bgicon.png", bar = "hbhunger_bar.png" }, 20, 20, false) if mcl_hunger.debug then - hb.register_hudbar("saturation", 0xFFFFFF, S("Saturation"), { icon = "mcl_hunger_icon_saturation.png", bgicon = "mcl_hunger_bgicon_saturation.png", bar = "mcl_hunger_bar_saturation.png" }, mcl_hunger.SATURATION_INIT, 200, false, S("%s: %d/%d")) + hb.register_hudbar("saturation", 0xFFFFFF, S("Saturation"), { icon = "mcl_hunger_icon_saturation.png", bgicon = "mcl_hunger_bgicon_saturation.png", bar = "mcl_hunger_bar_saturation.png" }, mcl_hunger.SATURATION_INIT, 200, false, S("%s: %.1f/%d")) hb.register_hudbar("exhaustion", 0xFFFFFF, S("Exhaust."), { icon = "mcl_hunger_icon_exhaustion.png", bgicon = "mcl_hunger_bgicon_exhaustion.png", bar = "mcl_hunger_bar_exhaustion.png" }, 0, mcl_hunger.EXHAUST_LVL, false, S("%s: %d/%d")) end -local RAW_VALUE_FOOD = 1 -local RAW_VALUE_SATURATION = 2 -local RAW_VALUE_EXHAUSTION = 3 - -local get_player_value_raw = function(player, id, default) - local inv = player:get_inventory() - if not inv then return nil end - local value = inv:get_stack("hunger", id):get_count() - if value == 0 then - inv:set_stack("hunger", id, ItemStack({name=":", count=default+1})) - return default - else - return value - 1 - end -end - -local set_player_value_raw = function(player, id, value) - local inv = player:get_inventory() - inv:set_stack("hunger", id, ItemStack({name=":", count=value+1})) - return true -end - -- API START -- mcl_hunger.get_hunger = function(player) - return get_player_value_raw(player, RAW_VALUE_FOOD, 20) + return tonumber(player:get_attribute("mcl_hunger:hunger")) or 20 end mcl_hunger.get_saturation = function(player) - return get_player_value_raw(player, RAW_VALUE_SATURATION, 50) + return tonumber(player:get_attribute("mcl_hunger:saturation")) or mcl_hunger.SATURATION_INIT end mcl_hunger.get_exhaustion = function(player) - return get_player_value_raw(player, RAW_VALUE_EXHAUSTION, 0) + return tonumber(player:get_attribute("mcl_hunger:exhaustion")) or 0 end mcl_hunger.set_hunger = function(player, hunger, update_hudbars) hunger = math.min(20, math.max(0, hunger)) - - local ok = set_player_value_raw(player, RAW_VALUE_FOOD, hunger) - if not ok then return false end - + player:set_attribute("mcl_hunger:hunger", tostring(hunger)) if update_hudbars ~= false then hb.change_hudbar(player, "hunger", hunger) mcl_hunger.update_saturation_hud(player, nil, hunger) @@ -138,11 +109,8 @@ mcl_hunger.set_hunger = function(player, hunger, update_hudbars) end mcl_hunger.set_saturation = function(player, saturation, update_hudbar) - saturation = math.min(mcl_hunger.get_hunger(player)*10, math.max(0, saturation)) - - local ok = set_player_value_raw(player, RAW_VALUE_SATURATION, saturation) - if not ok then return false end - + saturation = math.min(mcl_hunger.get_hunger(player), math.max(0, saturation)) + player:set_attribute("mcl_hunger:saturation", tostring(saturation)) if update_hudbar ~= false then mcl_hunger.update_saturation_hud(player, saturation) end @@ -151,10 +119,7 @@ end mcl_hunger.set_exhaustion = function(player, exhaustion, update_hudbar) exhaustion = math.min(mcl_hunger.EXHAUST_LVL, math.max(0.0, exhaustion)) - - local ok = set_player_value_raw(player, RAW_VALUE_EXHAUSTION, exhaustion) - if not ok then return false end - + player:set_attribute("mcl_hunger:exhaustion", tostring(exhaustion)) if update_hudbar ~= false then mcl_hunger.update_exhaustion_hud(player, exhaustion) end @@ -164,14 +129,18 @@ end -- END OF API -- +minetest.register_on_newplayer(function(player) + local name = player:get_player_name() + mcl_hunger.set_hunger(player, 20, false) + mcl_hunger.set_saturation(player, mcl_hunger.SATURATION_INIT, false) + mcl_hunger.set_exhaustion(player, 0, false) +end) minetest.register_on_joinplayer(function(player) local name = player:get_player_name() - local inv = player:get_inventory() - inv:set_size("hunger", 3) + init_hud(player) mcl_hunger.poisonings[name] = 0 mcl_hunger.last_eat[name] = -1 - init_hud(player) end) minetest.register_on_respawnplayer(function(player) @@ -208,7 +177,7 @@ function mcl_hunger.exhaust(playername, increase) local satuchanged = false local s = mcl_hunger.get_saturation(player) if s > 0 then - mcl_hunger.set_saturation(player, math.max(s - 10, 0)) + mcl_hunger.set_saturation(player, math.max(s - 1.0, 0)) satuchanged = true elseif s <= 0.0001 then h = mcl_hunger.get_hunger(player) @@ -217,7 +186,7 @@ function mcl_hunger.exhaust(playername, increase) satuchanged = true end if satuchanged then - if h ~= nil then h = h*10 end + if h ~= nil then h = h end mcl_hunger.update_saturation_hud(player, mcl_hunger.get_saturation(player), h) end end @@ -227,7 +196,7 @@ end function mcl_hunger.saturate(playername, increase, update_hudbar) local player = minetest.get_player_by_name(playername) - mcl_hunger.set_saturation(player, math.min(mcl_hunger.get_saturation(player) + increase, mcl_hunger.get_hunger(player)*10)) + mcl_hunger.set_saturation(player, math.min(mcl_hunger.get_saturation(player) + increase, mcl_hunger.get_hunger(player))) if update_hudbar ~= false then mcl_hunger.update_saturation_hud(player, mcl_hunger.get_saturation(player), mcl_hunger.get_hunger(player)) end