forked from minetest-mods/unified_inventory
Merge branch 'master' into nalc-1.1
This commit is contained in:
commit
e5dc9ed53a
69
bags.lua
69
bags.lua
@ -9,18 +9,19 @@ local F = minetest.formspec_escape
|
|||||||
unified_inventory.register_page("bags", {
|
unified_inventory.register_page("bags", {
|
||||||
get_formspec = function(player)
|
get_formspec = function(player)
|
||||||
local player_name = player:get_player_name()
|
local player_name = player:get_player_name()
|
||||||
local formspec = "background[0.06,0.99;7.92,7.52;ui_bags_main_form.png]"
|
return { formspec = table.concat({
|
||||||
formspec = formspec.."label[0,0;"..F(S("Bags")).."]"
|
"background[0.06,0.99;7.92,7.52;ui_bags_main_form.png]",
|
||||||
formspec = formspec.."button[0,2;2,0.5;bag1;"..F(S("Bag @1", 1)).."]"
|
"label[0,0;" .. F(S("Bags")) .. "]",
|
||||||
formspec = formspec.."button[2,2;2,0.5;bag2;"..F(S("Bag @1", 2)).."]"
|
"button[0,2;2,0.5;bag1;" .. F(S("Bag @1", 1)) .. "]",
|
||||||
formspec = formspec.."button[4,2;2,0.5;bag3;"..F(S("Bag @1", 3)).."]"
|
"button[2,2;2,0.5;bag2;" .. F(S("Bag @1", 2)) .. "]",
|
||||||
formspec = formspec.."button[6,2;2,0.5;bag4;"..F(S("Bag @1", 4)).."]"
|
"button[4,2;2,0.5;bag3;" .. F(S("Bag @1", 3)) .. "]",
|
||||||
formspec = formspec.."listcolors[#00000000;#00000000]"
|
"button[6,2;2,0.5;bag4;" .. F(S("Bag @1", 4)) .. "]",
|
||||||
formspec = formspec.."list[detached:"..F(player_name).."_bags;bag1;0.5,1;1,1;]"
|
"listcolors[#00000000;#00000000]",
|
||||||
formspec = formspec.."list[detached:"..F(player_name).."_bags;bag2;2.5,1;1,1;]"
|
"list[detached:" .. F(player_name) .. "_bags;bag1;0.5,1;1,1;]",
|
||||||
formspec = formspec.."list[detached:"..F(player_name).."_bags;bag3;4.5,1;1,1;]"
|
"list[detached:" .. F(player_name) .. "_bags;bag2;2.5,1;1,1;]",
|
||||||
formspec = formspec.."list[detached:"..F(player_name).."_bags;bag4;6.5,1;1,1;]"
|
"list[detached:" .. F(player_name) .. "_bags;bag3;4.5,1;1,1;]",
|
||||||
return {formspec=formspec}
|
"list[detached:" .. F(player_name) .. "_bags;bag4;6.5,1;1,1;]"
|
||||||
|
}) }
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -38,35 +39,37 @@ local function get_player_bag_stack(player, i)
|
|||||||
}):get_stack("bag" .. i, 1)
|
}):get_stack("bag" .. i, 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
for i = 1, 4 do
|
for bag_i = 1, 4 do
|
||||||
local bi = i
|
unified_inventory.register_page("bag" .. bag_i, {
|
||||||
unified_inventory.register_page("bag"..bi, {
|
|
||||||
get_formspec = function(player)
|
get_formspec = function(player)
|
||||||
local stack = get_player_bag_stack(player, bi)
|
local stack = get_player_bag_stack(player, bag_i)
|
||||||
local image = stack:get_definition().inventory_image
|
local image = stack:get_definition().inventory_image
|
||||||
local formspec = ("image[7,0;1,1;"..image.."]"
|
local fs = {
|
||||||
.."label[0,0;"..F(S("Bag @1", bi)).."]"
|
"image[7,0;1,1;" .. image .. "]",
|
||||||
.."listcolors[#00000000;#00000000]"
|
"label[0,0;" .. F(S("Bag @1", bag_i)) .. "]",
|
||||||
.."list[current_player;bag"..bi.."contents;0,1;8,3;]"
|
"listcolors[#00000000;#00000000]",
|
||||||
.."listring[current_name;bag"..bi.."contents]"
|
"list[current_player;bag" .. bag_i .. "contents;0,1;8,3;]",
|
||||||
.."listring[current_player;main]")
|
"listring[current_name;bag" .. bag_i .. "contents]",
|
||||||
|
"listring[current_player;main]"
|
||||||
|
}
|
||||||
local slots = stack:get_definition().groups.bagslots
|
local slots = stack:get_definition().groups.bagslots
|
||||||
if slots == 8 then
|
if slots == 8 then
|
||||||
formspec = formspec.."background[0.06,0.99;7.92,7.52;ui_bags_sm_form.png]"
|
fs[#fs + 1] = "background[0.06,0.99;7.92,7.52;ui_bags_sm_form.png]"
|
||||||
elseif slots == 16 then
|
elseif slots == 16 then
|
||||||
formspec = formspec.."background[0.06,0.99;7.92,7.52;ui_bags_med_form.png]"
|
fs[#fs + 1] = "background[0.06,0.99;7.92,7.52;ui_bags_med_form.png]"
|
||||||
elseif slots == 24 then
|
elseif slots == 24 then
|
||||||
formspec = formspec.."background[0.06,0.99;7.92,7.52;ui_bags_lg_form.png]"
|
fs[#fs + 1] = "background[0.06,0.99;7.92,7.52;ui_bags_lg_form.png]"
|
||||||
end
|
end
|
||||||
local player_name = player:get_player_name() -- For if statement.
|
local player_name = player:get_player_name() -- For if statement.
|
||||||
if unified_inventory.trash_enabled or unified_inventory.is_creative(player_name) or minetest.get_player_privs(player_name).give then
|
if unified_inventory.trash_enabled
|
||||||
formspec = (formspec.."background[6.06,0;0.92,0.92;ui_bags_trash.png]"
|
or unified_inventory.is_creative(player_name)
|
||||||
.."list[detached:trash;main;6,0.1;1,1;]")
|
or minetest.get_player_privs(player_name).give then
|
||||||
|
fs[#fs + 1] = "background[6.06,0;0.92,0.92;ui_bags_trash.png]"
|
||||||
|
.. "list[detached:trash;main;6,0.1;1,1;]"
|
||||||
end
|
end
|
||||||
local inv = player:get_inventory()
|
local inv = player:get_inventory()
|
||||||
for i = 1, 4 do
|
for i = 1, 4 do
|
||||||
local def = get_player_bag_stack(player, i):get_definition()
|
local def = get_player_bag_stack(player, i):get_definition()
|
||||||
local button
|
|
||||||
if def.groups.bagslots then
|
if def.groups.bagslots then
|
||||||
local list_name = "bag" .. i .. "contents"
|
local list_name = "bag" .. i .. "contents"
|
||||||
local size = inv:get_size(list_name)
|
local size = inv:get_size(list_name)
|
||||||
@ -79,13 +82,11 @@ for i = 1, 4 do
|
|||||||
end
|
end
|
||||||
local img = def.inventory_image
|
local img = def.inventory_image
|
||||||
local label = F(S("Bag @1", i)) .. "\n" .. used .. "/" .. size
|
local label = F(S("Bag @1", i)) .. "\n" .. used .. "/" .. size
|
||||||
button = "image_button["..(i+1)..",0;1,1;"..img..";bag"..i..";"..label.."]"
|
fs[#fs + 1] = string.format("image_button[%i,0;1,1;%s;bag%i;%s]",
|
||||||
else
|
i + 1, img, i, label)
|
||||||
button = ""
|
|
||||||
end
|
end
|
||||||
formspec = formspec..button
|
|
||||||
end
|
end
|
||||||
return {formspec=formspec}
|
return { formspec = table.concat(fs) }
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
95
doc/mod_api.txt
Normal file
95
doc/mod_api.txt
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
unified_inventory API
|
||||||
|
=====================
|
||||||
|
|
||||||
|
This file provides information about the API of unified_inventory.
|
||||||
|
|
||||||
|
|
||||||
|
Misc functions
|
||||||
|
--------------
|
||||||
|
Grouped by use-case, afterwards sorted alphabetically.
|
||||||
|
|
||||||
|
* `unified_inventory.is_creative(name)`
|
||||||
|
* Checks whether creative is enabled or the player has `creative`
|
||||||
|
|
||||||
|
|
||||||
|
Pages
|
||||||
|
-----
|
||||||
|
|
||||||
|
Register a new page: The callback inside this function is called on user input.
|
||||||
|
|
||||||
|
unified_inventory.register_page("pagename", {
|
||||||
|
get_formspec = function(player)
|
||||||
|
-- ^ `player` is an `ObjectRef`
|
||||||
|
-- Compute the formspec string here
|
||||||
|
return {
|
||||||
|
formspec = "button[2,2;2,1;mybutton;Press me]",
|
||||||
|
-- ^ Final form of the formspec to display
|
||||||
|
draw_inventory = false, -- default `true`
|
||||||
|
-- ^ Optional. Hides the player's `main` inventory list
|
||||||
|
draw_item_list = false, -- default `true`
|
||||||
|
-- ^ Optional. Hides the item list on the right side
|
||||||
|
formspec_prepend = false, -- default `false`
|
||||||
|
-- ^ Optional. When `false`: Disables the formspec prepend
|
||||||
|
}
|
||||||
|
end,
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
Buttons
|
||||||
|
-------
|
||||||
|
|
||||||
|
Register a new button for the bottom row:
|
||||||
|
|
||||||
|
unified_inventory.register_button("skins", {
|
||||||
|
type = "image",
|
||||||
|
image = "skins_skin_button.png",
|
||||||
|
tooltip = "Skins",
|
||||||
|
hide_lite = true
|
||||||
|
-- ^ Button is hidden when following two conditions are met:
|
||||||
|
-- Configuration line `unified_inventory_lite = true`
|
||||||
|
-- Player does not have the privilege `ui_full`
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Crafting
|
||||||
|
--------
|
||||||
|
|
||||||
|
The code blocks below document each possible parameter using exemplary values.
|
||||||
|
|
||||||
|
Provide information to display custom craft types:
|
||||||
|
|
||||||
|
unified_inventory.register_craft_type("mytype", {
|
||||||
|
-- ^ Unique identifier for `register_craft`
|
||||||
|
description = "Sample Craft",
|
||||||
|
-- ^ Text shown below the crafting arrow
|
||||||
|
icon = "dummy.png",
|
||||||
|
-- ^ Image shown above the crafting arrow
|
||||||
|
width = 3,
|
||||||
|
height = 3,
|
||||||
|
-- ^ Maximal input dimensions of the recipes
|
||||||
|
dynamic_display_size = function(craft)
|
||||||
|
-- ^ `craft` is the definition from `register_craft`
|
||||||
|
return {
|
||||||
|
width = 2,
|
||||||
|
height = 3
|
||||||
|
}
|
||||||
|
end,
|
||||||
|
-- ^ Optional callback to change the displayed recipe size
|
||||||
|
uses_crafting_grid = true,
|
||||||
|
})
|
||||||
|
|
||||||
|
Register a non-standard craft recipe:
|
||||||
|
|
||||||
|
unified_inventory.register_craft({
|
||||||
|
output = "default:foobar",
|
||||||
|
type = "mytype",
|
||||||
|
-- ^ Standard craft type or custom (see `register_craft_type`)
|
||||||
|
items = {
|
||||||
|
{ "default:foo" },
|
||||||
|
{ "default:bar" }
|
||||||
|
},
|
||||||
|
width = 3,
|
||||||
|
-- ^ Same as `minetest.register_recipe`
|
||||||
|
})
|
||||||
|
|
28
group.lua
28
group.lua
@ -2,7 +2,12 @@ local S = unified_inventory.gettext
|
|||||||
|
|
||||||
function unified_inventory.canonical_item_spec_matcher(spec)
|
function unified_inventory.canonical_item_spec_matcher(spec)
|
||||||
local specname = ItemStack(spec):get_name()
|
local specname = ItemStack(spec):get_name()
|
||||||
if specname:sub(1, 6) == "group:" then
|
if specname:sub(1, 6) ~= "group:" then
|
||||||
|
return function (itemname)
|
||||||
|
return itemname == specname
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local group_names = specname:sub(7):split(",")
|
local group_names = specname:sub(7):split(",")
|
||||||
return function (itemname)
|
return function (itemname)
|
||||||
local itemdef = minetest.registered_items[itemname]
|
local itemdef = minetest.registered_items[itemname]
|
||||||
@ -13,9 +18,6 @@ function unified_inventory.canonical_item_spec_matcher(spec)
|
|||||||
end
|
end
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
else
|
|
||||||
return function (itemname) return itemname == specname end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function unified_inventory.item_matches_spec(item, spec)
|
function unified_inventory.item_matches_spec(item, spec)
|
||||||
@ -25,23 +27,11 @@ end
|
|||||||
|
|
||||||
function unified_inventory.extract_groupnames(groupname)
|
function unified_inventory.extract_groupnames(groupname)
|
||||||
local specname = ItemStack(groupname):get_name()
|
local specname = ItemStack(groupname):get_name()
|
||||||
if specname:sub(1, 6) == "group:" then
|
if specname:sub(1, 6) ~= "group:" then
|
||||||
local group_names = specname:sub(7):split(",")
|
|
||||||
if #group_names == 1 then
|
|
||||||
return group_names[1], 1
|
|
||||||
end
|
|
||||||
local s = ""
|
|
||||||
for g=1,#group_names do
|
|
||||||
if g > 1 then
|
|
||||||
-- List connector
|
|
||||||
s = s .. S(" and ")
|
|
||||||
end
|
|
||||||
s = s .. group_names[g]
|
|
||||||
end
|
|
||||||
return s, #group_names
|
|
||||||
else
|
|
||||||
return nil, 0
|
return nil, 0
|
||||||
end
|
end
|
||||||
|
local group_names = specname:sub(7):split(",")
|
||||||
|
return table.concat(group_names, S(" and ")), #group_names
|
||||||
end
|
end
|
||||||
|
|
||||||
unified_inventory.registered_group_items = {
|
unified_inventory.registered_group_items = {
|
||||||
|
14
internal.lua
14
internal.lua
@ -58,15 +58,20 @@ function unified_inventory.get_formspec(player, page)
|
|||||||
unified_inventory.current_page[player_name] = page
|
unified_inventory.current_page[player_name] = page
|
||||||
local pagedef = unified_inventory.pages[page]
|
local pagedef = unified_inventory.pages[page]
|
||||||
|
|
||||||
|
if not pagedef then
|
||||||
|
return "" -- Invalid page name
|
||||||
|
end
|
||||||
|
|
||||||
local formspec = {
|
local formspec = {
|
||||||
"size[14,10]",
|
"size[14,10]",
|
||||||
|
pagedef.formspec_prepend and "" or "no_prepend[]",
|
||||||
"background[-0.19,-0.25;14.4,10.75;ui_form_bg.png]" -- Background
|
"background[-0.19,-0.25;14.4,10.75;ui_form_bg.png]" -- Background
|
||||||
}
|
}
|
||||||
local n = 3
|
local n = 4
|
||||||
|
|
||||||
if draw_lite_mode then
|
if draw_lite_mode then
|
||||||
formspec[1] = "size[11,7.7]"
|
formspec[1] = "size[11,7.7]"
|
||||||
formspec[2] = "background[-0.19,-0.2;11.4,8.4;ui_form_bg.png]"
|
formspec[3] = "background[-0.19,-0.2;11.4,8.4;ui_form_bg.png]"
|
||||||
end
|
end
|
||||||
|
|
||||||
if unified_inventory.is_creative(player_name)
|
if unified_inventory.is_creative(player_name)
|
||||||
@ -75,11 +80,6 @@ function unified_inventory.get_formspec(player, page)
|
|||||||
n = n+1
|
n = n+1
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Current page
|
|
||||||
if not unified_inventory.pages[page] then
|
|
||||||
return "" -- Invalid page name
|
|
||||||
end
|
|
||||||
|
|
||||||
local perplayer_formspec = unified_inventory.get_per_player_formspec(player_name)
|
local perplayer_formspec = unified_inventory.get_per_player_formspec(player_name)
|
||||||
local fsdata = pagedef.get_formspec(player, perplayer_formspec)
|
local fsdata = pagedef.get_formspec(player, perplayer_formspec)
|
||||||
|
|
||||||
|
254
register.lua
254
register.lua
@ -266,65 +266,77 @@ unified_inventory.register_page("craftguide", {
|
|||||||
|
|
||||||
local player_name = player:get_player_name()
|
local player_name = player:get_player_name()
|
||||||
local player_privs = minetest.get_player_privs(player_name)
|
local player_privs = minetest.get_player_privs(player_name)
|
||||||
local formspec = ""
|
local fs = {
|
||||||
formspec = formspec.."background[0,"..(formspecy + 3.5)..";8,4;ui_main_inventory.png]"
|
"background[0,"..(formspecy + 3.5)..";8,4;ui_main_inventory.png]",
|
||||||
formspec = formspec.."label[0,"..formheadery..";" .. F(S("Crafting Guide")) .. "]"
|
"label[0,"..formheadery..";" .. F(S("Crafting Guide")) .. "]",
|
||||||
formspec = formspec.."listcolors[#00000000;#00000000]"
|
"listcolors[#00000000;#00000000]"
|
||||||
|
}
|
||||||
local item_name = unified_inventory.current_item[player_name]
|
local item_name = unified_inventory.current_item[player_name]
|
||||||
if not item_name then return {formspec=formspec} end
|
if not item_name then
|
||||||
|
return { formspec = table.concat(fs) }
|
||||||
|
end
|
||||||
|
|
||||||
local item_name_shown
|
local item_name_shown
|
||||||
if minetest.registered_items[item_name] and minetest.registered_items[item_name].description then
|
if minetest.registered_items[item_name]
|
||||||
item_name_shown = string.format(S("%s (%s)"), minetest.registered_items[item_name].description, item_name)
|
and minetest.registered_items[item_name].description then
|
||||||
|
item_name_shown = string.format(S("%s (%s)"),
|
||||||
|
minetest.registered_items[item_name].description, item_name)
|
||||||
else
|
else
|
||||||
item_name_shown = item_name
|
item_name_shown = item_name
|
||||||
end
|
end
|
||||||
|
|
||||||
local dir = unified_inventory.current_craft_direction[player_name]
|
local dir = unified_inventory.current_craft_direction[player_name]
|
||||||
local rdir
|
local rdir = dir == "recipe" and "usage" or "recipe"
|
||||||
if dir == "recipe" then rdir = "usage" end
|
|
||||||
if dir == "usage" then rdir = "recipe" end
|
|
||||||
local crafts = unified_inventory.crafts_for[dir][item_name]
|
local crafts = unified_inventory.crafts_for[dir][item_name]
|
||||||
local alternate = unified_inventory.alternate[player_name]
|
local alternate = unified_inventory.alternate[player_name]
|
||||||
local alternates, craft
|
local alternates, craft
|
||||||
if crafts ~= nil and #crafts > 0 then
|
if crafts and #crafts > 0 then
|
||||||
alternates = #crafts
|
alternates = #crafts
|
||||||
craft = crafts[alternate]
|
craft = crafts[alternate]
|
||||||
end
|
end
|
||||||
local has_creative = player_privs.give or player_privs.creative or
|
local has_give = player_privs.give or unified_inventory.is_creative(player_name)
|
||||||
minetest.settings:get_bool("creative_mode")
|
|
||||||
|
|
||||||
formspec = formspec.."background[0.5,"..(formspecy + 0.2)..";8,3;ui_craftguide_form.png]"
|
fs[#fs + 1] = "background[0.5,"..(formspecy + 0.2)..";8,3;ui_craftguide_form.png]"
|
||||||
formspec = formspec.."textarea["..craftresultx..","..craftresulty
|
fs[#fs + 1] = string.format("textarea[%f,%f;10,1;;%s: %s;]",
|
||||||
..";10,1;;"..F(role_text[dir])..": "..item_name_shown..";]"
|
craftresultx, craftresulty, F(role_text[dir]), item_name_shown)
|
||||||
formspec = formspec..stack_image_button(0, formspecy, 1.1, 1.1, "item_button_"
|
fs[#fs + 1] = stack_image_button(0, formspecy, 1.1, 1.1,
|
||||||
.. rdir .. "_", ItemStack(item_name))
|
"item_button_" .. rdir .. "_", ItemStack(item_name))
|
||||||
|
|
||||||
if not craft then
|
if not craft then
|
||||||
formspec = formspec.."label[5.5,"..(formspecy + 2.35)..";"
|
-- No craft recipes available for this item.
|
||||||
|
fs[#fs + 1] = "label[5.5,"..(formspecy + 2.35)..";"
|
||||||
.. F(no_recipe_text[dir]) .. "]"
|
.. F(no_recipe_text[dir]) .. "]"
|
||||||
local no_pos = dir == "recipe" and 4.5 or 6.5
|
local no_pos = dir == "recipe" and 4.5 or 6.5
|
||||||
local item_pos = dir == "recipe" and 6.5 or 4.5
|
local item_pos = dir == "recipe" and 6.5 or 4.5
|
||||||
formspec = formspec.."image["..no_pos..","..formspecy..";1.1,1.1;ui_no.png]"
|
fs[#fs + 1] = "image["..no_pos..","..formspecy..";1.1,1.1;ui_no.png]"
|
||||||
formspec = formspec..stack_image_button(item_pos, formspecy, 1.1, 1.1, "item_button_"
|
fs[#fs + 1] = stack_image_button(item_pos, formspecy, 1.1, 1.1,
|
||||||
..other_dir[dir].."_", ItemStack(item_name))
|
"item_button_" .. other_dir[dir] .. "_", ItemStack(item_name))
|
||||||
if has_creative then
|
if has_give then
|
||||||
formspec = formspec.."label[0,"..(formspecy + 2.10)..";" .. F(S("Give me:")) .. "]"
|
fs[#fs + 1] = "label[0," .. (formspecy + 2.10) .. ";" .. F(S("Give me:")) .. "]"
|
||||||
.. "button[0, " .. (formspecy + 2.7) .. ";0.6,0.5;craftguide_giveme_1;1]"
|
.. "button[0, " .. (formspecy + 2.7) .. ";0.6,0.5;craftguide_giveme_1;1]"
|
||||||
.. "button[0.6," .. (formspecy + 2.7) .. ";0.7,0.5;craftguide_giveme_10;10]"
|
.. "button[0.6," .. (formspecy + 2.7) .. ";0.7,0.5;craftguide_giveme_10;10]"
|
||||||
.. "button[1.3," .. (formspecy + 2.7) .. ";0.8,0.5;craftguide_giveme_99;99]"
|
.. "button[1.3," .. (formspecy + 2.7) .. ";0.8,0.5;craftguide_giveme_99;99]"
|
||||||
end
|
end
|
||||||
return {formspec = formspec}
|
return { formspec = table.concat(fs) }
|
||||||
end
|
end
|
||||||
|
|
||||||
local craft_type = unified_inventory.registered_craft_types[craft.type] or
|
local craft_type = unified_inventory.registered_craft_types[craft.type] or
|
||||||
unified_inventory.craft_type_defaults(craft.type, {})
|
unified_inventory.craft_type_defaults(craft.type, {})
|
||||||
if craft_type.icon then
|
if craft_type.icon then
|
||||||
formspec = formspec..string.format(" image[%f,%f;%f,%f;%s]",5.7,(formspecy + 0.05),0.5,0.5,craft_type.icon)
|
fs[#fs + 1] = string.format("image[%f,%f;%f,%f;%s]",
|
||||||
|
5.7, (formspecy + 0.05), 0.5, 0.5, craft_type.icon)
|
||||||
end
|
end
|
||||||
formspec = formspec.."label[5.5,"..(formspecy + 1)..";" .. F(craft_type.description).."]"
|
fs[#fs + 1] = "label[5.5,"..(formspecy + 1)..";" .. F(craft_type.description).."]"
|
||||||
formspec = formspec..stack_image_button(6.5, formspecy, 1.1, 1.1, "item_button_usage_", ItemStack(craft.output))
|
fs[#fs + 1] = stack_image_button(6.5, formspecy, 1.1, 1.1,
|
||||||
local display_size = craft_type.dynamic_display_size and craft_type.dynamic_display_size(craft) or { width = craft_type.width, height = craft_type.height }
|
"item_button_usage_", ItemStack(craft.output))
|
||||||
local craft_width = craft_type.get_shaped_craft_width and craft_type.get_shaped_craft_width(craft) or display_size.width
|
|
||||||
|
local display_size = craft_type.dynamic_display_size
|
||||||
|
and craft_type.dynamic_display_size(craft)
|
||||||
|
or { width = craft_type.width, height = craft_type.height }
|
||||||
|
local craft_width = craft_type.get_shaped_craft_width
|
||||||
|
and craft_type.get_shaped_craft_width(craft)
|
||||||
|
or display_size.width
|
||||||
|
|
||||||
-- This keeps recipes aligned to the right,
|
-- This keeps recipes aligned to the right,
|
||||||
-- so that they're close to the arrow.
|
-- so that they're close to the arrow.
|
||||||
@ -359,63 +371,67 @@ unified_inventory.register_page("craftguide", {
|
|||||||
local xof = (fx-1) * of + of
|
local xof = (fx-1) * of + of
|
||||||
local yof = (y-1) * of + 1
|
local yof = (y-1) * of + 1
|
||||||
if item then
|
if item then
|
||||||
formspec = formspec..stack_image_button(
|
fs[#fs + 1] = stack_image_button(
|
||||||
xoffset - xof, formspecy - 1 + yof, bsize_w, bsize_h,
|
xoffset - xof, formspecy - 1 + yof, bsize_w, bsize_h,
|
||||||
"item_button_recipe_",
|
"item_button_recipe_",
|
||||||
ItemStack(item))
|
ItemStack(item))
|
||||||
else
|
else
|
||||||
-- Fake buttons just to make grid
|
-- Fake buttons just to make grid
|
||||||
formspec = formspec.."image_button["
|
fs[#fs + 1] = string.format("image_button[%f,%f;%f,%f;ui_blank_image.png;;]",
|
||||||
..tostring(xoffset - xof)..","..tostring(formspecy - 1 + yof)
|
xoffset - xof, formspecy - 1 + yof, bsize_w, bsize_h)
|
||||||
..";"..bsize_w..","..bsize_h..";ui_blank_image.png;;]"
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
-- Error
|
-- Error
|
||||||
formspec = formspec.."label["
|
fs[#fs + 1] = string.format("label[2,%f;%s]",
|
||||||
..tostring(2)..","..tostring(formspecy)
|
formspecy, F(S("This recipe is too\nlarge to be displayed.")))
|
||||||
..";"..F(S("This recipe is too\nlarge to be displayed.")).."]"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if craft_type.uses_crafting_grid and display_size.width <= 3 then
|
if craft_type.uses_crafting_grid and display_size.width <= 3 then
|
||||||
formspec = formspec.."label[0,"..(formspecy + 0.9)..";" .. F(S("To craft grid:")) .. "]"
|
fs[#fs + 1] = "label[0," .. (formspecy + 0.9) .. ";" .. F(S("To craft grid:")) .. "]"
|
||||||
.. "button[0, " .. (formspecy + 1.5) .. ";0.6,0.5;craftguide_craft_1;1]"
|
.. "button[0, " .. (formspecy + 1.5) .. ";0.6,0.5;craftguide_craft_1;1]"
|
||||||
.. "button[0.6," .. (formspecy + 1.5) .. ";0.7,0.5;craftguide_craft_10;10]"
|
.. "button[0.6," .. (formspecy + 1.5) .. ";0.7,0.5;craftguide_craft_10;10]"
|
||||||
.. "button[1.3," .. (formspecy + 1.5) .. ";0.8,0.5;craftguide_craft_max;" .. F(S("All")) .. "]"
|
.. "button[1.3," .. (formspecy + 1.5) .. ";0.8,0.5;craftguide_craft_max;" .. F(S("All")) .. "]"
|
||||||
end
|
end
|
||||||
if has_creative then
|
if has_give then
|
||||||
formspec = formspec.."label[0,"..(formspecy + 2.1)..";" .. F(S("Give me:")) .. "]"
|
fs[#fs + 1] = "label[0," .. (formspecy + 2.1) .. ";" .. F(S("Give me:")) .. "]"
|
||||||
.. "button[0, " .. (formspecy + 2.7) .. ";0.6,0.5;craftguide_giveme_1;1]"
|
.. "button[0, " .. (formspecy + 2.7) .. ";0.6,0.5;craftguide_giveme_1;1]"
|
||||||
.. "button[0.6," .. (formspecy + 2.7) .. ";0.7,0.5;craftguide_giveme_10;10]"
|
.. "button[0.6," .. (formspecy + 2.7) .. ";0.7,0.5;craftguide_giveme_10;10]"
|
||||||
.. "button[1.3," .. (formspecy + 2.7) .. ";0.8,0.5;craftguide_giveme_99;99]"
|
.. "button[1.3," .. (formspecy + 2.7) .. ";0.8,0.5;craftguide_giveme_99;99]"
|
||||||
end
|
end
|
||||||
|
|
||||||
if alternates and alternates > 1 then
|
if alternates and alternates > 1 then
|
||||||
formspec = formspec.."label[5.5,"..(formspecy + 1.6)..";"
|
fs[#fs + 1] = "label[5.5," .. (formspecy + 1.6) .. ";"
|
||||||
.. string.format(F(recipe_text[dir]), alternate, alternates) .. "]"
|
.. string.format(F(recipe_text[dir]), alternate, alternates) .. "]"
|
||||||
.. "image_button[5.5," .. (formspecy + 2) .. ";1,1;ui_left_icon.png;alternate_prev;]"
|
.. "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;]"
|
.. "image_button[6.5," .. (formspecy + 2) .. ";1,1;ui_right_icon.png;alternate;]"
|
||||||
.. "tooltip[alternate_prev;" .. F(prev_alt_text[dir]) .. "]"
|
.. "tooltip[alternate_prev;" .. F(prev_alt_text[dir]) .. "]"
|
||||||
.. "tooltip[alternate;" .. F(next_alt_text[dir]) .. "]"
|
.. "tooltip[alternate;" .. F(next_alt_text[dir]) .. "]"
|
||||||
end
|
end
|
||||||
return {formspec = formspec}
|
return { formspec = table.concat(fs) }
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
local function craftguide_giveme(player, formname, fields)
|
local function craftguide_giveme(player, formname, fields)
|
||||||
|
local player_name = player:get_player_name()
|
||||||
|
local player_privs = minetest.get_player_privs(player_name)
|
||||||
|
if not player_privs.give and
|
||||||
|
not unified_inventory.is_creative(player_name) then
|
||||||
|
minetest.log("action", "[unified_inventory] Denied give action to player " ..
|
||||||
|
player_name)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
local amount
|
local amount
|
||||||
for k, v in pairs(fields) do
|
for k, v in pairs(fields) do
|
||||||
amount = k:match("craftguide_giveme_(.*)")
|
amount = k:match("craftguide_giveme_(.*)")
|
||||||
if amount then break end
|
if amount then break end
|
||||||
end
|
end
|
||||||
if not amount then return end
|
|
||||||
|
|
||||||
amount = tonumber(amount)
|
amount = tonumber(amount) or 0
|
||||||
if amount == 0 then return end
|
if amount == 0 then return end
|
||||||
|
|
||||||
local player_name = player:get_player_name()
|
|
||||||
|
|
||||||
local output = unified_inventory.current_item[player_name]
|
local output = unified_inventory.current_item[player_name]
|
||||||
if (not output) or (output == "") then return end
|
if (not output) or (output == "") then return end
|
||||||
|
|
||||||
@ -424,78 +440,63 @@ local function craftguide_giveme(player, formname, fields)
|
|||||||
player_inv:add_item("main", {name = output, count = amount})
|
player_inv:add_item("main", {name = output, count = amount})
|
||||||
end
|
end
|
||||||
|
|
||||||
-- tells if an item can be moved and returns an index if so
|
-- Takes any stack from "main" where the `amount` of `needed_item` may fit
|
||||||
local function item_fits(player_inv, craft_item, needed_item)
|
-- into the given crafting stack (`craft_item`)
|
||||||
local need_group = string.sub(needed_item, 1, 6) == "group:"
|
local function craftguide_move_stacks(inv, craft_item, needed_item, amount)
|
||||||
if need_group then
|
if craft_item:get_count() >= amount then
|
||||||
need_group = string.sub(needed_item, 7)
|
|
||||||
end
|
|
||||||
if craft_item
|
|
||||||
and not craft_item:is_empty() then
|
|
||||||
local ciname = craft_item:get_name()
|
|
||||||
|
|
||||||
-- abort if the item there isn't usable
|
|
||||||
if ciname ~= needed_item
|
|
||||||
and not need_group then
|
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
-- abort if no item fits onto it
|
local get_item_group = minetest.get_item_group
|
||||||
if craft_item:get_count() >= craft_item:get_definition().stack_max then
|
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
|
return
|
||||||
end
|
end
|
||||||
|
else
|
||||||
-- use the item there if it's in the right group and a group item is needed
|
-- Take matching group from the inventory (biggest stack)
|
||||||
if need_group then
|
local main = inv:get_list("main")
|
||||||
if minetest.get_item_group(ciname, need_group) == 0 then
|
local max_found = 0
|
||||||
return
|
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
|
||||||
needed_item = ciname
|
end
|
||||||
need_group = false
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
if not craft_item:is_empty() and
|
||||||
|
craft_item:get_name() ~= needed_item then
|
||||||
|
return -- Item must be identical
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if need_group then
|
needed_item = ItemStack(needed_item)
|
||||||
-- search an item of the specific group
|
local to_take = math.min(amount, needed_item:get_stack_max())
|
||||||
for i,item in pairs(player_inv:get_list("main")) do
|
to_take = to_take - craft_item:get_count()
|
||||||
if not item:is_empty()
|
if to_take <= 0 then
|
||||||
and minetest.get_item_group(item:get_name(), need_group) > 0 then
|
return -- Nothing to do
|
||||||
return i
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
needed_item:set_count(to_take)
|
||||||
|
|
||||||
-- no index found
|
local taken = inv:remove_item("main", needed_item)
|
||||||
return
|
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
|
end
|
||||||
|
return taken
|
||||||
-- search an item with a the name needed_item
|
|
||||||
for i,item in pairs(player_inv:get_list("main")) do
|
|
||||||
if not item:is_empty()
|
|
||||||
and item:get_name() == needed_item then
|
|
||||||
return i
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- no index found
|
|
||||||
end
|
|
||||||
|
|
||||||
-- modifies the player inventory and returns the changed craft_item if possible
|
|
||||||
local function move_item(player_inv, craft_item, needed_item)
|
|
||||||
local stackid = item_fits(player_inv, craft_item, needed_item)
|
|
||||||
if not stackid then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
local wanted_stack = player_inv:get_stack("main", stackid)
|
|
||||||
local taken_item = wanted_stack:take_item()
|
|
||||||
player_inv:set_stack("main", stackid, wanted_stack)
|
|
||||||
|
|
||||||
if not craft_item
|
|
||||||
or craft_item:is_empty() then
|
|
||||||
return taken_item
|
|
||||||
end
|
|
||||||
|
|
||||||
craft_item:add_item(taken_item)
|
|
||||||
return craft_item
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function craftguide_craft(player, formname, fields)
|
local function craftguide_craft(player, formname, fields)
|
||||||
@ -505,15 +506,21 @@ local function craftguide_craft(player, formname, fields)
|
|||||||
if amount then break end
|
if amount then break end
|
||||||
end
|
end
|
||||||
if not amount then return end
|
if not amount then return end
|
||||||
|
|
||||||
|
amount = tonumber(amount) or 99 -- fallback for "all"
|
||||||
|
if amount <= 0 or amount > 99 then return end
|
||||||
|
|
||||||
local player_name = player:get_player_name()
|
local player_name = player:get_player_name()
|
||||||
|
|
||||||
local output = unified_inventory.current_item[player_name]
|
local output = unified_inventory.current_item[player_name] or ""
|
||||||
if (not output) or (output == "") then return end
|
if output == "" then return end
|
||||||
|
|
||||||
local player_inv = player:get_inventory()
|
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]
|
local crafts = unified_inventory.crafts_for[
|
||||||
if (not crafts) or (#crafts == 0) then return end
|
unified_inventory.current_craft_direction[player_name]][output] or {}
|
||||||
|
if #crafts == 0 then return end
|
||||||
|
|
||||||
local alternate = unified_inventory.alternate[player_name]
|
local alternate = unified_inventory.alternate[player_name]
|
||||||
|
|
||||||
@ -521,24 +528,17 @@ local function craftguide_craft(player, formname, fields)
|
|||||||
if craft.width > 3 then return end
|
if craft.width > 3 then return end
|
||||||
|
|
||||||
local needed = craft.items
|
local needed = craft.items
|
||||||
|
|
||||||
local craft_list = player_inv:get_list("craft")
|
|
||||||
|
|
||||||
local width = craft.width
|
local width = craft.width
|
||||||
if width == 0 then
|
if width == 0 then
|
||||||
-- Shapeless recipe
|
-- Shapeless recipe
|
||||||
width = 3
|
width = 3
|
||||||
end
|
end
|
||||||
|
|
||||||
amount = tonumber(amount) or 99
|
-- To spread the items evenly
|
||||||
--[[
|
local STEPSIZE = math.ceil(math.sqrt(amount) / 5) * 5
|
||||||
if amount == "max" then
|
local current_count = 0
|
||||||
amount = 99 -- Arbitrary; need better way to do this.
|
repeat
|
||||||
else
|
current_count = math.min(current_count + STEPSIZE, amount)
|
||||||
amount = tonumber(amount)
|
|
||||||
end--]]
|
|
||||||
|
|
||||||
for iter = 1, amount do
|
|
||||||
local index = 1
|
local index = 1
|
||||||
for y = 1, 3 do
|
for y = 1, 3 do
|
||||||
for x = 1, width do
|
for x = 1, width do
|
||||||
@ -546,7 +546,9 @@ local function craftguide_craft(player, formname, fields)
|
|||||||
if needed_item then
|
if needed_item then
|
||||||
local craft_index = ((y - 1) * 3) + x
|
local craft_index = ((y - 1) * 3) + x
|
||||||
local craft_item = craft_list[craft_index]
|
local craft_item = craft_list[craft_index]
|
||||||
local newitem = move_item(player_inv, craft_item, needed_item)
|
local newitem = craftguide_move_stacks(player_inv,
|
||||||
|
craft_item, needed_item, current_count)
|
||||||
|
|
||||||
if newitem then
|
if newitem then
|
||||||
craft_list[craft_index] = newitem
|
craft_list[craft_index] = newitem
|
||||||
end
|
end
|
||||||
@ -554,7 +556,7 @@ local function craftguide_craft(player, formname, fields)
|
|||||||
index = index + 1
|
index = index + 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
until current_count == amount
|
||||||
|
|
||||||
player_inv:set_list("craft", craft_list)
|
player_inv:set_list("craft", craft_list)
|
||||||
|
|
||||||
@ -562,6 +564,10 @@ local function craftguide_craft(player, formname, fields)
|
|||||||
end
|
end
|
||||||
|
|
||||||
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||||
|
if formname ~= "" then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
for k, v in pairs(fields) do
|
for k, v in pairs(fields) do
|
||||||
if k:match("craftguide_craft_") then
|
if k:match("craftguide_craft_") then
|
||||||
craftguide_craft(player, formname, fields)
|
craftguide_craft(player, formname, fields)
|
||||||
|
Loading…
Reference in New Issue
Block a user