Advertisement
LoganDark

Tellraw system for OC

Dec 23rd, 2017
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.41 KB | None | 0 0
  1. local event = require('event')
  2. local term = require('term')
  3. local gpu = term.gpu()
  4. local debug = require('component').debug
  5. local unicode = require('unicode')
  6. local fs = require('filesystem')
  7.  
  8. local hex = '0123456789abcdef'
  9. local colors = {
  10.   0x000000, -- 0
  11.   0x0000aa, -- 1
  12.   0x00aa00, -- 2
  13.   0x00aaaa, -- 3
  14.   0xaa0000, -- 4
  15.   0xaa00aa, -- 5
  16.   0xffaa00, -- 6
  17.   0xaaaaaa, -- 7
  18.   0x555555, -- 8
  19.   0x5555ff, -- 9
  20.   0x55ff55, -- a
  21.   0x55ffff, -- b
  22.   0xff5555, -- c
  23.   0xff55ff, -- d
  24.   0xffff55, -- e
  25.   0xffffff  -- f
  26. }
  27.  
  28. local ofg = gpu.setForeground(colors[16])
  29. local obg = gpu.setBackground(colors[1])
  30. local ow, oh = gpu.getResolution()
  31. gpu.setResolution(80, 25)
  32.  
  33. term.clear()
  34.  
  35. for i = 1, 16 do
  36.   if i < 3 then
  37.     gpu.setForeground(0xffffff)
  38.   else
  39.     gpu.setForeground(0x000000)
  40.   end
  41.  
  42.   gpu.setBackground(colors[i])
  43.  
  44.   gpu.set((i - 1) * 5 + 1, 4, '  ' .. hex:sub(i, i) .. '  ')
  45. end
  46.  
  47. gpu.set(76, 4, unicode.char(9656))
  48. gpu.set(80, 4, unicode.char(9666))
  49.  
  50. gpu.fill(1, 5, 80, 1, ' ')
  51. gpu.set(18, 5, 'Quit')
  52. gpu.set(54, 5, 'Run /tellraw')
  53.  
  54. gpu.setForeground(colors[16])
  55. gpu.setBackground(colors[1])
  56.  
  57. local text = ''
  58. local colr = ''
  59. local cursor = 1
  60. local scroll = 0
  61. local ccolor = 16
  62.  
  63. local function waitForEvent()
  64.   local char, fg, bg = gpu.get(cursor - scroll, 1)
  65.   local ofg = gpu.getForeground()
  66.   local obg = gpu.getBackground()
  67.   local isShown = true
  68.  
  69.   while true do
  70.     if isShown then
  71.       gpu.setForeground(bg)
  72.       gpu.setBackground(fg)
  73.     else
  74.       gpu.setForeground(fg)
  75.       gpu.setBackground(bg)
  76.     end
  77.    
  78.     isShown = not isShown
  79.    
  80.     gpu.set(cursor - scroll, 1, char)
  81.    
  82.     local evt, arg1, arg2, arg3, arg4, arg5, arg6 = event.pull(0.5)
  83.    
  84.     if evt then
  85.       gpu.setForeground(fg)
  86.       gpu.setBackground(bg)
  87.       gpu.set(cursor - scroll, 1, char)
  88.       gpu.setForeground(ofg)
  89.       gpu.setBackground(obg)
  90.      
  91.       return evt, arg1, arg2, arg3, arg4, arg5, arg6
  92.     end
  93.   end
  94. end
  95.  
  96. local function getWidth()
  97.   return ({gpu.getResolution()})[1]
  98. end
  99.  
  100. local function setCursor(x)
  101.   cursor = math.max(math.min(x, text:len() + 1), 1)
  102.  
  103.   if cursor - scroll < 6 then
  104.     scroll = math.max(cursor - 6, 0)
  105.   end
  106.  
  107.   local width = getWidth()
  108.  
  109.   if cursor - scroll > width - 7 then
  110.     scroll = cursor - width + 7
  111.   end
  112. end
  113.  
  114. local function setCursorBy(delta)
  115.   setCursor(cursor + delta)
  116. end
  117.  
  118. local function drawLine()
  119.   local width = getWidth()
  120.  
  121.   local toDraw = text:sub(scroll + 1, scroll + width - 3)
  122.   local toColr = colr:sub(scroll + 1, scroll + width - 3)
  123.  
  124.   local obg = gpu.setBackground(0x000000)
  125.   local ofg = gpu.setForeground(0xffffff)
  126.  
  127.   gpu.fill(1, 1, width - 3, 2, ' ')
  128.  
  129.   for i = 1, width do
  130.     local color = hex:find(toColr:sub(i, i))
  131.     gpu.setForeground(colors[color])
  132.    
  133.     if color == 1 then
  134.       gpu.setBackground(0xffffff)
  135.     else
  136.       gpu.setBackground(obg)
  137.     end
  138.    
  139.     gpu.set(i, 1, toDraw:sub(i, i))
  140.   end
  141.  
  142.   gpu.setForeground(colors[9])
  143.   gpu.setBackground(colors[1])
  144.   gpu.set(1, 2, toColr)
  145.  
  146.   gpu.setForeground(colors[1])
  147.   gpu.setBackground(colors[5])
  148.   gpu.set(width - 2, 1, ' x ')
  149.  
  150.   gpu.setBackground(obg)
  151.   gpu.setForeground(ofg)
  152. end
  153.  
  154. local function escape(char)
  155.   local hex = ('%x'):format(string.byte(char))
  156.  
  157.   return '\\u' .. string.rep('0', 4 - hex:len()) .. hex
  158. end
  159.  
  160. local function runCommand()
  161.   if text:len() > 0 then
  162.     local json = '["'
  163.     local last = nil
  164.    
  165.     for i = 1, colr:len() do
  166.       local color = colr:sub(i, i)
  167.      
  168.       if color ~= last then
  169.         json = json .. '\\u00a7' .. color
  170.         last = color
  171.       end
  172.      
  173.       local char = text:sub(i, i)
  174.       json = json .. escape(char)
  175.      
  176.       if char == '\n' then
  177.         last = 16
  178.       end
  179.     end
  180.    
  181.     json = json .. '"]'
  182.    
  183.     local status, result = debug.runCommand('/tellraw @a ' .. json)
  184.   end
  185. end
  186.  
  187. local function drawSaves()
  188.   local obg = gpu.getBackground()
  189.   local ofg = gpu.setForeground(colors[1])
  190.   local w, h = gpu.getResolution()
  191.  
  192.   for i = 1, h - 5 do
  193.     local num = tostring(i)
  194.    
  195.     gpu.setBackground(colors[4])
  196.     gpu.set(1, i + 5, ' ' .. string.rep('0', 3 - num:len()) .. num .. ' ')
  197.     gpu.setBackground(colors[5])
  198.     gpu.set(w - 2, i + 5, ' x ')
  199.     gpu.setBackground(colors[8])
  200.     gpu.fill(6, i + 5, (w / 2) - 4, 1, ' ')
  201.     gpu.set(2 + w / 4, i + 5, 'Save')
  202.    
  203.     if fs.exists('/home/tellraw.save.' .. num) then
  204.       gpu.setBackground(colors[8])
  205.     else
  206.       gpu.setBackground(colors[9])
  207.     end
  208.    
  209.     gpu.fill(w / 2 + 2, i + 5, (w / 2) - 4, 1, ' ')
  210.     gpu.set(w / 2 + (w / 4) - 2, i + 5, 'Load')
  211.   end
  212.  
  213.   gpu.setForeground(ofg)
  214.   gpu.setBackground(obg)
  215. end
  216.  
  217. drawSaves()
  218. drawLine()
  219.  
  220. local function deleteSave(num)
  221.   fs.remove('/home/tellraw.save.' .. tostring(num))
  222. end
  223.  
  224. local function loadSave(num)
  225.   local path = '/home/tellraw.save.' .. tostring(num)
  226.   local handle = fs.open(path, 'r')
  227.  
  228.   if handle then
  229.     local size = fs.size(path)
  230.     local data = handle:read(size)
  231.     handle:close()
  232.    
  233.     local match = data:find('(%x+)$')
  234.     local dcolr = data:sub(match)
  235.     local dtext = data:sub(1, match - 2)
  236.    
  237.     if dcolr:len() == dtext:len() then
  238.       text = dtext
  239.       colr = dcolr
  240.       scroll = 0
  241.       setCursor(text:len() + 1)
  242.       drawLine()
  243.     end
  244.   end
  245. end
  246.  
  247. local function saveTo(num)
  248.   local path = '/home/tellraw.save.' .. tostring(num)
  249.   local handle = fs.open(path, 'w')
  250.  
  251.   if handle then
  252.     handle:write(text .. '\n' .. colr)
  253.     handle:close()
  254.   end
  255. end
  256.  
  257. while true do
  258.   local oscroll = scroll
  259.   local otext = text
  260.   local evt, _, arg1, arg2 = waitForEvent()
  261.  
  262.   if evt == 'key_down' then
  263.     if arg1 > 0 and arg1 < 127 then
  264.       local char = unicode.char(arg1)
  265.      
  266.       if arg1 == 13 then
  267.         char = '\n'
  268.       end
  269.      
  270.       local cpos = hex:sub(ccolor, ccolor)
  271.      
  272.       text = text:sub(1, cursor - 1) .. char .. text:sub(cursor)
  273.       colr = colr:sub(1, cursor - 1) .. cpos .. colr:sub(cursor)
  274.       setCursorBy(1)
  275.     elseif arg1 == 127 and cursor > 1 then
  276.       text = text:sub(1, cursor - 2) .. text:sub(cursor)
  277.       colr = colr:sub(1, cursor - 2) .. colr:sub(cursor)
  278.       setCursorBy(-1)
  279.     elseif arg1 == 63234 then -- left arrow
  280.       setCursorBy(-1)
  281.     elseif arg1 == 63235 then -- right arrow
  282.       setCursorBy(1)
  283.     elseif arg1 == 63273 then -- Home
  284.       setCursor(1)
  285.     elseif arg1 == 63275 then -- End
  286.       setCursor(#text + 1)
  287.     end
  288.   elseif evt == 'clipboard' then
  289.     local cpos = hex:sub(ccolor, ccolor)
  290.    
  291.     text = text:sub(1, cursor - 1) .. arg1 .. text:sub(cursor)
  292.     colr = colr:sub(1, cursor - 1) .. string.rep(cpos, arg1:len()) .. colr:sub(cursor)
  293.     setCursorBy(arg1:len())
  294.     drawLine()
  295.   elseif evt == 'touch' or evt == 'drag' then
  296.     local w = getWidth()
  297.    
  298.     if arg2 == 1 then
  299.       if arg1 > w - 3 then
  300.         text = ''
  301.         colr = ''
  302.         setCursor(1)
  303.         drawLine()
  304.       else
  305.         setCursor(arg1 + scroll)
  306.       end
  307.     elseif arg2 == 2 then
  308.       if arg1 + scroll <= #colr then
  309.         colr = colr:sub(1, arg1 + scroll - 1) .. hex:sub(ccolor, ccolor) .. colr:sub(arg1 + scroll + 1)
  310.         drawLine()
  311.       end
  312.     elseif arg2 == 4 then
  313.       local ofg = gpu.setForeground(ccolor < 3 and 0xffffff or 0x000000)
  314.       local obg = gpu.setBackground(colors[ccolor])
  315.       gpu.set((ccolor - 1) * 5 + 1, 4, ' ')
  316.       gpu.set((ccolor - 1) * 5 + 5, 4, ' ')
  317.       ccolor = math.floor((arg1 - 1) / 5) + 1
  318.      
  319.       gpu.setForeground(ccolor < 3 and 0xffffff or 0x000000)
  320.       gpu.setBackground(colors[ccolor])
  321.       gpu.set((ccolor - 1) * 5 + 1, 4, unicode.char(9656))
  322.       gpu.set((ccolor - 1) * 5 + 5, 4, unicode.char(9666))
  323.      
  324.       gpu.setForeground(ofg)
  325.       gpu.setBackground(obg)
  326.     elseif arg2 == 5 then
  327.       if arg1 < 40 then
  328.         break
  329.       elseif arg1 <= 80 then
  330.         runCommand()
  331.       end
  332.     elseif arg2 > 5 then
  333.       local num = math.floor(arg2 - 5)
  334.      
  335.       if arg1 > 5 then
  336.         if arg1 > w - 3 then
  337.           deleteSave(num)
  338.         elseif arg1 > w / 2 + 1 then
  339.           loadSave(num)
  340.         elseif arg1 > 5 then
  341.           saveTo(num)
  342.         end
  343.       end
  344.      
  345.       drawSaves()
  346.     end
  347.   end
  348.  
  349.   if oscroll ~= scroll or otext ~= text then
  350.     drawLine()
  351.   end
  352. end
  353.  
  354. gpu.setForeground(ofg)
  355. gpu.setBackground(obg)
  356. gpu.setResolution(ow, oh)
  357. term.clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement