Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Barrel Tracking
- -- ----------------------------------------------------------
- -- If connected to a specified barrel, returns its item count.
- -- ----------------------------------------------------------
- -- Initialise important values:
- local barrel, unknownBarrel, tempSlot = {}, {}
- do
- local tempTable = peripheral.getNames()
- while #tempTable < 68 do
- print("I'm missing "..(68-#tempTable).." peripherals, will check again soon...")
- sleep(2)
- tempTable = peripheral.getNames()
- end
- local yieldTimer = os.clock() + 5
- for i=1,68 do
- if tempTable[i]:sub(1,6) == "barrel" then
- local tempPeri = peripheral.wrap(tempTable[i])
- local tempSlot = tempPeri.getStackInSlot(1)
- if tempSlot then
- barrel[tempSlot.name.." ("..tempSlot.id..":"..tempSlot.dmg..")"] = tempPeri
- else
- unknownBarrel[#unknownBarrel+1] = tempTable[i]
- end
- end
- if os.clock() > yieldTimer then
- sleep(1)
- yieldTimer = os.clock() + 5
- end
- end
- end
- rednet.open(peripheral.getType("right")=="modem" and "right" or "left")
- term.clear()
- term.setCursorPos(1,1)
- print("I'm an inventory server.")
- print("Other computers talk to me over rednet.")
- print("")
- print("Go find something else to play with and leave me alone!")
- print("")
- while true do
- sender, incoming = rednet.receive()
- if incoming == "Hello InvServer" then
- -- A new client wants to know my ID.
- sleep(1) -- Give the client time to switch to receiving mode.
- rednet.send(sender,"Hello, InvServerClient!")
- elseif not barrel[incoming] then for i=1,#unknownBarrel do
- if not unknownBarrel[i] then break end
- tempSlot = peripheral.call(unknownBarrel[i],"getStackInSlot",1)
- if tempSlot then
- barrel[tempSlot.name.." ("..tempSlot.id..":"..tempSlot.dmg..")"] = peripheral.wrap(unknownBarrel[i])
- table.remove(unknownBarrel,i)
- end
- end end
- if barrel[incoming] then
- -- Client is asking about a barrel.
- sleep(1) -- Give the client time to switch to receiving mode.
- if barrel[incoming].getStackInSlot(2) then
- if barrel[incoming].getStackInSlot(1).qty > 0 then
- rednet.send(sender,barrel[incoming].getStackInSlot(2).qty * 63 + barrel[incoming].getStackInSlot(1).qty)
- else
- rednet.send(sender,barrel[incoming].getStackInSlot(2).qty)
- end
- else
- rednet.send(sender,0)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement