Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Functions library created for CC Tweaked to interact mostly with storage drawers controller by firex20
- -- Function name: checkRoom.lua
- -- Checks the number of free slots in an inventory and returns it
- -- checkRoom (inventory{object})
- function checkRoom (inventory)
- local inventorySize = inventory.size()
- local ocSlots = 0
- for slot, item in pairs(inventory.list()) do
- ocSlots = ocSlots+1
- end
- return inventorySize-ocSlots
- end
- -- Function name: findItem.lua
- -- Find a item from a inventory
- -- findItem (itemName, inventory{obeject})
- function findItem (name, inventory)
- local list = inventory.list()
- for slot, item in pairs(list) do
- itemDetail = inventory.getItemDetail(slot)
- itemDetail["slot"] = slot
- if name == item.name or name == itemDetail.displayName then
- return itemDetail
- end
- end
- if name ~= itemDetail.name and name ~= itemDetail.displayName then
- print("Item '"..name.."' not found")
- return nil
- end
- end
- -- Function name: pushItems.lua
- -- Push a quantity of 1 type of item from the controller to another inventory (require findItem and checkRoom functions)
- -- push (fromInventory{object}, toInventory{object}, itemName, quantity)
- function push (fromInventory, toInventory, iName, qty)
- local item = findItem(iName, fromInventory)
- if type(item) == "string" then
- return "Item not found"
- else
- local room = checkRoom(toInventory)
- if room == 0 then
- return "Destination inventory full"
- else
- if item.count < qty then
- return "Not enought "..item.displayName
- else
- local inventoryName = peripheral.getName(toInventory)
- while qty > 64 do
- fromInventory.pushItems(inventoryName, item.slot, 64)
- qty = qty-64
- room = room-1
- if room == 0 then
- term.setCursorPos(1,4)
- term.clearLine()
- term.setTextColor(4)
- print("Destination full, waiting for more room...")
- term.setTextColor(1)
- while room == 0 do
- room = checkRoom(toInventory)
- sleep(1)
- end
- term.setCursorPos(1,4)
- term.clearLine()
- term.setCursorPos(1,1)
- end
- end
- while qty ~= 0 do
- fromInventory.pushItems(inventoryName, item.slot, 1)
- qty = qty-1
- end
- return "Successful"
- end
- end
- end
- end
- -- Function name: receiveData.lua
- -- Receive data from a computer througth a modem
- -- receiveData([protocol])
- function receiveData(protocol)
- peripheral.find("modem", rednet.open)
- if rednet.isOpen() then
- if protocol == nil then
- local id, data = rednet.receive()
- else
- local id, data = rednet.receive(protocol)
- end
- return "Data received"
- else
- print("Modem not found")
- sleep(1)
- end
- end
- -- Function name: sendData.lua
- -- Send data to a computer throught a modem
- -- sendData (receiverId, data, [protocol])
- function sendData (id, data, protocol)
- peripheral.find("modem", rednet.open)
- if rednet.isOpen() then
- if protocol == nil then
- rednet.send(id, data)
- else
- rednet.send(id, data, protocol)
- end
- return "Data sended"
- else
- print("Modem not found")
- sleep(1)
- end
- end
- -- Function name: timer.lua
- -- Make a timer for stoping the program foor x time
- -- timer(time, [line])
- function timer (time, line)
- if line == nil then
- line = 3
- end
- for i=time,1,-1 do
- term.setCursorPos(1,line)
- term.clearLine()
- print("Waiting time: "..i)
- sleep(1)
- end
- term.setCursorPos(1,line)
- term.clearLine()
- term.setCursorPos(1,1)
- end
Add Comment
Please, Sign In to add comment