123 lines
3.9 KiB
Lua
123 lines
3.9 KiB
Lua
-- Imports -----------------------------------------------------------------------------------------
|
|
|
|
local core = _G["core"]
|
|
local dump2 = _G["dump2"]
|
|
local ItemStack = _G["ItemStack"]
|
|
local math = _G["math"]
|
|
local table = _G["table"]
|
|
|
|
|
|
-- Constants ---------------------------------------------------------------------------------------
|
|
|
|
local MAIN = "main"
|
|
local HOTBAR_SIZE = 9
|
|
|
|
|
|
-- Local Functions ---------------------------------------------------------------------------------
|
|
|
|
local function consolidate_stacks(stacks)
|
|
local result = {}
|
|
for stack in stacks do
|
|
local remaining = stack.get_count()
|
|
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 result do
|
|
if stack.get_name() == existing.get_name() then
|
|
local available = item_def.stack_max - existing.get_count()
|
|
if available > 0 then
|
|
local allocate = math.min(stack.get_count(), available)
|
|
existing.set_count(existing.get_count() + allocate)
|
|
remaining = remaining - allocate
|
|
|
|
if remaining == 0 then break end
|
|
end
|
|
end
|
|
end
|
|
|
|
::continue::
|
|
end
|
|
|
|
return result
|
|
end
|
|
|
|
local function extract_node_inventory(inventory)
|
|
-- TODO: implement this
|
|
end
|
|
|
|
local function extract_player_inventory(player)
|
|
local inventory = player:get_inventory()
|
|
if inventory == nil then return end
|
|
|
|
local result = {}
|
|
local max_slots = inventory:get_size(MAIN)
|
|
|
|
for index in HOTBAR_SIZE+1, max_slots do
|
|
local stack = inventory:get_stack(MAIN, index)
|
|
if stack ~= nil then
|
|
table.insert(result, stack)
|
|
end
|
|
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 function sort_node_inventory(inventory)
|
|
local stacks = extract_node_inventory(inventory)
|
|
-- TODO: implement this
|
|
end
|
|
|
|
local function sort_player_inventory(player)
|
|
local stacks = extract_player_inventory(player:get_inventory())
|
|
-- TODO: implement this
|
|
end
|
|
|
|
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, count, player)
|
|
old_omi_move(pos, from_list, from_index, to_list, count, player)
|
|
sort_node_inventory(core.get_inventory(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(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(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 node_def in core.registered_nodes do
|
|
if not is_inventory_node(node_def) then goto continue end
|
|
watch_node_inventory(node_def)
|
|
::continue::
|
|
end
|
|
end)
|