2 Commits

2 changed files with 41 additions and 10 deletions

View File

@@ -509,7 +509,7 @@ end
---@return Formspec
function Formspec:field(x, y, w, h, name, label, default)
local element = ('field[%s,%s;%s,%s;%s;%s;%s]'):format(
x, y, w, h, name, label or '', default or '')
x, y, w, h, name, label or '', core.formspec_escape(default) or '')
self:addElement(element)
return self
@@ -590,7 +590,7 @@ end
---@return Formspec
function Formspec:textArea(x, y, w, h, name, label, default)
local element = ('textarea[%s,%s;%s,%s;%s;%s;%s]'):format(
x, y, w, h, name or '', label or '', default or '')
x, y, w, h, name or '', label or '', core.formspec_escape(default) or '')
self:addElement(element)
return self
@@ -610,7 +610,7 @@ end
---@return Formspec
function Formspec:label(x, y, label)
local element = ('label[%s,%s;%s]'):format(
x, y, label)
x, y, core.formspec_escape(label))
self:addElement(element)
return self
@@ -630,7 +630,7 @@ end
---@return Formspec
function Formspec:areaLabel(x, y, w, h, label)
local element = ('label[%s,%s;%s,%s;%s]'):format(
x, y, w, h, label)
x, y, w, h, core.formspec_escape(label))
self:addElement(element)
return self
@@ -664,7 +664,7 @@ end
---@return Formspec
function Formspec:vertlabel(x, y, label)
local element = ('vertlabel[%s,%s;%s]'):format(
x, y, label)
x, y, core.formspec_escape(label))
self:addElement(element)
return self

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)