2022-01-23 18:15:17 +01:00
|
|
|
local ui = unified_inventory
|
|
|
|
|
2014-04-30 11:45:45 +02:00
|
|
|
local function default_refill(stack)
|
|
|
|
stack:set_count(stack:get_stack_max())
|
|
|
|
local itemdef = minetest.registered_items[stack:get_name()]
|
2015-08-16 16:40:49 +02:00
|
|
|
if itemdef
|
|
|
|
and (itemdef.wear_represents or "mechanical_wear") == "mechanical_wear"
|
|
|
|
and stack:get_wear() ~= 0 then
|
2014-04-30 11:45:45 +02:00
|
|
|
stack:set_wear(0)
|
|
|
|
end
|
|
|
|
return stack
|
|
|
|
end
|
2013-09-29 00:15:37 +02:00
|
|
|
|
|
|
|
minetest.register_on_joinplayer(function(player)
|
|
|
|
local player_name = player:get_player_name()
|
|
|
|
unified_inventory.players[player_name] = {}
|
2022-02-26 11:36:56 +01:00
|
|
|
unified_inventory.current_index[player_name] = 1 -- Item (~page) index
|
2013-10-03 04:20:54 +02:00
|
|
|
unified_inventory.filtered_items_list[player_name] =
|
2022-02-26 11:36:56 +01:00
|
|
|
unified_inventory.items_list
|
2013-09-29 00:15:37 +02:00
|
|
|
unified_inventory.activefilter[player_name] = ""
|
2014-06-13 16:04:20 +02:00
|
|
|
unified_inventory.active_search_direction[player_name] = "nochange"
|
2014-04-29 20:36:52 +02:00
|
|
|
unified_inventory.current_searchbox[player_name] = ""
|
2021-04-05 20:07:14 +02:00
|
|
|
unified_inventory.current_category[player_name] = "all"
|
|
|
|
unified_inventory.current_category_scroll[player_name] = 0
|
2013-09-29 00:15:37 +02:00
|
|
|
unified_inventory.alternate[player_name] = 1
|
|
|
|
unified_inventory.current_item[player_name] = nil
|
2014-06-13 16:04:20 +02:00
|
|
|
unified_inventory.current_craft_direction[player_name] = "recipe"
|
2014-01-19 19:27:52 +01:00
|
|
|
|
2013-09-29 00:15:37 +02:00
|
|
|
-- Refill slot
|
|
|
|
local refill = minetest.create_detached_inventory(player_name.."refill", {
|
|
|
|
allow_put = function(inv, listname, index, stack, player)
|
2013-10-03 04:20:54 +02:00
|
|
|
if unified_inventory.is_creative(player_name) then
|
2013-09-29 00:15:37 +02:00
|
|
|
return stack:get_count()
|
|
|
|
else
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
on_put = function(inv, listname, index, stack, player)
|
2014-04-30 11:45:45 +02:00
|
|
|
local handle_refill = (minetest.registered_items[stack:get_name()] or {}).on_refill or default_refill
|
|
|
|
stack = handle_refill(stack)
|
2013-09-29 00:36:45 +02:00
|
|
|
inv:set_stack(listname, index, stack)
|
2013-10-03 04:20:54 +02:00
|
|
|
minetest.sound_play("electricity",
|
|
|
|
{to_player=player_name, gain = 1.0})
|
2013-09-29 00:15:37 +02:00
|
|
|
end,
|
2016-11-26 19:32:21 +01:00
|
|
|
}, player_name)
|
2013-09-29 00:15:37 +02:00
|
|
|
refill:set_size("main", 1)
|
|
|
|
end)
|
|
|
|
|
2022-04-11 19:42:06 +02:00
|
|
|
minetest.register_on_mods_loaded(function()
|
|
|
|
minetest.register_on_joinplayer(function(player)
|
|
|
|
-- After everything is initialized, set up the formspec
|
|
|
|
ui.apply_filter(player, "", "nochange")
|
|
|
|
ui.set_inventory_formspec(player, unified_inventory.default)
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
2019-08-02 16:39:43 +02:00
|
|
|
local function apply_new_filter(player, search_text, new_dir)
|
|
|
|
local player_name = player:get_player_name()
|
2022-01-23 18:15:17 +01:00
|
|
|
|
2023-01-10 21:27:56 +01:00
|
|
|
minetest.sound_play("ui_click", {to_player=player_name, gain = 0.1})
|
2022-01-23 18:15:17 +01:00
|
|
|
ui.apply_filter(player, search_text, new_dir)
|
|
|
|
ui.current_searchbox[player_name] = search_text
|
|
|
|
ui.set_inventory_formspec(player, ui.current_page[player_name])
|
2019-08-02 16:39:43 +02:00
|
|
|
end
|
|
|
|
|
2013-09-29 00:15:37 +02:00
|
|
|
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
2015-10-05 10:24:01 +02:00
|
|
|
local player_name = player:get_player_name()
|
|
|
|
|
|
|
|
local ui_peruser,draw_lite_mode = unified_inventory.get_per_player_formspec(player_name)
|
|
|
|
|
2013-12-04 16:43:49 +01:00
|
|
|
if formname ~= "" then
|
|
|
|
return
|
|
|
|
end
|
2013-09-29 00:15:37 +02:00
|
|
|
|
2014-04-29 20:36:52 +02:00
|
|
|
-- always take new search text, even if not searching on it yet
|
2022-01-23 18:15:17 +01:00
|
|
|
local dirty_search_filter = false
|
|
|
|
|
2015-08-16 16:40:49 +02:00
|
|
|
if fields.searchbox
|
|
|
|
and fields.searchbox ~= unified_inventory.current_searchbox[player_name] then
|
2014-04-29 20:36:52 +02:00
|
|
|
unified_inventory.current_searchbox[player_name] = fields.searchbox
|
2022-01-23 18:15:17 +01:00
|
|
|
dirty_search_filter = true
|
2014-04-29 20:36:52 +02:00
|
|
|
end
|
|
|
|
|
2021-04-05 20:07:14 +02:00
|
|
|
|
|
|
|
local clicked_category
|
|
|
|
for name, value in pairs(fields) do
|
|
|
|
local category_name = string.match(name, "^category_(.+)$")
|
|
|
|
if category_name then
|
|
|
|
clicked_category = category_name
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if clicked_category
|
|
|
|
and clicked_category ~= unified_inventory.current_category[player_name] then
|
|
|
|
unified_inventory.current_category[player_name] = clicked_category
|
|
|
|
unified_inventory.apply_filter(player, unified_inventory.current_searchbox[player_name], "nochange")
|
|
|
|
unified_inventory.set_inventory_formspec(player,
|
|
|
|
unified_inventory.current_page[player_name])
|
|
|
|
end
|
|
|
|
|
2022-01-23 18:15:17 +01:00
|
|
|
if fields.next_category or fields.prev_category then
|
|
|
|
local step = fields.next_category and 1 or -1
|
|
|
|
local scroll_old = ui.current_category_scroll[player_name]
|
|
|
|
local scroll_new = math.max(0, math.min(#ui.category_list - ui_peruser.pagecols, scroll_old + step))
|
|
|
|
|
|
|
|
if scroll_old ~= scroll_new then
|
|
|
|
ui.current_category_scroll[player_name] = scroll_new
|
|
|
|
ui.set_inventory_formspec(player,
|
2021-04-05 20:07:14 +02:00
|
|
|
unified_inventory.current_page[player_name])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-09-29 00:15:37 +02:00
|
|
|
for i, def in pairs(unified_inventory.buttons) do
|
|
|
|
if fields[def.name] then
|
|
|
|
def.action(player)
|
2023-01-10 21:27:56 +01:00
|
|
|
minetest.sound_play("ui_click",
|
2013-09-29 00:15:37 +02:00
|
|
|
{to_player=player_name, gain = 0.1})
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
2014-01-19 19:27:52 +01:00
|
|
|
|
2013-09-29 00:15:37 +02:00
|
|
|
-- Inventory page controls
|
2013-10-03 04:20:54 +02:00
|
|
|
local start = math.floor(
|
2015-10-05 10:24:01 +02:00
|
|
|
unified_inventory.current_index[player_name] / ui_peruser.items_per_page + 1)
|
2013-09-29 00:15:37 +02:00
|
|
|
local start_i = start
|
2013-10-03 04:20:54 +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-01-19 19:27:52 +01:00
|
|
|
|
2013-09-29 00:15:37 +02:00
|
|
|
if fields.start_list then
|
|
|
|
start_i = 1
|
|
|
|
end
|
|
|
|
if fields.rewind1 then
|
|
|
|
start_i = start_i - 1
|
|
|
|
end
|
|
|
|
if fields.forward1 then
|
|
|
|
start_i = start_i + 1
|
|
|
|
end
|
|
|
|
if fields.rewind3 then
|
|
|
|
start_i = start_i - 3
|
|
|
|
end
|
|
|
|
if fields.forward3 then
|
|
|
|
start_i = start_i + 3
|
|
|
|
end
|
|
|
|
if fields.end_list then
|
|
|
|
start_i = pagemax
|
|
|
|
end
|
|
|
|
if start_i < 1 then
|
|
|
|
start_i = 1
|
|
|
|
end
|
|
|
|
if start_i > pagemax then
|
|
|
|
start_i = pagemax
|
|
|
|
end
|
2015-08-16 16:40:49 +02:00
|
|
|
if start_i ~= start then
|
2014-04-29 21:40:48 +02:00
|
|
|
minetest.sound_play("paperflip1",
|
|
|
|
{to_player=player_name, gain = 1.0})
|
2015-10-05 10:24:01 +02:00
|
|
|
unified_inventory.current_index[player_name] = (start_i - 1) * ui_peruser.items_per_page + 1
|
2013-09-29 00:15:37 +02:00
|
|
|
unified_inventory.set_inventory_formspec(player,
|
|
|
|
unified_inventory.current_page[player_name])
|
|
|
|
end
|
|
|
|
|
2019-08-02 16:39:43 +02:00
|
|
|
-- Check clicked item image button
|
2015-08-16 16:40:49 +02:00
|
|
|
local clicked_item
|
2013-10-03 04:20:54 +02:00
|
|
|
for name, value in pairs(fields) do
|
2021-11-21 19:42:54 +01:00
|
|
|
local new_dir, mangled_item = string.match(name, "^[0-9]*_?item_button_([a-z]+)_(.*)$")
|
2019-08-02 16:39:43 +02:00
|
|
|
if new_dir and mangled_item then
|
2014-06-13 16:04:20 +02:00
|
|
|
clicked_item = unified_inventory.demangle_for_formspec(mangled_item)
|
2014-05-01 15:22:25 +02:00
|
|
|
if string.sub(clicked_item, 1, 6) == "group:" then
|
2019-08-02 16:39:43 +02:00
|
|
|
-- Change search filter to this group
|
2021-04-05 20:07:14 +02:00
|
|
|
unified_inventory.current_category[player_name] = "all"
|
2019-08-02 16:39:43 +02:00
|
|
|
apply_new_filter(player, clicked_item, new_dir)
|
2014-05-01 15:22:25 +02:00
|
|
|
return
|
|
|
|
end
|
2019-08-02 16:39:43 +02:00
|
|
|
if new_dir == "recipe" or new_dir == "usage" then
|
2014-06-13 16:04:20 +02:00
|
|
|
unified_inventory.current_craft_direction[player_name] = new_dir
|
|
|
|
end
|
2013-10-03 04:20:54 +02:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if clicked_item then
|
2023-01-10 21:27:56 +01:00
|
|
|
minetest.sound_play("ui_click",
|
2013-10-03 04:20:54 +02:00
|
|
|
{to_player=player_name, gain = 0.1})
|
|
|
|
local page = unified_inventory.current_page[player_name]
|
2015-08-16 16:40:49 +02:00
|
|
|
local player_creative = unified_inventory.is_creative(player_name)
|
|
|
|
if not player_creative then
|
2013-10-03 04:20:54 +02:00
|
|
|
page = "craftguide"
|
|
|
|
end
|
|
|
|
if page == "craftguide" then
|
|
|
|
unified_inventory.current_item[player_name] = clicked_item
|
|
|
|
unified_inventory.alternate[player_name] = 1
|
2015-08-16 16:40:49 +02:00
|
|
|
unified_inventory.set_inventory_formspec(player, "craftguide")
|
|
|
|
elseif player_creative then
|
2019-08-02 16:39:43 +02:00
|
|
|
-- Creative page: Add entire stack to inventory
|
2015-08-16 16:40:49 +02:00
|
|
|
local inv = player:get_inventory()
|
|
|
|
local stack = ItemStack(clicked_item)
|
|
|
|
stack:set_count(stack:get_stack_max())
|
|
|
|
if inv:room_for_item("main", stack) then
|
|
|
|
inv:add_item("main", stack)
|
2013-09-29 00:15:37 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-10-03 04:20:54 +02:00
|
|
|
|
2016-11-29 20:45:59 +01:00
|
|
|
if fields.searchbutton
|
|
|
|
or fields.key_enter_field == "searchbox" then
|
2022-01-23 18:15:17 +01:00
|
|
|
if dirty_search_filter then
|
|
|
|
ui.apply_filter(player, ui.current_searchbox[player_name], "nochange")
|
|
|
|
ui.set_inventory_formspec(player, ui.current_page[player_name])
|
|
|
|
minetest.sound_play("paperflip2",
|
|
|
|
{to_player=player_name, gain = 1.0})
|
|
|
|
end
|
2016-08-06 23:31:53 +02:00
|
|
|
elseif fields.searchresetbutton then
|
2022-01-24 15:20:57 +01:00
|
|
|
if ui.activefilter[player_name] ~= "" then
|
2022-01-23 18:15:17 +01:00
|
|
|
apply_new_filter(player, "", "nochange")
|
|
|
|
end
|
2014-01-19 19:27:52 +01:00
|
|
|
end
|
|
|
|
|
2016-08-06 23:04:51 +02:00
|
|
|
-- alternate buttons
|
|
|
|
if not (fields.alternate or fields.alternate_prev) then
|
2015-08-16 16:40:49 +02:00
|
|
|
return
|
2013-09-29 00:15:37 +02:00
|
|
|
end
|
2023-01-10 21:27:56 +01:00
|
|
|
minetest.sound_play("ui_click",
|
2015-08-16 16:40:49 +02:00
|
|
|
{to_player=player_name, gain = 0.1})
|
|
|
|
local item_name = unified_inventory.current_item[player_name]
|
|
|
|
if not item_name then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local crafts = unified_inventory.crafts_for[unified_inventory.current_craft_direction[player_name]][item_name]
|
|
|
|
if not crafts then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local alternates = #crafts
|
|
|
|
if alternates <= 1 then
|
|
|
|
return
|
|
|
|
end
|
2016-08-06 23:04:51 +02:00
|
|
|
local alternate
|
|
|
|
if fields.alternate then
|
|
|
|
alternate = unified_inventory.alternate[player_name] + 1
|
|
|
|
if alternate > alternates then
|
|
|
|
alternate = 1
|
|
|
|
end
|
|
|
|
elseif fields.alternate_prev then
|
|
|
|
alternate = unified_inventory.alternate[player_name] - 1
|
|
|
|
if alternate < 1 then
|
|
|
|
alternate = alternates
|
|
|
|
end
|
2015-08-16 16:40:49 +02:00
|
|
|
end
|
|
|
|
unified_inventory.alternate[player_name] = alternate
|
|
|
|
unified_inventory.set_inventory_formspec(player,
|
|
|
|
unified_inventory.current_page[player_name])
|
2013-09-29 00:15:37 +02:00
|
|
|
end)
|
|
|
|
|
2022-02-26 11:36:56 +01:00
|
|
|
minetest.register_on_leaveplayer(function(player)
|
|
|
|
local player_name = player:get_player_name()
|
|
|
|
minetest.remove_detached_inventory(player_name.."_bags")
|
|
|
|
minetest.remove_detached_inventory(player_name.."refill")
|
|
|
|
end)
|