PlayerInventoryUI: Added display of material availability in the crafting recipe. #34

This commit is contained in:
Van
2026-05-01 19:46:05 +03:00
parent c902aff4af
commit f60faa4293

View File

@@ -7,14 +7,43 @@ local PrimitiveCraftHandler = Smc.requireCraftSystemPrimitiveCraftHandler()
local Formspec = Smc.requireFormspec()
---A text indicator of the presence or absence
---of a material in the player's inventory.
local CHECK = {YES = 'x', NO = ' '}
---@param inventory InvRef
---@param material ItemStack
---@return string
local function getCheckMaterialItem(inventory, material)
if inventory:contains_item('main', material) then
return CHECK.YES
else
return CHECK.NO
end
end
---@param inventory InvRef
---@param material string
---@return string
local function getCheckMaterialGroup(inventory, material)
for _, item in ipairs(inventory:get_list('main')) do
if core.get_item_group(item:get_name(), material) > 0 then
return CHECK.YES
end
end
return CHECK.NO
end
---Returns a human-readable list of materials required for crafting (recipe).
---@param inventory InvRef
---@param craft CraftSystem.PrimitiveCraft
---@param index integer Recipe number. If `0` is specified,
--- the primary craft (`craft.recipe`) will be used;
--- if `1` or greater, the alternative craft
--- corresponding to the index will be used (`craft.altRecipes`).
---@return string
local function getRecipeDesc(craft, index)
local function getRecipeDesc(inventory, craft, index)
local recipe
if index == 0 or craft.altRecipes == nil then
recipe = craft.recipe
@@ -33,9 +62,9 @@ local function getRecipeDesc(craft, index)
if group == nil then
local recipeItem = ItemStack(itemName)
recipeDesc = recipeDesc..'- '..core.strip_colors(recipeItem:get_definition()._title)..' x'..recipeItem:get_count()..'\n'
recipeDesc = recipeDesc..('[%s] %s x%s\n'):format(getCheckMaterialItem(inventory, recipeItem), core.strip_colors(recipeItem:get_definition()._title), recipeItem:get_count())
else
recipeDesc = recipeDesc..'- any item from the group "'..group..'" x'..count..'\n'
recipeDesc = recipeDesc..('[%s] any item from the group %s x%s\n'):format(getCheckMaterialGroup(inventory, group), group, count)
end
end
@@ -89,7 +118,7 @@ local function getCraftTab(self, playerName, tabType)
local w, h = 0.0, 0.0
if selectedGroup ~= nil then
if selectedGroup ~= nil and (searchFieldValue == nil or searchFieldValue == '') then
craftsListForm
:imageButton(0, 0, 1.125, 1.125, 'player_inventory_craft_tab_close_group_button_background.png', tabType..'::close_group')
w = 1.125
@@ -152,7 +181,9 @@ local function getCraftTab(self, playerName, tabType)
self:setContex(playerName, tabType..'_tab_craft_selected_recipe_index', #craft.altRecipes)
end
local recipeDesc = getRecipeDesc(craft, recipeIndex)
local player = core.get_player_by_name(playerName)
assert(player ~= nil)
local recipeDesc = getRecipeDesc(player:get_inventory(), craft, recipeIndex)
CraftTab
:itemImage(5.5, 0.5, 2, 2, craft.output)