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