180 lines
5.5 KiB
Lua
180 lines
5.5 KiB
Lua
-- Imports -----------------------------------------------------------------------------------------
|
|
|
|
local core = _G["core"]
|
|
local math = _G["math"]
|
|
local string = _G["string"]
|
|
local table = _G["table"]
|
|
|
|
|
|
-- Constants ---------------------------------------------------------------------------------------
|
|
|
|
local MAIN = "main"
|
|
local HOTBAR_SIZE = 9
|
|
|
|
|
|
-- Local Functions ---------------------------------------------------------------------------------
|
|
|
|
local function consolidate_stacks(stacks)
|
|
local result = {}
|
|
for _, stack in ipairs(stacks) do
|
|
local item_def = core.registered_items[stack:get_name()]
|
|
if item_def == nil then goto continue end
|
|
if item_def.stack_max == nil then goto continue end
|
|
|
|
for _, existing in ipairs(result) do
|
|
if stack:get_name() == existing:get_name() then
|
|
local capacity = item_def.stack_max - existing:get_count()
|
|
if capacity > 0 then
|
|
local allocate = math.min(stack:get_count(), capacity)
|
|
existing:set_count(existing:get_count() + allocate)
|
|
stack:set_count(stack:get_count() - allocate)
|
|
|
|
if stack:get_count() == 0 then break end
|
|
end
|
|
end
|
|
end
|
|
|
|
if stack:get_count() > 0 then
|
|
table.insert(result, stack)
|
|
end
|
|
|
|
::continue::
|
|
end
|
|
|
|
return result
|
|
end
|
|
|
|
local function extract_inventory_stacks(inventory, skip_count)
|
|
skip_count = skip_count or 0
|
|
|
|
local result = {}
|
|
local max_slots = inventory:get_size(MAIN)
|
|
|
|
for index = 1 + skip_count, max_slots do
|
|
local stack = inventory:get_stack(MAIN, index)
|
|
if stack == nil then goto continue end
|
|
if stack:is_empty() then goto continue end
|
|
|
|
table.insert(result, stack)
|
|
|
|
::continue::
|
|
end
|
|
|
|
return result
|
|
end
|
|
|
|
local function get_human_name(stack)
|
|
local desc = stack:get_short_description()
|
|
local split, _ = string.find(desc, ")", 1, true)
|
|
if split == nil then return desc end
|
|
return string.sub(desc, split+1)
|
|
end
|
|
|
|
local function inject_inventory(inventory, stacks, skip_count)
|
|
skip_count = skip_count or 0
|
|
local last_index = inventory:get_size(MAIN)
|
|
for inv_index = 1 + skip_count, last_index do
|
|
inventory:set_stack(MAIN, inv_index, stacks[inv_index])
|
|
end
|
|
end
|
|
|
|
local function is_inventory_node(item_def)
|
|
if item_def.allow_metadata_inventory_move then return true end
|
|
if item_def.allow_metadata_inventory_put then return true end
|
|
if item_def.allow_metadata_inventory_take then return true end
|
|
return false
|
|
end
|
|
|
|
|
|
-- Local Functions (layer 2) ---------------------------------------------------
|
|
|
|
local function sort_stacks(stacks)
|
|
table.sort(stacks, function(a, b)
|
|
if a == nil then return false end
|
|
if b == nil then return true end
|
|
|
|
local a_name = get_human_name(a)
|
|
local b_name = get_human_name(b)
|
|
if a_name ~= b_name then
|
|
return a_name < b_name
|
|
end
|
|
|
|
if a:get_count() ~= b:get_count() then
|
|
return a:get_count() > b:get_count()
|
|
end
|
|
|
|
return false
|
|
end)
|
|
return stacks
|
|
end
|
|
|
|
|
|
-- Local Functions (layer 3) ---------------------------------------------------
|
|
|
|
local function sort_node_inventory(inventory)
|
|
local stacks = extract_inventory_stacks(inventory)
|
|
stacks = consolidate_stacks(stacks)
|
|
stacks = sort_stacks(stacks)
|
|
inject_inventory(inventory, stacks)
|
|
end
|
|
|
|
local function sort_player_inventory(player)
|
|
local inventory = player:get_inventory()
|
|
if inventory == nil then return end
|
|
|
|
local stacks = extract_inventory_stacks(inventory, HOTBAR_SIZE)
|
|
stacks = consolidate_stacks(stacks)
|
|
stacks = sort_stacks(stacks)
|
|
inject_inventory(inventory, stacks, HOTBAR_SIZE)
|
|
end
|
|
|
|
|
|
-- Local Functions (layer 4) ---------------------------------------------------
|
|
|
|
local function watch_node_inventory(node_def)
|
|
local old_omi_move = node_def.on_metadata_inventory_move
|
|
local old_omi_put = node_def.on_metadata_inventory_put
|
|
local old_omi_take = node_def.on_metadata_inventory_take
|
|
|
|
core.override_item(node_def.name, {
|
|
on_metadata_inventory_move = (function(
|
|
pos, from_list, from_index, to_list, to_index, count, player)
|
|
old_omi_move(pos, from_list, from_index, to_list, to_index, count, player)
|
|
sort_node_inventory(core.get_inventory({type="node", pos=pos}))
|
|
end),
|
|
on_metadata_inventory_put = (function(pos, listname, index, stack, player)
|
|
old_omi_put(pos, listname, index, stack, player)
|
|
sort_node_inventory(core.get_inventory({type="node", pos=pos}))
|
|
end),
|
|
on_metadata_inventory_take = (function(pos, listname, index, stack, player)
|
|
old_omi_take(pos, listname, index, stack, player)
|
|
sort_node_inventory(core.get_inventory({type="node", pos=pos}))
|
|
end)
|
|
})
|
|
end
|
|
|
|
|
|
-- Registrations -----------------------------------------------------------------------------------
|
|
|
|
core.register_on_joinplayer(function(player, last_login)
|
|
sort_player_inventory(player)
|
|
end)
|
|
|
|
core.register_on_player_inventory_action(function(player, action, inventory, inventory_info)
|
|
sort_player_inventory(player)
|
|
end)
|
|
|
|
core.register_on_item_eat(function(hp_change, replace_with_item, itemstack, player, pointed_thing)
|
|
sort_player_inventory(player)
|
|
end)
|
|
|
|
core.register_on_mods_loaded(function()
|
|
for name, node_def in pairs(core.registered_nodes) do
|
|
if not is_inventory_node(node_def) then goto continue end
|
|
|
|
watch_node_inventory(node_def)
|
|
|
|
::continue::
|
|
end
|
|
end)
|