Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- drive = peripheral.find("drive")
- delay = 0.05
- tX, tY = term.getSize()
- operatorName = "Console"
- function updateBlink()
- term.setCursorPos(tX,tY)
- if toggle ~= true then
- toggle = true
- term.setTextColor(colors.gray)
- else
- toggle = false
- term.setTextColor(colors.lightGray)
- end
- term.write("X")
- end
- function commandCheck()
- local potCommand = chat[#chat]
- if potCommand == "/clear" then
- chat = {}
- rawChat = {}
- messageOwners = {}
- sendMessage = 2
- elseif string.sub(potCommand,1,1) == "/" then
- sendMSG("[/clear] [/changename <new name>]",true)
- sendMessage = 2
- end
- end
- function waitChar()
- if entered == nil then
- entered = {}
- end
- waitForTimer = false
- enterDelay = os.startTimer(delay)
- event = {os.pullEvent()}
- if event[1] ~= "timer" then
- os.cancelTimer(enterDelay)
- end
- if event[1] == "char" then
- table.insert(entered,event[2])
- elseif event[1] == "key" then
- if event[2] == 28 then -- Enter
- sendMessage = 2
- textToSend = tableToText(entered)
- if string.len(textToSend) > 12 and string.sub(textToSend,1,12) == "/changename " then
- newPCName = string.sub(textToSend,13)
- sendMSG(pcName.." is now: \""..newPCName.."\"",true)
- pcName = newPCName
- os.setComputerLabel(newPCName)
- else
- sendMSG(textToSend)
- end
- entered = {}
- elseif event[2] == 14 then -- Backspace
- if #entered > 0 then
- table.remove(entered,#entered)
- end
- end
- end
- getMSG()
- commandCheck()
- updateInterface()
- end
- function tableToText(tableDef)
- local add = ""
- for i=1,#tableDef do
- add = add..tableDef[i]
- end
- return add
- end
- function updateInterface()
- if chat ~= oldChat or sendMessage > 0 then
- term.clear()
- for i=1,#chat do
- term.setCursorPos(1,i)
- ownerColor(messageOwners[i])
- term.write(messageOwners[i]..": ")
- term.setTextColor(colors.white)
- term.write(chat[i])
- end
- sendMessage = sendMessage - 1
- end
- oldChat = chat
- term.setCursorPos(1,tY)
- term.setTextColor(colors.green)
- term.write(pcName..": ")
- term.setTextColor(colors.white)
- term.write(tableToText(entered).." ")
- cX, cY = term.getCursorPos()
- -- updateBlink() -- Enable to see chat-update speed
- term.setCursorPos(cX-2,cY) -- Cursor Blink pos
- term.setCursorBlink(true)
- end
- function ownerColor(owner)
- if owner == operatorName then
- term.setTextColor(colors.lightGray)
- elseif owner ~= pcName then
- term.setTextColor(colors.red)
- else
- term.setTextColor(colors.lime)
- end
- end
- if not drive then
- error("Place this computer next to a drive!")
- end
- function init()
- sendMessage = 0
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- pcName = os.getComputerLabel()
- if not pcName then
- pcName = tostring(os.getComputerID())
- end
- local count = 0
- for k, v in pairs(peripheral.getNames()) do
- if peripheral.getType(v) == "drive" then
- count = count + 1
- driveSide = v
- end
- end
- if count > 1 then
- error("Too many drives!")
- end
- chat = {}
- messageOwners = {}
- rawChat = {}
- sendMSG(pcName.." joined the chat", true)
- end
- function getMSG()
- messageRaw = disk.getLabel(driveSide)
- if messageRaw ~= rawChat[#rawChat] then
- newMSG = true
- sendMessage = 1
- local subStart = string.find(messageRaw,"#$#")
- if subStart then
- message = string.sub(messageRaw,subStart+3)
- messageOwner = string.sub(messageRaw,1,subStart-1)
- table.insert(chat, message)
- table.insert(messageOwners, messageOwner)
- table.insert(rawChat,messageRaw)
- if #chat > tY-3 then
- table.remove(chat,1)
- table.remove(messageOwners, 1)
- table.remove(rawChat, 1)
- end
- else
- sendMSG("Failed to read label, fixed.",true)
- end
- else
- newMSG = false
- end
- return newMSG
- end
- function sendMSG(message, overRide)
- if overRide ~= nil then
- sleep(delay*2)
- disk.setLabel(driveSide,operatorName.."#$#"..message)
- overRide = nil
- else
- disk.setLabel(driveSide,pcName.."#$#"..message)
- end
- end
- init()
- while true do
- waitChar()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement