]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/client/cheats/inventory.lua
World Cheats improvements; Add BlockLava; Readd minetest.request_http_api for Compati...
[dragonfireclient.git] / builtin / client / cheats / inventory.lua
1 local drop_action = InventoryAction("drop")
2
3 local strip_move_act = InventoryAction("move")
4 strip_move_act:to("current_player", "craft", 1)
5 local strip_craft_act = InventoryAction("craft")
6 strip_craft_act:craft("current_player")
7 local strip_move_back_act = InventoryAction("move")
8 strip_move_back_act:from("current_player", "craftresult", 1)
9
10 core.register_globalstep(function(dtime)
11         local player = core.localplayer
12         if not player then return end
13         local item = player:get_wielded_item()
14         local itemdef = core.get_item_def(item:get_name())
15         local wieldindex = player:get_wield_index()
16         -- AutoRefill
17         if core.settings:get_bool("autorefill") and itemdef then
18                 local space = item:get_free_space()
19                 local i = core.find_item(item:get_name(), wieldindex + 1)
20                 if i and space > 0 then
21                         local move_act = InventoryAction("move")
22                         move_act:to("current_player", "main", wieldindex)
23                         move_act:from("current_player", "main", i)
24                         move_act:set_count(space)
25                         move_act:apply()
26                 end
27         end
28         -- Strip
29         if core.settings:get_bool("strip") then
30                 if itemdef and itemdef.groups.tree and player:get_control().place then
31                         strip_move_act:from("current_player", "main", wieldindex)
32                         strip_move_back_act:to("current_player", "main", wieldindex)
33                         strip_move_act:apply()
34                         strip_craft_act:apply()
35                         strip_move_back_act:apply()
36                 end
37         end
38         -- AutoEject
39         if core.settings:get_bool("autoeject") then
40                 local list = (core.settings:get("eject_items") or ""):split(",")
41                 local inventory = core.get_inventory("current_player")
42                 for index, stack in pairs(inventory.main) do
43                         if table.indexof(list, stack:get_name()) ~= -1 then
44                                 drop_action:from("current_player", "main", index)
45                                 drop_action:apply()
46                         end
47                 end
48         end
49 end)
50
51 core.register_list_command("eject", "Configure AutoEject", "eject_items")
52
53 -- AutoTool
54
55 local function check_tool(stack, node_groups, old_best_time)
56         local toolcaps = stack:get_tool_capabilities()
57         if not toolcaps then return end
58         local best_time = old_best_time
59         for group, groupdef in pairs(toolcaps.groupcaps) do
60                 local level = node_groups[group]
61                 if level then
62                         local this_time = groupdef.times[level]
63                         if this_time < best_time then
64                                 best_time = this_time
65                         end
66                 end
67         end
68         return best_time < old_best_time, best_time
69 end
70
71 local function find_best_tool(nodename)
72         local player = core.localplayer
73         local inventory = core.get_inventory("current_player")
74         local node_groups = core.get_node_def(nodename).groups
75         local new_index = player:get_wield_index()
76         local is_better, best_time = false, math.huge
77         is_better, best_time = check_tool(player:get_wielded_item(), node_groups, best_time)
78         is_better, best_time = check_tool(inventory.hand[1], node_groups, best_time)
79         for index, stack in ipairs(inventory.main) do
80                 is_better, best_time = check_tool(stack, node_groups, best_time)
81                 if is_better then
82                         new_index = index
83                 end
84         end
85         return new_index
86 end
87
88 function core.select_best_tool(nodename)
89         core.localplayer:set_wield_index(find_best_tool(nodename))
90 end
91
92 local new_index, old_index, pointed_pos
93
94 core.register_on_punchnode(function(pos, node)
95         if core.settings:get_bool("autotool") then
96                 pointed_pos = pos
97                 old_index = old_index or core.localplayer:get_wield_index()
98                 new_index = find_best_tool(node.name)
99         end
100 end)
101
102 core.register_globalstep(function()
103         local player = core.localplayer
104         if not new_index then return end
105         if core.settings:get_bool("autotool") then
106                 local pt = core.get_pointed_thing()
107                 if pt and pt.type == "node" and vector.equals(core.get_pointed_thing_position(pt), pointed_pos) and player:get_control().dig then
108                         player:set_wield_index(new_index)
109                         return
110                 end
111         end
112         player:set_wield_index(old_index)
113         new_index, old_index, pointed_pos = nil
114 end)
115
116 -- Enderchest
117
118 function get_itemslot_bg(x, y, w, h)
119         local out = ""
120         for i = 0, w - 1, 1 do
121                 for j = 0, h - 1, 1 do
122                         out = out .."image["..x+i..","..y+j..";1,1;mcl_formspec_itemslot.png]"
123                 end
124         end
125         return out
126 end
127
128 local enderchest_formspec = "size[9,8.75]"..
129         "label[0,0;"..minetest.formspec_escape(minetest.colorize("#313131", "Ender Chest")).."]"..
130         "list[current_player;enderchest;0,0.5;9,3;]"..
131         get_itemslot_bg(0,0.5,9,3)..
132         "label[0,4.0;"..minetest.formspec_escape(minetest.colorize("#313131", "Inventory")).."]"..
133         "list[current_player;main;0,4.5;9,3;9]"..
134         get_itemslot_bg(0,4.5,9,3)..
135         "list[current_player;main;0,7.74;9,1;]"..
136         get_itemslot_bg(0,7.74,9,1)..
137         "listring[current_player;enderchest]"..
138         "listring[current_player;main]"
139
140 function core.open_enderchest()
141         core.show_formspec("__builtin__:enderchest", enderchest_formspec)
142 end
143
144 -- HandSlot
145
146 local hand_formspec = "size[9,8.75]"..
147         "label[0,0;"..minetest.formspec_escape(minetest.colorize("#313131", "Hand")).."]"..
148         "list[current_player;hand;0,0.5;1,1;]"..
149         get_itemslot_bg(0,0.5,1,1)..
150         "label[0,4.0;"..minetest.formspec_escape(minetest.colorize("#313131", "Inventory")).."]"..
151         "list[current_player;main;0,4.5;9,3;9]"..
152         get_itemslot_bg(0,4.5,9,3)..
153         "list[current_player;main;0,7.74;9,1;]"..
154         get_itemslot_bg(0,7.74,9,1)..
155         "listring[current_player;hand]"..
156         "listring[current_player;main]"
157         
158 function core.open_handslot()
159         core.show_formspec("__builtin__:hand", hand_formspec)
160 end
161
162
163