Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- MineChat v1.0.0 by Codian
- -- no stealing
- local term = require "term"
- local colors = require "colors"
- local component = require "component"
- local gpu = component.gpu
- local w, h = gpu.getResolution()
- uname = 'Player'
- message_buffer = {}
- commands = {
- ['exit'] = exit_to_shell,
- ['quit'] = exit_to_shell,
- ['help'] = show_help,
- ['clear'] = clear_buffer
- }
- help_msg = [===[
- ----------------------
- MineChat Command List:
- ----------------------
- /help - Show this message
- /clear - Clear the messages on screen
- /exit|quit - Quit MineChat
- ----------------------
- ]===]
- function print_reverse_buffer(t)
- out = {}
- y = h - 3
- if #t > h - 4 then
- table.remove(t, #t - (h - 4))
- end
- for i = #t, 1, -1 do
- mdata = t[i]
- _, linecount = string.gsub(mdata['content'], '\n', '\n')
- if linecount == 0 then
- linecount = 1
- end
- y = y - linecount
- term.setCursor(1, y)
- gpu.setForeground(mdata['fg'])
- gpu.setBackground(mdata['bg'])
- term.write(mdata['author'] .. ' ' .. mdata['content'])
- end
- return out
- end
- function read_input()
- return term.read()
- end
- function update()
- term.clear()
- gpu.setForeground(0x000000)
- gpu.setBackground(0xffffff)
- gpu.fill(1, h-1, w, h, " ")
- term.setCursor(1, h-1)
- term.write("MineChat Client - [Logged in as " .. uname .. "]")
- term.setCursor(w-6, h-1)
- term.write("v1.0.0")
- print_reverse_buffer(message_buffer)
- gpu.setForeground(0x828282)
- gpu.setBackground(0x000000)
- term.setCursor(1, h)
- term.clearLine()
- term.write('> ')
- gpu.setForeground(0xffffff)
- end
- function show_new_message(content, author, fg, bg)
- author = author or '[System]'
- fg = fg or 0xffffff
- bg = bg or 0x000000
- m_table = {
- ['author'] = author,
- ['fg'] = fg,
- ['bg'] = bg,
- ['content'] = content
- }
- table.insert(message_buffer, m_table)
- update()
- end
- function exit_to_shell()
- term.clear()
- term.setCursor(1, 1)
- gpu.setForeground(0xffffff)
- gpu.setBackground(0x000000)
- print('Thank you for using MineChat!')
- print('Exiting to shell.')
- os.exit()
- end
- function show_help()
- show_new_message(help_msg)
- end
- function clear_buffer()
- message_buffer = {}
- update()
- end
- term.clear()
- term.setCursor(1, 1)
- gpu.setForeground(0xffffff)
- gpu.setBackground(0x000000)
- while true do
- gpu.setForeground(0xffff00)
- term.write('Set your username: ')
- gpu.setForeground(0xffffff)
- x = string.gsub(string.gsub(term.read(), "\n", ""), ' ', '')
- if string.len(x) > 2 and string.len(x) < 32 then
- uname = x
- break
- else
- gpu.setForeground(0xff0000)
- print('Username needs to be between 3 and 32 characters in length.')
- end
- end
- show_new_message('Welcome to MineChat!', nil, 0xffff00)
- show_new_message('Type in /help for command help.', nil, 0xffff00)
- show_new_message(uname..' joined the chatroom.', '*', 0x00ff00)
- while true do
- x = string.gsub(string.gsub(string.lower(term.read()), "\n", ""), ' ', '')
- if string.len(x) > 0 then
- is_command = false
- slash_pos = string.find(x, '/')
- if slash_pos ~= nil then
- if slash_pos == 1 then
- is_command = true
- end
- end
- if is_command then
- callback = commands[string.gsub(x, '/', '')]
- if callback == nil then
- show_new_message('Unknown command. Type /help for list of commands.', nil, 0xff0000)
- else
- callback()
- end
- else
- show_new_message(x, uname .. ':')
- end
- else
- update()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement