From a7a9164e1fbe65142d987fa59a4d9b04bd991493 Mon Sep 17 00:00:00 2001 From: Andrew Miner Date: Mon, 12 Oct 2026 09:38:56 -0600 Subject: [PATCH] Work in progress --- .gitignore | 1 + .luacheckrc | 1 + LICENSE.txt | 16 +++++++ init.lua | 122 ++++++++++++++++++++++++++++++++++++++++++++++++++++ mod.conf | 4 ++ 5 files changed, 144 insertions(+) create mode 100644 .gitignore create mode 100644 .luacheckrc create mode 100644 LICENSE.txt create mode 100644 init.lua create mode 100644 mod.conf diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e43b0f9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store diff --git a/.luacheckrc b/.luacheckrc new file mode 100644 index 0000000..f95a947 --- /dev/null +++ b/.luacheckrc @@ -0,0 +1 @@ +unused_args = false diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..a244f15 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,16 @@ +Copyright (c) 2026, Andrew Miner + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and +associated documentation files (the "Software"), to deal in the Software without restriction, +including without limitation the rights to use, copy, modify, merge, publish, distribute, +sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial +portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT +NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES +OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/init.lua b/init.lua new file mode 100644 index 0000000..543c87c --- /dev/null +++ b/init.lua @@ -0,0 +1,122 @@ +-- 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) diff --git a/mod.conf b/mod.conf new file mode 100644 index 0000000..db2f34d --- /dev/null +++ b/mod.conf @@ -0,0 +1,4 @@ +name = tungstonminer_sort +title = Tungston's Inventory Sort +description = Sort the current inventory with Aux1 +author = tungstonminer