Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --rewritten better chat !
- term.clear()
- x,y=term.getSize()
- midX=x/2
- midY=y/2
- history={}
- function setUsername()
- term.setCursorPos(14,10)
- write("Your username: ")
- usr=read()
- term.setCursorPos(14,11)
- write("Channel:")
- channel=tonumber(read())
- term.clear()
- end
- function showBar()
- term.setCursorPos(1,1)
- term.setBackgroundColor(colors.white)
- for i=1,x do
- write(" ")
- end
- term.setCursorPos(1,1)
- term.setTextColor(colors.black)
- print("You are chatting in channel: "..channel)
- term.setCursorPos(x-2,1)
- write("[X]")
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- term.setCursorPos(1,2)
- for i=1,x do
- write("-")
- end
- end
- function exit()
- local event, button, xPos, yPos = os.pullEvent("mouse_click")
- if yPos==1 and xPos==x-1 then
- term.clear()
- os.shutdown()
- end
- end
- function drawmessages()
- term.setCursorPos(1,3)
- for k,v in pairs(history) do
- term.clearLine()
- term.setCursorPos(1,k+2)
- print(v)
- end
- end
- function openSide()
- for _,side in ipairs({"top", "bottom", "front", "left", "right", "back"}) do
- if peripheral.isPresent(side)
- then
- if peripheral.getType(side)=="modem"
- then
- modemside=side
- rednet.open(modemside)
- end
- end
- end
- end
- openSide()
- function receive()
- local id,message=rednet.receive("chatmessage/"..channel)
- if message ~="" then table.insert(history,message)
- drawmessages()
- end
- end
- function sendMessage()
- showBar()
- drawmessages()
- term.setCursorPos(1,y-1)
- term.clearLine()
- term.setCursorPos(1,y)
- write(">>")
- msg=read()
- if msg~="" then
- msg="["..usr.."]:"..msg
- table.insert(history,msg)
- rednet.broadcast(msg,"chatmessage/"..channel)
- end
- end
- setUsername()
- while true do
- parallel.waitForAny(sendMessage,receive,exit)
- end
Advertisement
Add Comment
Please, Sign In to add comment