Advertisement
Guest User

MineChat v1.0.0

a guest
Jan 21st, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.37 KB | None | 0 0
  1. -- MineChat v1.0.0 by Codian
  2. -- no stealing
  3.  
  4. local term = require "term"
  5. local colors = require "colors"
  6. local component = require "component"
  7. local gpu = component.gpu
  8. local w, h = gpu.getResolution()
  9.  
  10. uname = 'Player'
  11.  
  12. message_buffer = {}
  13. commands = {
  14.     ['exit'] = exit_to_shell,
  15.     ['quit'] = exit_to_shell,
  16.     ['help'] = show_help,
  17.     ['clear'] = clear_buffer
  18. }
  19. help_msg = [===[
  20. ----------------------
  21. MineChat Command List:
  22. ----------------------
  23. /help - Show this message
  24. /clear - Clear the messages on screen
  25. /exit|quit - Quit MineChat
  26. ----------------------
  27. ]===]
  28.  
  29. function print_reverse_buffer(t)
  30.     out = {}
  31.     y = h - 3
  32.    
  33.     if #t > h - 4 then
  34.         table.remove(t, #t - (h - 4))
  35.     end
  36.    
  37.     for i = #t, 1, -1 do
  38.         mdata = t[i]
  39.         _, linecount = string.gsub(mdata['content'], '\n', '\n')
  40.         if linecount == 0 then
  41.             linecount = 1
  42.         end
  43.         y = y - linecount
  44.        
  45.         term.setCursor(1, y)
  46.         gpu.setForeground(mdata['fg'])
  47.         gpu.setBackground(mdata['bg'])
  48.         term.write(mdata['author'] .. ' ' .. mdata['content'])
  49.     end
  50.     return out
  51. end
  52.  
  53. function read_input()
  54.     return term.read()
  55. end
  56.  
  57. function update()
  58.     term.clear()
  59.     gpu.setForeground(0x000000)
  60.     gpu.setBackground(0xffffff)
  61.     gpu.fill(1, h-1, w, h, " ")
  62.     term.setCursor(1, h-1)
  63.     term.write("MineChat Client - [Logged in as " .. uname .. "]")
  64.     term.setCursor(w-6, h-1)
  65.     term.write("v1.0.0")
  66.     print_reverse_buffer(message_buffer)
  67.     gpu.setForeground(0x828282)
  68.     gpu.setBackground(0x000000)
  69.     term.setCursor(1, h)
  70.     term.clearLine()
  71.     term.write('> ')
  72.     gpu.setForeground(0xffffff)
  73. end
  74.  
  75. function show_new_message(content, author, fg, bg)
  76.     author = author or '[System]'
  77.     fg = fg or 0xffffff
  78.     bg = bg or 0x000000
  79.  
  80.     m_table = {
  81.         ['author'] = author,
  82.         ['fg'] = fg,
  83.         ['bg'] = bg,
  84.         ['content'] = content
  85.     }
  86.  
  87.     table.insert(message_buffer, m_table)
  88.     update()
  89. end
  90.  
  91. function exit_to_shell()
  92.     term.clear()
  93.     term.setCursor(1, 1)
  94.     gpu.setForeground(0xffffff)
  95.     gpu.setBackground(0x000000)
  96.     print('Thank you for using MineChat!')
  97.     print('Exiting to shell.')
  98.     os.exit()
  99. end
  100.  
  101. function show_help()
  102.     show_new_message(help_msg)
  103. end
  104.  
  105. function clear_buffer()
  106.     message_buffer = {}
  107.     update()
  108. end
  109.  
  110. term.clear()
  111. term.setCursor(1, 1)
  112. gpu.setForeground(0xffffff)
  113. gpu.setBackground(0x000000)
  114.  
  115. while true do
  116.     gpu.setForeground(0xffff00)
  117.     term.write('Set your username: ')
  118.     gpu.setForeground(0xffffff)
  119.     x = string.gsub(string.gsub(term.read(), "\n", ""), ' ', '')
  120.    
  121.     if string.len(x) > 2 and string.len(x) < 32 then
  122.         uname = x
  123.         break
  124.     else
  125.         gpu.setForeground(0xff0000)
  126.         print('Username needs to be between 3 and 32 characters in length.')
  127.     end
  128. end
  129.  
  130. show_new_message('Welcome to MineChat!', nil, 0xffff00)
  131. show_new_message('Type in /help for command help.', nil, 0xffff00)
  132. show_new_message(uname..' joined the chatroom.', '*', 0x00ff00)
  133.  
  134. while true do
  135.     x = string.gsub(string.gsub(string.lower(term.read()), "\n", ""), ' ', '')
  136.    
  137.     if string.len(x) > 0 then
  138.         is_command = false
  139.         slash_pos = string.find(x, '/')
  140.         if slash_pos ~= nil then
  141.             if slash_pos == 1 then
  142.                 is_command = true
  143.             end
  144.         end
  145.        
  146.         if is_command then
  147.             callback = commands[string.gsub(x, '/', '')]
  148.             if callback == nil then
  149.                 show_new_message('Unknown command. Type /help for list of commands.', nil, 0xff0000)
  150.             else
  151.                 callback()
  152.             end
  153.         else
  154.             show_new_message(x, uname .. ':')
  155.         end
  156.     else
  157.         update()
  158.     end
  159. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement