ecco7777

CC Chest Storage System

Oct 5th, 2018
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.47 KB | None | 0 0
  1. function wrapPs(peripheralName)
  2.     periTab={}
  3.     sideTab={}
  4.     if peripheralName==nil then
  5.         print("Fehler")
  6.     end
  7.     local peripherals = peripheral.getNames()
  8.     local i2 = 1
  9.     for i =1, #peripherals do
  10.         if peripheral.getType(peripherals[i])==peripheralName then
  11.             periTab[i2]=peripheral.wrap(peripherals[i])
  12.             sideTab[i2]=peripherals[i]
  13.             i2=i2+1
  14.         end
  15.     end
  16.     if periTab~={} then
  17.         return periTab,sideTab
  18.     else
  19.         return nil
  20.     end
  21. end
  22.  
  23. t=wrapPs("openperipheral_bridge")[1]
  24.  
  25. function getInventorys()
  26.     names = peripheral.getNames()
  27.     p={}
  28.     i2=1
  29.     for i=1,#names do
  30.         if peripheral.wrap(names[i]).getAllStacks~=nil then
  31.             p[i2]=peripheral.wrap(names[i])
  32.             i2=i2+1
  33.         end
  34.     end
  35.     return p
  36. end
  37.  
  38. function loadConfig()
  39.     if fs.exists("config")==false then
  40.         fp=fs.open("config","w")
  41.         fp.writeLine("importDirection='north'")
  42.         fp.writeLine("exportDirection='south'")
  43.         fp.writeLine("windowSizeX=950")
  44.         fp.writeLine("windowSizeY=400")
  45.         fp.close()
  46.     end
  47.     shell.run("config")
  48. end
  49.  
  50. function getItems(inv)
  51.     items={}
  52.     allStacks=inv.getAllStacks()
  53.     n2=1
  54.     for n1=1, inv.getInventorySize() do
  55.         if allStacks[n1]~=nil then
  56.             items[#items+1]=allStacks[n1].all()
  57.         end
  58.     end
  59.     return items
  60. end
  61.  
  62. function getItemId(item)
  63.     if item.nbt_hash~=nil then
  64.         itemId=item.id..item.dmg..item.nbt_hash
  65.     else
  66.         itemId=item.id..item.dmg
  67.     end
  68.     return itemId
  69. end
  70.  
  71. function addStackToList(item,invIndex,slotNumber)
  72.     itemId=getItemId(item)
  73.     if allItems[itemId]~=nil then
  74.         allItems[itemId].qty=allItems[itemId].qty+item.qty
  75.         allItems[itemId].pos=item.pos
  76.     else
  77.         allItems[itemId]=item
  78.     end
  79. end
  80.  
  81. function scanAllItems()
  82. allItems={}
  83. itemIds={}
  84.     for i=1,#invs do
  85.         for n1=1, invs[i].getInventorySize() do
  86.             allStacks=invs[i].getAllStacks()
  87.             if allStacks[n1]~=nil then
  88.                 item=allStacks[n1].all()
  89.                 item.pos={invNum=i,slotNum=n1}
  90.                 addStackToList(item)
  91.             end
  92.         end
  93.     end
  94.     i=1
  95.     for name in pairs(allItems) do
  96.         itemIds[i]=name
  97.         i=i+1
  98.     end
  99.     return allItems, itemIds
  100. end
  101.  
  102. function fitStacks(item1,item2)
  103.     itemCanStack=false
  104.     itemsToStack=0
  105.     if item1.id==item2.id and item1.dmg==item2.dmg and item2.qty<item2.max_size then
  106.         itemCanStack=true
  107.     end
  108.     if itemCanStack then
  109.         itemsToStack=item2.max_size-item2.qty
  110.     end
  111.     return itemCanStack, itemsToStack
  112. end
  113.  
  114. function fitItemsOLD(inv)
  115.     exportSlot=inv.getInventorySize()-1
  116.     item=inv.getStackInSlot(exportSlot)
  117.     items=getItems(inv)
  118.     for ifit=1, #inv.getInventorySize() do
  119.         if items[ifit]~=nil then
  120.             itemCanStack,itemsToStack=fitStacks(item,items[ifit]) --NotFinished
  121.         end
  122.     end
  123. end
  124.  
  125. function giveItem(item)
  126.     invs[item.pos.invNum].pushItemIntoSlot(importDirection,item.pos.slotNum,1,importSlot)
  127.     importflowCircle(invs)
  128. end
  129.  
  130. function fitItems(inv)
  131.     exportSlot=inv.getInventorySize()-1
  132.     item=inv.getStackInSlot(exportSlot)
  133.     items=getItems(inv)
  134.     invHasSpace=false
  135.     for ifit=1, inv.getInventorySize()-2 do
  136.         if items[ifit]~=nil then
  137.             itemCanStack,itemsToStack=fitStacks(item,items[ifit])
  138.             if itemCanStack and itemsToStack==0 then
  139.                 invHasSpace=true
  140.             end
  141.         else
  142.             invHasSpace=true
  143.         end
  144.     end
  145.     if invHasSpace then
  146.         inv.condenseItems()
  147.     end
  148.     return invHasSpace
  149. end
  150.  
  151. function exportFlow(inv)
  152.     importSlot=inv.getInventorySize()
  153.     exportSlot=inv.getInventorySize()-1
  154.     items=getItems(inv)
  155.     if inv.getStackInSlot(exportSlot)~=nil then
  156.         invHasSpace=fitItems(inv)
  157.         if invHasSpace==false then
  158.             inv.pushItemIntoSlot(exportDirection,exportSlot,64,exportSlot)
  159.         end
  160.     end
  161. end
  162.  
  163. function importFlow(inv)
  164.     importSlot=inv.getInventorySize()
  165.     if inv.getStackInSlot(importSlot)~=nil then
  166.         inv.pushItemIntoSlot(importDirection,importSlot,64,importSlot)
  167.     end
  168. end
  169.  
  170. function importflowCircle(invs)
  171.     for i1=1,#invs do
  172.         for i2=1,#invs do
  173.             importFlow(invs[i2])
  174.         end
  175.     end
  176. end
  177.  
  178. function exportflowCircle(invs)
  179.     for i1=1,#invs do
  180.         for i2=1,#invs do
  181.             exportFlow(invs[i2])
  182.         end
  183.     end
  184. end
  185.  
  186. function showItemSelection(items)
  187.     buttons={}
  188.     t.clear()
  189.     y=1
  190.     x=1
  191.     xMax=windowSizeX
  192.     yMax=windowSizeY
  193.     i1=1
  194.     t.addBox(1,1,xMax,math.ceil(#itemIds/(xMax/20))*20,0x000000,0.5)
  195.     while i1<=yMax do
  196.         while x<=xMax do
  197.             if items[itemIds[i1]]~=nil then
  198.                 data={}
  199.                 data.component=t.addIcon(x,y,items[itemIds[i1]].id,items[itemIds[i1]].dmg)
  200.                 if items[itemIds[i1]].qty>1 then
  201.                     data.text=t.addText(x,y+10,tostring(items[itemIds[i1]].qty),0)
  202.                 end
  203.                
  204.                 data.item=items[itemIds[i1]]
  205.                 function func(data)
  206.                     giveItem(data.item)
  207.                     print(data.item.name)
  208.                     if data.text~=nil then
  209.                         data.text.setText(data.text.getText()-1)
  210.                     else
  211.                         data.component.delete()
  212.                     end
  213.                     t.sync()
  214.                 end
  215.                 addButton(data.component.getId(),data,func)
  216.             end
  217.             i1=i1+1
  218.             x=x+20
  219.         end
  220.         x=1
  221.         y=y+20
  222.     end
  223.     t.sync()
  224. end
  225.  
  226. function addButton(id,data,func)
  227.     button={}
  228.     button.data=data
  229.     button.func=func
  230.     buttons[id]=button
  231. end
  232.  
  233. function showGrid()
  234.     local x=0
  235.     local y=0
  236.     t.clear()
  237.         while y<=1000 do  
  238.                 while x<=1000 do
  239.                     t.addText(x+1,1,x,0)
  240.                     x=x+50
  241.                 end
  242.             t.addText(1,y+1,y,0)
  243.             y=y+10
  244.         end
  245.     t.sync()
  246. end
  247.  
  248. function setAlarms()
  249.     i=0
  250.     while i<=24 do
  251.         os.setAlarm(i)
  252.         i=i+0.02
  253.     end
  254. end
  255.  
  256. function itemsInFlow(invs)
  257.     local areItemsInFlow=false
  258.     for i=1,#invs do
  259.         importSlot=invs[i].getInventorySize()
  260.         exportSlot=invs[i].getInventorySize()-1
  261.         if invs[i].getStackInSlot(importSlot)~=nil or invs[i].getStackInSlot(exportSlot)~=nil then
  262.             areItemsInFlow=true
  263.         end
  264.     end
  265.     return areItemsInFlow
  266. end
  267.  
  268. function main()
  269.     setAlarms()
  270.     loadConfig()
  271.     invs=getInventorys()
  272.     os.startTimer(1)
  273.     while true do
  274.         args={os.pullEvent()}
  275.         event=args[1]
  276.  
  277.         if event=="glasses_capture" then
  278.             scanAllItems()
  279.             showItemSelection(allItems)
  280.         end
  281.         if event=="glasses_component_mouse_down" then
  282.             componentId=args[5]
  283.             if buttons[componentId]~=nil then
  284.                 if buttons[componentId].func~=nil then
  285.                     buttons[componentId].func(buttons[componentId].data)
  286.                 end
  287.             end
  288.         end
  289.         if event=="glasses_release" then
  290.             t.clear()
  291.             t.sync()
  292.         end
  293.         if event=="alarm" then
  294.             if itemsInFlow(invs) then
  295.             exportflowCircle(invs)
  296.             importflowCircle(invs)
  297.             end
  298.         end
  299.     end
  300. end
  301.  
  302. main()
Add Comment
Please, Sign In to add comment