Advertisement
Redxone

[ComputerCraft] - FakePlayer Enhanced

May 28th, 2016
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.02 KB | None | 0 0
  1. com = peripheral.wrap("left")
  2. local w, h = term.getSize()
  3. local sbc = term.setBackgroundColor
  4. local stc = term.setTextColor
  5. local scp = term.setCursorPos
  6. local clr = term.clear
  7. local plist = {}
  8. local c = colors
  9. local pa = paintutils
  10. local yoff = 0
  11. local selname = 1
  12. local insystem = true
  13. local colorencode = {
  14.     ['&l'] = nil,
  15.     ['&m'] = nil,
  16.     ['&n'] = nil,
  17.     ['&o'] = nil,
  18.     ['&k'] = 123,
  19.     ['&r'] = "white",
  20.     ['&1'] = "blue",
  21.     ['&2'] = "green",
  22.     ['&3'] = "cyan",
  23.     ['&4'] = "red",
  24.     ['&5'] = "purple",
  25.     ['&6'] = "brown",
  26.     ['&7'] = "lightGray",
  27.     ['&8'] = "gray",
  28.     ['&9'] = "lightBlue",
  29.     ['&a'] = "lime",
  30.     ['&b'] = "lightBlue",
  31.     ['&c'] = "pink",
  32.     ['&d'] = "magenta",
  33.     ['&e'] = "yellow",
  34.     ['&f'] = "white",
  35. }
  36. function cwrite(t,y)
  37.    scp((w/2) - (#t/2),y)
  38.    write(t)
  39. end
  40. local function getinput()
  41.     scp(1,1)
  42.     sbc(colors.orange)
  43.     term.clearLine()
  44.     return read()
  45. end
  46. local function drawtop(tx,nw)
  47.     scp(1,1)
  48.     sbc(colors.orange)
  49.     term.clearLine()
  50.     write(tx)
  51.     if(not nw)then os.pullEvent("mouse_click") end
  52. end
  53. local function resettop()
  54.     sbc(colors.orange)
  55.     scp(1,1)
  56.     term.clearLine()
  57.     stc(colors.white)
  58.     write("FakePlayer - Enhanced")
  59. end
  60. local function checkforname(nm)
  61.     for i = 1, #plist do
  62.         if(plist[i] == nm)then return true end
  63.     end
  64.     return false
  65. end
  66. function talk(nc,c,notpl)
  67.     if(notpl)then suff = "&f " else suff = "&f: " end
  68.   com.setCommand("sudo @a ping " .. plist[nc] .. suff .. c)
  69.   com.runCommand()
  70. end
  71. local function addpname(nm)
  72.     plist[#plist+1] = nm
  73. end
  74. local futils = {
  75.     writeLine = function (to,ln)
  76.         if(fs.exists(to))then
  77.             local f = fs.open(to,"w")
  78.             f.writeLine(ln)
  79.             f.close()
  80.         else
  81.             error("No such file: " .. to .. ". ")
  82.         end
  83.     end,
  84.     readLine = function (to,ln)
  85.         if(fs.exists(to))then
  86.             local f = fs.open(to,"r")
  87.             local cont = f.readLine(ln)
  88.             f.close()
  89.             return cont
  90.         else
  91.             error("No such file: " .. to .. ". ")
  92.         end
  93.     end,
  94.     readAll = function (to)
  95.         if(fs.exists(to))then
  96.             local f = fs.open(to,"r")
  97.             local cont = f.readAll()
  98.             f.close()
  99.             return cont
  100.         else
  101.             error("No such file: " .. to .. ". ")
  102.         end
  103.     end,
  104.     write = function (to,ln)
  105.         if(fs.exists(to))then
  106.             local f = fs.open(to,"w")
  107.             f.write(ln)
  108.             f.close()
  109.         else
  110.             error("No such file: " .. to .. ". ")
  111.         end
  112.     end,
  113. }
  114. function getnames()
  115.     if(not fs.exists(".pnames"))then
  116.         futils.write(".pnames",{})
  117.     end
  118.      plist = textutils.unserialize(futils.readAll(".pnames"))
  119. end
  120. local function clearname()
  121.     write(string.rep(" ",w-42)
  122.     drawback()
  123.     drawoptions()
  124. end
  125. local function drawencodestr(str)
  126.     for a = 1, #str do
  127.         if(a >= 42)then
  128.            
  129.         else
  130.             if(string.sub(str,a,a) == "&")then
  131.                 if(colorencode[string.sub(str,a,a+1)] ~= nil)then
  132.                     stc(colors[colorencode[string.sub(str,a,a+1)]])
  133.                 end
  134.                 str = string.sub(str,1,a-1) .. string.sub(str,a+1,#str)
  135.             else
  136.                 write(string.sub(str,a,a))
  137.             end
  138.         end
  139.     end  
  140. end
  141. local function drawlist(offupd)
  142.     for i = 1+yoff, (h-2)+yoff do
  143.         scp(3,(i-yoff)+2)
  144.         sbc(colors.white)
  145.         stc(colors.black)
  146.         if(plist[i] == nil)then
  147.             scp(1,(i-yoff)+2)
  148.             clearname()
  149.         else
  150.             clearname()
  151.             scp(1,(i-yoff)+2)
  152.             sbc(colors.blue)
  153.             stc(colors.white)
  154.             write("~")
  155.             local nowrite = false
  156.             scp(2,(i-yoff)+2)
  157.             if(i == selname)then
  158.                 sbc(colors.lightBlue)
  159.                 stc(colors.white)
  160.                 if(string.find(plist[i],"&"))then
  161.                     write(" ")
  162.                     drawencodestr(plist[i])
  163.                 else
  164.                     write(" " .. string.sub(plist[i],1,42))
  165.                 end
  166.             else
  167.                 sbc(colors.white)
  168.                 stc(colors.black)
  169.                 if(string.find(plist[i],"&"))then
  170.                     write(" ")
  171.                     drawencodestr(plist[i])
  172.                 else
  173.                     write(" " .. string.sub(plist[i],1,42))
  174.                 end
  175.             end
  176.         end
  177.     end
  178. end
  179. local options = {
  180.     {n="Add name",func = function()
  181.             local name = getinput()
  182.             if(checkforname(name))then
  183.                 drawtop("Name already in database! ")
  184.                 resettop()
  185.             else
  186.                 addpname(name)
  187.                 drawtop("Named added: " .. string.sub(name,1,30) .. "...")
  188.                 resettop()
  189.                 drawlist()
  190.             end
  191.     end},
  192.     {n='Del name',func = function()
  193.           drawtop("Named removed: " .. string.sub(plist[selname],1,30) .. "... ! ")
  194.           resettop()
  195.           table.remove(plist,selname)
  196.           drawlist()
  197.     end},
  198.  
  199.     {n='Chat as',func = function()
  200.         local rchat = true
  201.         while rchat do
  202.          drawtop("type /exit to exit: ",true)
  203.          local ms = read()
  204.          if(ms == "/exit")then
  205.             rchat = false
  206.             break
  207.          end
  208.             talk(selname,ms)
  209.         end
  210.         return
  211.     end},
  212.     {n='Say as',func = function()
  213.         local rchat = true
  214.         while rchat do
  215.          drawtop("type /exit to exit: ",true)
  216.          local ms = read()
  217.          if(ms == "/exit")then
  218.             rchat = false
  219.             break
  220.          end
  221.             talk(selname,ms,true)
  222.         end
  223.         return
  224.     end},
  225.     {n='Save names',func = function()
  226.             f = fs.open(".pnames","w")
  227.             f.write(textutils.serialize(plist))
  228.             f.close()
  229.         drawtop("Names saved!")
  230.         resettop()
  231.     end},
  232.     {n='Quit',func = function()
  233.         sbc(colors.black)
  234.         clr()
  235.         stc(colors.white)
  236.         scp(1,1)
  237.         cwrite("Thank you for using advanced FakePlayer system!",1)
  238.         cwrite("Created by: Lewisk3",2)
  239.         print(" ")
  240.         insystem = false
  241.         return
  242.     end},
  243. }
  244. local function drawoptions()
  245.     sbc(colors.gray)
  246.     stc(colors.white)
  247.     for i = 1, #options do
  248.         scp(w-10,i+2)
  249.         write(options[i].n)
  250.     end
  251. end
  252. local function flashoption(num)
  253.     sbc(colors.black)
  254.     scp(w-10,num+2)
  255.     stc(colors.white)
  256.     write(options[num].n)
  257.     sleep(0.1)
  258.     sbc(colors.gray)
  259.     scp(w-10,num+2)
  260.     stc(colors.white)
  261.     write(options[num].n)
  262. end
  263. local function reselect()
  264.     drawlist()
  265.     if(selname > 1)then -- up
  266.         selname = selname - 1
  267.         if(yoff > 0)then yoff = yoff - 1 end
  268.         drawlist()
  269.     elseif(selname < #plist)then -- down
  270.         selname = selname + 1
  271.         if(selname >= (h+yoff)-1)then yoff = yoff + 1 end
  272.         drawlist()
  273.     end
  274. end
  275. local function update(e)
  276.     if(plist[selname] == nil)then
  277.         reselect()
  278.     end
  279.     if(e[1] == "mouse_scroll")then
  280.         if(e[2] == -1 and selname > 1)then -- up
  281.             selname = selname - 1
  282.             if(yoff > 0)then yoff = yoff - 1 end
  283.             drawlist()
  284.         elseif(e[2] == 1 and selname < #plist)then -- down
  285.             selname = selname + 1
  286.             if(selname >= (h+yoff)-1)then yoff = yoff + 1 end
  287.             drawlist()
  288.         end
  289.     end
  290.     if(e[1] == "mouse_click" or e[1] == "mouse_drag")then
  291.         local x,y = e[3], e[4]
  292.         if(x < w-11)then
  293.             for i = 1+yoff, (h-2)+yoff do
  294.                 if(y-2 == i-yoff)then
  295.                     if(x >= 2 and x <= 2+#tostring(plist[i]))then
  296.                         selname = i
  297.                         drawlist()
  298.                         drawback()
  299.                         drawoptions()
  300.                     end
  301.                 end
  302.             end
  303.         else
  304.             for i = 1, #options do
  305.                 if(x >= w-11 and x <= (w-11)+#options[i].n and y == i+2)then
  306.                     flashoption(i)
  307.                     options[i].func()
  308.                 end
  309.             end
  310.         end
  311.     end
  312. end
  313.  
  314. local function drawback()
  315.     sbc(colors.white)
  316.     clr()
  317.     pa.drawFilledBox(w-12,2,w,h,colors.gray)
  318.     sbc(colors.orange)
  319.     scp(1,1)
  320.     term.clearLine()
  321.     stc(colors.white)
  322.     write("FakePlayer - Enhanced")
  323. end
  324. drawback()
  325. getnames()
  326. drawlist()
  327. drawoptions()
  328. while insystem do
  329.     local e = {os.pullEvent()}
  330.     update(e)
  331. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement