wip
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
-- Imports -----------------------------------------------------------------------------------------
|
||||
|
||||
local core = _G["core"]
|
||||
local dump2 = _G["dump2"]
|
||||
local ItemStack = _G["ItemStack"]
|
||||
local math = _G["math"]
|
||||
local string = _G["string"]
|
||||
local table = _G["table"]
|
||||
|
||||
|
||||
@@ -17,47 +16,65 @@ local HOTBAR_SIZE = 9
|
||||
|
||||
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()]
|
||||
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 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
|
||||
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 remaining == 0 then break end
|
||||
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_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 function extract_inventory_stacks(inventory, skip_count)
|
||||
skip_count = skip_count or 0
|
||||
|
||||
local result = {}
|
||||
local max_slots = inventory:get_size(MAIN)
|
||||
|
||||
for index in HOTBAR_SIZE+1, max_slots do
|
||||
for index = 1 + skip_count, max_slots do
|
||||
local stack = inventory:get_stack(MAIN, index)
|
||||
if stack ~= nil then
|
||||
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
|
||||
|
||||
@@ -68,37 +85,75 @@ local function is_inventory_node(item_def)
|
||||
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_node_inventory(inventory)
|
||||
-- TODO: implement this
|
||||
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 stacks = extract_player_inventory(player:get_inventory())
|
||||
-- TODO: implement this
|
||||
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, count, player)
|
||||
old_omi_move(pos, from_list, from_index, to_list, count, player)
|
||||
sort_node_inventory(core.get_inventory(pos))
|
||||
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(pos))
|
||||
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(pos))
|
||||
sort_node_inventory(core.get_inventory({type="node", pos=pos}))
|
||||
end)
|
||||
})
|
||||
end
|
||||
|
||||
|
||||
-- Registrations -----------------------------------------------------------------------------------
|
||||
|
||||
core.register_on_joinplayer(function(player, last_login)
|
||||
@@ -114,9 +169,11 @@ core.register_on_item_eat(function(hp_change, replace_with_item, itemstack, play
|
||||
end)
|
||||
|
||||
core.register_on_mods_loaded(function()
|
||||
for node_def in core.registered_nodes do
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user