Guest User

Untitled

a guest
Jun 16th, 2016
679
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.73 KB | None | 0 0
  1. --================================================================================================--
  2. --
  3. --  Applied Energistics Item Exporter
  4. --  Version 0.2
  5. --
  6. --  Original Author : Zacherl
  7. --  Modifications   :
  8. --
  9. --================================================================================================--
  10. --
  11. -- This program was designed to work with the mods and versions in the "FTB Infinity 1.10.1" pack.
  12. --
  13. -- Features:
  14. --  * Periodically exports items from an Applied Energistics ME Interface to an adjacent inventory
  15. --  * Ability to preserve a certain amount of items in the system.
  16. --
  17. -- Table format (items.cfg):
  18. -- {
  19. --   {
  20. --     fingerprint = {
  21. --       id = "minecraft:gold_ore",
  22. --       dmg = 0,
  23. --       nbt_hash = "3288456351062a1d4b01b5774241a664"
  24. --     },
  25. --     name = "Gold Ore",
  26. --     preserve = 1000,
  27. --   },
  28. -- }
  29. --
  30. -- The "dmg" and "nbt_hash" fields are optional. Just remove them to ignore damage values and
  31. -- NBT data for the specified item.
  32. --
  33. --================================================================================================--
  34.  
  35. -- The name or side of the ME Interface.
  36. local interfaceName   = "top"
  37.  
  38. -- The export direction (target inventory) relative to the ME Interface.
  39. local exportDirection = "north"
  40.  
  41. -- The export program tick interval. Recommended range is between 5 and 60 seconds.
  42. local tickInterval    = 5
  43.  
  44. --================================================================================================--
  45.  
  46. -- Internal variables
  47. local interface = nil
  48. local exports   = {}
  49.  
  50. -- Main
  51. local function mainTick()
  52.   local items = interface.getAvailableItems()
  53.   for index, item in pairs(exports) do
  54.     local fingerprint = {}
  55.     local itemCount = 0
  56.     for _, a in pairs(items) do
  57.       if ((a.fingerprint.id == item.fingerprint.id) and
  58.           ((item.fingerprint.dmg == nil) or (a.fingerprint.dmg == item.fingerprint.dmg)) and
  59.           ((item.fingerprint.nbt_hash == nil) or
  60.           (a.fingerprint.nbt_hash == item.fingerprint.nbt_hash))) then
  61.         fingerprint = a.fingerprint
  62.         itemCount = a.size
  63.         break
  64.       end
  65.     end
  66.     itemCount = itemCount - item.preserve
  67.     if (itemCount > 0) then
  68.       print("Exporting [" .. item.name .. "]")
  69.       print("  id      : " .. fingerprint.id)
  70.       print("  dmg     : " .. fingerprint.dmg)
  71.       print("  nbt_hash: " .. fingerprint.nbt_hash)
  72.       print("  Amount  : " .. itemCount + item.preserve)
  73.       print("  Preserve: " .. item.preserve)
  74.       while (itemCount > 0) do
  75.         -- Prevent "Too long without yielding" error
  76.         local loopTimerId = os.startTimer(0.1)
  77.         while (true) do
  78.           local amount = 64
  79.           if (itemCount < amount) then
  80.             amount = itemCount
  81.           end
  82.           local status = interface.exportItem(fingerprint, exportDirection, amount)
  83.           itemCount = itemCount - status.size
  84.           if (status.size ~= amount) then
  85.             print("Container space exceeded.")
  86.             return
  87.           end
  88.           local event, timerId = os.pullEvent("timer")    
  89.           if (timerId == loopTimerId) then
  90.             break
  91.           end
  92.         end
  93.  
  94.       end
  95.     end
  96.   end
  97. end
  98.  
  99. local function main()
  100.   interface = peripheral.wrap(interfaceName)
  101.   if (fs.exists("items.cfg")) then
  102.     local f = fs.open("items.cfg", "r")
  103.     exports = textutils.unserialise(f.readAll())
  104.     f.close()
  105.   end
  106.   while (true) do
  107.     mainTick()
  108.     local loopTimerId = os.startTimer(tickInterval)
  109.     while (true) do
  110.       local event, timerId = os.pullEvent("timer")    
  111.       if (timerId == loopTimerId) then
  112.         break
  113.       end
  114.     end
  115.   end
  116. end
  117.  
  118. while true do
  119.   -- Prevent "not attached" error
  120.   pcall(main())
  121. end
Advertisement
Add Comment
Please, Sign In to add comment