Guest User

simplecounter.lua

a guest
Dec 17th, 2021
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.16 KB | None | 0 0
  1. local count = {}
  2. local scrollPos = 1
  3.  
  4. if fs.exists("simplecounter.txt") then
  5.     local f = fs.open("simplecounter.txt","r")
  6.     count = textutils.unserialize(f.readAll())
  7.     f.close()
  8. end
  9. local saveCount = function()
  10.     local f = fs.open("simplecounter.txt","w")
  11.     f.write(textutils.serialize(count))
  12.     f.close()
  13. end
  14.  
  15. term.clear()
  16.  
  17. while true do
  18.     for a, b in ipairs(count) do
  19.         term.setCursorPos(1, 1+a-scrollPos)
  20.         term.setBackgroundColor(colors.black)
  21.         term.clearLine()
  22.        
  23.         term.blit("x R "..string.char(24)..string.char(25).." -+", "fffffffff", "8f8f88f88")
  24.         term.setTextColor(colors.black)
  25.         term.setBackgroundColor(colors.white)
  26.         term.write(b.count)
  27.        
  28.         term.setTextColor(colors.white)
  29.         term.setBackgroundColor(colors.black)
  30.         term.write(" "..b.name)
  31.     end
  32.     term.setCursorPos(1, #count+2-scrollPos)
  33.     term.setBackgroundColor(colors.black)
  34.     term.clearLine()
  35.     term.blit("Add", "fff", "888")
  36.    
  37.     local event = {os.pullEvent()}
  38.     if event[1] == "mouse_click" then
  39.         local clickPos = event[4] + scrollPos - 1
  40.         if clickPos == #count + 1 and event[3] >= 1 and event[3] <= 3 then
  41.             term.setBackgroundColor(colors.black)
  42.             term.setTextColor(colors.white)
  43.             term.clear()
  44.             term.setCursorPos(1, 1)
  45.             write("Counter Name: ")
  46.             table.insert(count, #count+1, {count=0, name=read()})
  47.             saveCount()
  48.         elseif clickPos <= #count then
  49.             if event[3] == 1 then
  50.                 table.remove(count, clickPos)
  51.                 term.setBackgroundColor(colors.black)
  52.                 term.clear()
  53.                 saveCount()
  54.             elseif event[3] == 3 then
  55.                 term.setBackgroundColor(colors.black)
  56.                 term.clear()
  57.                 term.setCursorPos(1, 1)
  58.                 term.setTextColor(colors.yellow)
  59.                 term.write(count[clickPos].name)
  60.                 term.setTextColor(colors.white)
  61.                 write("\nRename Counter: ")
  62.                 count[clickPos].name = read()
  63.                 term.clear()
  64.                 saveCount()
  65.             elseif event[3] == 5 and clickPos ~= 1 then
  66.                 table.insert(count, clickPos-1, count[clickPos])
  67.                 table.remove(count, clickPos+1)
  68.                 saveCount()
  69.             elseif event[3] == 6 and clickPos ~= #count then
  70.                 table.insert(count, clickPos+2, count[clickPos])
  71.                 table.remove(count, clickPos)
  72.                 saveCount()
  73.             elseif event[3] == 8 then
  74.                 count[clickPos].count = count[clickPos].count - 1
  75.                 saveCount()
  76.             elseif event[3] == 9 then
  77.                 count[clickPos].count = count[clickPos].count + 1
  78.                 saveCount()
  79.             end
  80.         end
  81.     elseif event[1] == "mouse_scroll" then
  82.         local xSize, ySize = term.getSize()
  83.         if event[2] == -1 and scrollPos > 1 then
  84.             scrollPos = scrollPos - 1
  85.         elseif event[2] == 1 and scrollPos+ySize-1 <= #count then
  86.             scrollPos = scrollPos + 1
  87.         end
  88.     end
  89. end
Advertisement
Add Comment
Please, Sign In to add comment