mirror of
https://github.com/minetest-mods/unified_inventory.git
synced 2024-08-20 21:34:09 +02:00
Tidy and comment filtering functions
This commit is contained in:
parent
177debd13c
commit
9533200e25
80
api.lua
80
api.lua
@ -2,48 +2,59 @@ local S = minetest.get_translator("unified_inventory")
|
|||||||
local F = minetest.formspec_escape
|
local F = minetest.formspec_escape
|
||||||
local ui = unified_inventory
|
local ui = unified_inventory
|
||||||
|
|
||||||
|
local function is_recipe_craftable(recipe)
|
||||||
|
-- Ensure the ingedients exist
|
||||||
|
for _, itemname in pairs(recipe.items) do
|
||||||
|
local groups = string.find(itemname, "group:")
|
||||||
|
if groups then
|
||||||
|
if not ui.get_group_item(string.sub(groups, 8)).item then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
else
|
||||||
|
-- Possibly an item
|
||||||
|
if not minetest.registered_items[itemname]
|
||||||
|
or minetest.get_item_group(itemname, "not_in_craft_guide") ~= 0 then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
|
||||||
-- Create detached creative inventory after loading all mods
|
-- Create detached creative inventory after loading all mods
|
||||||
minetest.after(0.01, function()
|
minetest.after(0.01, function()
|
||||||
local rev_aliases = {}
|
local rev_aliases = {}
|
||||||
for source, target in pairs(minetest.registered_aliases) do
|
for original, newname in pairs(minetest.registered_aliases) do
|
||||||
if not rev_aliases[target] then rev_aliases[target] = {} end
|
if not rev_aliases[newname] then
|
||||||
table.insert(rev_aliases[target], source)
|
rev_aliases[newname] = {}
|
||||||
end
|
end
|
||||||
|
table.insert(rev_aliases[newname], original)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Filtered item list
|
||||||
ui.items_list = {}
|
ui.items_list = {}
|
||||||
for name, def in pairs(minetest.registered_items) do
|
for name, def in pairs(minetest.registered_items) do
|
||||||
if (not def.groups.not_in_creative_inventory or
|
if ui.is_itemdef_listable(def) then
|
||||||
def.groups.not_in_creative_inventory == 0) and
|
|
||||||
def.description and def.description ~= "" then
|
|
||||||
table.insert(ui.items_list, name)
|
table.insert(ui.items_list, name)
|
||||||
|
|
||||||
|
-- Alias processing: Find recipes that belong to the current item name
|
||||||
local all_names = rev_aliases[name] or {}
|
local all_names = rev_aliases[name] or {}
|
||||||
table.insert(all_names, name)
|
table.insert(all_names, name)
|
||||||
for _, player_name in ipairs(all_names) do
|
for _, itemname in ipairs(all_names) do
|
||||||
local recipes = minetest.get_all_craft_recipes(player_name)
|
local recipes = minetest.get_all_craft_recipes(itemname)
|
||||||
if recipes then
|
for _, recipe in ipairs(recipes or {}) do
|
||||||
for _, recipe in ipairs(recipes) do
|
if is_recipe_craftable(recipe) then
|
||||||
|
|
||||||
local unknowns
|
|
||||||
|
|
||||||
for _,chk in pairs(recipe.items) do
|
|
||||||
local groupchk = string.find(chk, "group:")
|
|
||||||
if (not groupchk and not minetest.registered_items[chk])
|
|
||||||
or (groupchk and not ui.get_group_item(string.gsub(chk, "group:", "")).item)
|
|
||||||
or minetest.get_item_group(chk, "not_in_craft_guide") ~= 0 then
|
|
||||||
unknowns = true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if not unknowns then
|
|
||||||
ui.register_craft(recipe)
|
ui.register_craft(recipe)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
table.sort(ui.items_list)
|
table.sort(ui.items_list)
|
||||||
ui.items_list_size = #ui.items_list
|
ui.items_list_size = #ui.items_list
|
||||||
print("Unified Inventory. Inventory size: "..ui.items_list_size)
|
print("Unified Inventory. Inventory size: "..ui.items_list_size)
|
||||||
|
|
||||||
|
-- Analyse dropped items -> custom "digging" recipes
|
||||||
for _, name in ipairs(ui.items_list) do
|
for _, name in ipairs(ui.items_list) do
|
||||||
local def = minetest.registered_items[name]
|
local def = minetest.registered_items[name]
|
||||||
-- Simple drops
|
-- Simple drops
|
||||||
@ -201,8 +212,8 @@ minetest.after(0.01, function()
|
|||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
---------------- Home API ----------------
|
||||||
|
|
||||||
-- load_home
|
|
||||||
local function load_home()
|
local function load_home()
|
||||||
local input = io.open(ui.home_filename, "r")
|
local input = io.open(ui.home_filename, "r")
|
||||||
if not input then
|
if not input then
|
||||||
@ -219,6 +230,7 @@ local function load_home()
|
|||||||
end
|
end
|
||||||
io.close(input)
|
io.close(input)
|
||||||
end
|
end
|
||||||
|
|
||||||
load_home()
|
load_home()
|
||||||
|
|
||||||
function ui.set_home(player, pos)
|
function ui.set_home(player, pos)
|
||||||
@ -247,7 +259,8 @@ function ui.go_home(player)
|
|||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
-- register_craft
|
---------------- Crafting API ----------------
|
||||||
|
|
||||||
function ui.register_craft(options)
|
function ui.register_craft(options)
|
||||||
if not options.output then
|
if not options.output then
|
||||||
return
|
return
|
||||||
@ -270,14 +283,12 @@ function ui.register_craft(options)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local craft_type_defaults = {
|
local craft_type_defaults = {
|
||||||
width = 3,
|
width = 3,
|
||||||
height = 3,
|
height = 3,
|
||||||
uses_crafting_grid = false,
|
uses_crafting_grid = false,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function ui.craft_type_defaults(name, options)
|
function ui.craft_type_defaults(name, options)
|
||||||
if not options.description then
|
if not options.description then
|
||||||
options.description = name
|
options.description = name
|
||||||
@ -288,8 +299,7 @@ end
|
|||||||
|
|
||||||
|
|
||||||
function ui.register_craft_type(name, options)
|
function ui.register_craft_type(name, options)
|
||||||
ui.registered_craft_types[name] =
|
ui.registered_craft_types[name] = ui.craft_type_defaults(name, options)
|
||||||
ui.craft_type_defaults(name, options)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@ -346,6 +356,8 @@ ui.register_craft_type("digging_chance", {
|
|||||||
height = 1,
|
height = 1,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
---------------- GUI registrations ----------------
|
||||||
|
|
||||||
function ui.register_page(name, def)
|
function ui.register_page(name, def)
|
||||||
ui.pages[name] = def
|
ui.pages[name] = def
|
||||||
end
|
end
|
||||||
@ -361,6 +373,8 @@ function ui.register_button(name, def)
|
|||||||
table.insert(ui.buttons, def)
|
table.insert(ui.buttons, def)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---------------- Callback registrations ----------------
|
||||||
|
|
||||||
function ui.register_on_initialized(callback)
|
function ui.register_on_initialized(callback)
|
||||||
if type(callback) ~= "function" then
|
if type(callback) ~= "function" then
|
||||||
error(("Initialized callback must be a function, %s given."):format(type(callback)))
|
error(("Initialized callback must be a function, %s given."):format(type(callback)))
|
||||||
@ -375,6 +389,8 @@ function ui.register_on_craft_registered(callback)
|
|||||||
table.insert(ui.craft_registered_callbacks, callback)
|
table.insert(ui.craft_registered_callbacks, callback)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---------------- List getters ----------------
|
||||||
|
|
||||||
function ui.get_recipe_list(output)
|
function ui.get_recipe_list(output)
|
||||||
return ui.crafts_for.recipe[output]
|
return ui.crafts_for.recipe[output]
|
||||||
end
|
end
|
||||||
@ -387,11 +403,15 @@ function ui.get_registered_outputs()
|
|||||||
return outputs
|
return outputs
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---------------- Player utilities ----------------
|
||||||
|
|
||||||
function ui.is_creative(playername)
|
function ui.is_creative(playername)
|
||||||
return minetest.check_player_privs(playername, {creative=true})
|
return minetest.check_player_privs(playername, {creative=true})
|
||||||
or minetest.settings:get_bool("creative_mode")
|
or minetest.settings:get_bool("creative_mode")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---------------- Formspec helpers ----------------
|
||||||
|
|
||||||
function ui.single_slot(xpos, ypos, bright)
|
function ui.single_slot(xpos, ypos, bright)
|
||||||
return string.format("background9[%f,%f;%f,%f;ui_single_slot%s.png;false;16]",
|
return string.format("background9[%f,%f;%f,%f;ui_single_slot%s.png;false;16]",
|
||||||
xpos, ypos, ui.imgscale, ui.imgscale, (bright and "_bright" or "") )
|
xpos, ypos, ui.imgscale, ui.imgscale, (bright and "_bright" or "") )
|
||||||
|
@ -96,6 +96,9 @@ function unified_inventory.register_category(category_name, config)
|
|||||||
end
|
end
|
||||||
update_category_list()
|
update_category_list()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- TODO: Mark these for removal. They are pretty much useless
|
||||||
|
|
||||||
function unified_inventory.set_category_symbol(category_name, symbol)
|
function unified_inventory.set_category_symbol(category_name, symbol)
|
||||||
ensure_category_exists(category_name)
|
ensure_category_exists(category_name)
|
||||||
unified_inventory.registered_categories[category_name].symbol = symbol
|
unified_inventory.registered_categories[category_name].symbol = symbol
|
||||||
|
33
internal.lua
33
internal.lua
@ -329,7 +329,7 @@ function ui.set_inventory_formspec(player, page)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function valid_def(def)
|
function ui.is_itemdef_listable(def)
|
||||||
return (not def.groups.not_in_creative_inventory
|
return (not def.groups.not_in_creative_inventory
|
||||||
or def.groups.not_in_creative_inventory == 0)
|
or def.groups.not_in_creative_inventory == 0)
|
||||||
and def.description
|
and def.description
|
||||||
@ -342,9 +342,11 @@ function ui.apply_filter(player, filter, search_dir)
|
|||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
local player_name = player:get_player_name()
|
local player_name = player:get_player_name()
|
||||||
|
|
||||||
local lfilter = string.lower(filter)
|
local lfilter = string.lower(filter)
|
||||||
local ffilter
|
local ffilter
|
||||||
if lfilter:sub(1, 6) == "group:" then
|
if lfilter:sub(1, 6) == "group:" then
|
||||||
|
-- Group filter: all groups of the item must match
|
||||||
local groups = lfilter:sub(7):split(",")
|
local groups = lfilter:sub(7):split(",")
|
||||||
ffilter = function(name, def)
|
ffilter = function(name, def)
|
||||||
for _, group in ipairs(groups) do
|
for _, group in ipairs(groups) do
|
||||||
@ -356,6 +358,7 @@ function ui.apply_filter(player, filter, search_dir)
|
|||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
|
-- Name filter: fuzzy match item names and descriptions
|
||||||
local player_info = minetest.get_player_information(player_name)
|
local player_info = minetest.get_player_information(player_name)
|
||||||
local lang = player_info and player_info.lang_code or ""
|
local lang = player_info and player_info.lang_code or ""
|
||||||
|
|
||||||
@ -368,35 +371,41 @@ function ui.apply_filter(player, filter, search_dir)
|
|||||||
or llocaldesc and string.find(llocaldesc, lfilter, 1, true)
|
or llocaldesc and string.find(llocaldesc, lfilter, 1, true)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
ui.filtered_items_list[player_name]={}
|
|
||||||
|
local is_itemdef_listable = ui.is_itemdef_listable
|
||||||
|
local filtered_items = {}
|
||||||
|
|
||||||
local category = ui.current_category[player_name] or 'all'
|
local category = ui.current_category[player_name] or 'all'
|
||||||
if category == 'all' then
|
if category == 'all' then
|
||||||
for name, def in pairs(minetest.registered_items) do
|
for name, def in pairs(minetest.registered_items) do
|
||||||
if valid_def(def)
|
if is_itemdef_listable(def)
|
||||||
and ffilter(name, def) then
|
and ffilter(name, def) then
|
||||||
table.insert(ui.filtered_items_list[player_name], name)
|
table.insert(filtered_items, name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
elseif category == 'uncategorized' then
|
elseif category == 'uncategorized' then
|
||||||
for name, def in pairs(minetest.registered_items) do
|
for name, def in pairs(minetest.registered_items) do
|
||||||
if (not ui.find_category(name))
|
if is_itemdef_listable(def)
|
||||||
and valid_def(def)
|
and not ui.find_category(name)
|
||||||
and ffilter(name, def) then
|
and ffilter(name, def) then
|
||||||
table.insert(ui.filtered_items_list[player_name], name)
|
table.insert(filtered_items, name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
for name,exists in pairs(ui.registered_category_items[category]) do
|
-- Any other category is selected
|
||||||
|
for name, exists in pairs(ui.registered_category_items[category]) do
|
||||||
local def = minetest.registered_items[name]
|
local def = minetest.registered_items[name]
|
||||||
if exists and def
|
if exists and def
|
||||||
and valid_def(def)
|
and is_itemdef_listable(def)
|
||||||
and ffilter(name, def) then
|
and ffilter(name, def) then
|
||||||
table.insert(ui.filtered_items_list[player_name], name)
|
table.insert(filtered_items, name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
table.sort(ui.filtered_items_list[player_name])
|
table.sort(filtered_items)
|
||||||
ui.filtered_items_list_size[player_name] = #ui.filtered_items_list[player_name]
|
|
||||||
|
ui.filtered_items_list_size[player_name] = #filtered_items
|
||||||
|
ui.filtered_items_list[player_name] = filtered_items
|
||||||
ui.current_index[player_name] = 1
|
ui.current_index[player_name] = 1
|
||||||
ui.activefilter[player_name] = filter
|
ui.activefilter[player_name] = filter
|
||||||
ui.active_search_direction[player_name] = search_dir
|
ui.active_search_direction[player_name] = search_dir
|
||||||
|
Loading…
Reference in New Issue
Block a user