incrementaltp: respect physics overrides
[waspsaliva.git] / clientmods / dragonfire / inventory / init.lua
blob22131039d37eef511efe31056af7e5ce70023c4b
1 local drop_action = InventoryAction("drop")
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)
10 minetest.register_globalstep(function(dtime)
11 local player = minetest.localplayer
12 if not player then return end
13 local item = player:get_wielded_item()
14 local itemdef = minetest.get_item_def(item:get_name())
15 local wieldindex = player:get_wield_index()
16 -- AutoRefill
17 if minetest.settings:get_bool("autorefill") and itemdef then
18 local space = item:get_free_space()
19 local i = minetest.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 -- AutoPlanks (Strip in DF)
29 if minetest.settings:get_bool("autoplanks") 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 minetest.settings:get_bool("autoeject") then
40 local list = (minetest.settings:get("eject_items") or ""):split(",")
41 local inventory = minetest.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)
51 minetest.register_list_command("eject", "Configure AutoEject", "eject_items")
53 -- AutoTool
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 and 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
71 local function find_best_tool(nodename, switch)
72 local player = minetest.localplayer
73 local inventory = minetest.get_inventory("current_player")
74 local node_groups = minetest.get_node_def(nodename).groups
75 local new_index = player:get_wield_index()
76 local is_better, best_time = false, math.huge
78 is_better, best_time = check_tool(player:get_wielded_item(), node_groups, best_time)
79 if inventory.hand then
80 is_better, best_time = check_tool(inventory.hand[1], node_groups, best_time)
81 end
83 for index, stack in ipairs(inventory.main) do
84 is_better, best_time = check_tool(stack, node_groups, best_time)
85 if is_better then
86 new_index = index
87 end
88 end
90 return new_index
91 end
93 function minetest.select_best_tool(nodename)
94 minetest.localplayer:set_wield_index(find_best_tool(nodename))
95 end
97 local new_index, old_index, pointed_pos
99 minetest.register_on_punchnode(function(pos, node)
100 if minetest.settings:get_bool("autotool") then
101 pointed_pos = pos
102 old_index = old_index or minetest.localplayer:get_wield_index()
103 new_index = find_best_tool(node.name)
105 end)
107 minetest.register_globalstep(function()
108 local player = minetest.localplayer
109 if not new_index then return end
110 if minetest.settings:get_bool("autotool") then
111 local pt = minetest.get_pointed_thing()
112 if pt and pt.type == "node" and vector.equals(minetest.get_pointed_thing_position(pt), pointed_pos) and player:get_control().dig then
113 player:set_wield_index(new_index)
114 return
117 player:set_wield_index(old_index)
118 new_index, old_index, pointed_pos = nil
119 end)
121 -- Enderchest
123 function get_itemslot_bg(x, y, w, h)
124 local out = ""
125 for i = 0, w - 1, 1 do
126 for j = 0, h - 1, 1 do
127 out = out .."image["..x+i..","..y+j..";1,1;mcl_formspec_itemslot.png]"
130 return out
133 local enderchest_formspec = "size[9,8.75]"..
134 "label[0,0;"..minetest.formspec_escape(minetest.colorize("#313131", "Ender Chest")).."]"..
135 "list[current_player;enderchest;0,0.5;9,3;]"..
136 get_itemslot_bg(0,0.5,9,3)..
137 "label[0,4.0;"..minetest.formspec_escape(minetest.colorize("#313131", "Inventory")).."]"..
138 "list[current_player;main;0,4.5;9,3;9]"..
139 get_itemslot_bg(0,4.5,9,3)..
140 "list[current_player;main;0,7.74;9,1;]"..
141 get_itemslot_bg(0,7.74,9,1)..
142 "listring[current_player;enderchest]"..
143 "listring[current_player;main]"
145 function minetest.open_enderchest()
146 minetest.show_formspec("inventory:enderchest", enderchest_formspec)
149 -- HandSlot
151 local hand_formspec = "size[9,8.75]"..
152 "label[0,0;"..minetest.formspec_escape(minetest.colorize("#313131", "Hand")).."]"..
153 "list[current_player;hand;0,0.5;1,1;]"..
154 get_itemslot_bg(0,0.5,1,1)..
155 "label[0,4.0;"..minetest.formspec_escape(minetest.colorize("#313131", "Inventory")).."]"..
156 "list[current_player;main;0,4.5;9,3;9]"..
157 get_itemslot_bg(0,4.5,9,3)..
158 "list[current_player;main;0,7.74;9,1;]"..
159 get_itemslot_bg(0,7.74,9,1)..
160 "listring[current_player;hand]"..
161 "listring[current_player;main]"
163 function minetest.open_handslot()
164 minetest.show_formspec("inventory:hand", hand_formspec)
167 minetest.register_cheat("AutoEject", "Inventory", "autoeject")
168 minetest.register_cheat("AutoTool", "Inventory", "autotool")
169 minetest.register_cheat("Hand", "Inventory", minetest.open_handslot)
170 minetest.register_cheat("Enderchest", "Inventory", minetest.open_enderchest)
171 minetest.register_cheat("AutoPlanks", "Inventory", "autoplanks")
172 minetest.register_cheat("AutoRefill", "Inventory", "autorefill")
174 local modname = minetest.get_current_modname()
175 local modpath = minetest.get_modpath(modname)
177 dofile(modpath .. "/next_item.lua")
178 dofile(modpath .. "/invhack.lua")