Skip to content

Commit 149d8fc

Browse files
authoredAug 27, 2021
Add group-based tool filtering for node drops (#10141)
Supports both AND and OR requirements, e.g. * "a tool that's in any of these groups" * "a tool that's in all of these groups"
1 parent d36dca3 commit 149d8fc

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed
 

Diff for: ‎builtin/game/item.lua

+34-1
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,18 @@ function core.strip_param2_color(param2, paramtype2)
175175
return param2
176176
end
177177

178+
local function has_all_groups(tbl, required_groups)
179+
if type(required_groups) == "string" then
180+
return (tbl[required_groups] or 0) ~= 0
181+
end
182+
for _, group in ipairs(required_groups) do
183+
if (tbl[group] or 0) == 0 then
184+
return false
185+
end
186+
end
187+
return true
188+
end
189+
178190
function core.get_node_drops(node, toolname)
179191
-- Compatibility, if node is string
180192
local nodename = node
@@ -214,7 +226,7 @@ function core.get_node_drops(node, toolname)
214226
if item.rarity ~= nil then
215227
good_rarity = item.rarity < 1 or math.random(item.rarity) == 1
216228
end
217-
if item.tools ~= nil then
229+
if item.tools ~= nil or item.tool_groups ~= nil then
218230
good_tool = false
219231
end
220232
if item.tools ~= nil and toolname then
@@ -229,6 +241,27 @@ function core.get_node_drops(node, toolname)
229241
end
230242
end
231243
end
244+
if item.tool_groups ~= nil and toolname then
245+
local tooldef = core.registered_items[toolname]
246+
if tooldef ~= nil and type(tooldef.groups) == "table" then
247+
if type(item.tool_groups) == "string" then
248+
-- tool_groups can be a string which specifies the required group
249+
good_tool = core.get_item_group(toolname, item.tool_groups) ~= 0
250+
else
251+
-- tool_groups can be a list of sufficient requirements.
252+
-- i.e. if any item in the list can be satisfied then the tool is good
253+
assert(type(item.tool_groups) == "table")
254+
for _, required_groups in ipairs(item.tool_groups) do
255+
-- required_groups can be either a string (a single group),
256+
-- or an array of strings where all must be in tooldef.groups
257+
good_tool = has_all_groups(tooldef.groups, required_groups)
258+
if good_tool then
259+
break
260+
end
261+
end
262+
end
263+
end
264+
end
232265
if good_rarity and good_tool then
233266
got_count = got_count + 1
234267
for _, add_item in ipairs(item.items) do

Diff for: ‎doc/lua_api.txt

+12-2
Original file line numberDiff line numberDiff line change
@@ -7798,14 +7798,24 @@ Used by `minetest.register_node`.
77987798
inherit_color = true,
77997799
},
78007800
{
7801-
-- Only drop if using a item whose name contains
7801+
-- Only drop if using an item whose name contains
78027802
-- "default:shovel_" (this item filtering by string matching
7803-
-- is deprecated).
7803+
-- is deprecated, use tool_groups instead).
78047804
tools = {"~default:shovel_"},
78057805
rarity = 2,
78067806
-- The item list dropped.
78077807
items = {"default:sand", "default:desert_sand"},
78087808
},
7809+
{
7810+
-- Only drop if using an item in the "magicwand" group, or
7811+
-- an item that is in both the "pickaxe" and the "lucky"
7812+
-- groups.
7813+
tool_groups = {
7814+
"magicwand",
7815+
{"pickaxe", "lucky"}
7816+
},
7817+
items = {"default:coal_lump"},
7818+
},
78097819
},
78107820
},
78117821

0 commit comments

Comments
 (0)
Please sign in to comment.