This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
TeamDman's profile picture

CCChat ( Rednet chat script )

Started by TeamDman, 04 July 2013 - 01:31 PM
TeamDman #1
Posted 04 July 2013 - 03:31 PM
The following is a script I wrote for fun on a tekkit server.
What it does is just lets you talk to other players on computers via rednet.
The setup is simple, just run the following line then its installed on the computer.
pastebin get H0YpZjhK chat

Once you run the program you will be sent to a menu asking for a username. This username is what will be displayed to other players. From then on you will be sent to the default chatroom and can send+receive messages. You can also change the color of the display by clicking the second line.

To send a message click the last line of the screen or press "t", then type your message and press enter. To change rooms click the top line and enter the new room name.

*NOTE Messages are not saved, so if you restart the computer/program the room will appear blank until people start sending messages.

In the attached pictures the colors appear to be going outside of the terminal window. I am not sure why this is happening and if it will happen to others. I'm playing FTB Ultimate if that has anything to do with it.

[attachment=1227:2013-07-04_15.21.31.png]
[attachment=1228:2013-07-04_15.21.57.png]
[attachment=1229:2013-07-04_15.23.08.png]

THE CODE
Spoiler
function printCenter(text)
term.setCursorPos(sizeX/2-#text/2,1)
print(text)
end

function fill(color)
for line=1,sizeY do
paintutils.drawLine(1,line,sizeX,line,colors[color])
end
end

function updateChatHud()
term.clear()
fill("black")


paintutils.drawPixel(sizeX,1,colors.red)
term.setCursorPos(sizeX,1)
io.write("X")

paintutils.drawPixel(sizeX-1,1,colors.blue)
term.setCursorPos(sizeX-1,1)
io.write("U")

paintutils.drawLine(1,sizeY,sizeX,sizeY,colors[user.color])
paintutils.drawLine(sizeX/2-(11+#user.room)/2-1,1,sizeX/2-(11+#user.room)/2+11+#user.room,1,colors[user.color])

printCenter("Chatroom : "..user.room)
term.setCursorPos(1,2)
io.write("Color : "..user.color.." ")

for i=1,sizeY-3 do
local disp = chats[#chats-(i-3)]
if disp ~= nil then
term.setCursorPos(1,sizeY-i)
io.write(disp)
end
end

term.setCursorPos(1,sizeY)
io.write("\\\\>")
end

function send(userName,room,input)
local package = userName..";"..room..";"..input
--chats[#chats+1] = (userName ~= "Cont." and (user.userName.." : ") or "")..input
print(package)
rednet.broadcast(package)
updateChatHud()
end

user = {
color = "black",
room = "Default",
}

sizeX,sizeY = term.getSize()
chats = {}
version = "v1.0"

for _,side in pairs({"left","right","top","bottom","front","back"}) do
if peripheral.getType(side) == "modem" then
rednet.open(side)
break
end
end
if not term.isColor() then
for i,v in pairs(paintutils) do
paintutils[i] = function() return 0 end
end
end

term.clear()
printCenter("CCChat "..version.." by TeamDman")
io.write("Enter your username : ")
user.userName = io.read()

term.clear()
updateChatHud()

while true do
local event,arg1,arg2,arg3 = os.pullEvent()
if event == "rednet_message" then
local name,room,message = arg2:match("(.*);(.*);(.*)")
if name == "Cont." and room == user.room then
chats[#chats+1] = message
elseif room == user.room then
chats[#chats+1] = name.." : "..message
end
updateChatHud()
elseif event == "mouse_click" or event == "key" then
if (event == "mouse_click" and arg3 == sizeY) or (event == "key" and keys.getName(arg1) == "t") then
sleep(0.1)
term.setCursorPos(4,sizeY)
local input = io.read()
if #input+#user.userName+3 > sizeX then
send(user.userName,user.room,input:sub(1,sizeX-(#user.userName+3)))
input = input:sub(sizeX-(#input+#user.userName+3)+1)
repeat
send("Cont.",user.room,input:sub(1,sizeX))
input = input:sub(sizeX+1)
until #input == 0
else
send(user.userName,user.room,input)
end
elseif event == "mouse_click" and (arg3 == 2 or arg3 == 1) then
if arg3 == 1 then
if arg2 == sizeX then -- Close
fill("black")
break
elseif arg2 == sizeX-1 then -- Update
fs.delete("chat")
shell.run("pastebin","get","H0YpZjhK","/chat")
fill("black")
break
else
term.setCursorPos(sizeX/2-(11+#user.room)/2,1)
term.clearLine()
io.write("Chatroom : ")
user.room = io.read()
end
else
term.setCursorPos(1,2)
term.clearLine()
io.write("Color : ")
user.color = io.read()
end
updateChatHud()
end
end
end
UMayBleed #2
Posted 04 July 2013 - 03:36 PM
I remember seeing this a few weeks ago, Are you working with anyone?
TeamDman #3
Posted 04 July 2013 - 03:52 PM
SmackingWhacking or Shaqs mean anything to you?
vonaHD #4
Posted 08 July 2013 - 02:04 PM
This is a really Good Tool But, how can I change the channel and exit at a normal computer please make a Hotkey :D/>
Robotonic #5
Posted 11 July 2013 - 06:03 PM
Surprisingly, I nearly forgot about that server. Heh, too much time spent coding on mudkip's :P/>.

Didn't expect you to continue it.
MudkipTheEpic #6
Posted 11 July 2013 - 10:51 PM
Surprisingly, I nearly forgot about that server. Heh, too much time spent coding on mudkip's :P/>/>/>.

Didn't expect you to continue it.

Lolthx.

Anyway, this program looks great! Reminds me of PSChat. Although I would reccomend localizing your functions.