summaryrefslogtreecommitdiff
path: root/hunger.lua
blob: 36f2ddd0ef16f263357ad6c9892dd5166d6c3d13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
function hud.item_eat(hunger_change, replace_with_item)
			minetest.chat_send_all("eat")
	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
			if h>30 then h=30 end
			hud.hunger[user:get_player_name()]=h
			hud.save_hunger(user)
			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
	overwrite("farming:bread", 4, false)
end