Advertisement
gelatine87

Colorapplicator

Aug 16th, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.01 KB | None | 0 0
  1. local maxItem = 2176
  2.  
  3. --######################--
  4.  
  5. local IMPORT = true
  6. local chest = {}
  7. local hasChest = false
  8. local hasRChest = false
  9. local rChestName = ""
  10. local Int = {}
  11. local hasInt = false
  12. local hasRInt = false
  13. local rIntName = ""
  14. local directions = {"top", "bottom", "front", "back", "left", "right"}
  15. local validItems ={"Snowball","Paint Ball"}
  16. local IIDs =
  17.     {
  18.         "minecraft:snowball",
  19.         "appliedenergistics2:item.ItemPaintBall",
  20.         "appliedenergistics2:item.ItemPaintBall",
  21.         "appliedenergistics2:item.ItemPaintBall",
  22.         "appliedenergistics2:item.ItemPaintBall",
  23.         "appliedenergistics2:item.ItemPaintBall",
  24.         "appliedenergistics2:item.ItemPaintBall",
  25.         "appliedenergistics2:item.ItemPaintBall",
  26.         "appliedenergistics2:item.ItemPaintBall",
  27.         "appliedenergistics2:item.ItemPaintBall",
  28.         "appliedenergistics2:item.ItemPaintBall",
  29.         "appliedenergistics2:item.ItemPaintBall",
  30.         "appliedenergistics2:item.ItemPaintBall",
  31.         "appliedenergistics2:item.ItemPaintBall",
  32.         "appliedenergistics2:item.ItemPaintBall",
  33.         "appliedenergistics2:item.ItemPaintBall",
  34.         "appliedenergistics2:item.ItemPaintBall"
  35.     }
  36. local IDMGs =
  37.     {
  38.         0,
  39.         0,
  40.         1,
  41.         2,
  42.         3,
  43.         4,
  44.         5,
  45.         6,
  46.         7,
  47.         8,
  48.         9,
  49.         10,
  50.         11,
  51.         12,
  52.         13,
  53.         14,
  54.         15
  55.     }
  56. local exportDirection = "south"
  57.  
  58. local function findPeripherals()
  59.     for k, v in pairs(peripheral.getNames()) do
  60.         if string.find(v, "draconic_chest") then
  61.             hasChest = true
  62.             hasRChest = true
  63.             rChestName = v
  64.             term.setCursorPos(1, 1)
  65.             term.write("INFO: Found remote "..v)
  66.             os.sleep(1)
  67.         elseif string.find(v, "tileinterface") then
  68.             hasInt = true
  69.             hasRInt = true
  70.             rIntName = v
  71.             term.setCursorPos(1, 2)
  72.             term.write("INFO: Found remote "..v)
  73.             os.sleep(1)
  74.         end
  75.     end
  76. end
  77.  
  78. local function findInt()
  79.     if hasRInt then return end
  80.         for k, v in pairs(directions) do
  81.             local p = peripheral.wrap(v)
  82.             if p ~= nil then
  83.                 if string.find(peripheral.getType(v), "tileinterface") then
  84.                     Int = p
  85.                     hasInt = true
  86.                     return
  87.                 end
  88.             end
  89.         end
  90.     term.setCursorPos(1, 2)
  91.     term.write("ERROR: No Interface detected!")
  92.     os.sleep(5)
  93. end
  94.  
  95. local function findChest()
  96.     if hasRChest then return end
  97.     for k, v in pairs(directions) do
  98.         local p = peripheral.wrap(v)
  99.         if p ~= nil then
  100.             if string.find(peripheral.getType(v), "draconic_chest") then
  101.                 chest = p
  102.                 hasChest = true
  103.                 return
  104.             end
  105.         end
  106.     end
  107.     term.setCursorPos(1, 1)
  108.     term.write("WARNING: No Draconic Chest detected!")
  109.     os.sleep(5)
  110. end
  111.  
  112. function invSize()
  113.     local chest
  114.     if hasChest then
  115.         chest = peripheral.call(rChestName, "getInventorySize")
  116.     else
  117.         chest = chest.getInventorySize()
  118.     end
  119.     return chest
  120. end
  121.  
  122. function StackSlot(Slot)
  123.     local chest
  124.     if hasChest then
  125.         chest = peripheral.call(rChestName, "getStackInSlot", Slot)
  126.     else
  127.         chest = chest.getStackInSlot(Slot)
  128.     end
  129.     return chest
  130. end
  131.  
  132. function string.starts(String,Start)
  133.     pre = string.sub(Start,1,string.len(String))
  134.     return pre == Start
  135. end
  136.  
  137. function tablelength(T)
  138.     local count = 0
  139.     for _ in pairs(T) do
  140.         count = count + 1
  141.     end
  142.     return count
  143. end
  144.  
  145. function contains(table, String)
  146.     for i = 1, tablelength(table), 1 do
  147.         if string.starts(String, table[i]) then
  148.             return true
  149.         end
  150.     end
  151.     return false
  152. end
  153.  
  154. function isValidItem(itemStack)
  155.     if contains(validItems, itemStack.display_name) then
  156.         return true
  157.     end
  158.     return false
  159. end
  160.  
  161. function setGreen(sSide)
  162.     rs.setBundledOutput(sSide, colors.green)
  163. end
  164.  
  165. function chestEmpty()
  166.     ic = 0
  167.     for i = 1, invSize() - 1, 1 do
  168.         stack = StackSlot(i)
  169.         if stack ~= nil then
  170.             if isValidItem(stack) then
  171.                 ic = ic + stack.qty
  172.             end
  173.         end
  174.     end
  175.     return ic == 0
  176. end
  177.  
  178. function setRed(sSide)
  179.     rs.setBundledOutput(sSide, colors.red)
  180. end
  181.  
  182. function setNull(sSide)
  183.     rs.setBundledOutput(sSide, 0)
  184. end
  185.  
  186. function chestValid(validItemCount)
  187.     if validItemCount > maxItem then
  188.         term.setCursorPos(1, 3)
  189.         term.write("ERROR - Chest inventory exceeds set storage limits!")
  190.     elseif validItemCount == maxItem then
  191.         setGreen("bottom")
  192.         IMPORT = false
  193.     end
  194. end
  195.  
  196. local function init()
  197.     findPeripherals()
  198.     findChest()
  199.     findInt()
  200. end
  201.  
  202. init()
  203.  
  204. function refill()
  205.     for _, aes in pairs(Int.getAvailableItems()) do
  206.         for i = 1, tablelength(IIDs), 1 do
  207.             id = IIDs[i]
  208.             dmg = IDMGs[i]
  209.             if aes.fingerprint.id == id and aes.fingerprint.dmg == dmg then
  210.                 valS = 0
  211.                 for j = 1, invSize() - 1, 1 do
  212.                     stack = StackSlot(j)
  213.                     if stack ~= nil then
  214.                         if stack.id == id and stack.dmg == dmg then
  215.                             valS = valS + stack.qty
  216.                             --print(aReq)
  217.                             --print(valS)
  218.                         end
  219.                     end
  220.                 end
  221.                 aReq = 128 - valS
  222.                 if aReq > 0 then
  223.                     --print(aes.size)
  224.                     --print(aReq)
  225.                     if aes.size > aReq then
  226.                         --Transfer items from ME to Chest (*aReq)
  227.                         Int.exportItem(aes.fingerprint, exportDirection, aReq)
  228.                     else
  229.                         --Transfer and request recrafting
  230.                         Int.requestCrafting(aes.fingerprint, 512)
  231.                     end
  232.                 end
  233.             end
  234.         end
  235.      end
  236. end
  237.  
  238. while true do
  239.     if IMPORT then
  240.         slot = 0
  241.         slotqt = 0
  242.         valI = 0
  243.         for CurrSlot = 1, invSize() -1, 1 do
  244.             tableIS = StackSlot(CurrSlot)
  245.             if tableIS ~= nil then
  246.                 slotqt = tableIS.qty
  247.                 slot = slot + slotqt
  248.                 if isValidItem(tableIS) then
  249.                     --print("valid")
  250.                     valI = valI + slotqt
  251.                 else
  252.                     term.setCursorPos(1, 3)
  253.                     term.write("Invalid Items found! (Please kill the person responsible.)")
  254.                 end
  255.                 --print("Slot Stack size: "..slotqt)
  256.                 --print("Total Item count: "..slot)
  257.                 --print("Valid Items: "..valI)
  258.             end
  259.         end
  260.         if valI >= maxItem then
  261.             chestValid(valI)
  262.         else
  263.             term.setCursorPos(1, 3)
  264.             term.write("Refilling chest")
  265.             refill()
  266.         end
  267.     else
  268.         prevImport = IMPORT
  269.         IMPORT = chestEmpty()
  270.     end
  271.     if not IMPORT then
  272.         term.setCursorPos(1, 3)
  273.         term.write("Import Process on hold until chest is emptied")
  274.         sleep(2)
  275.     elseif prevImport == false then
  276.         term.setCursorPos(1, 3)
  277.         term.write("Pausing Operations for Refilling Color Applicator")
  278.         setRed("bottom")
  279.         sleep(1)
  280.         setNull("bottom")
  281.     else
  282.         sleep(5)
  283.     end
  284. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement