Advertisement
D_Puppy

Applied energistics 2 interface export library

Aug 27th, 2016
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.21 KB | None | 0 0
  1. local interface = {}
  2.  
  3. local component = require("component")
  4. --local ser = require("serialization")
  5.  
  6. if component.isAvailable("me_interface") == false then
  7.   print("No interface attached found")
  8.   os.exit()
  9. end
  10. local meInterface = component.getPrimary('me_interface')
  11.  
  12. local directionChest = "NONE"               -- Where the chest is connected to the interface
  13. local exportTime = 0.8                  -- time to wait after exporting an item
  14.  
  15. function interface.setDirection(direction)
  16.   if direction ~= "UP" then
  17.     if direction ~= "SOUTH" then
  18.       if direction ~= "NORTH" then
  19.     if direction ~= "WEST" then
  20.       if direction ~= "EAST" then
  21.         if direction ~= "DOWN" then
  22.           return false, "Direction must be UP, NORTH, SOUTH, WEST, EAST or DOWN"
  23.         end
  24.       end
  25.     end
  26.       end
  27.     end
  28.   end
  29.   directionChest = direction
  30.   if meInterface.canExport(direction) == false then
  31.     return false, "Interface can't export to " .. direction
  32.   end
  33.   return true
  34. end
  35.  
  36. function interface.setExportTime(time)
  37.   if time < 0 then
  38.     return false, "Export time must be 0 or positive"
  39.   end
  40.   exportTime = time
  41.   return true
  42. end
  43.  
  44. function getItem(selection)
  45.   local tmpTable = {}
  46.   local x = meInterface.getItemsInNetwork()     -- Get all items in network
  47.   for key, value in pairs(x) do             -- loop over the list
  48.     if key ~= "n" then
  49.       if value.label == selection then          -- label of selected item found
  50.     -- build the fingerprint
  51.     tmpTable["id"] = value.name
  52.     tmpTable["dmg"] = value.damage
  53.     return tmpTable, value.size         -- return amount of items in network and the fingerprint
  54.       end
  55.     end
  56.   end
  57.   return false, "Item not found"            -- return nil if not found
  58. end
  59.  
  60.  
  61. function interface.getItemList()
  62.   local returnTable = {}
  63.   local x = meInterface.getItemsInNetwork()     -- Get all items in network
  64.   if x == nil then
  65.     return false, "No items found"
  66.   end
  67.   for key, value in pairs(x) do             -- loop over the list
  68.     if key ~= "n" then
  69.       local tmpTable = {}               -- clear temp table
  70.       tmpTable["size"] = value.size
  71.       tmpTable["label"] = value.label
  72.       table.insert(returnTable, tmpTable)       -- insert item in return table
  73.     end
  74.   end
  75.   return returnTable                    -- return the list
  76. end
  77.  
  78. function interface.export(itemLabel, amount, dots)
  79.   if directionChest == "NONE" then
  80.     return false, "Direction not set"
  81.   end
  82.   if directionChest ~= "UP" then
  83.     if directionChest ~= "SOUTH" then
  84.       if directionChest ~= "NORTH" then
  85.     if directionChest ~= "WEST" then
  86.       if directionChest ~= "EAST" then
  87.         if directionChest ~= "DOWN" then
  88.           return false, "Direction must be UP, NORTH, SOUTH, WEST, EAST or DOWN"
  89.         end
  90.       end
  91.     end
  92.       end
  93.     end
  94.   end
  95.   if meInterface.canExport(directionChest) == false then
  96.     return false, "Interface can't export to " .. directionChest
  97.   end
  98.   local item, count = getItem(itemLabel)
  99.   if item == false then
  100.     return false, "Item not found"
  101.   end
  102.   if count < amount then
  103.     amount = count
  104.   end
  105.   for x = 1, amount do
  106.     meInterface.exportItem(item, directionChest, 1, 0)  -- Export item
  107.     os.sleep(exportTime)                -- wait for export
  108.     if dots == true then
  109.       io.write(".")
  110.     end
  111.   end
  112.   return true, amount
  113. end
  114.  
  115. return interface
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement