Advertisement
Bollie

cube

May 11th, 2025 (edited)
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.21 KB | None | 0 0
  1. -- Config
  2. local redstoneSide = "left"   -- redstone output side
  3. local pollInterval = 1         -- seconds
  4. local lowThreshold = 0.25
  5. local highThreshold = 0.75
  6.  
  7. -- State
  8. local redstoneState = false
  9.  
  10. -- Function to find a connected energy cube
  11. function findEnergyCube()
  12.   for _, side in ipairs(peripheral.getNames()) do
  13.     local p = peripheral.wrap(side)
  14.     if p and type(p.getEnergy) == "function" and type(p.getMaxEnergy) == "function" then
  15.       return p, side
  16.     end
  17.   end
  18.   return nil, nil
  19. end
  20.  
  21. -- Discover energy cube
  22. local ecube, cubeSide = findEnergyCube()
  23. if not ecube then
  24.   error("No compatible energy cube found.")
  25. else
  26.   print("Found energy cube on side: " .. cubeSide)
  27. end
  28.  
  29. -- Main loop
  30. while true do
  31.   local energy = ecube.getEnergy()
  32.   local max = ecube.getMaxEnergy()
  33.   local fill = energy / max
  34.  
  35.   print(string.format("Energy: %.1f%%", fill * 100))
  36.  
  37.   if not redstoneState and fill < lowThreshold then
  38.     redstone.setOutput(redstoneSide, true)
  39.     redstoneState = true
  40.     print("Redstone ON")
  41.   elseif redstoneState and fill > highThreshold then
  42.     redstone.setOutput(redstoneSide, false)
  43.     redstoneState = false
  44.     print("Redstone OFF")
  45.   end
  46.  
  47.   sleep(pollInterval)
  48. end
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement
OSZAR »