1 minetest.register_on_joinplayer(function(player)
2 local meta = player:get_meta()
3 --give players new hunger when they join
4 if meta:get_int("hunger") == 0 then
5 meta:set_int("hunger", 20)
6 meta:set_int("satiation", 5)
9 hud_elem_type = "statbar",
10 position = {x = 0.5, y = 1},
11 text = "hunger_icon_bg.png",
14 size = {x = 24, y = 24},
15 offset = {x = 24*10, y= -(48 + 50 + 39)},
17 local hunger_bar = player:hud_add({
18 hud_elem_type = "statbar",
19 position = {x = 0.5, y = 1},
20 text = "hunger_icon.png",
23 size = {x = 24, y = 24},
24 offset = {x = 24*10, y= -(48 + 50 + 39)},
26 meta:set_int("hunger_bar", hunger_bar)
29 local function hunger_update()
30 for _,player in ipairs(minetest.get_connected_players()) do
31 local meta = player:get_meta()
32 local satiation = meta:get_int("satiation")
33 local hunger = meta:get_int("hunger")
34 local running = (meta:get_string("player.player_movement_state") == "1")
35 local bunny_hopping = (meta:get_string("player.player_movement_state") == "2")
36 local sneaking = (meta:get_string("player.player_movement_state") == "3")
37 local got_hungry = math.random()
39 if running and got_hungry > 0.95 then
40 satiation = satiation - 1
41 elseif bunny_hopping and got_hungry > 0.90 then
42 satiation = satiation - 1
43 elseif sneaking and got_hungry > 0.997 then
44 satiation = satiation - 1
45 elseif got_hungry > 0.998 then
46 satiation = satiation - 1
50 if satiation == 0 then
52 if running and got_hungry > 0.82 then
54 elseif bunny_hopping and got_hungry > 0.77 then
56 elseif sneaking and got_hungry > 0.954 then
58 elseif got_hungry > 0.958 then
62 meta:set_int("hunger", hunger)
64 local hp = player:get_hp()
71 local hp = player:get_hp()
72 if hunger >= 20 and hp < 20 then
74 satiation = satiation - 1
80 meta:set_int("satiation", satiation)
81 local hunger_bar = meta:get_int("hunger_bar")
82 player:hud_change(hunger_bar, "number", hunger)
85 minetest.after(1, function()
92 --allow players to eat food
93 function minetest.eat_food(player,item)
94 local meta = player:get_meta()
96 local player_hunger = meta:get_int("hunger")
97 local player_satiation = meta:get_int("satiation")
100 if type(item) == "string" then
101 item = ItemStack(item)
102 elseif type(item) == "table" then
103 item = ItemStack(item.name)
106 item = item:get_name()
108 local satiation = minetest.get_item_group(item, "satiation")
109 local hunger = minetest.get_item_group(item, "hunger")
111 if player_hunger < 20 then
112 player_hunger = player_hunger + hunger
113 if player_hunger > 20 then
117 if player_satiation < 20 then
118 player_satiation = player_satiation + satiation
119 if player_satiation > 20 then
120 player_satiation = 20
124 meta:set_int("hunger", player_hunger)
125 meta:set_int("satiation", player_satiation)
126 local hunger_bar = meta:get_int("hunger_bar")
127 player:hud_change(hunger_bar, "number", player_hunger)
128 local stack = player:get_wielded_item()
130 player:set_wielded_item(stack)