Advertisement
lucifersamfr

chests

Mar 17th, 2014
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.29 KB | None | 0 0
  1. --API simplifying the use of embedded chests
  2.  
  3. --init slots table
  4. slots = {["coal"] = 16, ["signal"] = 16, ["sort"] = 16}
  5. print("All slots initialized to 16.")
  6. for key, value in pairs(slots) do
  7.   print(key.." : "..value)
  8. end
  9.  
  10.  
  11. --CHEST UTILS INTERNAL FUNCTION
  12. local function refuel(side)
  13.   print("[FUEL] refuel with at least a stack of charcoal.")
  14.   turtle.select(1)
  15.   --Wait for a full stack and refuel
  16.   while ( turtle.getItemCount(1) < 64 ) do
  17.     if side == "top" then
  18.       turtle.suckUp()
  19.     elseif side == "down" then
  20.       turtle.suckDown()
  21.     elseif side == "front" then
  22.       turtle.suck()
  23.     else
  24.       return false
  25.     end
  26.   end
  27.   turtle.refuel()
  28.   turtle.select(2)
  29.   turtle.refuel()
  30.   turtle.select(1)
  31.   return true
  32. end
  33.  
  34.  
  35. local function wait4Signal(side)
  36.   print("[SIGNAL] wait4Signal (Redstone Torch).")
  37.   local chest = peripheral.wrap(side)
  38.   local nbSlot = chest.getInventorySize()
  39.   while true do
  40.     for i = 1,nbSlot do
  41.       local tableInfo = chest.getStackInSlot(i)
  42.       if tableInfo ~= nil then
  43.         for key, value in pairs(tableInfo) do
  44.           if (key ~= "ench") then
  45.             print(key.." - "..value)
  46.           end
  47.           if ( key == "name" and value == "Redstone Torch") then -- Find Mining signal
  48.             print("Redstone Torch found! Sending operation to workers!")
  49.             return true
  50.           end
  51.         end
  52.       end
  53.     end
  54.     sleep(5)
  55.   end
  56. end
  57.  
  58.  
  59.  
  60. local function useChest(chestType, side)
  61.   --print("DEBUG useChest - chestType : "..chestType..", side : "..side)
  62.   if chestType == "coal" then
  63.     print("[INV] Providing Coal chest.")
  64.     slot = slots["coal"]
  65.   elseif chestType == "signal" then
  66.     print("[INV] Providing Signal chest.")
  67.     slot = slots["signal"]
  68.   elseif chestType == "sort" then
  69.       print("[INV] Providing Sorting chest.")
  70.       slot = slots["sort"]
  71.   else
  72.     return false
  73.   end
  74.  
  75.   --PLACE CHEST
  76.   turtle.select(slot)
  77.   if side == "top" then
  78.     while ( turtle.detectUp() ) do
  79.       turtle.digUp()
  80.     end
  81.     print("[INV] placing chest on top.")
  82.     while ( not turtle.placeUp() ) do
  83.       sleep(1)
  84.     end
  85.   elseif side == "down" then
  86.     while ( turtle.detectDown() ) do
  87.       turtle.digDown()
  88.     end
  89.     print("[INV] placing chest under.")
  90.     while ( not turtle.placeDown() ) do
  91.       sleep(1)
  92.     end
  93.   elseif side == "front" then
  94.     while ( turtle.detect() ) do
  95.       turtle.dig()
  96.     end
  97.     print("[INV] placing chest in front.")
  98.     while ( not turtle.place() ) do
  99.       sleep(1)
  100.     end
  101.   else
  102.   end
  103.  
  104.   --USE CHEST
  105.   if chestType == "signal" then
  106.     wait4Signal(side)
  107.   elseif chestType == "coal" then
  108.     refuel(side)
  109.   elseif chestType == "sort" then
  110.     print("TODO : redo sort process")
  111.   else
  112.     return false
  113.   end
  114.  
  115.   --STORE CHEST
  116.   print("[INV] retrieving chest.")
  117.   turtle.select(slot)
  118.   while ( turtle.getItemCount(slot) == 0 ) do
  119.     if side == "top" then
  120.       turtle.digUp()
  121.     elseif side == "down" then
  122.       turtle.digDown()
  123.     elseif side == "front" then
  124.       turtle.dig()
  125.     else
  126.       return false
  127.     end
  128.     sleep(1)
  129.   end
  130.  
  131.   turtle.select(1)
  132.   return true
  133. end
  134.  
  135. --CHEST UTILS USER FUNCTION
  136. function useCoalChest(side)
  137.   --print("DEBUG useCoalChest - side : "..side)
  138.   return useChest("coal", side)
  139. end
  140.  
  141. function useSignalChest(side)
  142.   --print("DEBUG useSignalChest - side : "..side)
  143.   return useChest("signal", side)
  144. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement