mirror of
https://github.com/luanti-org/minetest_game.git
synced 2025-05-21 06:43:17 -04:00
Hide uncraftable recipes
This commit is contained in:
parent
f63b1fa055
commit
8e903813e2
1 changed files with 52 additions and 33 deletions
|
@ -19,15 +19,6 @@ local function table_replace(t, val, new)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function item_in_recipe(item, recipe)
|
|
||||||
for _, recipe_item in pairs(recipe.items) do
|
|
||||||
if recipe_item == item then
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
local function extract_groups(str)
|
local function extract_groups(str)
|
||||||
if str:sub(1, 6) == "group:" then
|
if str:sub(1, 6) == "group:" then
|
||||||
return str:sub(7):split()
|
return str:sub(7):split()
|
||||||
|
@ -43,6 +34,53 @@ local function item_has_groups(item_groups, groups)
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function groups_to_item(groups)
|
||||||
|
if #groups == 1 then
|
||||||
|
local group = groups[1]
|
||||||
|
if group_stereotypes[group] then
|
||||||
|
return group_stereotypes[group]
|
||||||
|
elseif minetest.registered_items["default:"..group] then
|
||||||
|
return "default:"..group
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
for name, def in pairs(minetest.registered_items) do
|
||||||
|
if item_has_groups(def.groups, groups) then
|
||||||
|
return name
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return ":unknown"
|
||||||
|
end
|
||||||
|
|
||||||
|
local function get_craftable_recipes(output)
|
||||||
|
local recipes = minetest.get_all_craft_recipes(output) or {}
|
||||||
|
|
||||||
|
for i = #recipes, 1, -1 do
|
||||||
|
for _, item in pairs(recipes[i].items) do
|
||||||
|
local groups = extract_groups(item)
|
||||||
|
if groups then
|
||||||
|
item = groups_to_item(groups)
|
||||||
|
end
|
||||||
|
if not minetest.registered_items[item] then
|
||||||
|
table.remove(recipes, i)
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return recipes
|
||||||
|
end
|
||||||
|
|
||||||
|
local function item_in_recipe(item, recipe)
|
||||||
|
for _, recipe_item in pairs(recipe.items) do
|
||||||
|
if recipe_item == item then
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
-- If item can be used in recipe because recipe takes a `group:` item that item
|
-- If item can be used in recipe because recipe takes a `group:` item that item
|
||||||
-- matches, return a copy of recipe with the `group:` item replaced with item.
|
-- matches, return a copy of recipe with the `group:` item replaced with item.
|
||||||
local function groups_item_in_recipe(item, recipe)
|
local function groups_item_in_recipe(item, recipe)
|
||||||
|
@ -74,19 +112,19 @@ local function get_usages(item)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return #usages > 0 and usages
|
return usages
|
||||||
end
|
end
|
||||||
|
|
||||||
minetest.register_on_mods_loaded(function()
|
minetest.register_on_mods_loaded(function()
|
||||||
for name, def in pairs(minetest.registered_items) do
|
for name, def in pairs(minetest.registered_items) do
|
||||||
if def.groups.not_in_craft_guide ~= 1 and def.description ~= "" then
|
if def.groups.not_in_craft_guide ~= 1 and def.description ~= "" then
|
||||||
recipes_cache[name] = minetest.get_all_craft_recipes(name)
|
recipes_cache[name] = get_craftable_recipes(name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
for name, def in pairs(minetest.registered_items) do
|
for name, def in pairs(minetest.registered_items) do
|
||||||
if def.groups.not_in_craft_guide ~= 1 and def.description ~= "" then
|
if def.groups.not_in_craft_guide ~= 1 and def.description ~= "" then
|
||||||
usages_cache[name] = get_usages(name)
|
usages_cache[name] = get_usages(name)
|
||||||
if recipes_cache[name] or usages_cache[name] then
|
if #recipes_cache[name] > 0 or #usages_cache[name] > 0 then
|
||||||
table.insert(init_items, name)
|
table.insert(init_items, name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -94,25 +132,6 @@ minetest.register_on_mods_loaded(function()
|
||||||
table.sort(init_items)
|
table.sort(init_items)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
local function groups_to_item(groups)
|
|
||||||
if #groups == 1 then
|
|
||||||
local group = groups[1]
|
|
||||||
if group_stereotypes[group] then
|
|
||||||
return group_stereotypes[group]
|
|
||||||
elseif minetest.registered_items["default:"..group] then
|
|
||||||
return "default:"..group
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
for name, def in pairs(minetest.registered_items) do
|
|
||||||
if item_has_groups(def.groups, groups) then
|
|
||||||
return name
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
return ":unknown"
|
|
||||||
end
|
|
||||||
|
|
||||||
local function is_fuel(item)
|
local function is_fuel(item)
|
||||||
return minetest.get_craft_result({
|
return minetest.get_craft_result({
|
||||||
method = "fuel",
|
method = "fuel",
|
||||||
|
@ -330,9 +349,9 @@ local function on_receive_fields(player, fields)
|
||||||
data.show_usages = nil
|
data.show_usages = nil
|
||||||
end
|
end
|
||||||
if data.show_usages then
|
if data.show_usages then
|
||||||
data.recipes = usages_cache[item] or {}
|
data.recipes = usages_cache[item]
|
||||||
else
|
else
|
||||||
data.recipes = recipes_cache[item] or {}
|
data.recipes = recipes_cache[item]
|
||||||
end
|
end
|
||||||
data.prev_item = item
|
data.prev_item = item
|
||||||
data.rnum = 1
|
data.rnum = 1
|
||||||
|
|
Loading…
Add table
Reference in a new issue