PlayerInventoryUI: Added display of material availability in the crafting recipe. #34
This commit is contained in:
@@ -7,14 +7,43 @@ local PrimitiveCraftHandler = Smc.requireCraftSystemPrimitiveCraftHandler()
|
|||||||
|
|
||||||
local Formspec = Smc.requireFormspec()
|
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).
|
---Returns a human-readable list of materials required for crafting (recipe).
|
||||||
|
---@param inventory InvRef
|
||||||
---@param craft CraftSystem.PrimitiveCraft
|
---@param craft CraftSystem.PrimitiveCraft
|
||||||
---@param index integer Recipe number. If `0` is specified,
|
---@param index integer Recipe number. If `0` is specified,
|
||||||
--- the primary craft (`craft.recipe`) will be used;
|
--- the primary craft (`craft.recipe`) will be used;
|
||||||
--- if `1` or greater, the alternative craft
|
--- if `1` or greater, the alternative craft
|
||||||
--- corresponding to the index will be used (`craft.altRecipes`).
|
--- corresponding to the index will be used (`craft.altRecipes`).
|
||||||
---@return string
|
---@return string
|
||||||
local function getRecipeDesc(craft, index)
|
local function getRecipeDesc(inventory, craft, index)
|
||||||
local recipe
|
local recipe
|
||||||
if index == 0 or craft.altRecipes == nil then
|
if index == 0 or craft.altRecipes == nil then
|
||||||
recipe = craft.recipe
|
recipe = craft.recipe
|
||||||
@@ -33,9 +62,9 @@ local function getRecipeDesc(craft, index)
|
|||||||
|
|
||||||
if group == nil then
|
if group == nil then
|
||||||
local recipeItem = ItemStack(itemName)
|
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
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -89,7 +118,7 @@ local function getCraftTab(self, playerName, tabType)
|
|||||||
|
|
||||||
local w, h = 0.0, 0.0
|
local w, h = 0.0, 0.0
|
||||||
|
|
||||||
if selectedGroup ~= nil then
|
if selectedGroup ~= nil and (searchFieldValue == nil or searchFieldValue == '') then
|
||||||
craftsListForm
|
craftsListForm
|
||||||
:imageButton(0, 0, 1.125, 1.125, 'player_inventory_craft_tab_close_group_button_background.png', tabType..'::close_group')
|
:imageButton(0, 0, 1.125, 1.125, 'player_inventory_craft_tab_close_group_button_background.png', tabType..'::close_group')
|
||||||
w = 1.125
|
w = 1.125
|
||||||
@@ -152,7 +181,9 @@ local function getCraftTab(self, playerName, tabType)
|
|||||||
self:setContex(playerName, tabType..'_tab_craft_selected_recipe_index', #craft.altRecipes)
|
self:setContex(playerName, tabType..'_tab_craft_selected_recipe_index', #craft.altRecipes)
|
||||||
end
|
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
|
CraftTab
|
||||||
:itemImage(5.5, 0.5, 2, 2, craft.output)
|
:itemImage(5.5, 0.5, 2, 2, craft.output)
|
||||||
|
|||||||
Reference in New Issue
Block a user