Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function wrapPs(peripheralName)
- periTab={}
- sideTab={}
- if peripheralName==nil then
- print("Fehler")
- end
- local peripherals = peripheral.getNames()
- local i2 = 1
- for i =1, #peripherals do
- if peripheral.getType(peripherals[i])==peripheralName then
- periTab[i2]=peripheral.wrap(peripherals[i])
- sideTab[i2]=peripherals[i]
- i2=i2+1
- end
- end
- if periTab~={} then
- return periTab,sideTab
- else
- return nil
- end
- end
- t=wrapPs("openperipheral_bridge")[1]
- function getInventorys()
- names = peripheral.getNames()
- p={}
- i2=1
- for i=1,#names do
- if peripheral.wrap(names[i]).getAllStacks~=nil then
- p[i2]=peripheral.wrap(names[i])
- i2=i2+1
- end
- end
- return p
- end
- function loadConfig()
- if fs.exists("config")==false then
- fp=fs.open("config","w")
- fp.writeLine("importDirection='north'")
- fp.writeLine("exportDirection='south'")
- fp.writeLine("windowSizeX=950")
- fp.writeLine("windowSizeY=400")
- fp.close()
- end
- shell.run("config")
- end
- function getItems(inv)
- items={}
- allStacks=inv.getAllStacks()
- n2=1
- for n1=1, inv.getInventorySize() do
- if allStacks[n1]~=nil then
- items[#items+1]=allStacks[n1].all()
- end
- end
- return items
- end
- function getItemId(item)
- if item.nbt_hash~=nil then
- itemId=item.id..item.dmg..item.nbt_hash
- else
- itemId=item.id..item.dmg
- end
- return itemId
- end
- function addStackToList(item,invIndex,slotNumber)
- itemId=getItemId(item)
- if allItems[itemId]~=nil then
- allItems[itemId].qty=allItems[itemId].qty+item.qty
- allItems[itemId].pos=item.pos
- else
- allItems[itemId]=item
- end
- end
- function scanAllItems()
- allItems={}
- itemIds={}
- for i=1,#invs do
- for n1=1, invs[i].getInventorySize() do
- allStacks=invs[i].getAllStacks()
- if allStacks[n1]~=nil then
- item=allStacks[n1].all()
- item.pos={invNum=i,slotNum=n1}
- addStackToList(item)
- end
- end
- end
- i=1
- for name in pairs(allItems) do
- itemIds[i]=name
- i=i+1
- end
- return allItems, itemIds
- end
- function fitStacks(item1,item2)
- itemCanStack=false
- itemsToStack=0
- if item1.id==item2.id and item1.dmg==item2.dmg and item2.qty<item2.max_size then
- itemCanStack=true
- end
- if itemCanStack then
- itemsToStack=item2.max_size-item2.qty
- end
- return itemCanStack, itemsToStack
- end
- function fitItemsOLD(inv)
- exportSlot=inv.getInventorySize()-1
- item=inv.getStackInSlot(exportSlot)
- items=getItems(inv)
- for ifit=1, #inv.getInventorySize() do
- if items[ifit]~=nil then
- itemCanStack,itemsToStack=fitStacks(item,items[ifit]) --NotFinished
- end
- end
- end
- function giveItem(item)
- invs[item.pos.invNum].pushItemIntoSlot(importDirection,item.pos.slotNum,1,importSlot)
- importflowCircle(invs)
- end
- function fitItems(inv)
- exportSlot=inv.getInventorySize()-1
- item=inv.getStackInSlot(exportSlot)
- items=getItems(inv)
- invHasSpace=false
- for ifit=1, inv.getInventorySize()-2 do
- if items[ifit]~=nil then
- itemCanStack,itemsToStack=fitStacks(item,items[ifit])
- if itemCanStack and itemsToStack==0 then
- invHasSpace=true
- end
- else
- invHasSpace=true
- end
- end
- if invHasSpace then
- inv.condenseItems()
- end
- return invHasSpace
- end
- function exportFlow(inv)
- importSlot=inv.getInventorySize()
- exportSlot=inv.getInventorySize()-1
- items=getItems(inv)
- if inv.getStackInSlot(exportSlot)~=nil then
- invHasSpace=fitItems(inv)
- if invHasSpace==false then
- inv.pushItemIntoSlot(exportDirection,exportSlot,64,exportSlot)
- end
- end
- end
- function importFlow(inv)
- importSlot=inv.getInventorySize()
- if inv.getStackInSlot(importSlot)~=nil then
- inv.pushItemIntoSlot(importDirection,importSlot,64,importSlot)
- end
- end
- function importflowCircle(invs)
- for i1=1,#invs do
- for i2=1,#invs do
- importFlow(invs[i2])
- end
- end
- end
- function exportflowCircle(invs)
- for i1=1,#invs do
- for i2=1,#invs do
- exportFlow(invs[i2])
- end
- end
- end
- function showItemSelection(items)
- buttons={}
- t.clear()
- y=1
- x=1
- xMax=windowSizeX
- yMax=windowSizeY
- i1=1
- t.addBox(1,1,xMax,math.ceil(#itemIds/(xMax/20))*20,0x000000,0.5)
- while i1<=yMax do
- while x<=xMax do
- if items[itemIds[i1]]~=nil then
- data={}
- data.component=t.addIcon(x,y,items[itemIds[i1]].id,items[itemIds[i1]].dmg)
- if items[itemIds[i1]].qty>1 then
- data.text=t.addText(x,y+10,tostring(items[itemIds[i1]].qty),0)
- end
- data.item=items[itemIds[i1]]
- function func(data)
- giveItem(data.item)
- print(data.item.name)
- if data.text~=nil then
- data.text.setText(data.text.getText()-1)
- else
- data.component.delete()
- end
- t.sync()
- end
- addButton(data.component.getId(),data,func)
- end
- i1=i1+1
- x=x+20
- end
- x=1
- y=y+20
- end
- t.sync()
- end
- function addButton(id,data,func)
- button={}
- button.data=data
- button.func=func
- buttons[id]=button
- end
- function showGrid()
- local x=0
- local y=0
- t.clear()
- while y<=1000 do
- while x<=1000 do
- t.addText(x+1,1,x,0)
- x=x+50
- end
- t.addText(1,y+1,y,0)
- y=y+10
- end
- t.sync()
- end
- function setAlarms()
- i=0
- while i<=24 do
- os.setAlarm(i)
- i=i+0.02
- end
- end
- function itemsInFlow(invs)
- local areItemsInFlow=false
- for i=1,#invs do
- importSlot=invs[i].getInventorySize()
- exportSlot=invs[i].getInventorySize()-1
- if invs[i].getStackInSlot(importSlot)~=nil or invs[i].getStackInSlot(exportSlot)~=nil then
- areItemsInFlow=true
- end
- end
- return areItemsInFlow
- end
- function main()
- setAlarms()
- loadConfig()
- invs=getInventorys()
- os.startTimer(1)
- while true do
- args={os.pullEvent()}
- event=args[1]
- if event=="glasses_capture" then
- scanAllItems()
- showItemSelection(allItems)
- end
- if event=="glasses_component_mouse_down" then
- componentId=args[5]
- if buttons[componentId]~=nil then
- if buttons[componentId].func~=nil then
- buttons[componentId].func(buttons[componentId].data)
- end
- end
- end
- if event=="glasses_release" then
- t.clear()
- t.sync()
- end
- if event=="alarm" then
- if itemsInFlow(invs) then
- exportflowCircle(invs)
- importflowCircle(invs)
- end
- end
- end
- end
- main()
Add Comment
Please, Sign In to add comment