Advertisement
ZeeDerp

ChatGlasses

Jan 4th, 2021 (edited)
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.88 KB | None | 0 0
  1. -- Mounting Function
  2. mount = function (peripheral_type) -- Wraps a peripheral
  3.     for _,location in pairs(peripheral.getNames()) do
  4.         if peripheral.getType(location) == peripheral_type then
  5.             return peripheral.wrap(location)
  6.         end
  7.     end
  8.     return false
  9. end
  10.  
  11. -- Initializes Main Variables
  12. glass = mount("openperipheral_glassesbridge")
  13. sensor = mount("sensor")
  14. controller = mount("appeng_me_tilecontroller")
  15. lamp = mount("modem")
  16. maxLines = 7
  17. messages = {}
  18. authedusers = {"ZeeDerpMaster", "Sleetyy","Rapoosa","DragonSlayer"}
  19. trackOnTab = {}
  20. staffList = {"DragonSlayer","eytixis","iim_wolf","oozoozami"}
  21. trackedPlayers = {}
  22. lamps = {}
  23. sen_pos = {x=4872,y=118,z=3678}
  24. -- Creates chat colors.
  25. chatColors = {}
  26. chatColors["ZeeDerpMaster"] = 0x3C93C2
  27. chatColors["Sleetyy"] = 0xFFFFFF
  28. chatColors["mpfthprblmtq"] = 0x800080
  29. chatColors["SoundsOfMadness"] = 0x883388
  30. chatColors["Rapoosa"] = 0xE55934
  31. -- Initializes message table
  32. for i = 1, maxLines do
  33.     table.insert(messages, "$$$$")
  34. end
  35. --
  36. for _,v in pairs(lamp.getNamesRemote()) do
  37.     table.insert(lamps,v)
  38. end
  39. --
  40. function initialize() -- Main running function. displays messages on glasses.
  41.     while true do
  42.         authCheck()
  43.         trackOn()
  44.         glass.clear()
  45.         height = (maxLines * 10)
  46.         glass.addBox(0, 20, 335, height, 0x000000, 0.5)
  47.         for i = 1, #messages do
  48.             pos = 10 + (i * 10)
  49.             message = messages[i]
  50.             color = chatColors[getName(message)]
  51.             glass.addText(5, pos, message, color)
  52.         end
  53.         onlineList()
  54.         sleep(0.1)
  55.     end
  56. end
  57. --
  58. function listener() -- Listens for incoming chat commands
  59.     while true do
  60.         authCheck()
  61.         local tEvent = {os.pullEventRaw()}
  62.         if tEvent[1] == "chat_command" then
  63.             cmd = split(tEvent[2])
  64.             user = tostring(tEvent[3])
  65.             parseCMD(cmd, user)
  66.         end
  67.     end
  68. end
  69. --
  70. function parseCMD(cmd, usr) -- Parses incoming chat commands using a terrible nested if
  71.     local surface = glass.getUserSurface(usr)
  72.     if not cmd then
  73.         sleep(.01)
  74.     end
  75.     local cmd_lower = cmd[1]:lower()
  76.     if cmd_lower == "chatcolor" then -- Changes chat color for the user
  77.         chatColors[usr] = loadstring("return " .. cmd[2])()
  78.         glassCMDOutput(usr,"Chat color is now "..cmd[2])
  79.         sleep(2)
  80.         surface.clear()
  81.     elseif cmd_lower == "nuke" then -- Clears chat and reboots PC
  82.         nuke()
  83.     elseif cmd_lower == "invsee" then -- Shows selected player's inventory for 3 seconds
  84.         invsee(sensor, cmd[2], usr)
  85.         sleep(3)
  86.         surface.clear()
  87.     elseif cmd_lower == "request" then -- Requests an item from an ae system to an inventory
  88.         controller.extractItem({id=tonumber(cmd[2]),dmg=tonumber(cmd[3]),qty=tonumber(cmd[4])}, "south")
  89.         glassCMDOutput(usr,"Requested "..cmd[4].." of "..cmd[2])
  90.         sleep(2)
  91.         surface.clear()
  92.     elseif cmd_lower == "auth" then  -- Adds a user to authorized user list
  93.         table.insert(authedusers,cmd[2])
  94.         glassCMDOutput(user,"Added "..cmd[2].." to authorized users.")
  95.         sleep(2)
  96.         surface.clear()
  97.     elseif cmd_lower == "deauth" then -- Removes a user from the authorized user list
  98.         local index={}
  99.         for k,v in pairs(authedusers) do
  100.             index[v]=k
  101.         end
  102.         table.remove(authedusers,print(index[cmd[2]]))
  103.         glassCMDOutput(usr,"Removed "..cmd[2].." from authorized users.")
  104.         sleep(2)
  105.         surface.clear()
  106.     elseif cmd_lower == "whereis" then -- Shows coordinates of selected player for 2 seconds
  107.         if table.contains(staffList, cmd[2]) then
  108.             glassCMDOutput(usr,"You cannot track that player")
  109.             sleep(2)
  110.             surface.clear()
  111.         else
  112.             getPos(cmd[2],usr)
  113.             sleep(3)
  114.             surface.clear()
  115.         end
  116.     elseif cmd_lower == "track" then -- Adds selected player to an updating coordinates output
  117.         table.insert(trackedPlayers,cmd[2])
  118.         table.insert(trackOnTab,usr)
  119.     elseif cmd_lower == "clear" then -- Clears user surface
  120.         surface.clear()
  121.     elseif cmd_lower == "lamp" then
  122.         for _,v in pairs(lamps) do
  123.             sleep(0.05)
  124.             print(v)
  125.             lamp.callRemote(v,"setColor",tonumber(cmd[2]))
  126.             sleep(.05)
  127.         end
  128.     elseif cmd_lower == "help" then -- Displays possible commands
  129.         glassCMDOutput(user,"Commands are : chatcolor, nuke, invsee, request, auth, deauth, whereis, lamp, track, and clear")
  130.         sleep(3)
  131.         surface.clear()
  132.     else
  133.         local cmd_msg = table.concat(cmd, " ")
  134.         local totalmsg = usr .. ": " .. cmd_msg
  135.         if glass.getStringWidth(totalmsg) > 325 then
  136.             cutMsgOne = string.sub(cmd_msg, 1, 48)
  137.             cutMsgTwo = string.sub(cmd_msg, 49, string.len(cmd_msg))
  138.             table.insert(messages, usr .. ": " .. cutMsgOne)
  139.             table.insert(messages, usr .. ": " .. cutMsgTwo)
  140.             table.remove(messages, 1)
  141.             table.remove(messages, 1)
  142.         else
  143.             table.insert(messages, usr .. ": " .. cmd_msg)
  144.             table.remove(messages, 1)
  145.         end
  146.     end
  147. end
  148. --
  149. function getPos(player,usr) -- Displays position of given player to a user
  150.     if not player then
  151.         sleep(.01)
  152.     end
  153.     glassCMDOutput(usr,player..': x '..math.ceil(sensor.getPlayerData(player).position.x+sen_pos.x)..' y '..math.ceil(sensor.getPlayerData(player).position.y+sen_pos.y)..' z '..math.ceil(sensor.getPlayerData(player).position.z+sen_pos.z),0xFF1100)
  154.  
  155. end
  156. --
  157. function track(player,usr,pos) -- Handles the placement and updating of users in the tracked list
  158.     if not player then
  159.         sleep(.01)
  160.     end
  161.     local pos = 80 + pos  
  162.     local surface = glass.getUserSurface(usr)
  163.     surface.addText(0,pos,player..': x '..math.ceil(sensor.getPlayerData(player).position.x+sen_pos.x)..' y '..math.ceil(sensor.getPlayerData(player).position.y+sen_pos.y)..' z '..math.ceil(sensor.getPlayerData(player).position.z+sen_pos.z),0xFF1100)
  164. end
  165. --
  166. function trackOn() -- Shows tracked players to users who enable it
  167.     for i,v in pairs(trackOnTab) do
  168.         local surface = glass.getUserSurface(trackOnTab[i])
  169.         for i,v in pairs(trackedPlayers) do
  170.             surface.clear()
  171.             pos = 80
  172.             track(v,trackOnTab[i],pos)
  173.             pos = pos + 10
  174.         end
  175.     end
  176. end
  177. --
  178. function glassCMDOutput(usr,text)  -- Displays given text to a user surface
  179.     local surface = glass.getUserSurface(usr)
  180.     surface.addBox(0,90,glass.getStringWidth(text),10, 0x000000, 0.5)
  181.     surface.addText(0,90,text)
  182. end
  183. --
  184. function onlineList() -- Displays the players currently wearing ChatGlass
  185.     if #glass.getUsers() > 0 then
  186.         local usrNum = #glass.getUsers()
  187.         local usrNam = glass.getUsers()
  188.         glass.addBox(336, 20, 91, 70, 0x000000, 0.5)
  189.         for i = 1, usrNum do
  190.             h = 10 + (i * 10)
  191.             glass.addText(337, h, usrNam[i], chatColors[getName(usrNam[i])])
  192.         end
  193.     end
  194. end
  195. --
  196. function authCheck() -- Ensures all players wearing ChatGlass are contained within authedusers
  197.     currentUsers = glass.getUsers()
  198.     for i = 1, #currentUsers do
  199.         if table.contains(authedusers, currentUsers[i]) == false then
  200.             for i, v in pairs(currentUsers) do
  201.                 print(v)
  202.             end
  203.             nuke()
  204.         else
  205.             print''
  206.         end
  207.     end
  208. end
  209. --
  210.  
  211. --
  212. function invsee(sen, player, usr) -- Handles the displaying of items from a given players inventory to a user
  213.     local inventory = sen.getPlayerData(player).inventory
  214.     if not inventory then
  215.         error("Player does not exist/is not online")
  216.     end
  217.     row = 5
  218.     column = 1
  219.     for i = 10, 36 do
  220.         drawItem(row, column, inventory[i].id, inventory[i].dmg, usr)
  221.         if row == 9 then
  222.             row = 1
  223.             column = column + 1
  224.         else
  225.             row = row + 1
  226.         end
  227.     end
  228.     row = 8
  229.     column = 1
  230.     for i = 1, 9 do
  231.         drawItem(row, column, inventory[i].id, inventory[i].dmg, usr)
  232.         row = row + 1
  233.     end
  234.     row = 1
  235.     column = 1
  236.     for i = 37, 40 do
  237.         drawItem(row, column, inventory[i].id, inventory[i].dmg, usr)
  238.         column = column + 1
  239.     end
  240.     sleep(2)
  241. end
  242. --
  243. function table.contains(tab, ele) -- Checks if an element : ele , is in a table : tab
  244.     for i = 1, #tab do
  245.         if tab[i] == ele then
  246.             return true
  247.         end
  248.     end
  249.     return false
  250. end
  251. --
  252. function split(str) -- Splits a string on spaces
  253.     local words = {}
  254.     for word in str:gmatch("%S+") do
  255.         words[#words + 1] = word
  256.     end
  257.     return words
  258. end
  259. --
  260. function getName(message) -- Returns the name from a string containing a username followed me a colon and a message
  261.     local name = nil
  262.     while true do
  263.         if string.find(message, ":") then
  264.             message = string.match(message, "(.*):")
  265.         else
  266.             name = message
  267.             break
  268.         end
  269.     end
  270.     return name
  271. end
  272. --
  273. function nuke() -- Clears all glasses, clears messages table, and reboots.
  274.     glass.clear()
  275.     messages = {}
  276.     glass.clear()
  277.     shell.run("reboot")
  278.     glass.clear()
  279. end
  280. --
  281. function drawItem(x, y, id, dmg, usr) -- Draws a given item on the players surface.
  282.     local surface = glass.getUserSurface(usr)
  283.     local margin = 20
  284.     local bg = 0x404040
  285.     local fg = 0x9e9e9e
  286.     surface.addBox((x * margin) - 1, (y * margin) - 1, margin, margin, bg, 1)
  287.     surface.addBox((x * margin) - 1, (y * margin) - 1, margin - 2, margin - 2, bg, 1)
  288.     surface.addIcon(x * margin, y * margin, id, dmg)
  289. end
  290. --
  291. function indexOf(item,table) -- Returns the idex of item in a table
  292.     local index={}
  293.     for k,v in pairs(table) do
  294.        index[v]=k
  295.     end
  296.     return index[item]
  297. end
  298. --
  299. parallel.waitForAny(listener, initialize) -- Fake multithreading
  300. --
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement