minetest_hudbars/hunger.lua

37 lines
960 B
Lua
Raw Normal View History

2013-06-28 22:58:06 +02:00
function hud.item_eat(hunger_change, replace_with_item)
2013-06-28 23:03:54 +02:00
minetest.chat_send_all("eat")
2013-06-28 22:58:06 +02:00
return function(itemstack, user, pointed_thing)
if itemstack:take_item() ~= nil then
local h = tonumber(hud.hunger[user:get_player_name()])
h=h+hunger_change
2013-06-28 23:03:54 +02:00
if h>30 then h=30 end
2013-06-28 22:58:06 +02:00
hud.hunger[user:get_player_name()]=h
2013-06-28 23:03:54 +02:00
hud.save_hunger(user)
2013-06-28 22:58:06 +02:00
itemstack:add_item(replace_with_item) -- note: replace_with_item is optional
--sound:eat
end
return itemstack
end
end
local function overwrite(name, hunger_change, as_node)
local tab = minetest.registered_items[name]
if tab == nil then return end
local tab2 = {}
for i,v in pairs(tab) do
tab2[i] = v
end
tab2.on_use = hud.item_eat(hunger_change)
if as_node then
minetest.register_node(":"..name, tab2)
else
minetest.register_craftitem(":"..name, tab2)
end
end
overwrite("default:apple", 2, true)
if minetest.get_modpath("farming") ~= nil then
2013-06-28 23:03:54 +02:00
overwrite("farming:bread", 4, false)
2013-06-28 22:58:06 +02:00
end