]> git.lizzy.rs Git - autoeat.git/blob - init.lua
Add Cheat Menu entry
[autoeat.git] / init.lua
1 autoeat = {}
2 autoeat.lock = false
3
4 local autodupe = rawget(_G, "autodupe")
5 local hud_id = nil
6
7 local function get_float(name, default)
8         return tonumber(minetest.settings:get("autoeat_" .. name) or "") or default
9 end
10
11 local etime = 0
12
13 function autoeat.eat()
14         local food_index
15         local food_count = 0
16         for index, stack in pairs(minetest.get_inventory("current_player").main) do
17                 local stackname = stack:get_name()
18                 if stackname ~= "" then
19                         local def = minetest.get_item_def(stackname)
20                         if def and def.groups.food then
21                                 food_count = food_count + 1
22                                 if food_index then
23                                         break
24                                 end
25                                 food_index = index
26                         end
27                 end
28         end
29         if food_index then
30                 if food_count == 1 and autodupe then
31                         autodupe.needed(food_index)
32                         autoeat.lock = true
33                 else
34                         local player = minetest.localplayer
35                         local old_index = player:get_wield_index()
36                         player:set_wield_index(food_index)
37                         minetest.interact("activate", {type = "nothing"})
38                         player:set_wield_index(old_index)
39                         autoeat.lock = false
40                 end
41         end
42 end
43
44 function autoeat.get_hunger()
45         if hud_id then
46                 return minetest.localplayer:hud_get(hud_id).number
47         else
48                 return 20
49         end
50 end
51
52 minetest.register_globalstep(function(dtime)
53         if not minetest.localplayer then return end
54         etime = etime + dtime
55         if autoeat.lock or minetest.settings:get_bool("autoeat") and etime >= get_float("cooldown", 0.5) and autoeat.get_hunger() < get_float("hunger", 9) then
56                 etime = 0
57                 autoeat.eat()
58         end
59 end)
60
61 minetest.after(3, function()
62         local player = minetest.localplayer
63         local def
64         local i = -1
65         repeat
66                 i = i + 1
67                 def = player:hud_get(i)
68         until not def or def.text == "hbhunger_icon.png"
69         if def then
70                 hud_id = i
71         end
72 end)
73
74 minetest.register_cheat("AutoEat", "Player", "autoeat")