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