subtixx

Oxygen tank supplier

Aug 27th, 2016
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.31 KB | None | 0 0
  1. local component = require("component")
  2. local pi = component.playerinterface
  3. local chest = component.chest
  4. local chatbox = nil
  5.  
  6. ------ CONFIG SECTION STARTS HERE -------
  7.  
  8. pi.setOutputSide("south") -- Hopper side
  9. pi.setInputSide("west") -- Oxygen supply chest side
  10.  
  11. local playername = "Subtixx" -- (Your) Playername to chat to
  12. local delay = 5 -- Delay between checks
  13. local tankToKeepInInv = 3 -- Amount of tanks to keep supplied in your inventory
  14. local shouldSmallTanksCount = false -- Count small tanks to tank keep in inv
  15. local stockSmallTanks = false -- Should small tanks be transferred to player
  16. local stockHotbar = false -- Should the hotbar counted in when searching for a free inventory spot?
  17. local enableChatBox = true --
  18.  
  19. ------ DO NOT MODIFY BEYOND THIS POINT -------
  20. if(enableChatBox) then
  21.   chatbox = component.peripheralsplusplus_chatbox
  22. end
  23.  
  24. local player = pi.getPlayerInv(playername).value
  25.  
  26. local cooldown = {}
  27.  
  28. function outputCooldown(text)
  29.   if(cooldown[text] ~= nil) then
  30.     if(cooldown[text] <= (os.time() * 1000/60/60) - 6000) then
  31.       cooldown[text] = nil
  32.     end
  33.   else
  34.     if(enableChatbox) then
  35.       chatbox.tell(playername, tostring(text))
  36.     end
  37.     print(tostring(text))
  38.     cooldown[text] = (os.time() * 1000/60/60) + 6000 -- 12000 (-6000)
  39.   end
  40. end
  41.  
  42. function findFirstFreeInventorySlot()
  43.   for i=0, 36, 1 do
  44.     local itemStack = player.getStackInSlot(i)
  45.     if(itemStack == nil) then
  46.       return i
  47.     end
  48.   end
  49.   outputCooldown("NO ROOM! YOUR INVENTORY IS FULL!")
  50. end
  51.  
  52. function findFirstOxygenChestSlot()
  53.   for i=1, chest.getInventorySize() do
  54.     local itemStack = chest.getStackInSlot(i)
  55.     if(itemStack ~= nil) then
  56.       if(itemStack.id == "GalacticraftCore:item.oxygenTankHeavyFull" or
  57.           (stockSmallTanks and (itemStack.id == "GalacticraftCore:item.oxygenTankMedFull" or
  58.             itemStack.id == "GalacticraftCore:item.oxygenTankLightFull"))) then
  59.         if(i <= 9 and stockHotbar) or (i > 9) then
  60.            return i - 1
  61.         end
  62.       end
  63.     end
  64.   end  
  65.   outputCooldown("NO OXYGEN TANKS LEFT! CHECK THE OXYGEN CHEST!")
  66. end
  67.  
  68. function replaceTanks()
  69.   local totalTanks = 0
  70.   for i=0, 36, 1 do
  71.     local itemStack = player.getStackInSlot(i)
  72.     if(itemStack ~= nil) then
  73.       if(itemStack.name == "GalacticraftCore:item.oxygenTankHeavyFull") then
  74.         if(itemStack.meta >= 2700) then
  75.           -- Extract
  76.           player.retrieveFromSlot(i, 1)
  77.         else
  78.           totalTanks = totalTanks + 1
  79.         end
  80.       elseif(itemStack.name == "GalacticraftCore:item.oxygenTankMedFull") then
  81.         if(itemStack.meta >= 1800) then
  82.           player.retrieveFromSlot(i, 1)
  83.         elseif(shouldSmallTanksCount) then
  84.           totalTanks = totalTanks + 1
  85.         end
  86.       elseif(itemStack.name == "GalacticraftCore:item.oxygenTankLightFull") then
  87.         if(itemStack.meta >= 900) then
  88.           player.retrieveFromSlot(i, 1)
  89.         elseif(shouldSmallTanksCount) then
  90.           totalTanks = totalTanks + 1
  91.         end
  92.       end
  93.     end
  94.   end
  95.  
  96.   while totalTanks < tankToKeepInInv do
  97.     local oxygenSlot = findFirstOxygenChestSlot()
  98.     local inventorySlot = findFirstFreeInventorySlot()
  99.  
  100.     player.push(oxygenSlot, inventorySlot)
  101.     totalTanks = totalTanks + 1
  102.   end
  103. end
  104.  
  105. while true do
  106.   replaceTanks()
  107.   os.sleep(delay)
  108. end
Add Comment
Please, Sign In to add comment