mcl_inventory creative fixes

- Add many comments (I had a really hard time understanding the code)
- Add some more type annotations (https://github.com/minetest-toolkit/minetest-lsp-api)
- Rename non english variable
This commit is contained in:
AFCMS 2023-06-29 08:39:58 +02:00
parent 0e13190ea4
commit 4db0631133
No known key found for this signature in database
GPG key ID: 8720389A25B652E3
2 changed files with 90 additions and 33 deletions

View file

@ -3,20 +3,35 @@ local F = minetest.formspec_escape
local C = minetest.colorize
-- Prepare player info table
---@type table<string, {page: string, filter: string, start_i: integer, inv_size: integer}>
local players = {}
-- Containing all the items for each Creative Mode tab
---@type table<string, string[]>
local inventory_lists = {}
--local mod_player = minetest.get_modpath("mcl_player")
-- Create tables
local builtin_filter_ids = { "blocks", "deco", "redstone", "rail", "food", "tools", "combat", "mobs", "brew", "matr",
"misc", "all" }
---@type string[]
local builtin_filter_ids = {
"blocks",
"deco",
"redstone",
"rail",
"food",
"tools",
"combat",
"mobs",
"brew",
"matr",
"misc",
"all"
}
for _, f in pairs(builtin_filter_ids) do
inventory_lists[f] = {}
end
---@param tbl string[]
local function replace_enchanted_books(tbl)
for k, item in ipairs(tbl) do
if item:find("mcl_enchanting:book_enchanted") == 1 then
@ -29,21 +44,24 @@ local function replace_enchanted_books(tbl)
end
end
--[[ Populate all the item tables. We only do this once. Note this code must be
executed after loading all the other mods in order to work. ]]
-- Populate all the item tables. We only do this once.
-- Note this code must be executed after loading all the other mods in order to work.
minetest.register_on_mods_loaded(function()
for name, def in pairs(minetest.registered_items) do
if (not def.groups.not_in_creative_inventory or def.groups.not_in_creative_inventory == 0) and def.description and
def.description ~= "" then
---@param def mt.ItemDef|mt.NodeDef
local function is_redstone(def)
return def.mesecons or def.groups.mesecon or def.groups.mesecon_conductor_craftable or
def.groups.mesecon_effecor_off
end
---@param def mt.ItemDef|mt.NodeDef
local function is_tool(def)
return def.groups.tool or (def.tool_capabilities and def.tool_capabilities.damage_groups == nil)
end
---@param def mt.ItemDef|mt.NodeDef
local function is_weapon_or_armor(def)
return def.groups.weapon or def.groups.weapon_ranged or def.groups.ammo or def.groups.combat_item or
(
@ -120,6 +138,11 @@ minetest.register_on_mods_loaded(function()
end
end)
---@param name string
---@param description string
---@param lang mt.LangCode
---@param filter string
---@return integer
local function filter_item(name, description, lang, filter)
local desc
if not lang then
@ -130,6 +153,8 @@ local function filter_item(name, description, lang, filter)
return string.find(name, filter, nil, true) or string.find(desc, filter, nil, true)
end
---@param filter string
---@param player mt.PlayerObjectRef
local function set_inv_search(filter, player)
local playername = player:get_player_name()
local inv = minetest.get_inventory({ type = "detached", name = "creative_" .. playername })
@ -158,6 +183,8 @@ local function set_inv_search(filter, player)
inv:set_list("main", creative_list)
end
---@param page string
---@param player mt.PlayerObjectRef
local function set_inv_page(page, player)
local playername = player:get_player_name()
local inv = minetest.get_inventory({ type = "detached", name = "creative_" .. playername })
@ -171,6 +198,8 @@ local function set_inv_page(page, player)
inv:set_list("main", creative_list)
end
---@param player mt.PlayerObjectRef
local function init(player)
local playername = player:get_player_name()
minetest.create_detached_inventory("creative_" .. playername, {
@ -208,18 +237,39 @@ local trash = minetest.create_detached_inventory("trash", {
inv:set_stack(listname, index, "")
end,
})
trash:set_size("main", 1)
local noffset = {} -- numeric tab offset
local offset = {} -- string offset:
local boffset = {} --
local hoch = {}
------------------------------
-- Formspec Precalculations --
------------------------------
-- Numeric position of tab background image, indexed by tab name
---@type table<string, {[0]: number, [1]: number}>
local noffset = {}
-- String position of tab button background image, indexed by tab name
---@type table<string, string>
local offset = {}
-- String position of tab button, indexed by tab name
---@type table<string, string>
local boffset = {}
-- Used to determine the tab button background image
---@type table<string, ""|"_down">
local button_bg_postfix = {}
-- Tab caption/tooltip translated string, indexed by tab name
---@type table<string, string>
local filtername = {}
--local bg = {}
local noffset_x_start = 0.2
local noffset_x = noffset_x_start
local noffset_y = -1.34
---@param id string
---@param right? boolean
local function next_noffset(id, right)
if right then
noffset[id] = { 11.3, noffset_y }
@ -254,20 +304,20 @@ for k, v in pairs(noffset) do
boffset[k] = tostring(v[1] + 0.24) .. "," .. tostring(v[2] + 0.25)
end
hoch["blocks"] = ""
hoch["deco"] = ""
hoch["redstone"] = ""
hoch["rail"] = ""
hoch["brew"] = ""
hoch["misc"] = ""
hoch["nix"] = ""
hoch["default"] = ""
hoch["food"] = "_down"
hoch["tools"] = "_down"
hoch["combat"] = "_down"
hoch["mobs"] = "_down"
hoch["matr"] = "_down"
hoch["inv"] = "_down"
button_bg_postfix["blocks"] = ""
button_bg_postfix["deco"] = ""
button_bg_postfix["redstone"] = ""
button_bg_postfix["rail"] = ""
button_bg_postfix["brew"] = ""
button_bg_postfix["misc"] = ""
button_bg_postfix["nix"] = ""
button_bg_postfix["default"] = ""
button_bg_postfix["food"] = "_down"
button_bg_postfix["tools"] = "_down"
button_bg_postfix["combat"] = "_down"
button_bg_postfix["mobs"] = "_down"
button_bg_postfix["matr"] = "_down"
button_bg_postfix["inv"] = "_down"
filtername["blocks"] = S("Building Blocks")
filtername["deco"] = S("Decoration Blocks")
@ -302,6 +352,8 @@ filtername["inv"] = S("Survival Inventory")
bg["default"] = dark_bg
end]]
-- Item name representing a tab, indexed by tab name
---@type table<string, string>
local tab_icon = {
blocks = "mcl_core:brick_block",
deco = "mcl_flowers:peony",
@ -318,13 +370,15 @@ local tab_icon = {
inv = "mcl_chests:chest",
}
---@param player ObjectRef
-- Get the player configured stack size when taking items from creative inventory
---@param player mt.PlayerObjectRef
---@return integer
local function get_stack_size(player)
return player:get_meta():get_int("mcl_inventory:switch_stack")
end
---@param player ObjectRef
-- Set the player configured stack size when taking items from creative inventory
---@param player mt.PlayerObjectRef
---@param n integer
local function set_stack_size(player, n)
player:get_meta():set_int("mcl_inventory:switch_stack", n)
@ -336,6 +390,7 @@ minetest.register_on_joinplayer(function(player)
end
end)
---@param player mt.PlayerObjectRef
function mcl_inventory.set_creative_formspec(player)
local playername = player:get_player_name()
if not players[playername] then return end
@ -459,12 +514,15 @@ function mcl_inventory.set_creative_formspec(player)
})
end
---@param current_tab string
---@param this_tab string
---@return string
local function tab(current_tab, this_tab)
local bg_img
if current_tab == this_tab then
bg_img = "crafting_creative_active" .. hoch[this_tab] .. ".png"
bg_img = "crafting_creative_active" .. button_bg_postfix[this_tab] .. ".png"
else
bg_img = "crafting_creative_inactive" .. hoch[this_tab] .. ".png"
bg_img = "crafting_creative_inactive" .. button_bg_postfix[this_tab] .. ".png"
end
return table.concat({
"style[" .. this_tab .. ";border=false;bgimg=;bgimg_pressed=;noclip=true]",
@ -476,7 +534,7 @@ function mcl_inventory.set_creative_formspec(player)
local caption = ""
if name ~= "inv" and filtername[name] then
caption = "label[0.375,0.375;" .. F(minetest.colorize("#313131", filtername[name])) .. "]"
caption = "label[0.375,0.375;" .. F(C(mcl_formspec.label_color, filtername[name])) .. "]"
end
local formspec = table.concat({
@ -490,8 +548,7 @@ function mcl_inventory.set_creative_formspec(player)
"list[current_player;main;0.375,7.375;9,1;]",
-- Trash
mcl_formspec.get_itemslot_bg_v4(11.625, 7.375, 1, 1, mcl_formspec.itemslot_border_size,
"crafting_creative_trash.png"),
mcl_formspec.get_itemslot_bg_v4(11.625, 7.375, 1, 1, nil, "crafting_creative_trash.png"),
"list[detached:trash;main;11.625,7.375;1,1;]",
main_list,

View file

@ -135,7 +135,7 @@ function mcl_player.player_set_armor(player, texture)
update_player_textures(player)
end
---@param player ObjectRef
---@param player mt.PlayerObjectRef
---@param x number
---@param y number
---@param w number