mirror of
https://github.com/minetest-mods/unified_inventory.git
synced 2025-06-30 07:20:39 +02:00
Merge branch 'master' of yunohost.local:minetest-mods/unified_inventory into nalc-1.2-dev
This commit is contained in:
114
register.lua
114
register.lua
@ -1,4 +1,5 @@
|
||||
local S = unified_inventory.gettext
|
||||
local S = minetest.get_translator("unified_inventory")
|
||||
local NS = function(s) return s end
|
||||
local F = minetest.formspec_escape
|
||||
|
||||
minetest.register_privilege("creative", {
|
||||
@ -170,9 +171,9 @@ local function stack_image_button(x, y, w, h, buttonname_prefix, item)
|
||||
local groupstring, andcount = unified_inventory.extract_groupnames(name)
|
||||
local grouptip
|
||||
if andcount == 1 then
|
||||
grouptip = string.format(S("Any item belonging to the %s group"), groupstring)
|
||||
grouptip = S("Any item belonging to the @1 group", groupstring)
|
||||
elseif andcount > 1 then
|
||||
grouptip = string.format(S("Any item belonging to the groups %s"), groupstring)
|
||||
grouptip = S("Any item belonging to the groups @1", groupstring)
|
||||
end
|
||||
grouptip = F(grouptip)
|
||||
if andcount >= 1 then
|
||||
@ -183,8 +184,8 @@ local function stack_image_button(x, y, w, h, buttonname_prefix, item)
|
||||
end
|
||||
|
||||
local recipe_text = {
|
||||
recipe = S("Recipe %d of %d"),
|
||||
usage = S("Usage %d of %d"),
|
||||
recipe = NS("Recipe @1 of @2"),
|
||||
usage = NS("Usage @1 of @2"),
|
||||
}
|
||||
local no_recipe_text = {
|
||||
recipe = S("No recipes"),
|
||||
@ -230,7 +231,7 @@ unified_inventory.register_page("craftguide", {
|
||||
local item_name_shown
|
||||
if minetest.registered_items[item_name]
|
||||
and minetest.registered_items[item_name].description then
|
||||
item_name_shown = string.format(S("%s (%s)"),
|
||||
item_name_shown = S("@1 (@2)",
|
||||
minetest.registered_items[item_name].description, item_name)
|
||||
else
|
||||
item_name_shown = item_name
|
||||
@ -354,7 +355,7 @@ unified_inventory.register_page("craftguide", {
|
||||
|
||||
if alternates and alternates > 1 then
|
||||
fs[#fs + 1] = "label[5.5," .. (formspecy + 1.6) .. ";"
|
||||
.. string.format(F(recipe_text[dir]), alternate, alternates) .. "]"
|
||||
.. F(S(recipe_text[dir], alternate, alternates)) .. "]"
|
||||
.. "image_button[5.5," .. (formspecy + 2) .. ";1,1;ui_left_icon.png;alternate_prev;]"
|
||||
.. "image_button[6.5," .. (formspecy + 2) .. ";1,1;ui_right_icon.png;alternate;]"
|
||||
.. "tooltip[alternate_prev;" .. F(prev_alt_text[dir]) .. "]"
|
||||
@ -391,65 +392,6 @@ local function craftguide_giveme(player, formname, fields)
|
||||
player_inv:add_item("main", {name = output, count = amount})
|
||||
end
|
||||
|
||||
-- Takes any stack from "main" where the `amount` of `needed_item` may fit
|
||||
-- into the given crafting stack (`craft_item`)
|
||||
local function craftguide_move_stacks(inv, craft_item, needed_item, amount)
|
||||
if craft_item:get_count() >= amount then
|
||||
return
|
||||
end
|
||||
|
||||
local get_item_group = minetest.get_item_group
|
||||
local group = needed_item:match("^group:(.+)")
|
||||
if group then
|
||||
if not craft_item:is_empty() then
|
||||
-- Source item must be the same to fill
|
||||
if get_item_group(craft_item:get_name(), group) ~= 0 then
|
||||
needed_item = craft_item:get_name()
|
||||
else
|
||||
-- TODO: Maybe swap unmatching "craft" items
|
||||
-- !! Would conflict with recursive function call
|
||||
return
|
||||
end
|
||||
else
|
||||
-- Take matching group from the inventory (biggest stack)
|
||||
local main = inv:get_list("main")
|
||||
local max_found = 0
|
||||
for i, stack in ipairs(main) do
|
||||
if stack:get_count() > max_found and
|
||||
get_item_group(stack:get_name(), group) ~= 0 then
|
||||
needed_item = stack:get_name()
|
||||
max_found = stack:get_count()
|
||||
if max_found >= amount then
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
else
|
||||
if not craft_item:is_empty() and
|
||||
craft_item:get_name() ~= needed_item then
|
||||
return -- Item must be identical
|
||||
end
|
||||
end
|
||||
|
||||
needed_item = ItemStack(needed_item)
|
||||
local to_take = math.min(amount, needed_item:get_stack_max())
|
||||
to_take = to_take - craft_item:get_count()
|
||||
if to_take <= 0 then
|
||||
return -- Nothing to do
|
||||
end
|
||||
needed_item:set_count(to_take)
|
||||
|
||||
local taken = inv:remove_item("main", needed_item)
|
||||
local leftover = taken:add_item(craft_item)
|
||||
if not leftover:is_empty() then
|
||||
-- Somehow failed to add the existing "craft" item. Undo the action.
|
||||
inv:add_item("main", leftover)
|
||||
return taken
|
||||
end
|
||||
return taken
|
||||
end
|
||||
|
||||
local function craftguide_craft(player, formname, fields)
|
||||
local amount
|
||||
for k, v in pairs(fields) do
|
||||
@ -458,17 +400,14 @@ local function craftguide_craft(player, formname, fields)
|
||||
end
|
||||
if not amount then return end
|
||||
|
||||
amount = tonumber(amount) or 99 -- fallback for "all"
|
||||
if amount <= 0 or amount > 99 then return end
|
||||
amount = tonumber(amount) or -1 -- fallback for "all"
|
||||
if amount == 0 or amount < -1 or amount > 99 then return end
|
||||
|
||||
local player_name = player:get_player_name()
|
||||
|
||||
local output = unified_inventory.current_item[player_name] or ""
|
||||
if output == "" then return end
|
||||
|
||||
local player_inv = player:get_inventory()
|
||||
local craft_list = player_inv:get_list("craft")
|
||||
|
||||
local crafts = unified_inventory.crafts_for[
|
||||
unified_inventory.current_craft_direction[player_name]][output] or {}
|
||||
if #crafts == 0 then return end
|
||||
@ -478,38 +417,7 @@ local function craftguide_craft(player, formname, fields)
|
||||
local craft = crafts[alternate]
|
||||
if craft.width > 3 then return end
|
||||
|
||||
local needed = craft.items
|
||||
local width = craft.width
|
||||
if width == 0 then
|
||||
-- Shapeless recipe
|
||||
width = 3
|
||||
end
|
||||
|
||||
-- To spread the items evenly
|
||||
local STEPSIZE = math.ceil(math.sqrt(amount) / 5) * 5
|
||||
local current_count = 0
|
||||
repeat
|
||||
current_count = math.min(current_count + STEPSIZE, amount)
|
||||
local index = 1
|
||||
for y = 1, 3 do
|
||||
for x = 1, width do
|
||||
local needed_item = needed[index]
|
||||
if needed_item then
|
||||
local craft_index = ((y - 1) * 3) + x
|
||||
local craft_item = craft_list[craft_index]
|
||||
local newitem = craftguide_move_stacks(player_inv,
|
||||
craft_item, needed_item, current_count)
|
||||
|
||||
if newitem then
|
||||
craft_list[craft_index] = newitem
|
||||
end
|
||||
end
|
||||
index = index + 1
|
||||
end
|
||||
end
|
||||
until current_count == amount
|
||||
|
||||
player_inv:set_list("craft", craft_list)
|
||||
unified_inventory.craftguide_match_craft(player, "main", "craft", craft, amount)
|
||||
|
||||
unified_inventory.set_inventory_formspec(player, "craft")
|
||||
end
|
||||
|
Reference in New Issue
Block a user