Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- ...use with responsibility.
- --
- -- Get with
- -- pastebin get ugMfh2wc fc_server
- -- pastebin get ugMfh2wc startup
- -- std ld fakechat_server
- local tArg = {...}
- if not commands then
- error("This requires a command computer.")
- end
- local channel = 1251 --default 1251
- local sHistory = {}
- local scr_x, scr_y = term.getSize()
- local modems
- local setModems = function()
- modems = {peripheral.find("modem")}
- for a = 1, #modems do
- if modems[a].isWireless() then
- modem = modems[a]
- modem.open(channel)
- return
- end
- end
- modem = nil
- end
- setModems()
- local prefix = ""
- local suffix = ""
- local npos = 1
- local dnb = function(phunk, ...)
- local t,b = term.getTextColor(), term.getBackgroundColor()
- local x,y = term.getCursorPos()
- local output = {phunk(...)}
- term.setCursorPos(x,y)
- term.setTextColor(t)
- term.setBackgroundColor(b)
- return unpack(output)
- end
- local prevnames = {}
- local colnames = {
- ["0"] = "black",
- ["1"] = "dark_blue",
- ["2"] = "dark_green",
- ["3"] = "dark_aqua",
- ["4"] = "dark_red",
- ["5"] = "dark_purple",
- ["6"] = "gold",
- ["7"] = "gray",
- ["8"] = "dark_gray",
- ["9"] = "blue",
- ["a"] = "green",
- ["b"] = "aqua",
- ["c"] = "red",
- ["d"] = "light_purple",
- ["e"] = "yellow",
- ["f"] = "white",
- }
- local colors_names = { --for use with colors api, you see
- ["0"] = colors.black,
- ["1"] = colors.blue,
- ["2"] = colors.green,
- ["3"] = colors.cyan,
- ["4"] = colors.red,
- ["5"] = colors.purple,
- ["6"] = colors.orange,
- ["7"] = colors.lightGray,
- ["8"] = colors.gray,
- ["9"] = colors.blue, --they don't translate perfectly, okay??
- ["a"] = colors.lime,
- ["b"] = colors.lightBlue,
- ["c"] = colors.red,
- ["d"] = colors.magenta,
- ["e"] = colors.yellow,
- ["f"] = colors.white,
- }
- local codeNames = { --just for checking
- ["k"] = "obfuscate",
- ["o"] = "italic",
- ["l"] = "bold",
- ["m"] = "strikethrough",
- ["n"] = "underline",
- ["r"] = "reset",
- }
- filterColors = function(str,doprint)
- local p = 1
- local output = ""
- local code = "&"
- local col = "f"
- while p <= #str do
- if str:sub(p,p) == code then
- if colors_names[str:sub(p+1,p+1)] then
- col = str:sub(p+1,p+1)
- p = p + 1
- elseif codeNames[str:sub(p+1,p+1)] then
- if str:sub(p+1,p+1) == "r" then
- col = "f"
- end
- p = p + 1
- else
- if doprint then
- term.setTextColor(colors_names[col])
- write(str:sub(p,p))
- end
- end
- p = p + 1
- else
- output = output..str:sub(p,p)
- if doprint then
- term.setTextColor(colors_names[col])
- write(str:sub(p,p))
- end
- p = p + 1
- end
- end
- return output
- end
- colorFormat = function(str)
- local color = "f"
- local obfuscated = false
- local bold = false
- local strikethrough = false
- local underline = false
- local italic = false
- local code = "&" --ONE CHARACTER
- local pos = 1
- local opos = 1
- local output = {}
- while pos <= #str do
- output[opos] = {}
- if str:sub(pos,pos) == code and pos < #str then
- local changed = false
- if colnames[str:sub(pos+1,pos+1)] then
- color = str:sub(pos+1,pos+1)
- changed = true
- else
- if str:sub(pos+1,pos+1) == "r" then
- color = "f"
- obfuscated = false
- bold = false
- strikethrough = false
- underline = false
- italic = false
- changed = true
- end
- if str:sub(pos+1,pos+1) == "k" then
- obfuscated = true
- changed = true
- end
- if str:sub(pos+1,pos+1) == "l" then
- bold = true
- changed = true
- end
- if str:sub(pos+1,pos+1) == "m" then
- strikethrough = true
- changed = true
- end
- if str:sub(pos+1,pos+1) == "n" then
- underline = true
- changed = true
- end
- if str:sub(pos+1,pos+1) == "o" then
- italic = true
- changed = true
- end
- end
- if changed then
- output[opos].text = ""
- pos = pos + 2
- else
- output[opos].text = str:sub(pos,pos)
- pos = pos + 1
- end
- else
- output[opos].text = str:sub(pos,pos)
- pos = pos + 1
- end
- output[opos].color = colnames[color]
- output[opos].obfuscated = obfuscated
- output[opos].bold = bold
- output[opos].strikethrough = strikethrough
- output[opos].underline = underline
- output[opos].italic = italic
- opos = opos + 1
- end
- return textutils.serialiseJSON(output)
- end
- local players = { --Add playernames here if you want to filter who receives messages
- "dan200",
- }
- local doFilter = false --If true, filters according to 'players' table
- local send = function(name,msg,np,ns,realname,doprint)
- local compiled --compiled message, not compiled program
- local filtmsg = filterColors(msg)
- if filtmsg:sub(1,3) == "___" then --should this be a client option? hmm...
- if filtmsg == "___join" then
- compiled = "&e"..filterColors(realname).." joined the game."
- elseif filtmsg == "___leave" then
- compiled = "&e"..filterColors(realname).." left the game."
- else
- compiled = msg:sub(4)
- end
- else
- compiled = prefix.."&r"..np.."&r"..name.."&r"..ns.."&r"..suffix.."&r"..msg
- end
- compiled = colorFormat(compiled)
- if doFilter then
- for a = 1, #players do
- commands.tellraw(players[a],compiled)
- end
- else
- commands.tellraw("@a",compiled)
- end
- end
- local netsend = function(n,m,p,s,mp,rn) --name, message, prefix, suffix
- local data = {
- name = n,
- realname = rn or n,
- msg = mp..m,
- prefix = p or "<",
- suffix = s or "> ",
- }
- modem.transmit(channel,channel,data)
- end
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- local name
- if tArg[1] then
- name = tArg[1]
- if name:gsub(" ","") ~= "" and prevnames[#prevnames] ~= name then
- table.insert(prevnames,name)
- end
- else
- print("Enter your name:")
- write(">")
- name = read(nil,prevnames)
- if name:gsub(" ","") ~= "" and prevnames[#prevnames] ~= name then
- table.insert(prevnames,name)
- end
- end
- local names = { --Add names to quickly switch between using UP and DOWN
- {
- name = name,
- prefix = "<",
- suffix = "> ",
- realname = name
- },
- {
- name = "&o&6Jesus",
- prefix = "<",
- suffix = "> ",
- },
- {
- name = "&dServer",
- prefix = "&d[",
- suffix = "&d] ",
- msgprefix = "&d",
- realname = "But nobody"
- },
- {
- name = "dan200",
- prefix = "<",
- suffix = "> ",
- },
- }
- local insend = function(n,m,p,s,mp,rn) --name, message, prefix, suffix
- local data = {
- name = n,
- realname = rn or n,
- msg = mp..m,
- prefix = p or "<",
- suffix = s or "> ",
- }
- os.queueEvent("modem_message","top",channel,channel,data,0)
- end
- local function doRead()
- while true do
- term.setBackgroundColor(colors.black)
- term.clear()
- term.setCursorPos(1,1)
- term.setCursorPos(1,scr_y-1)
- term.setBackgroundColor(colors.gray)
- term.clearLine()
- term.setTextColor(colors.white)
- write(">")
- local msg = read()
- if msg:gsub(" ","") == "/exit" then
- return
- end
- if msg:gsub(" ","") ~= "" then
- local sdata = {
- names[npos].name,
- msg,
- prefix..names[npos].prefix or "<",
- suffix..names[npos].suffix or "> ",
- names[npos].msgprefix or "",
- }
- if type(names[npos].realname) == "string" then
- table.insert(sdata,names[npos].realname)
- end
- insend(unpack(sdata))
- end
- end
- end
- local renderHistory = function()
- local y = scr_y-2
- local r = "&r"
- term.setBackgroundColor(colors.black)
- for a = #sHistory, #sHistory-(scr_y-2), -1 do
- if not sHistory[a] then break end
- term.setCursorPos(1,y)
- term.clearLine()
- filterColors(sHistory[a].p..r..sHistory[a].n..r..sHistory[a].s..r..sHistory[a].m,true)
- y = y - 1
- end
- end
- local server = function()
- while true do
- local evt = {os.pullEvent("modem_message")}
- if type(evt[5]) == "table" then
- local i = evt[5]
- if type(i.name) == "string" and type(i.msg) == "string" and type(i.prefix) == "string" and type(i.suffix) == "string" and type(i.realname) == "string" then
- table.insert(sHistory,{n=i.name, m=i.msg, p=i.prefix, s=i.suffix})
- send( i.name, i.msg, i.prefix, i.suffix, i.realname, true)
- dnb(function() return renderHistory() end)
- end
- end
- end
- end
- local client = function()
- doRead()
- end
- moreModems = function()
- while true do
- setModems()
- sleep(0)
- end
- end
- local funclist = {
- client,
- moreModems,
- server,
- }
- parallel.waitForAny(unpack(funclist))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement