LoaderB0T

wither counter

May 2nd, 2025 (edited)
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. local previous_state = false -- Variable to store the previous state
  2. local current_counter = 0
  3.  
  4. -- read previous counter from file
  5. local file = fs.open("counter.txt", "r")
  6. if file then
  7. local line = file.readLine()
  8. if line then
  9. current_counter = tonumber(line) or 0 -- Convert the line to a number, default to 0 if conversion fails
  10. end
  11. file.close()
  12. end
  13.  
  14. local function setRedstone()
  15. -- values 0-9.
  16. -- back side is 0-9
  17. -- right side is 00-90
  18. -- front side is 000-900
  19. -- top side is 0000-9000
  20. local back = current_counter % 10 -- Get the last digit for the back side
  21. local right = math.floor(current_counter / 10) % 10 -- Get the second last digit for the right side
  22. local front = math.floor(current_counter / 100) % 10 -- Get the third last digit for the front side
  23. local top = math.floor(current_counter / 1000) % 10 -- Get the fourth last digit for the top side
  24.  
  25. if (front == 0 and top > 0) then
  26. front = 10 -- Set front to 10 if it is 0
  27. end
  28. if (right == 0 and front > 0) then
  29. right = 10 -- Set right to 10 if it is 0
  30. end
  31. if (back == 0 and right > 0) then
  32. back = 10 -- Set back to 10 if it is 0
  33. end
  34.  
  35. redstone.setAnalogOutput("back", back) -- Set the redstone signal on the back side
  36. redstone.setAnalogOutput("right", right) -- Set the redstone signal on the right side
  37. redstone.setAnalogOutput("front", front) -- Set the redstone signal on the front side
  38. redstone.setAnalogOutput("top", top) -- Set the redstone signal on the top side
  39. end
  40.  
  41. setRedstone() -- Set the initial counter value
  42.  
  43. -- function to save the counter to a file
  44. local function save_counter()
  45. local file = fs.open("counter.txt", "w")
  46. if file then
  47. file.writeLine(tostring(current_counter)) -- Write the counter to the file
  48. file.close()
  49. else
  50. print("Error: Unable to open counter.txt for writing.")
  51. end
  52. end
  53.  
  54. while true do
  55. local current_state = redstone.getInput("left")
  56.  
  57. if (current_state == previous_state) then
  58. os.sleep(1) -- Sleep for 1 second before checking the states again
  59. goto continue -- Skip to the next iteration of the loop
  60. end
  61.  
  62. previous_state = current_state -- Update the previous state
  63.  
  64. if (current_state) then
  65. current_counter = current_counter + 1 -- Increment the counter
  66. print("Counter: " .. current_counter) -- Log the counter to the console
  67. save_counter() -- Save the counter to the file
  68. end
  69.  
  70. -- Emit redstone signal to the right side with the current counter value
  71. setRedstone()
  72.  
  73. os.sleep(1)
  74.  
  75. ::continue:: -- Label to continue to the next iteration of the loop
  76.  
  77. end
  78.  
Advertisement
Add Comment
Please, Sign In to add comment