Advertisement
xX-AAAAAAAAAA-Xx

ComputerCraft Storage System

Sep 2nd, 2024 (edited)
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. -- Wrap peripherals
  2. local modem = peripheral.wrap("back") -- Adjust if needed
  3. local inputChest = peripheral.wrap("minecraft:chest_9") -- Your input chest
  4. local storageChests = {
  5. peripheral.wrap("minecraft:chest_5"),
  6. peripheral.wrap("minecraft:chest_6"),
  7. peripheral.wrap("minecraft:chest_7"),
  8. peripheral.wrap("minecraft:chest_8")
  9. }
  10.  
  11. -- Print guide for commands
  12. local function printGuide()
  13. print("Commands:")
  14. print(" str - Prints this guide.")
  15. print(" str -i - Inserts input chest's contents into the system.")
  16. print(" str -e [string] - Extracts and lists all items with a name containing [string].")
  17. end
  18.  
  19. function split(input, delimiter)
  20. local result = {}
  21. local start = 1
  22. local delimiterStart, delimiterEnd = string.find(input, delimiter, start)
  23.  
  24. while delimiterStart do
  25. table.insert(result, string.sub(input, start, delimiterStart - 1))
  26. start = delimiterEnd + 1
  27. delimiterStart, delimiterEnd = string.find(input, delimiter, start)
  28. end
  29.  
  30. table.insert(result, string.sub(input, start))
  31. return result
  32. end
  33.  
  34. -- Insert items from input chest to storage chests
  35. local function insertItems()
  36. local items = inputChest.list()
  37. for slot, item in pairs(items) do
  38. local remaining = item.count
  39. for _, chest in pairs(storageChests) do
  40. if remaining <= 0 then break end
  41.  
  42. local freeSpace = chest.getFreeSpace(item.name)
  43. local moveCount = math.min(remaining, freeSpace)
  44.  
  45. if moveCount > 0 then
  46. inputChest.pushItems(chest.getName(), slot, moveCount)
  47. remaining = remaining - moveCount
  48. end
  49. end
  50. end
  51. print("Items inserted into the system.")
  52. end
  53.  
  54. -- Extract and list items by name
  55. local function extractItems(searchString)
  56. local results = {}
  57. for _, chest in pairs(storageChests) do
  58. local items = chest.list()
  59. for slot, item in pairs(items) do
  60. if string.find(item.name, searchString) then
  61. table.insert(results, {name = item.name, count = item.count})
  62. end
  63. end
  64. end
  65.  
  66. if #results == 0 then
  67. print("No items found containing: " .. searchString)
  68. else
  69. print("Items containing '" .. searchString .. "':")
  70. for _, result in pairs(results) do
  71. print("Item: " .. result.name .. " Count: " .. result.count)
  72. end
  73. end
  74. end
  75.  
  76. -- Main command handler
  77. local function handleCommand(command, ...)
  78. if command == "str" then
  79. printGuide()
  80. elseif command == "str -i" then
  81. insertItems()
  82. elseif command == "str -e" then
  83. local searchString = table.concat({...}, " ")
  84. extractItems(searchString)
  85. else
  86. print("Unknown command. Use 'str' for help.")
  87. end
  88. end
  89.  
  90. -- Main loop
  91. while true do
  92. print("Enter command:")
  93. local input = read()
  94. local args = split(input, " ")
  95. local command = args[1]
  96. table.remove(args, 1)
  97. handleCommand(command, table.unpack(args))
  98. end
  99.  
  100. -- Utility function to split a string into a table of words
  101. function split(inputstr, sep)
  102. if sep == nil then
  103. sep = "%s"
  104. end
  105. local t = {}
  106. for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  107. table.insert(t, str)
  108. end
  109. return t
  110. end
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement