Hiranus

Level Emitter Extended

Jul 13th, 2015
512
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.79 KB | None | 0 0
  1. function Item (name,damage,hash)
  2.     local tab = {}
  3.    
  4.     tab.mode = "above"
  5.     tab.amount = 50
  6.     tab.fingerprint = Fingerprint(name,damage,hash)
  7.     tab.redstone = {}
  8.     tab.redstone.def = true
  9.     tab.redstone.side = "top"
  10.     tab.redstone.bundled = 0
  11.     return tab
  12. end
  13. function Fingerprint (name,damage,hash)
  14.     local tab = {}
  15.     tab.id = name
  16.     tab.dmg = damage
  17.     tab.nbt_hash = hash
  18.     return tab
  19. end
  20.  
  21. function Set_Redstone(state,item)
  22.    
  23.     if item.redstone.bundled==0 then
  24.         rs.setOutput(item.redstone.side,state)
  25.     else
  26.         signal = rs.testBundledInput(item.redstone.side,item.redstone.bundled)
  27.         if signal ~= state then
  28.             if signal ==true then
  29.                 rs.setBundledOutput(item.redstone.side,rs.getBundledInput(item.redstone.side) - item.redstone.bundled)
  30.             else
  31.                 rs.setBundledOutput(item.redstone.side,rs.getBundledInput(item.redstone.side) + item.redstone.bundled)
  32.             end
  33.         end
  34.     end
  35. end
  36. local items = {}
  37. items.levels = {}
  38. function PrepareConfig ()
  39.     items.levels[1] = Item("minecraft:redstone",0,nil)
  40.     items.interval = 5
  41.     cfg=fs.open("config.cfg","w")
  42.     cfg.write(textutils.serialize(items))
  43.     cfg.flush()
  44.     cfg.close()
  45. end
  46. function LoadConfig()
  47.     local cfg = fs.open("config.cfg", "r")
  48.     items = textutils.unserialize( cfg.readAll() )  
  49.     cfg.close()
  50. end
  51.  
  52.  
  53. local aeInterface
  54. function Peripherals ()
  55.     per_list = peripheral.getNames()
  56.     for i = 1, #per_list do
  57.         if peripheral.getType(per_list[i]) == "tileinterface" then
  58.             aeInterface = peripheral.wrap(per_list[i])
  59.             return
  60.         end
  61.     end
  62.     error("missing Peripherals")
  63. end
  64.  
  65. function RefreshAmount ()
  66.     for i=1, #items.levels do
  67.         detail = aeInterface.getItemDetail(items.levels[i].fingerprint)
  68.         if detail ~= nil then
  69.             items.levels[i].currentAmount = detail.basic().qty
  70.         else
  71.             items.levels[i].currentAmount = 0
  72.         end
  73.     end
  74. end
  75.  
  76. function LevelEmitter()
  77.     RefreshAmount()
  78.    
  79.     for i=1,#items.levels do
  80.        
  81.         if items.levels[i].mode =="above" then
  82.             if items.levels[i].amount<items.levels[i].currentAmount then
  83.                
  84.                 Set_Redstone(true,items.levels[i])
  85.             else
  86.                
  87.                 Set_Redstone(false,items.levels[i])
  88.             end
  89.         else
  90.             if items.levels[i].amount>items.levels[i].currentAmount then
  91.                
  92.                 Set_Redstone(true,items.levels[i])
  93.             else
  94.            
  95.                 Set_Redstone(false,items.levels[i])
  96.             end
  97.         end
  98.     end
  99. end
  100. function Reset_Redstone ()
  101.     sides = rs.getSides()
  102.     for i=1,#sides do
  103.         rs.setOutput(sides[i],false)
  104.         rs.setBundledOutput(sides[i],0)
  105.     end
  106.    
  107.     for i = 1, #items.levels do
  108.         Set_Redstone(items.levels[i].redstone.def,items.levels[i])
  109.     end
  110. end
  111. if fs.exists("config.cfg") then
  112.    
  113.     LoadConfig()
  114.     Reset_Redstone()
  115.     Peripherals()
  116.     sleep(5)
  117.     while true do
  118.         LevelEmitter()
  119.         sleep(items.interval)
  120.     end
  121.    
  122. else
  123.     PrepareConfig()
  124.     print (" File 'config.cfg' was generated. Edit it to suit your needs and launch app again")
  125. end
Advertisement
Add Comment
Please, Sign In to add comment