Advertisement
dadragon84

Counter

Mar 1st, 2014
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.44 KB | None | 0 0
  1. --Will count redstone pulses on the right side of the pc. Will continue even if the server restarts.
  2.  
  3. --Made by Severino
  4.  
  5. --Tested with tekkit
  6. --You need to make a file in the computer's
  7. --program folder called count.txt and put the
  8. --number 0 in it first.
  9.  
  10. term.clear()
  11. term.setCursorPos(1, 1)
  12. minerID = os.getComputerID()
  13. turtleName = nil
  14. consoleID = 0
  15. version = "1.0"
  16. function getConsoleID()
  17.   local fname = "ConsoleID"
  18.   if fs.exists(fname) then
  19.     file = fs.open(fname, "r")
  20.     consoleID = tonumber(file.readAll())
  21.     file.close()
  22.     print("Monitor Console ID: "..consoleID)
  23.   else
  24.     updateStatus("Waiting for Console ID...",true)
  25.     term.write("Monitor Console ID: ")
  26.     consoleID = io.read()
  27.     file = fs.open(fname, "w")
  28.     file.write(consoleID)
  29.     file.close()
  30.     consoleID = tonumber(consoleID)
  31.   end
  32. end
  33. function updateStatus(msg, silent)
  34.   if not rednet.isOpen("top") then rednet.open("top") end
  35.   if rednet.isOpen("top") then
  36.     if turtleName == nil then return end
  37.     networkmsg = "MM:"..turtleName..": "..msg
  38.     if consoleID == 0 then
  39.       rednet.broadcast(networkmsg)
  40.     else
  41.       rednet.send(consoleID,networkmsg)
  42.     end
  43.   end
  44.   if not silent then print(msg) end
  45. end
  46. function getTurtleName()
  47.   local fname = "TurtleName"
  48.   if fs.exists(fname) then
  49.     file = fs.open(fname, "r")
  50.     turtleName = file.readAll()
  51.     file.close()
  52.   else
  53.     term.write("Turtle Name: ")
  54.     turtleName = io.read()
  55.     file = fs.open(fname, "w")
  56.     file.write(turtleName)
  57.     file.close()
  58.   end
  59. end
  60. -- Main Program
  61. updateStatus("Starting")
  62. term.clear()
  63. term.setCursorPos(1,1)
  64. getTurtleName()
  65. getConsoleID()
  66. function update()
  67. end
  68.  
  69. local iCount = 0
  70. local hMonitor = peripheral.wrap("right")
  71. file = io.open("count.txt","r")
  72. count = file:read()
  73. file:close()
  74. -- Monitor is on top of the computer
  75.  
  76. term.redirect(hMonitor) --redirect console output to monitor
  77.  
  78. while true do
  79.  event = os.pullEvent()
  80.  
  81. --Redstone from the side
  82.  if redstone.getInput("bottom") == true then
  83.  
  84.   count = count + 1
  85.   file = io.open("count.txt", "w")
  86.   file:write(count)
  87.   file:close()
  88.   term.clear()
  89.   term.setCursorPos(1,1)
  90.   updateStatus("Amount of items generated so far is: " .. count)
  91.  
  92.  end
  93.  
  94. --Current from the left will stop this
  95.  if redstone.getInput("back") == true then
  96.   count = 0
  97.   term.clear()
  98.   updateStatus("Count reset")
  99.   term.clear()
  100.   term.restore()
  101.  end
  102.  
  103. end
  104.  
  105. term.clear()
  106. term.restore()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement