2015-02-05 10:03:07 +01:00
|
|
|
local S = unified_inventory.gettext
|
2016-08-06 17:23:46 +02:00
|
|
|
local F = unified_inventory.fgettext
|
2014-06-21 12:44:31 +02:00
|
|
|
|
2014-06-13 11:40:52 +02:00
|
|
|
-- This pair of encoding functions is used where variable text must go in
|
|
|
|
-- button names, where the text might contain formspec metacharacters.
|
|
|
|
-- We can escape button names for the formspec, to avoid screwing up
|
|
|
|
-- form structure overall, but they then don't get de-escaped, and so
|
|
|
|
-- the input we get back from the button contains the formspec escaping.
|
|
|
|
-- This is a game engine bug, and in the anticipation that it might be
|
|
|
|
-- fixed some day we don't want to rely on it. So for safety we apply
|
|
|
|
-- an encoding that avoids all formspec metacharacters.
|
|
|
|
function unified_inventory.mangle_for_formspec(str)
|
|
|
|
return string.gsub(str, "([^A-Za-z0-9])", function (c) return string.format("_%d_", string.byte(c)) end)
|
|
|
|
end
|
|
|
|
function unified_inventory.demangle_for_formspec(str)
|
|
|
|
return string.gsub(str, "_([0-9]+)_", function (v) return string.char(v) end)
|
|
|
|
end
|
2013-09-29 00:15:37 +02:00
|
|
|
|
2015-10-05 10:24:01 +02:00
|
|
|
function unified_inventory.get_per_player_formspec(player_name)
|
|
|
|
local lite = unified_inventory.lite_mode and not minetest.check_player_privs(player_name, {ui_full=true})
|
|
|
|
|
|
|
|
local ui = {}
|
|
|
|
ui.pagecols = unified_inventory.pagecols
|
|
|
|
ui.pagerows = unified_inventory.pagerows
|
|
|
|
ui.page_y = unified_inventory.page_y
|
|
|
|
ui.formspec_y = unified_inventory.formspec_y
|
|
|
|
ui.main_button_x = unified_inventory.main_button_x
|
|
|
|
ui.main_button_y = unified_inventory.main_button_y
|
|
|
|
ui.craft_result_x = unified_inventory.craft_result_x
|
|
|
|
ui.craft_result_y = unified_inventory.craft_result_y
|
|
|
|
ui.form_header_y = unified_inventory.form_header_y
|
|
|
|
|
|
|
|
if lite then
|
|
|
|
ui.pagecols = 4
|
|
|
|
ui.pagerows = 6
|
|
|
|
ui.page_y = 0.25
|
|
|
|
ui.formspec_y = 0.47
|
|
|
|
ui.main_button_x = 8.2
|
|
|
|
ui.main_button_y = 6.5
|
|
|
|
ui.craft_result_x = 2.8
|
|
|
|
ui.craft_result_y = 3.4
|
|
|
|
ui.form_header_y = -0.1
|
|
|
|
end
|
|
|
|
|
|
|
|
ui.items_per_page = ui.pagecols * ui.pagerows
|
|
|
|
return ui, lite
|
|
|
|
end
|
|
|
|
|
2013-09-29 00:15:37 +02:00
|
|
|
function unified_inventory.get_formspec(player, page)
|
2015-10-05 10:24:01 +02:00
|
|
|
|
2013-09-29 00:15:37 +02:00
|
|
|
if not player then
|
|
|
|
return ""
|
|
|
|
end
|
2015-10-05 10:24:01 +02:00
|
|
|
|
2013-09-29 00:15:37 +02:00
|
|
|
local player_name = player:get_player_name()
|
2015-10-05 10:24:01 +02:00
|
|
|
local ui_peruser,draw_lite_mode = unified_inventory.get_per_player_formspec(player_name)
|
|
|
|
|
2013-09-29 00:15:37 +02:00
|
|
|
unified_inventory.current_page[player_name] = page
|
2013-10-03 05:25:07 +02:00
|
|
|
local pagedef = unified_inventory.pages[page]
|
2013-09-29 00:15:37 +02:00
|
|
|
|
2015-08-16 16:40:49 +02:00
|
|
|
local formspec = {
|
|
|
|
"size[14,10]",
|
|
|
|
"background[-0.19,-0.25;14.4,10.75;ui_form_bg.png]" -- Background
|
|
|
|
}
|
|
|
|
local n = 3
|
2015-06-28 09:47:03 +02:00
|
|
|
|
2015-10-05 10:24:01 +02:00
|
|
|
if draw_lite_mode then
|
2015-08-16 16:40:49 +02:00
|
|
|
formspec[1] = "size[11,7.7]"
|
|
|
|
formspec[2] = "background[-0.19,-0.2;11.4,8.4;ui_form_bg.png]"
|
2015-06-28 09:47:03 +02:00
|
|
|
end
|
|
|
|
|
2015-08-16 16:40:49 +02:00
|
|
|
if unified_inventory.is_creative(player_name)
|
|
|
|
and page == "craft" then
|
2015-10-05 10:24:01 +02:00
|
|
|
formspec[n] = "background[0,"..(ui_peruser.formspec_y + 2)..";1,1;ui_single_slot.png]"
|
2015-08-16 16:40:49 +02:00
|
|
|
n = n+1
|
2015-08-05 14:37:25 +02:00
|
|
|
end
|
|
|
|
|
2013-09-29 00:15:37 +02:00
|
|
|
-- Current page
|
2015-08-16 16:40:49 +02:00
|
|
|
if not unified_inventory.pages[page] then
|
2013-09-29 00:15:37 +02:00
|
|
|
return "" -- Invalid page name
|
|
|
|
end
|
2015-10-05 10:24:01 +02:00
|
|
|
|
|
|
|
local perplayer_formspec = unified_inventory.get_per_player_formspec(player_name)
|
|
|
|
local fsdata = pagedef.get_formspec(player, perplayer_formspec)
|
|
|
|
|
2015-08-16 16:40:49 +02:00
|
|
|
formspec[n] = fsdata.formspec
|
|
|
|
n = n+1
|
2013-09-29 00:15:37 +02:00
|
|
|
|
2015-06-28 09:47:03 +02:00
|
|
|
local button_row = 0
|
|
|
|
local button_col = 0
|
|
|
|
|
2013-09-29 00:15:37 +02:00
|
|
|
-- Main buttons
|
2015-10-05 10:24:01 +02:00
|
|
|
|
|
|
|
local filtered_inv_buttons = {}
|
|
|
|
|
2013-09-29 00:15:37 +02:00
|
|
|
for i, def in pairs(unified_inventory.buttons) do
|
2015-10-05 10:24:01 +02:00
|
|
|
if not (draw_lite_mode and def.hide_lite) then
|
|
|
|
table.insert(filtered_inv_buttons, def)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
for i, def in pairs(filtered_inv_buttons) do
|
2015-06-28 09:47:03 +02:00
|
|
|
|
2015-10-05 10:24:01 +02:00
|
|
|
if draw_lite_mode and i > 4 then
|
2015-06-28 09:47:03 +02:00
|
|
|
button_row = 1
|
|
|
|
button_col = 1
|
|
|
|
end
|
|
|
|
|
2013-09-29 00:15:37 +02:00
|
|
|
if def.type == "image" then
|
2015-08-16 16:40:49 +02:00
|
|
|
formspec[n] = "image_button["
|
2015-10-05 10:24:01 +02:00
|
|
|
formspec[n+1] = ( ui_peruser.main_button_x + 0.65 * (i - 1) - button_col * 0.65 * 4)
|
|
|
|
formspec[n+2] = ","..(ui_peruser.main_button_y + button_row * 0.7)..";0.8,0.8;"
|
2015-08-16 16:40:49 +02:00
|
|
|
formspec[n+3] = minetest.formspec_escape(def.image)..";"
|
|
|
|
formspec[n+4] = minetest.formspec_escape(def.name)..";]"
|
|
|
|
formspec[n+5] = "tooltip["..minetest.formspec_escape(def.name)
|
|
|
|
formspec[n+6] = ";"..(def.tooltip or "").."]"
|
|
|
|
n = n+7
|
2013-09-29 00:15:37 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-03 05:25:07 +02:00
|
|
|
if fsdata.draw_inventory ~= false then
|
|
|
|
-- Player inventory
|
2015-08-16 16:40:49 +02:00
|
|
|
formspec[n] = "listcolors[#00000000;#00000000]"
|
2015-10-05 10:24:01 +02:00
|
|
|
formspec[n+1] = "list[current_player;main;0,"..(ui_peruser.formspec_y + 3.5)..";8,4;]"
|
2015-08-16 16:40:49 +02:00
|
|
|
n = n+2
|
2013-10-03 05:25:07 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
if fsdata.draw_item_list == false then
|
2015-08-16 16:40:49 +02:00
|
|
|
return table.concat(formspec, "")
|
2013-10-03 05:25:07 +02:00
|
|
|
end
|
|
|
|
|
2013-09-29 00:15:37 +02:00
|
|
|
-- Controls to flip items pages
|
|
|
|
local start_x = 9.2
|
2014-06-24 12:39:12 +02:00
|
|
|
|
2015-10-05 10:24:01 +02:00
|
|
|
if not draw_lite_mode then
|
2015-08-16 16:40:49 +02:00
|
|
|
formspec[n] =
|
|
|
|
"image_button[" .. (start_x + 0.6 * 0)
|
2015-06-28 09:47:03 +02:00
|
|
|
.. ",9;.8,.8;ui_skip_backward_icon.png;start_list;]"
|
2016-08-06 17:23:46 +02:00
|
|
|
.. "tooltip[start_list;" .. F("First page") .. "]"
|
2014-06-24 12:39:12 +02:00
|
|
|
|
2015-06-28 09:47:03 +02:00
|
|
|
.. "image_button[" .. (start_x + 0.6 * 1)
|
|
|
|
.. ",9;.8,.8;ui_doubleleft_icon.png;rewind3;]"
|
2016-08-06 17:23:46 +02:00
|
|
|
.. "tooltip[rewind3;" .. F("Back three pages") .. "]"
|
2015-06-28 09:47:03 +02:00
|
|
|
.. "image_button[" .. (start_x + 0.6 * 2)
|
|
|
|
.. ",9;.8,.8;ui_left_icon.png;rewind1;]"
|
2016-08-06 17:23:46 +02:00
|
|
|
.. "tooltip[rewind1;" .. F("Back one page") .. "]"
|
2014-06-24 12:39:12 +02:00
|
|
|
|
2015-06-28 09:47:03 +02:00
|
|
|
.. "image_button[" .. (start_x + 0.6 * 3)
|
|
|
|
.. ",9;.8,.8;ui_right_icon.png;forward1;]"
|
2016-08-06 17:23:46 +02:00
|
|
|
.. "tooltip[forward1;" .. F("Forward one page") .. "]"
|
2015-06-28 09:47:03 +02:00
|
|
|
.. "image_button[" .. (start_x + 0.6 * 4)
|
|
|
|
.. ",9;.8,.8;ui_doubleright_icon.png;forward3;]"
|
2016-08-06 17:23:46 +02:00
|
|
|
.. "tooltip[forward3;" .. F("Forward three pages") .. "]"
|
2014-06-24 12:39:12 +02:00
|
|
|
|
2015-06-28 09:47:03 +02:00
|
|
|
.. "image_button[" .. (start_x + 0.6 * 5)
|
|
|
|
.. ",9;.8,.8;ui_skip_forward_icon.png;end_list;]"
|
2016-08-06 17:23:46 +02:00
|
|
|
.. "tooltip[end_list;" .. F("Last page") .. "]"
|
2015-06-28 09:47:03 +02:00
|
|
|
else
|
2015-08-16 16:40:49 +02:00
|
|
|
formspec[n] =
|
|
|
|
"image_button[" .. (8.2 + 0.65 * 0)
|
2015-06-28 09:47:03 +02:00
|
|
|
.. ",5.8;.8,.8;ui_skip_backward_icon.png;start_list;]"
|
2016-08-06 17:23:46 +02:00
|
|
|
.. "tooltip[start_list;" .. F("First page") .. "]"
|
2015-06-28 09:47:03 +02:00
|
|
|
.. "image_button[" .. (8.2 + 0.65 * 1)
|
|
|
|
.. ",5.8;.8,.8;ui_left_icon.png;rewind1;]"
|
2016-08-06 17:23:46 +02:00
|
|
|
.. "tooltip[rewind1;" .. F("Back one page") .. "]"
|
2015-06-28 09:47:03 +02:00
|
|
|
.. "image_button[" .. (8.2 + 0.65 * 2)
|
|
|
|
.. ",5.8;.8,.8;ui_right_icon.png;forward1;]"
|
2016-08-06 17:23:46 +02:00
|
|
|
.. "tooltip[forward1;" .. F("Forward one page") .. "]"
|
2015-06-28 09:47:03 +02:00
|
|
|
.. "image_button[" .. (8.2 + 0.65 * 3)
|
|
|
|
.. ",5.8;.8,.8;ui_skip_forward_icon.png;end_list;]"
|
2016-08-06 17:23:46 +02:00
|
|
|
.. "tooltip[end_list;" .. F("Last page") .. "]"
|
2015-06-28 09:47:03 +02:00
|
|
|
end
|
2015-08-16 16:40:49 +02:00
|
|
|
n = n+1
|
2013-09-29 00:15:37 +02:00
|
|
|
|
|
|
|
-- Search box
|
2015-06-28 09:47:03 +02:00
|
|
|
|
2015-10-05 10:24:01 +02:00
|
|
|
if not draw_lite_mode then
|
2015-08-16 16:40:49 +02:00
|
|
|
formspec[n] = "field[9.5,8.325;3,1;searchbox;;"
|
2015-06-28 09:47:03 +02:00
|
|
|
.. minetest.formspec_escape(unified_inventory.current_searchbox[player_name]) .. "]"
|
2015-08-16 16:40:49 +02:00
|
|
|
formspec[n+1] = "image_button[12.2,8.1;.8,.8;ui_search_icon.png;searchbutton;]"
|
2016-08-06 17:23:46 +02:00
|
|
|
.. "tooltip[searchbutton;" ..F("Search") .. "]"
|
2016-08-06 23:31:53 +02:00
|
|
|
formspec[n+2] = "image_button[12.9,8.1;.8,.8;ui_reset_icon.png;searchresetbutton;]"
|
|
|
|
.. "tooltip[searchbutton;" ..F("Search") .. "]"
|
|
|
|
.. "tooltip[searchresetbutton;" ..F("Reset search and display everything") .. "]"
|
2015-06-28 09:47:03 +02:00
|
|
|
else
|
2015-08-16 16:40:49 +02:00
|
|
|
formspec[n] = "field[8.5,5.225;2.2,1;searchbox;;"
|
2015-06-28 09:47:03 +02:00
|
|
|
.. minetest.formspec_escape(unified_inventory.current_searchbox[player_name]) .. "]"
|
2015-08-16 16:40:49 +02:00
|
|
|
formspec[n+1] = "image_button[10.3,5;.8,.8;ui_search_icon.png;searchbutton;]"
|
2016-08-06 17:23:46 +02:00
|
|
|
.. "tooltip[searchbutton;" ..F("Search") .. "]"
|
2016-08-06 23:31:53 +02:00
|
|
|
formspec[n+2] = "image_button[11,5;.8,.8;ui_reset_icon.png;searchresetbutton;]"
|
|
|
|
.. "tooltip[searchbutton;" ..F("Search") .. "]"
|
|
|
|
.. "tooltip[searchresetbutton;" ..F("Reset search and display everything") .. "]"
|
2015-06-28 09:47:03 +02:00
|
|
|
end
|
2016-08-06 23:31:53 +02:00
|
|
|
n = n+3
|
2015-06-28 09:47:03 +02:00
|
|
|
|
|
|
|
local no_matches = "No matching items"
|
2015-10-05 10:24:01 +02:00
|
|
|
if draw_lite_mode then
|
2015-06-28 09:47:03 +02:00
|
|
|
no_matches = "No matches."
|
|
|
|
end
|
2013-09-29 00:15:37 +02:00
|
|
|
|
|
|
|
-- Items list
|
2014-04-29 21:08:04 +02:00
|
|
|
if #unified_inventory.filtered_items_list[player_name] == 0 then
|
2016-08-06 17:23:46 +02:00
|
|
|
formspec[n] = "label[8.2,"..ui_peruser.form_header_y..";" .. F(no_matches) .. "]"
|
2014-04-29 21:08:04 +02:00
|
|
|
else
|
2014-06-13 16:04:20 +02:00
|
|
|
local dir = unified_inventory.active_search_direction[player_name]
|
2014-04-29 21:08:04 +02:00
|
|
|
local list_index = unified_inventory.current_index[player_name]
|
2015-10-05 10:24:01 +02:00
|
|
|
local page = math.floor(list_index / (ui_peruser.items_per_page) + 1)
|
2014-04-29 21:08:04 +02:00
|
|
|
local pagemax = math.floor(
|
|
|
|
(#unified_inventory.filtered_items_list[player_name] - 1)
|
2015-10-05 10:24:01 +02:00
|
|
|
/ (ui_peruser.items_per_page) + 1)
|
2014-04-29 21:08:04 +02:00
|
|
|
local item = {}
|
2015-10-05 10:24:01 +02:00
|
|
|
for y = 0, ui_peruser.pagerows - 1 do
|
|
|
|
for x = 0, ui_peruser.pagecols - 1 do
|
2015-06-28 09:47:03 +02:00
|
|
|
local name = unified_inventory.filtered_items_list[player_name][list_index]
|
|
|
|
if minetest.registered_items[name] then
|
2016-08-07 02:14:46 +02:00
|
|
|
-- Clicked on current item: Flip crafting direction
|
|
|
|
if name == unified_inventory.current_item[player_name] then
|
|
|
|
local cdir = unified_inventory.current_craft_direction[player_name]
|
|
|
|
if cdir == "recipe" then
|
|
|
|
dir = "usage"
|
|
|
|
elseif cdir == "usage" then
|
|
|
|
dir = "recipe"
|
|
|
|
end
|
|
|
|
else
|
|
|
|
-- Default: use active search direction by default
|
|
|
|
dir = unified_inventory.active_search_direction[player_name]
|
|
|
|
end
|
2015-08-16 16:40:49 +02:00
|
|
|
formspec[n] = "item_image_button["
|
|
|
|
..(8.2 + x * 0.7)..","
|
2015-10-05 10:24:01 +02:00
|
|
|
..(ui_peruser.formspec_y + ui_peruser.page_y + y * 0.7)..";.81,.81;"
|
2015-08-16 16:40:49 +02:00
|
|
|
..name..";item_button_"..dir.."_"
|
|
|
|
..unified_inventory.mangle_for_formspec(name)..";]"
|
|
|
|
n = n+1
|
2015-06-28 09:47:03 +02:00
|
|
|
list_index = list_index + 1
|
|
|
|
end
|
2014-04-29 21:08:04 +02:00
|
|
|
end
|
|
|
|
end
|
2016-08-06 17:23:46 +02:00
|
|
|
formspec[n] = "label[8.2,"..ui_peruser.form_header_y..";"..F("Page") .. ": "
|
2014-06-22 00:34:35 +02:00
|
|
|
.. S("%s of %s"):format(page,pagemax).."]"
|
2013-09-29 00:15:37 +02:00
|
|
|
end
|
2015-08-16 16:40:49 +02:00
|
|
|
n= n+1
|
|
|
|
|
2014-04-29 21:08:04 +02:00
|
|
|
if unified_inventory.activefilter[player_name] ~= "" then
|
2016-08-06 17:23:46 +02:00
|
|
|
formspec[n] = "label[8.2,"..(ui_peruser.form_header_y + 0.4)..";" .. F("Filter") .. ":]"
|
2015-10-05 10:24:01 +02:00
|
|
|
formspec[n+1] = "label[9.1,"..(ui_peruser.form_header_y + 0.4)..";"..minetest.formspec_escape(unified_inventory.activefilter[player_name]).."]"
|
2013-09-29 00:15:37 +02:00
|
|
|
end
|
2015-08-16 16:40:49 +02:00
|
|
|
return table.concat(formspec, "")
|
2013-09-29 00:15:37 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function unified_inventory.set_inventory_formspec(player, page)
|
|
|
|
if player then
|
2015-08-16 16:40:49 +02:00
|
|
|
player:set_inventory_formspec(unified_inventory.get_formspec(player, page))
|
2013-09-29 00:15:37 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--apply filter to the inventory list (create filtered copy of full one)
|
2014-06-13 16:04:20 +02:00
|
|
|
function unified_inventory.apply_filter(player, filter, search_dir)
|
2015-08-16 16:40:49 +02:00
|
|
|
if not player then
|
|
|
|
return false
|
|
|
|
end
|
2014-01-19 19:27:52 +01:00
|
|
|
local player_name = player:get_player_name()
|
2013-09-29 00:15:37 +02:00
|
|
|
local lfilter = string.lower(filter)
|
2014-05-01 21:44:16 +02:00
|
|
|
local ffilter
|
2014-01-19 17:34:44 +01:00
|
|
|
if lfilter:sub(1, 6) == "group:" then
|
|
|
|
local groups = lfilter:sub(7):split(",")
|
2014-05-01 21:44:16 +02:00
|
|
|
ffilter = function(name, def)
|
|
|
|
for _, group in ipairs(groups) do
|
2015-08-16 16:40:49 +02:00
|
|
|
if not def.groups[group]
|
|
|
|
or def.groups[group] <= 0 then
|
2014-05-01 21:44:16 +02:00
|
|
|
return false
|
2014-01-19 17:34:44 +01:00
|
|
|
end
|
|
|
|
end
|
2014-05-01 21:44:16 +02:00
|
|
|
return true
|
2014-01-19 17:34:44 +01:00
|
|
|
end
|
|
|
|
else
|
2014-05-01 21:44:16 +02:00
|
|
|
ffilter = function(name, def)
|
|
|
|
local lname = string.lower(name)
|
|
|
|
local ldesc = string.lower(def.description)
|
2014-06-13 09:34:29 +02:00
|
|
|
return string.find(lname, lfilter, 1, true) or string.find(ldesc, lfilter, 1, true)
|
2014-05-01 21:44:16 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
unified_inventory.filtered_items_list[player_name]={}
|
|
|
|
for name, def in pairs(minetest.registered_items) do
|
2015-08-16 16:40:49 +02:00
|
|
|
if (not def.groups.not_in_creative_inventory
|
|
|
|
or def.groups.not_in_creative_inventory == 0)
|
|
|
|
and def.description
|
|
|
|
and def.description ~= ""
|
|
|
|
and ffilter(name, def)
|
|
|
|
and (unified_inventory.is_creative(player_name)
|
|
|
|
or unified_inventory.crafts_for.recipe[def.name]) then
|
2014-05-01 21:44:16 +02:00
|
|
|
table.insert(unified_inventory.filtered_items_list[player_name], name)
|
2013-09-29 00:15:37 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
table.sort(unified_inventory.filtered_items_list[player_name])
|
2014-05-01 21:44:16 +02:00
|
|
|
unified_inventory.filtered_items_list_size[player_name] = #unified_inventory.filtered_items_list[player_name]
|
2013-09-29 00:15:37 +02:00
|
|
|
unified_inventory.current_index[player_name] = 1
|
|
|
|
unified_inventory.activefilter[player_name] = filter
|
2014-06-13 16:04:20 +02:00
|
|
|
unified_inventory.active_search_direction[player_name] = search_dir
|
2013-09-29 00:15:37 +02:00
|
|
|
unified_inventory.set_inventory_formspec(player,
|
2015-10-05 10:24:01 +02:00
|
|
|
unified_inventory.current_page[player_name])
|
2013-09-29 00:15:37 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function unified_inventory.items_in_group(groups)
|
|
|
|
local items = {}
|
|
|
|
for name, item in pairs(minetest.registered_items) do
|
|
|
|
for _, group in pairs(groups:split(',')) do
|
|
|
|
if item.groups[group] then
|
|
|
|
table.insert(items, name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return items
|
|
|
|
end
|
2014-05-22 21:24:10 +02:00
|
|
|
|
|
|
|
function unified_inventory.sort_inventory(inv)
|
|
|
|
local inlist = inv:get_list("main")
|
|
|
|
local typecnt = {}
|
|
|
|
local typekeys = {}
|
|
|
|
for _, st in ipairs(inlist) do
|
|
|
|
if not st:is_empty() then
|
|
|
|
local n = st:get_name()
|
|
|
|
local w = st:get_wear()
|
|
|
|
local m = st:get_metadata()
|
|
|
|
local k = string.format("%s %05d %s", n, w, m)
|
|
|
|
if not typecnt[k] then
|
|
|
|
typecnt[k] = {
|
|
|
|
name = n,
|
|
|
|
wear = w,
|
|
|
|
metadata = m,
|
|
|
|
stack_max = st:get_stack_max(),
|
|
|
|
count = 0,
|
|
|
|
}
|
|
|
|
table.insert(typekeys, k)
|
|
|
|
end
|
|
|
|
typecnt[k].count = typecnt[k].count + st:get_count()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
table.sort(typekeys)
|
|
|
|
local outlist = {}
|
|
|
|
for _, k in ipairs(typekeys) do
|
|
|
|
local tc = typecnt[k]
|
|
|
|
while tc.count > 0 do
|
|
|
|
local c = math.min(tc.count, tc.stack_max)
|
|
|
|
table.insert(outlist, ItemStack({
|
|
|
|
name = tc.name,
|
|
|
|
wear = tc.wear,
|
|
|
|
metadata = tc.metadata,
|
|
|
|
count = c,
|
|
|
|
}))
|
|
|
|
tc.count = tc.count - c
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if #outlist > #inlist then return end
|
|
|
|
while #outlist < #inlist do
|
|
|
|
table.insert(outlist, ItemStack(nil))
|
|
|
|
end
|
|
|
|
inv:set_list("main", outlist)
|
|
|
|
end
|