Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Function to count the total number of items in the chest
- local function countItems(chest)
- local itemCount = {}
- for slot = 1, chest.size() do
- local stack = chest.getItemDetail(slot)
- if stack then
- local itemName = stack.name
- itemCount[itemName] = (itemCount[itemName] or 0) + stack.count
- end
- end
- return itemCount
- end
- -- Function to compare two item counts
- local function compareItemCount(itemCount1, itemCount2)
- for itemName, count in pairs(itemCount1) do
- if itemCount2[itemName] and itemCount2[itemName] > count then
- return true -- itemCount2 has more of this item
- end
- end
- return false
- end
- -- Function to move items from one slot to another within the same chest
- local function moveItems(chest, slot1, slot2)
- chest.pushItems(peripheral.getName(chest), slot1, 1, slot2)
- end
- -- Function to sort the items in the chest
- local function sortChest(chest)
- local sorted = false
- while not sorted do
- sorted = true
- for slot = 1, chest.size() - 1 do
- local stack1 = chest.getItemDetail(slot)
- local stack2 = chest.getItemDetail(slot + 1)
- if stack1 and stack2 and compareItemCount({[stack1.name] = stack1.count}, {[stack2.name] = stack2.count}) then
- moveItems(chest, slot, slot + 1)
- sorted = false
- end
- end
- end
- end
- -- Main function
- local function main()
- local chest = peripheral.find("minecraft:chest")
- if chest then
- sortChest(chest)
- print("Chest sorted successfully!")
- else
- print("No chest detected.")
- end
- end
- -- Call the main function
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement