Advertisement
Shaka01

bigStorageMove startup

Feb 2nd, 2023 (edited)
824
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.03 KB | None | 0 0
  1. -- inventory to remove items from
  2. storage_controller = peripheral.wrap("occultism:storage_controller_0")
  3.  
  4. -- inventory to store big amount of items in
  5. targetInventory = peripheral.wrap("minecraft:chest_0")
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12. if fs.exists("addItem") == false then
  13. shell.run("pastebin", "get", "8f0wHqLK", "addItem")
  14. shell.run("addItem")
  15. end
  16.  
  17. -- Connect peripherals
  18. local sides = {"left", "right", "top", "bottom", "front", "back"}
  19. for i, side in ipairs(sides) do
  20.   local device = peripheral.getType(side)
  21.   if device == "modem" then
  22.     rednet.open(side)
  23. elseif device == "monitor" then
  24.     monitor = peripheral.wrap(side)
  25.   elseif device then
  26.     inventory = peripheral.wrap(side)
  27. end
  28. end
  29. -- load the list of wanted items
  30. wantedItems = dofile("wanteditems.lua")
  31. switchmenu = false
  32. targetName = peripheral.getName(targetInventory)
  33.  
  34.  
  35. function getItemCounts()
  36. -- keep track of the total amount of each item to be moved
  37. totalAmount = {}
  38. -- loop through all wanted items
  39.     for wantedItemName, wantedAmount in pairs(wantedItems) do
  40.       -- initialize the total amount of each item to be moved
  41.       totalAmount[wantedItemName] = 0
  42.         for slot, item in pairs(storage_controller.list()) do
  43.             if item.name == wantedItemName then
  44.                 totalAmount[wantedItemName] = totalAmount[wantedItemName] + item.count
  45.                 -- print(wantedItemName, totalAmount[wantedItemName])
  46.             end
  47.         end
  48.     end
  49. end
  50.  
  51. function calculateDifference()
  52. removeAmount = {}
  53.     for items, count in pairs(wantedItems) do
  54.         -- removeAmount[items] = 0
  55.         if totalAmount[items] > count then
  56.             removeAmount[items] = totalAmount[items] - count
  57.             term.setTextColor(colors.yellow)
  58.             -- print(items, removeAmount[items])
  59.              term.setTextColor(colors.white)
  60.             suckItem(items, count)
  61.         end
  62.     end
  63. end
  64.  
  65. function centerText(monitor, y, text)
  66.   local w, h = monitor.getSize()
  67.   local x = math.floor((w - string.len(text) + 2) / 2)
  68.   monitor.setCursorPos(x , y)
  69.   monitor.write(text)
  70. end
  71.  
  72. function splitAndWrite(str)
  73.   local index = string.find(str, " ")
  74.   if index then
  75.     local firstPart = string.sub(str, 1, index)
  76.     local secondPart = string.sub(str, index+1)
  77.     centerText(monitor, 4, firstPart)
  78.     centerText(monitor, 5, secondPart)
  79.   else
  80.     centerText(monitor, 4, str)
  81.   end
  82. end
  83.  
  84.  
  85.  
  86.  
  87. function suckItem(itemName, targetCount)
  88. monitor.setTextColor(colors.black)
  89. monitor.setBackgroundColor(colors.yellow)
  90.     for slot, item in pairs(storage_controller.list()) do
  91.     -- centerText(monitor, 3, screenKey)
  92.             if item.name == itemName then
  93.                 while removeAmount[item.name] > 0 do
  94.                         if removeAmount[item.name] == 0  then
  95.                             break
  96.                         end
  97.                         if removeAmount[item.name] >= item.count then
  98.                      countCC = item.count
  99.                         else
  100.                     countCC = removeAmount[item.name]
  101.                         end
  102.                         local transferred = storage_controller.pushItems(peripheral.getName(targetInventory), slot, countCC)
  103.                         -- sleep(0.1)
  104.                         removeAmount[item.name] = removeAmount[item.name] - transferred
  105.                         -- print(slot, item.name, removeAmount[item.name], countCC)
  106.                         displayCount = removeAmount[item.name]
  107.                         monitor.clear()
  108.                         screenKey = string.match(item.name, ":(.*)")
  109.                         screenKey = string.gsub(screenKey, "_", " ")
  110.                         monitor.setTextScale(0.5)
  111.                         monitor.setCursorPos(1,1)
  112.                         splitAndWrite(screenKey)
  113.                         centerText(monitor, 6, tostring(displayCount))
  114.                         if item.count > 0 then
  115.                         suckItem(itemName, targetCount)
  116.                         end
  117.                        
  118.                         break
  119.                 end
  120.             end
  121.     end
  122. end
  123.  
  124. function allthethings()
  125. getItemCounts()
  126. monitor.setBackgroundColor(colors.green)
  127. monitor.clear()
  128. centerText(monitor, 4, "Waiting")
  129. calculateDifference()
  130. sleep(1)
  131. end
  132.  
  133.  
  134. function switchToMenu()
  135.     local event, key, isHeld = os.pullEvent("mouse_click")
  136.     if event then
  137.         switchmenu = true
  138.     end
  139. end
  140.  
  141. while true do
  142. term.clear()
  143. x, y = term.getSize()
  144. term.setCursorPos(9,y/2)
  145. print("Click to search and add items.")
  146.  
  147. if switchmenu == true then
  148.     shell.run("addItem")
  149.     switchmenu = false
  150. end
  151. parallel.waitForAny(allthethings, switchToMenu)
  152. end
  153.  
  154. -- scanAndSendInventories(targetInventory)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement