firex20

functionLibrary

Aug 17th, 2022 (edited)
599
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.17 KB | Software | 0 0
  1. -- Functions library created for CC Tweaked to interact mostly with storage drawers controller by firex20
  2.  
  3.  
  4. -- Function name: checkRoom.lua
  5. -- Checks the number of free slots in an inventory and returns it
  6. -- checkRoom (inventory{object})
  7. function checkRoom (inventory)
  8.     local inventorySize = inventory.size()
  9.     local ocSlots = 0
  10.     for slot, item in pairs(inventory.list()) do
  11.         ocSlots = ocSlots+1
  12.     end
  13.     return inventorySize-ocSlots
  14. end
  15.  
  16. -- Function name: findItem.lua
  17. -- Find a item from a inventory
  18. -- findItem (itemName, inventory{obeject})
  19. function findItem (name, inventory)
  20.     local list = inventory.list()
  21.     for slot, item in pairs(list) do
  22.         itemDetail = inventory.getItemDetail(slot)
  23.         itemDetail["slot"] = slot
  24.         if name == item.name or name == itemDetail.displayName then
  25.             return itemDetail
  26.         end
  27.     end
  28.     if name ~= itemDetail.name and name ~= itemDetail.displayName then
  29.         print("Item '"..name.."' not found")
  30.         return nil
  31.     end
  32. end
  33.  
  34. -- Function name: pushItems.lua
  35. -- Push a quantity of 1 type of item from the controller to another inventory (require findItem and checkRoom functions)
  36. -- push (fromInventory{object}, toInventory{object}, itemName, quantity)
  37. function push (fromInventory, toInventory, iName, qty)
  38.     local item = findItem(iName, fromInventory)
  39.     if type(item) == "string" then
  40.         return "Item not found"
  41.     else
  42.         local room = checkRoom(toInventory)
  43.         if room == 0 then
  44.             return "Destination inventory full"
  45.         else
  46.             if item.count < qty then
  47.                 return "Not enought "..item.displayName
  48.             else
  49.                 local inventoryName = peripheral.getName(toInventory)
  50.                 while qty > 64 do
  51.                     fromInventory.pushItems(inventoryName, item.slot, 64)
  52.                     qty = qty-64
  53.                     room = room-1
  54.                     if room == 0 then
  55.                         term.setCursorPos(1,4)
  56.                         term.clearLine()
  57.                         term.setTextColor(4)
  58.                         print("Destination full, waiting for more room...")
  59.                         term.setTextColor(1)
  60.                         while room == 0 do
  61.                             room = checkRoom(toInventory)
  62.                             sleep(1)
  63.                         end
  64.                         term.setCursorPos(1,4)
  65.                         term.clearLine()
  66.                         term.setCursorPos(1,1)
  67.                     end
  68.                 end
  69.                 while qty ~= 0 do
  70.                     fromInventory.pushItems(inventoryName, item.slot, 1)
  71.                     qty = qty-1
  72.                 end
  73.                 return "Successful"
  74.             end
  75.         end
  76.     end
  77.  
  78. end
  79.  
  80. -- Function name: receiveData.lua
  81. -- Receive data from a computer througth a modem
  82. -- receiveData([protocol])
  83. function receiveData(protocol)
  84.     peripheral.find("modem", rednet.open)
  85.     if rednet.isOpen() then
  86.         if protocol == nil then
  87.             local id, data = rednet.receive()
  88.         else
  89.             local id, data = rednet.receive(protocol)
  90.         end
  91.         return "Data received"
  92.     else
  93.         print("Modem not found")
  94.         sleep(1)
  95.     end
  96. end
  97.  
  98. -- Function name: sendData.lua
  99. -- Send data to a computer throught a modem
  100. -- sendData (receiverId, data, [protocol])
  101. function sendData (id, data, protocol)
  102.     peripheral.find("modem", rednet.open)
  103.     if rednet.isOpen() then
  104.         if protocol == nil then
  105.             rednet.send(id, data)
  106.         else
  107.             rednet.send(id, data, protocol)
  108.         end
  109.         return "Data sended"
  110.     else
  111.         print("Modem not found")
  112.         sleep(1)
  113.     end
  114. end
  115.  
  116. -- Function name: timer.lua
  117. -- Make a timer for stoping the program foor x time
  118. -- timer(time, [line])
  119. function timer (time, line)
  120.     if line == nil then
  121.         line = 3
  122.     end
  123.     for i=time,1,-1 do
  124.         term.setCursorPos(1,line)
  125.         term.clearLine()
  126.         print("Waiting time: "..i)
  127.         sleep(1)
  128.     end
  129.     term.setCursorPos(1,line)
  130.     term.clearLine()
  131.     term.setCursorPos(1,1)
  132. end
  133.  
Add Comment
Please, Sign In to add comment