2015-02-05 10:03:07 +01:00
|
|
|
local S = unified_inventory.gettext
|
2018-04-02 13:33:36 +02:00
|
|
|
local F = minetest.formspec_escape
|
2013-09-29 00:15:37 +02:00
|
|
|
|
|
|
|
minetest.register_privilege("creative", {
|
2016-08-05 19:34:40 +02:00
|
|
|
description = S("Can use the creative inventory"),
|
2013-09-29 00:15:37 +02:00
|
|
|
give_to_singleplayer = false,
|
|
|
|
})
|
|
|
|
|
2015-10-05 10:24:01 +02:00
|
|
|
minetest.register_privilege("ui_full", {
|
2016-08-05 19:34:40 +02:00
|
|
|
description = S("Forces Unified Inventory to be displayed in Full mode if Lite mode is configured globally"),
|
2015-10-05 10:24:01 +02:00
|
|
|
give_to_singleplayer = false,
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2013-09-29 00:15:37 +02:00
|
|
|
local trash = minetest.create_detached_inventory("trash", {
|
|
|
|
--allow_put = function(inv, listname, index, stack, player)
|
|
|
|
-- if unified_inventory.is_creative(player:get_player_name()) then
|
|
|
|
-- return stack:get_count()
|
|
|
|
-- else
|
|
|
|
-- return 0
|
|
|
|
-- end
|
|
|
|
--end,
|
|
|
|
on_put = function(inv, listname, index, stack, player)
|
|
|
|
inv:set_stack(listname, index, nil)
|
|
|
|
local player_name = player:get_player_name()
|
|
|
|
minetest.sound_play("trash", {to_player=player_name, gain = 1.0})
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
trash:set_size("main", 1)
|
|
|
|
|
|
|
|
unified_inventory.register_button("craft", {
|
|
|
|
type = "image",
|
|
|
|
image = "ui_craft_icon.png",
|
2014-06-22 00:34:35 +02:00
|
|
|
tooltip = S("Crafting Grid")
|
2013-09-29 00:15:37 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
unified_inventory.register_button("craftguide", {
|
|
|
|
type = "image",
|
|
|
|
image = "ui_craftguide_icon.png",
|
2014-06-21 12:44:31 +02:00
|
|
|
tooltip = S("Crafting Guide")
|
2013-09-29 00:15:37 +02:00
|
|
|
})
|
|
|
|
|
2015-10-05 10:24:01 +02:00
|
|
|
unified_inventory.register_button("misc_set_day", {
|
|
|
|
type = "image",
|
|
|
|
image = "ui_sun_icon.png",
|
|
|
|
tooltip = S("Set time to day"),
|
|
|
|
hide_lite=true,
|
|
|
|
action = function(player)
|
|
|
|
local player_name = player:get_player_name()
|
|
|
|
if minetest.check_player_privs(player_name, {settime=true}) then
|
|
|
|
minetest.sound_play("birds",
|
|
|
|
{to_player=player_name, gain = 1.0})
|
|
|
|
minetest.set_timeofday((6000 % 24000) / 24000)
|
|
|
|
minetest.chat_send_player(player_name,
|
|
|
|
S("Time of day set to 6am"))
|
|
|
|
else
|
|
|
|
minetest.chat_send_player(player_name,
|
|
|
|
S("You don't have the settime privilege!"))
|
2016-11-06 02:28:45 +01:00
|
|
|
unified_inventory.set_inventory_formspec(player, unified_inventory.current_page[player_name])
|
2015-10-05 10:24:01 +02:00
|
|
|
end
|
|
|
|
end,
|
2016-11-06 02:28:45 +01:00
|
|
|
condition = function(player)
|
|
|
|
return minetest.check_player_privs(player:get_player_name(), {settime=true})
|
|
|
|
end,
|
2015-10-05 10:24:01 +02:00
|
|
|
})
|
2015-06-28 09:47:03 +02:00
|
|
|
|
2015-10-05 10:24:01 +02:00
|
|
|
unified_inventory.register_button("misc_set_night", {
|
|
|
|
type = "image",
|
|
|
|
image = "ui_moon_icon.png",
|
|
|
|
tooltip = S("Set time to night"),
|
|
|
|
hide_lite=true,
|
|
|
|
action = function(player)
|
|
|
|
local player_name = player:get_player_name()
|
|
|
|
if minetest.check_player_privs(player_name, {settime=true}) then
|
|
|
|
minetest.sound_play("owl",
|
|
|
|
{to_player=player_name, gain = 1.0})
|
|
|
|
minetest.set_timeofday((21000 % 24000) / 24000)
|
|
|
|
minetest.chat_send_player(player_name,
|
|
|
|
S("Time of day set to 9pm"))
|
|
|
|
else
|
|
|
|
minetest.chat_send_player(player_name,
|
|
|
|
S("You don't have the settime privilege!"))
|
2016-11-06 02:28:45 +01:00
|
|
|
unified_inventory.set_inventory_formspec(player, unified_inventory.current_page[player_name])
|
2015-10-05 10:24:01 +02:00
|
|
|
end
|
|
|
|
end,
|
2016-11-06 02:28:45 +01:00
|
|
|
condition = function(player)
|
|
|
|
return minetest.check_player_privs(player:get_player_name(), {settime=true})
|
|
|
|
end,
|
2015-10-05 10:24:01 +02:00
|
|
|
})
|
2013-09-29 00:15:37 +02:00
|
|
|
|
|
|
|
unified_inventory.register_button("clear_inv", {
|
|
|
|
type = "image",
|
|
|
|
image = "ui_trash_icon.png",
|
2014-06-21 12:44:31 +02:00
|
|
|
tooltip = S("Clear inventory"),
|
2013-09-29 00:15:37 +02:00
|
|
|
action = function(player)
|
|
|
|
local player_name = player:get_player_name()
|
|
|
|
if not unified_inventory.is_creative(player_name) then
|
|
|
|
minetest.chat_send_player(player_name,
|
2014-06-22 00:34:35 +02:00
|
|
|
S("This button has been disabled outside"
|
2013-09-29 00:15:37 +02:00
|
|
|
.." of creative mode to prevent"
|
|
|
|
.." accidental inventory trashing."
|
2014-06-22 00:34:35 +02:00
|
|
|
.."\nUse the trash slot instead."))
|
2016-11-06 02:28:45 +01:00
|
|
|
unified_inventory.set_inventory_formspec(player, unified_inventory.current_page[player_name])
|
2013-09-29 00:15:37 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
player:get_inventory():set_list("main", {})
|
2016-08-05 19:34:40 +02:00
|
|
|
minetest.chat_send_player(player_name, S('Inventory cleared!'))
|
2013-09-29 00:15:37 +02:00
|
|
|
minetest.sound_play("trash_all",
|
|
|
|
{to_player=player_name, gain = 1.0})
|
|
|
|
end,
|
2016-11-06 02:28:45 +01:00
|
|
|
condition = function(player)
|
|
|
|
return unified_inventory.is_creative(player:get_player_name())
|
|
|
|
end,
|
2013-09-29 00:15:37 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
unified_inventory.register_page("craft", {
|
2015-10-05 10:24:01 +02:00
|
|
|
get_formspec = function(player, perplayer_formspec)
|
|
|
|
|
|
|
|
local formspecy = perplayer_formspec.formspec_y
|
|
|
|
local formheadery = perplayer_formspec.form_header_y
|
|
|
|
|
2013-09-29 00:15:37 +02:00
|
|
|
local player_name = player:get_player_name()
|
2015-10-05 10:24:01 +02:00
|
|
|
local formspec = "background[2,"..formspecy..";6,3;ui_crafting_form.png]"
|
|
|
|
formspec = formspec.."background[0,"..(formspecy + 3.5)..";8,4;ui_main_inventory.png]"
|
2018-04-02 13:33:36 +02:00
|
|
|
formspec = formspec.."label[0,"..formheadery..";" ..F(S("Crafting")).."]"
|
2013-11-05 16:43:50 +01:00
|
|
|
formspec = formspec.."listcolors[#00000000;#00000000]"
|
2015-10-05 10:24:01 +02:00
|
|
|
formspec = formspec.."list[current_player;craftpreview;6,"..formspecy..";1,1;]"
|
|
|
|
formspec = formspec.."list[current_player;craft;2,"..formspecy..";3,3;]"
|
2017-03-01 23:28:40 +01:00
|
|
|
if unified_inventory.trash_enabled or unified_inventory.is_creative(player_name) or minetest.get_player_privs(player_name).give then
|
2018-04-02 13:33:36 +02:00
|
|
|
formspec = formspec.."label[7,"..(formspecy + 1.5)..";" .. F(S("Trash:")) .. "]"
|
2017-03-01 23:28:40 +01:00
|
|
|
formspec = formspec.."background[7,"..(formspecy + 2)..";1,1;ui_single_slot.png]"
|
|
|
|
formspec = formspec.."list[detached:trash;main;7,"..(formspecy + 2)..";1,1;]"
|
|
|
|
end
|
2015-10-10 04:25:49 +02:00
|
|
|
formspec = formspec.."listring[current_name;craft]"
|
|
|
|
formspec = formspec.."listring[current_player;main]"
|
2013-09-29 00:15:37 +02:00
|
|
|
if unified_inventory.is_creative(player_name) then
|
2018-04-02 13:33:36 +02:00
|
|
|
formspec = formspec.."label[0,"..(formspecy + 1.5)..";" .. F(S("Refill:")) .. "]"
|
|
|
|
formspec = formspec.."list[detached:"..F(player_name).."refill;main;0,"..(formspecy +2)..";1,1;]"
|
2013-09-29 00:15:37 +02:00
|
|
|
end
|
2013-10-03 05:25:07 +02:00
|
|
|
return {formspec=formspec}
|
2013-09-29 00:15:37 +02:00
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
2014-05-01 15:22:25 +02:00
|
|
|
-- stack_image_button(): generate a form button displaying a stack of items
|
|
|
|
--
|
2014-05-01 17:08:24 +02:00
|
|
|
-- The specified item may be a group. In that case, the group will be
|
|
|
|
-- represented by some item in the group, along with a flag indicating
|
|
|
|
-- that it's a group. If the group contains only one item, it will be
|
|
|
|
-- treated as if that item had been specified directly.
|
2014-05-12 04:00:32 +02:00
|
|
|
|
|
|
|
local function stack_image_button(x, y, w, h, buttonname_prefix, item)
|
|
|
|
local name = item:get_name()
|
|
|
|
local count = item:get_count()
|
|
|
|
local show_is_group = false
|
2016-02-09 04:42:31 +01:00
|
|
|
local displayitem = name.." "..count
|
2014-05-12 04:00:32 +02:00
|
|
|
local selectitem = name
|
|
|
|
if name:sub(1, 6) == "group:" then
|
|
|
|
local group_name = name:sub(7)
|
|
|
|
local group_item = unified_inventory.get_group_item(group_name)
|
2014-06-13 10:54:21 +02:00
|
|
|
show_is_group = not group_item.sole
|
2014-05-12 04:00:32 +02:00
|
|
|
displayitem = group_item.item or "unknown"
|
|
|
|
selectitem = group_item.sole and displayitem or name
|
2014-05-01 15:22:25 +02:00
|
|
|
end
|
2016-02-09 04:42:31 +01:00
|
|
|
local label = show_is_group and "G" or ""
|
2018-04-02 13:33:36 +02:00
|
|
|
local buttonname = F(buttonname_prefix..unified_inventory.mangle_for_formspec(selectitem))
|
2016-08-07 01:39:19 +02:00
|
|
|
local button = string.format("item_image_button[%f,%f;%f,%f;%s;%s;%s]",
|
2014-05-12 04:00:32 +02:00
|
|
|
x, y, w, h,
|
2018-04-02 13:33:36 +02:00
|
|
|
F(displayitem), buttonname, label)
|
2016-08-07 01:39:19 +02:00
|
|
|
if show_is_group then
|
|
|
|
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)
|
|
|
|
elseif andcount > 1 then
|
|
|
|
grouptip = string.format(S("Any item belonging to the groups %s"), groupstring)
|
|
|
|
end
|
2018-04-02 13:33:36 +02:00
|
|
|
grouptip = F(grouptip)
|
2016-08-07 01:39:19 +02:00
|
|
|
if andcount >= 1 then
|
|
|
|
button = button .. string.format("tooltip[%s;%s]", buttonname, grouptip)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return button
|
2014-05-01 15:22:25 +02:00
|
|
|
end
|
|
|
|
|
2014-06-13 16:04:20 +02:00
|
|
|
local recipe_text = {
|
2018-04-02 13:33:36 +02:00
|
|
|
recipe = S("Recipe %d of %d"),
|
|
|
|
usage = S("Usage %d of %d"),
|
2014-06-13 16:04:20 +02:00
|
|
|
}
|
|
|
|
local no_recipe_text = {
|
2018-04-02 13:33:36 +02:00
|
|
|
recipe = S("No recipes"),
|
|
|
|
usage = S("No usages"),
|
2014-06-13 16:04:20 +02:00
|
|
|
}
|
|
|
|
local role_text = {
|
2018-04-02 13:33:36 +02:00
|
|
|
recipe = S("Result"),
|
|
|
|
usage = S("Ingredient"),
|
2014-06-13 16:04:20 +02:00
|
|
|
}
|
2016-08-06 23:04:51 +02:00
|
|
|
local next_alt_text = {
|
2018-04-02 13:33:36 +02:00
|
|
|
recipe = S("Show next recipe"),
|
|
|
|
usage = S("Show next usage"),
|
2016-08-06 23:04:51 +02:00
|
|
|
}
|
|
|
|
local prev_alt_text = {
|
2018-04-02 13:33:36 +02:00
|
|
|
recipe = S("Show previous recipe"),
|
|
|
|
usage = S("Show previous usage"),
|
2016-08-06 23:04:51 +02:00
|
|
|
}
|
2014-06-13 16:04:20 +02:00
|
|
|
local other_dir = {
|
2018-04-08 22:21:44 +02:00
|
|
|
recipe = "usage",
|
|
|
|
usage = "recipe",
|
2014-06-13 16:04:20 +02:00
|
|
|
}
|
|
|
|
|
2013-09-29 00:15:37 +02:00
|
|
|
unified_inventory.register_page("craftguide", {
|
2015-10-05 10:24:01 +02:00
|
|
|
get_formspec = function(player, perplayer_formspec)
|
|
|
|
|
|
|
|
local formspecy = perplayer_formspec.formspec_y
|
|
|
|
local formheadery = perplayer_formspec.form_header_y
|
|
|
|
local craftresultx = perplayer_formspec.craft_result_x
|
|
|
|
local craftresulty = perplayer_formspec.craft_result_y
|
|
|
|
|
2013-09-29 00:15:37 +02:00
|
|
|
local player_name = player:get_player_name()
|
2015-02-07 18:10:14 +01:00
|
|
|
local player_privs = minetest.get_player_privs(player_name)
|
2019-03-31 11:26:02 +02:00
|
|
|
local fs = {
|
|
|
|
"background[0,"..(formspecy + 3.5)..";8,4;ui_main_inventory.png]",
|
|
|
|
"label[0,"..formheadery..";" .. F(S("Crafting Guide")) .. "]",
|
|
|
|
"listcolors[#00000000;#00000000]"
|
|
|
|
}
|
2014-06-13 13:35:12 +02:00
|
|
|
local item_name = unified_inventory.current_item[player_name]
|
2019-03-31 11:26:02 +02:00
|
|
|
if not item_name then
|
|
|
|
return { formspec = table.concat(fs) }
|
|
|
|
end
|
|
|
|
|
2016-10-29 14:20:11 +02:00
|
|
|
local item_name_shown
|
2019-03-31 11:26:02 +02:00
|
|
|
if minetest.registered_items[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)
|
2016-10-29 14:20:11 +02:00
|
|
|
else
|
|
|
|
item_name_shown = item_name
|
|
|
|
end
|
2014-06-13 13:35:12 +02:00
|
|
|
|
2014-06-13 16:04:20 +02:00
|
|
|
local dir = unified_inventory.current_craft_direction[player_name]
|
2019-03-31 11:26:02 +02:00
|
|
|
local rdir = dir == "recipe" and "usage" or "recipe"
|
|
|
|
|
2014-06-13 16:04:20 +02:00
|
|
|
local crafts = unified_inventory.crafts_for[dir][item_name]
|
|
|
|
local alternate = unified_inventory.alternate[player_name]
|
|
|
|
local alternates, craft
|
2019-03-31 11:26:02 +02:00
|
|
|
if crafts and #crafts > 0 then
|
2014-04-30 02:10:46 +02:00
|
|
|
alternates = #crafts
|
|
|
|
craft = crafts[alternate]
|
|
|
|
end
|
2019-02-17 15:14:38 +01:00
|
|
|
local has_give = player_privs.give or unified_inventory.is_creative(player_name)
|
2014-05-12 04:00:32 +02:00
|
|
|
|
2019-03-31 11:26:02 +02:00
|
|
|
fs[#fs + 1] = "background[0.5,"..(formspecy + 0.2)..";8,3;ui_craftguide_form.png]"
|
|
|
|
fs[#fs + 1] = string.format("textarea[%f,%f;10,1;;%s: %s;]",
|
|
|
|
craftresultx, craftresulty, F(role_text[dir]), item_name_shown)
|
|
|
|
fs[#fs + 1] = stack_image_button(0, formspecy, 1.1, 1.1,
|
|
|
|
"item_button_" .. rdir .. "_", ItemStack(item_name))
|
2014-06-13 16:04:20 +02:00
|
|
|
|
2014-06-13 13:35:12 +02:00
|
|
|
if not craft then
|
2019-03-31 11:26:02 +02:00
|
|
|
-- No craft recipes available for this item.
|
|
|
|
fs[#fs + 1] = "label[5.5,"..(formspecy + 2.35)..";"
|
|
|
|
.. F(no_recipe_text[dir]) .. "]"
|
2015-06-28 10:47:30 +02:00
|
|
|
local no_pos = dir == "recipe" and 4.5 or 6.5
|
|
|
|
local item_pos = dir == "recipe" and 6.5 or 4.5
|
2019-03-31 11:26:02 +02:00
|
|
|
fs[#fs + 1] = "image["..no_pos..","..formspecy..";1.1,1.1;ui_no.png]"
|
|
|
|
fs[#fs + 1] = stack_image_button(item_pos, formspecy, 1.1, 1.1,
|
|
|
|
"item_button_" .. other_dir[dir] .. "_", ItemStack(item_name))
|
2019-02-17 15:14:38 +01:00
|
|
|
if has_give then
|
2019-03-31 11:26:02 +02:00
|
|
|
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.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]"
|
2015-02-07 18:10:14 +01:00
|
|
|
end
|
2019-03-31 11:26:02 +02:00
|
|
|
return { formspec = table.concat(fs) }
|
2014-04-30 01:11:10 +02:00
|
|
|
end
|
2013-10-03 04:20:54 +02:00
|
|
|
|
2014-06-13 16:04:20 +02:00
|
|
|
local craft_type = unified_inventory.registered_craft_types[craft.type] or
|
2014-06-13 13:35:12 +02:00
|
|
|
unified_inventory.craft_type_defaults(craft.type, {})
|
2015-02-07 18:10:40 +01:00
|
|
|
if craft_type.icon then
|
2019-03-31 11:26:02 +02:00
|
|
|
fs[#fs + 1] = string.format("image[%f,%f;%f,%f;%s]",
|
|
|
|
5.7, (formspecy + 0.05), 0.5, 0.5, craft_type.icon)
|
2015-02-07 18:10:40 +01:00
|
|
|
end
|
2019-03-31 11:26:02 +02:00
|
|
|
fs[#fs + 1] = "label[5.5,"..(formspecy + 1)..";" .. F(craft_type.description).."]"
|
|
|
|
fs[#fs + 1] = stack_image_button(6.5, formspecy, 1.1, 1.1,
|
|
|
|
"item_button_usage_", ItemStack(craft.output))
|
|
|
|
|
|
|
|
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
|
2013-10-03 04:20:54 +02:00
|
|
|
|
2014-05-12 04:00:32 +02:00
|
|
|
-- This keeps recipes aligned to the right,
|
|
|
|
-- so that they're close to the arrow.
|
2016-08-07 13:20:42 +02:00
|
|
|
local xoffset = 5.5
|
|
|
|
-- Offset factor for crafting grids with side length > 4
|
|
|
|
local of = (3/math.max(3, math.max(display_size.width, display_size.height)))
|
2016-08-07 14:54:50 +02:00
|
|
|
local od = 0
|
|
|
|
-- Minimum grid size at which size optimazation measures kick in
|
|
|
|
local mini_craft_size = 6
|
|
|
|
if display_size.width >= mini_craft_size then
|
|
|
|
od = math.max(1, display_size.width - 2)
|
|
|
|
xoffset = xoffset - 0.1
|
|
|
|
end
|
2016-08-07 13:20:42 +02:00
|
|
|
-- Size modifier factor
|
2016-08-07 14:54:50 +02:00
|
|
|
local sf = math.min(1, of * (1.05 + 0.05*od))
|
2016-08-07 16:01:53 +02:00
|
|
|
-- Button size
|
2016-08-07 14:54:50 +02:00
|
|
|
local bsize_h = 1.1 * sf
|
|
|
|
local bsize_w = bsize_h
|
|
|
|
if display_size.width >= mini_craft_size then
|
|
|
|
bsize_w = 1.175 * sf
|
|
|
|
end
|
|
|
|
if (bsize_h > 0.35 and display_size.width) then
|
2014-06-13 13:15:50 +02:00
|
|
|
for y = 1, display_size.height do
|
2016-08-07 16:01:53 +02:00
|
|
|
for x = 1, display_size.width do
|
2014-06-13 13:15:50 +02:00
|
|
|
local item
|
|
|
|
if craft and x <= craft_width then
|
|
|
|
item = craft.items[(y-1) * craft_width + x]
|
|
|
|
end
|
2016-08-07 16:01:53 +02:00
|
|
|
-- Flipped x, used to build formspec buttons from right to left
|
|
|
|
local fx = display_size.width - (x-1)
|
|
|
|
-- x offset, y offset
|
|
|
|
local xof = (fx-1) * of + of
|
2016-08-07 13:20:42 +02:00
|
|
|
local yof = (y-1) * of + 1
|
2013-10-03 04:20:54 +02:00
|
|
|
if item then
|
2019-03-31 11:26:02 +02:00
|
|
|
fs[#fs + 1] = stack_image_button(
|
2016-08-07 14:54:50 +02:00
|
|
|
xoffset - xof, formspecy - 1 + yof, bsize_w, bsize_h,
|
2014-06-13 16:04:20 +02:00
|
|
|
"item_button_recipe_",
|
|
|
|
ItemStack(item))
|
2014-05-12 04:00:32 +02:00
|
|
|
else
|
|
|
|
-- Fake buttons just to make grid
|
2019-03-31 11:26:02 +02:00
|
|
|
fs[#fs + 1] = string.format("image_button[%f,%f;%f,%f;ui_blank_image.png;;]",
|
|
|
|
xoffset - xof, formspecy - 1 + yof, bsize_w, bsize_h)
|
2013-10-03 04:20:54 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-08-07 14:54:50 +02:00
|
|
|
else
|
|
|
|
-- Error
|
2019-03-31 11:26:02 +02:00
|
|
|
fs[#fs + 1] = string.format("label[2,%f;%s]",
|
|
|
|
formspecy, F(S("This recipe is too\nlarge to be displayed.")))
|
2016-08-07 14:54:50 +02:00
|
|
|
end
|
2014-04-30 01:11:10 +02:00
|
|
|
|
2016-08-07 15:08:40 +02:00
|
|
|
if craft_type.uses_crafting_grid and display_size.width <= 3 then
|
2019-03-31 11:26:02 +02:00
|
|
|
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.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")) .. "]"
|
2015-02-07 18:10:14 +01:00
|
|
|
end
|
2019-02-17 15:14:38 +01:00
|
|
|
if has_give then
|
2019-03-31 11:26:02 +02:00
|
|
|
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.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]"
|
2014-04-30 01:11:10 +02:00
|
|
|
end
|
|
|
|
|
2014-05-12 04:00:32 +02:00
|
|
|
if alternates and alternates > 1 then
|
2019-03-31 11:26:02 +02:00
|
|
|
fs[#fs + 1] = "label[5.5," .. (formspecy + 1.6) .. ";"
|
|
|
|
.. string.format(F(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]) .. "]"
|
|
|
|
.. "tooltip[alternate;" .. F(next_alt_text[dir]) .. "]"
|
2014-04-30 01:11:10 +02:00
|
|
|
end
|
2019-03-31 11:26:02 +02:00
|
|
|
return { formspec = table.concat(fs) }
|
2013-09-29 00:15:37 +02:00
|
|
|
end,
|
|
|
|
})
|
2013-10-03 05:25:07 +02:00
|
|
|
|
2015-02-07 18:10:14 +01:00
|
|
|
local function craftguide_giveme(player, formname, fields)
|
2019-02-17 15:14:38 +01:00
|
|
|
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
|
|
|
|
|
2015-02-07 18:10:14 +01:00
|
|
|
local amount
|
|
|
|
for k, v in pairs(fields) do
|
|
|
|
amount = k:match("craftguide_giveme_(.*)")
|
|
|
|
if amount then break end
|
|
|
|
end
|
|
|
|
|
2019-02-17 15:14:38 +01:00
|
|
|
amount = tonumber(amount) or 0
|
2015-02-07 18:10:14 +01:00
|
|
|
if amount == 0 then return end
|
|
|
|
|
|
|
|
local output = unified_inventory.current_item[player_name]
|
|
|
|
if (not output) or (output == "") then return end
|
|
|
|
|
|
|
|
local player_inv = player:get_inventory()
|
|
|
|
|
|
|
|
player_inv:add_item("main", {name = output, count = amount})
|
|
|
|
end
|
|
|
|
|
2019-03-02 08:56:14 +01:00
|
|
|
-- 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)
|
2019-03-02 09:27:23 +01:00
|
|
|
if craft_item:get_count() >= amount then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2019-03-02 08:56:14 +01:00
|
|
|
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
|
2015-08-16 16:40:49 +02:00
|
|
|
return
|
|
|
|
end
|
2019-03-02 08:56:14 +01:00
|
|
|
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()
|
2019-03-02 09:27:23 +01:00
|
|
|
if max_found >= amount then
|
|
|
|
break
|
|
|
|
end
|
2019-03-02 08:56:14 +01:00
|
|
|
end
|
2015-08-16 16:40:49 +02:00
|
|
|
end
|
|
|
|
end
|
2019-03-02 08:56:14 +01:00
|
|
|
else
|
|
|
|
if not craft_item:is_empty() and
|
|
|
|
craft_item:get_name() ~= needed_item then
|
|
|
|
return -- Item must be identical
|
2015-08-16 16:40:49 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-02 08:56:14 +01:00
|
|
|
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
|
2015-08-16 16:40:49 +02:00
|
|
|
end
|
2019-03-02 08:56:14 +01:00
|
|
|
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.
|
2019-03-02 09:27:23 +01:00
|
|
|
inv:add_item("main", leftover)
|
|
|
|
return taken
|
2015-08-16 16:40:49 +02:00
|
|
|
end
|
2019-03-02 08:56:14 +01:00
|
|
|
return taken
|
2015-08-16 16:40:49 +02:00
|
|
|
end
|
|
|
|
|
2015-02-07 18:10:14 +01:00
|
|
|
local function craftguide_craft(player, formname, fields)
|
2013-10-21 00:02:25 +02:00
|
|
|
local amount
|
|
|
|
for k, v in pairs(fields) do
|
|
|
|
amount = k:match("craftguide_craft_(.*)")
|
|
|
|
if amount then break end
|
|
|
|
end
|
|
|
|
if not amount then return end
|
2019-03-02 08:56:14 +01:00
|
|
|
|
|
|
|
amount = tonumber(amount) or 99 -- fallback for "all"
|
|
|
|
if amount <= 0 or amount > 99 then return end
|
|
|
|
|
2013-10-21 00:02:25 +02:00
|
|
|
local player_name = player:get_player_name()
|
|
|
|
|
2019-03-02 08:56:14 +01:00
|
|
|
local output = unified_inventory.current_item[player_name] or ""
|
|
|
|
if output == "" then return end
|
2013-10-21 00:02:25 +02:00
|
|
|
|
|
|
|
local player_inv = player:get_inventory()
|
2019-03-02 08:56:14 +01:00
|
|
|
local craft_list = player_inv:get_list("craft")
|
2013-10-21 00:02:25 +02:00
|
|
|
|
2019-03-02 08:56:14 +01:00
|
|
|
local crafts = unified_inventory.crafts_for[
|
|
|
|
unified_inventory.current_craft_direction[player_name]][output] or {}
|
|
|
|
if #crafts == 0 then return end
|
2013-10-21 00:02:25 +02:00
|
|
|
|
|
|
|
local alternate = unified_inventory.alternate[player_name]
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2019-03-02 09:27:23 +01:00
|
|
|
-- 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
|
2013-10-21 00:02:25 +02:00
|
|
|
end
|
2019-03-02 09:27:23 +01:00
|
|
|
index = index + 1
|
2013-10-21 00:02:25 +02:00
|
|
|
end
|
|
|
|
end
|
2019-03-02 09:27:23 +01:00
|
|
|
until current_count == amount
|
2013-10-21 00:02:25 +02:00
|
|
|
|
|
|
|
player_inv:set_list("craft", craft_list)
|
|
|
|
|
|
|
|
unified_inventory.set_inventory_formspec(player, "craft")
|
2015-02-07 18:10:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
2019-02-17 15:14:38 +01:00
|
|
|
if formname ~= "" then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2015-02-07 18:10:14 +01:00
|
|
|
for k, v in pairs(fields) do
|
|
|
|
if k:match("craftguide_craft_") then
|
|
|
|
craftguide_craft(player, formname, fields)
|
2015-08-16 16:40:49 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
if k:match("craftguide_giveme_") then
|
2015-02-07 18:10:14 +01:00
|
|
|
craftguide_giveme(player, formname, fields)
|
2015-08-16 16:40:49 +02:00
|
|
|
return
|
2015-02-07 18:10:14 +01:00
|
|
|
end
|
|
|
|
end
|
2013-10-21 00:02:25 +02:00
|
|
|
end)
|