daily pastebin goal
4%
SHARE
TWEET

GCAPI

GravityCube Mar 2nd, 2018 (edited) 2,453 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[
  2.    
  3.             --*--*--*--*--*--*--*--*--*--*--*--*--*--*--   
  4.             --*                GCAPI                 *--
  5.             --*     https://pastebin.com/0uAaAcrW    *--
  6.             --*            by: GravityCube           *--
  7.             --*--*--*--*--*--*--*--*--*--*--*--*--*--*--
  8.  
  9.            
  10.     Changelog:
  11.         1.0.0 First release
  12.         1.1.0 Image displayer for monitors added and bugs in the economy fixed
  13.         1.2.0 Tracker of players and chatSpy added, a custom http.get was added and global peripheral.find.
  14.         1.3.0 New listener, chatEvents and local admins for selling programs!
  15.         1.4.0 Various bugs fixed (repited chatEvents, and rgb api changed). Added version changelog and getVersion()
  16.         1.5.0 table.contains added
  17.             1.5.1 ChatEvent Listener little fix
  18.             1.5.2 Split function modified (self) and getChar added
  19.             1.5.3 getCenter added and centered of monitors fixed
  20.             1.5.4 getCenter now supports number and string in the #2 argument
  21.             1.5.5 Added some default "URLs" to the images
  22.             1.5.6 URL Alias updated
  23.             1.5.7 Added password for unfiltered tracker (akaElite request)
  24.         1.6.0 New method for tracking and some minor modifications.
  25.             1.6.1 Some minor changes and bugs fixed
  26.         1.7.0 New method gcapi.customMonitorWrite()
  27.             1.7.1 Added table.isEmpty
  28.             1.7.2 New admin (Freecss).
  29.         1.8.0 New method printAvatar()
  30.             1.8.1 The methods for Rule34 were removed.
  31.        
  32. --]]
  33. --------------------------------------------
  34. -->               Version                <--
  35. --------------------------------------------
  36. local debugAPI = false
  37.  
  38. local version = "1.8.1"
  39.  
  40. print("GCAPI Version: " .. version)
  41.  
  42. function getVersion()
  43.     return version
  44. end
  45. --------------------------------------------
  46. -->              URL ALIAS               <--
  47. --------------------------------------------
  48. local urlAlias = {
  49.     ["emx"] = "http://bmo.emx.cl/logo.jpg",
  50.     ["cl"] = "http://craftersland.net/images/logo.png",
  51.     ["deadpool"] = "http://pixelartmaker.com/art/de17950892c0026.png"
  52. }
  53. --------------------------------------------
  54. -->                Tools                 <--
  55. --------------------------------------------
  56.  
  57. local charToColor = {["0"]=0, ["1"]= 1,["2"]= 2, ["3"]=3, ["4"]=4, ["5"]=5, ["6"]=6, ["7"]=7, ["8"]=8, ["9"]=9, ["a"]=10 ,["b"]=11, ["c"]=12, ["d"]=13, ["e"]=14, ["f"]=15}
  58. function customMonitorWrite(mon, text)
  59.     local xi, yi = mon.getCursorPos()
  60.     local chars = {}
  61.     for i = 1, string.len(text) do
  62.         local c = text:sub(i,i)
  63.         table.insert(chars, c)
  64.     end
  65.    
  66.     --charToColor["g"] = mon.getBackgroundColor()
  67.     local isNumber = false
  68.     for i=1, #chars, 1 do
  69.         local c = chars[i]
  70.         if c == "&" and charToColor[chars[i+1]] then
  71.             color = 2^chars[i+1]
  72.             mon.setBackgroundColor(color)
  73.             isNumber = true
  74.         else
  75.             if not isNumber then
  76.                 mon.write(c)
  77.             end
  78.             isNumber = false
  79.         end
  80.     end
  81. end
  82.  
  83.  
  84. function verifyVar(rawVar, varType)
  85.     verifyVar(varType, "string")
  86.     if type(rawVar) ~= varType then
  87.         error( "bad argument (expected " .. varType .. ", got " .. type( rawVar ) .. ")", 2 )
  88.     end
  89. end
  90.  
  91. -- RANDOM STRING
  92. local charset = {}
  93. -- qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890
  94. for i = 48,  57 do table.insert(charset, string.char(i)) end
  95. for i = 65,  90 do table.insert(charset, string.char(i)) end
  96. for i = 97, 122 do table.insert(charset, string.char(i)) end
  97.  
  98. math.randomseed(os.time()*100)
  99. function randomString(length)
  100.     if length > 0 then
  101.         return string.random(length - 1) .. charset[math.random(1, #charset)]
  102.     else
  103.         return ""
  104.     end
  105. end
  106. string.random = randomString
  107. --END RANDOM STRING
  108.  
  109. function getRandom(minValue, maxValue)
  110.     return math.random(minValue, maxValue)
  111. end
  112.  
  113. --peripheral.find() in Tekkit
  114. function findPeripheral(pType)
  115.     local pList = {}
  116.     for _,pName in pairs(peripheral.getNames()) do
  117.         if peripheral.getType(pName) == pType then
  118.             table.insert(pList, peripheral.wrap(pName))
  119.         end
  120.     end
  121.     return unpack(pList)
  122. end
  123. peripheral.find = findPeripheral
  124.  
  125. --Wipe table "t"
  126. function wipeTable(t)
  127.     local listForDeletion = {}
  128.     for k,v in pairs(t) do
  129.         table.insert(listForDeletion, k)
  130.     end
  131.    
  132.     for _,i in pairs(listForDeletion) do
  133.         t[i] = nil
  134.     end
  135. end
  136. table.wipe = wipeTable
  137.  
  138. --Split string
  139. function split(self, delimiter)
  140.     result = {};
  141.     for match in (self..delimiter):gmatch("(.-)"..delimiter) do
  142.         table.insert(result, match);
  143.     end
  144.     return result;
  145. end
  146. string.split = split
  147.  
  148. string.getChar = function (self, nVar)
  149.     if type(nVar) ~= "number" then
  150.         error( "bad argument #2 (expected number, got " .. type( nVar ) .. ")")
  151.     else
  152.         return self:sub(nVar, nVar)
  153.     end
  154. end
  155.  
  156. table.isEmpty = function(self)
  157.     for k,v in pairs(self) do
  158.         return true
  159.     end
  160.     return false
  161. end
  162.  
  163. table.contains = function (self, eVar)
  164.     for k,v in pairs(self) do
  165.         if v == eVar then
  166.             return true
  167.         end
  168.     end
  169.     return false
  170. end
  171.  
  172. --Print to a file and term (message, dir(optional) )
  173. function printLog(line, dir)
  174.     if line == nil then
  175.         return nil
  176.     end
  177.     if dir == nil then
  178.         dir = "/log"
  179.     end
  180.     line = line
  181.     print(line)
  182.    
  183.     file = fs.open(dir,"a")
  184.     file.writeLine(line)
  185.     file.close()
  186. end
  187.  
  188. --Are items equal? Ignoring quantity
  189. function equalItems(item1, item2)
  190.     if item1 == nil or item2 == nil then
  191.         return false
  192.     end
  193.     for k,v in pairs(item1) do
  194.         if k ~= qty then
  195.             if item2[k] ~= v then
  196.                 return false
  197.             end
  198.         end
  199.     end
  200.     return true
  201. end
  202.  
  203. --Get number of lines file
  204. function getNumberOfLinesFile(filePath)
  205.     if not fs.exists(filePath) then
  206.         print("[GCAPI] File not found")
  207.         return 0
  208.     end
  209.     file = fs.open(filePath, "r")
  210.     if file then
  211.         local i = 0
  212.         while file.readLine() do
  213.             i = i + 1
  214.         end
  215.         file.close()
  216.         return i
  217.     end
  218.     return 0
  219. end
  220.  
  221. function getListFromFile(filePath)
  222.     local list = {}
  223.     if not fs.exists(filePath) then
  224.         saveListToFile(filePath, list)
  225.         return list
  226.     end
  227.     local file = fs.open(filePath,"r")
  228.     local data = file.readAll()
  229.     file.close()
  230.     list = textutils.unserialize(data)
  231.     if textutils.unserialize(data) == nil then
  232.         list = {}
  233.     end
  234.     return list
  235. end
  236.  
  237. function saveListToFile(filePath, list)
  238.     file = fs.open(filePath,"w")
  239.     file.write(textutils.serialize(list))
  240.     file.close()
  241. end
  242.  
  243. string.starts = function (self,Start)
  244.    return string.sub(self,1,string.len(Start))==Start
  245. end
  246.  
  247. function getCenter(f, varP)
  248.     if not varP then
  249.         varP = 0
  250.     end
  251.    
  252.     local lengthOfVar = 0
  253.     if type(varP) == "number" then
  254.         lengthOfVar = varP
  255.     elseif type(varP) == "string" then
  256.         lengthOfVar = string.len(varP)
  257.     else
  258.         error("For function gcapi.getCenter expected number or string in argument #2")
  259.     end
  260.    
  261.     if (f - string.len(varP) % 2) == 0 then
  262.         return math.floor((f - lengthOfVar)/2)
  263.     end
  264.    
  265.     return math.floor((f - lengthOfVar)/2)+1
  266.    
  267. end
  268.  
  269. --------------------------------------------
  270. -->          Custom Http Request         <--
  271. -->    http.get with timeout variable    <--
  272. --------------------------------------------
  273.  
  274. function http.getStringWithTimeout(url, headers, timeout)
  275.     if timeout == nil then
  276.         local response = http.get(url, headers)
  277.         local responseString = response.readAll()
  278.         response.close()
  279.         return responseString
  280.     end
  281.    
  282.     --seconds to ticks
  283.     timeout = timeout*20
  284.    
  285.     http.request(url, nil, headers)
  286.    
  287.     local requesting = true
  288.    
  289.     local localReloj = 0
  290.     local nextTimeEventID = os.startTimer(0.1)
  291.     while requesting do
  292.        
  293.         --Wait for event.
  294.         tEvent = {os.pullEvent()}
  295.        
  296.         if "timer" == tEvent[1] then
  297.             if tEvent[2] == nextTimeEventID then
  298.                 if timeout < localReloj then
  299.                     return nil
  300.                 end
  301.                 localReloj = localReloj + 2
  302.                 nextTimeEventID = os.startTimer(0.1)
  303.             end
  304.         else
  305.             --Success.
  306.             if tEvent[1] == "http_success" and url == tEvent[2] then
  307.                 local response = tEvent[3]
  308.                 local responseString = response.readAll()
  309.                 response.close()
  310.                 return responseString
  311.             end
  312.             --Failure
  313.             if tEvent[1] == "http_failure" and url == tEvent[2] then
  314.                 return nil
  315.             end
  316.            
  317.             nextTimeEventID = os.startTimer(0.1)
  318.         end
  319.        
  320.     end
  321. end
  322.  
  323.  
  324. --------------------------------------------
  325. -->             ImageDisplay             <--
  326. -->                                      <--
  327. -->              IMPORTANT:              <--
  328. -->  The API server get bugged sometimes <--
  329. -->  so I had to make a timeout http.get <--
  330. --------------------------------------------
  331.  
  332. -- ** Display Image from URL **
  333.  
  334. -- Example:
  335. -- url = http://www.image.com/image.jpg
  336. -- glassesPeripheral = peripheral.wrap(glassesName) [Optional]
  337. -- maxSize = 10 (10 max of height and weight) [Optional]
  338.  
  339. -- NOTE: If you want the image to be send to only 1 player use
  340. -- peripheral.wrap(glassesName).getUserSurface(playerName)
  341. -- instead of peripheral.wrap(glassesName)
  342.  
  343. function displayImageFromURL(url, glassesPeripheral, maxSize, xo, yo)
  344.    
  345.     local filePath, ok, err = getImagePathFromURL(url, maxSize)
  346.     if not ok then
  347.         return false, err
  348.     end
  349.    
  350.     displayImageFromFile(filePath, glassesPeripheral, xo, yo)
  351.     fs.delete(filePath)
  352.    
  353.     return true
  354. end
  355.  
  356. -- ** Display Image from Rule34 **
  357.  
  358. -- Example:
  359. -- tag = random (just 1)
  360. -- glassesPeripheral = peripheral.wrap(glassesName) [Optional]
  361. -- maxSize = 10 (10 max of height and weight) [Optional]
  362.  
  363. -- NOTE: If you want the image to be send to only 1 player use
  364. -- peripheral.wrap(glassesName).getUserSurface(playerName)
  365. -- instead of peripheral.wrap(glassesName)
  366.  
  367. function getGlasses(glassesPeripheral)
  368.     if glassesPeripheral == nil then
  369.         return peripheral.find("openperipheral_glassesbridge")
  370.     end
  371.     return glassesPeripheral
  372. end
  373.  
  374. -- file = fs.open("imagen","r")
  375. -- glassesPeripheral = peripheral.wrap(glassesName) [Optional]
  376. function displayImageFromFile(filePath, glassesPeripheral, xo, yo)
  377.    
  378.     if xo == nil then
  379.         xo = 1
  380.     end
  381.     if yo == nil then
  382.         yo = 1
  383.     end
  384.    
  385.     numberLines = getNumberOfLinesFile(filePath)
  386.     file = fs.open(filePath, "r")
  387.    
  388.     gb = getGlasses(glassesPeripheral) 
  389.     if gb == nil then
  390.         error("[GCAPI] Glasses peripheral not found")
  391.     end
  392.    
  393.     for i=1,numberLines,1 do
  394.         linea = file.readLine(i)
  395.         lineaQ = {}
  396.         lineaQ = split(linea,",")
  397.         for x,color in pairs(lineaQ) do
  398.             hcolor = assert(loadstring("return "..color))()
  399.             gb.addBox(x+xo,i+yo,1,1,hcolor,1)
  400.         end
  401.     end
  402.     file.close()
  403. end
  404.  
  405. function getImagePathFromURL(url, maxSize, max_x, max_y, forMonitor, filePath)
  406.    
  407.     if filePath == nil then
  408.         filePath = 'gcAPIImage'
  409.     end
  410.  
  411.     if url == nil then
  412.         err = '[GCAPI] No URL for image'
  413.         print(err)
  414.         return nil, false, err
  415.     end
  416.    
  417.     --DELETE SAVED IMAGE
  418.     if url == "none" then
  419.         fs.delete(filePath)
  420.     end
  421.    
  422.     --URL ALIAS
  423.     if urlAlias[url] then
  424.         url = urlAlias[url]
  425.     end
  426.    
  427.     extraParameters = ""
  428.     if maxSize ~= nil and tonumber(maxSize) then
  429.         extraParameters = extraParameters .. '&max_size=' .. maxSize
  430.     end
  431.    
  432.     if max_x ~= nil and tonumber(max_x) then
  433.         extraParameters = extraParameters .. '&max_x=' .. max_x
  434.     end
  435.    
  436.     if max_y ~= nil and tonumber(max_y) then
  437.         extraParameters = extraParameters .. '&max_y=' .. max_y
  438.     end
  439.    
  440.     script = "imageCC"
  441.    
  442.     if forMonitor then
  443.         script = "monitorImageCC"
  444.     end
  445.    
  446.     local dataWeb = http.getStringWithTimeout("http://bmo.emx.cl/" .. script .. ".php?url=" .. url .. extraParameters, nil, 3)
  447.     if not dataWeb then
  448.         err = "[GCAPI] Server error or timeout"
  449.         print(err)
  450.         return nil, false, err
  451.     end
  452.    
  453.     if #dataWeb < 30 then
  454.         err = "[GCAPI] " .. dataWeb
  455.         print(err)
  456.         return nil, false, err
  457.     end
  458.    
  459.     fs.delete(filePath)
  460.    
  461.     f = fs.open(filePath, "w")
  462.     f.write(dataWeb)
  463.     f.close()
  464.    
  465.     return filePath, true, nil
  466.    
  467. end
  468.  
  469. --------------------------------------------
  470. -->             ImageDisplay for         <--
  471. -->                 monitors             <--
  472. --------------------------------------------
  473. function getMonitor(monSelected)
  474.     if monSelected == nil then
  475.         return peripheral.find("monitor")
  476.     end
  477.     return monSelected
  478. end
  479. function printImageFromURL(url, monSelected, centered)
  480.    
  481.     mon = getMonitor(monSelected)
  482.    
  483.     if mon == nil then
  484.         error("[GCAPI] Monitor not found")
  485.     end
  486.    
  487.     mon.setTextScale(0.5)
  488.     mon.clear()
  489.     local max_x, max_y = mon.getSize()
  490.    
  491.     local filePath, ok, err = getImagePathFromURL(url, nil, max_x, max_y, true)
  492.     if not ok then
  493.         return false, err
  494.     end
  495.  
  496.     return printImageFromFile(filePath, mon, centered, true)
  497. end
  498.  
  499. function printAvatar(mon, player_name, x, y, max_side)
  500.     local xo = x
  501.     local yo = y
  502.    
  503.     local filePath, ok, err = getImagePathFromURL("https://minotar.net/avatar/" .. player_name .. "/"..max_side, nil, max_side, max_side, true)
  504.     if not ok then
  505.         return false, err
  506.     end
  507.    
  508.     numberLines = getNumberOfLinesFile(filePath)
  509.     local file = fs.open(filePath, "r")
  510.     for i=1,numberLines,1 do
  511.         linea = file.readLine(i)
  512.         lineaQ = {}
  513.         lineaQ = split(linea,",")
  514.         for x,colorSet in pairs(lineaQ) do
  515.             colors0 = split(colorSet,"-")
  516.             r = tonumber(colors0[1])
  517.             g = tonumber(colors0[2])
  518.             b = tonumber(colors0[3])
  519.            
  520.             if r and g and b then
  521.                 hcolor = colors.fromRGB(r, g, b)
  522.                 mon.setBackgroundColour(hcolor)
  523.                 mon.setCursorPos(x+xo,i+yo)
  524.                 mon.write(" ")
  525.             end
  526.         end
  527.     end
  528.     file.close()
  529.    
  530.     return true
  531. end
  532.  
  533. function printImageFromFile(filePath, monSelected, centered, monSetup)
  534.    
  535.     mon = monSelected
  536.    
  537.     if monSetup == nil or monSetup == false then
  538.         mon = getMonitor(monSelected)
  539.         if mon == nil then
  540.             error("[GCAPI] Monitor not found")
  541.         end
  542.     end
  543.    
  544.     mon.setTextScale(0.5)
  545.     mon.clear()
  546.     local max_x, max_y = mon.getSize()
  547.  
  548.     numberLines = getNumberOfLinesFile(filePath)
  549.    
  550.     xo = 0
  551.     yo = 0
  552.    
  553.     if centered and numberLines > 0 then
  554.         file = fs.open(filePath, "r")
  555.         real_x = #(split(file.readLine(1), ","))
  556.         file.close()
  557.         xo = getCenter(max_x, real_x)
  558.         yo = getCenter(max_y, numberLines)
  559.     end
  560.    
  561.    
  562.     file = fs.open(filePath, "r")
  563.     for i=1,numberLines,1 do
  564.         linea = file.readLine(i)
  565.         lineaQ = {}
  566.         lineaQ = split(linea,",")
  567.         for x,colorSet in pairs(lineaQ) do
  568.             colors0 = split(colorSet,"-")
  569.             r = tonumber(colors0[1])
  570.             g = tonumber(colors0[2])
  571.             b = tonumber(colors0[3])
  572.            
  573.             if r and g and b then
  574.                 hcolor = colors.fromRGB(r, g, b)
  575.                 mon.setBackgroundColour(hcolor)
  576.                 mon.setCursorPos(x+xo,i+yo)
  577.                 mon.write(" ")
  578.             end
  579.         end
  580.     end
  581.     file.close()
  582.    
  583.     return true
  584. end
  585.  
  586. --------------------------------------------
  587. -->             ImageDisplay for         <--
  588. -->            chunks of monitors        <--
  589. --------------------------------------------
  590. local chunksHeight = 0
  591. local chunksWidth = 0
  592. monitors = {}
  593.  
  594. function printPixel(x,y,color)
  595.    
  596. end
  597.  
  598.  
  599. --------------------------------------------
  600. -->            Tracker of players        <--
  601. -->          Just for craftersland       <--
  602. -->                  DYNMAP              <--
  603. --------------------------------------------
  604. function getPosPlayer(player, dataPlayers)
  605.     if player == nil or player == "" then
  606.         err = "User not found"
  607.         print("[GCAPI] " .. err)
  608.         return nil, false, err
  609.     end
  610.    
  611.     player = string.lower(player)
  612.    
  613.     if not dataPlayers then
  614.         dataPlayers, ok, err = getPosPlayers()
  615.     end
  616.    
  617.     for i,dataPlayer in pairs(dataPlayers) do
  618.         user = string.lower(dataPlayer["name"])
  619.         if string.match(user, player) then
  620.             return dataPlayer, true, nil
  621.         end
  622.     end
  623.    
  624.     err = "User not found"
  625.     return nil, false, err
  626. end
  627.  
  628. function getPosPlayers(password)
  629.     local url = "http://bmo.emx.cl/dynmapPos.php"
  630.     if password then
  631.         url = url .. "?password=" .. password
  632.     end
  633.     local dataWeb = http.get(url)
  634.     local dataPlayers = textutils.unserialize(dataWeb.readAll())
  635.     dataWeb.close()
  636.    
  637.     if not dataWeb then
  638.         return nil, false, "[GCAPI] Server Error"
  639.     end
  640.    
  641.     if dataPlayers["error"] ~= nil then
  642.         return nil, false, dataPlayers["error"]
  643.     end
  644.    
  645.     return dataPlayers, true, nil
  646. end
  647.  
  648. function getClosePlayers(user, range)
  649.     local url = "http://bmo.emx.cl/closePlayers.php?user=" .. user .. "&range=" .. range
  650.    
  651.     local dataWeb = http.get(url)
  652.     local closePlayers = textutils.unserialize(dataWeb.readAll())
  653.     dataWeb.close()
  654.    
  655.     if not dataWeb then
  656.         return nil, false, "[GCAPI] Server Error"
  657.     end
  658.    
  659.     if closePlayers["error"] ~= nil then
  660.         return nil, false, closePlayers["error"]
  661.     end
  662.    
  663.     return closePlayers, true, nil
  664. end
  665. --------------------------------------------
  666. -->            Chat tracker for          <--
  667. -->              Craftersland            <--
  668. --------------------------------------------
  669.  
  670. function getChat()
  671.     dataWeb = http.get("http://bmo.emx.cl/chatSpy.php")
  672.     dataChat = textutils.unserialize(dataWeb.readAll())
  673.     dataWeb.close()
  674.    
  675.     if not dataWeb then
  676.         return nil, false, "[GCAPI] Server Error"
  677.     end
  678.    
  679.     return dataChat, true, nil
  680. end
  681.  
  682.  
  683. --------------------------------------------
  684. -->                Economy               <--
  685. -->                                      <--
  686. -->         This is saved every use      <--
  687. -->           for safety purposes.       <--
  688. --------------------------------------------
  689.  
  690.  
  691. --------------------------------------------
  692. -->          Admins for selling          <--
  693. --------------------------------------------
  694. admins = {"GravityCube", "Archmaestro", "Lancellot", "Freecss"}
  695. function hasPermissions(player, globalAdmins)
  696.     if globalAdmins == nil then
  697.         globalAdmins = true
  698.     end
  699.     for _,user in pairs(getAdmins(globalAdmins)) do
  700.         if user == player then
  701.             return true
  702.         end
  703.     end
  704.     return false
  705. end
  706.  
  707. function getAdmins(globalAdmins)
  708.     localAdmins = {}
  709.     if fs.exists("admins") then
  710.         localAdmins = getListFromFile("admins")
  711.     end
  712.    
  713.     if globalAdmins then
  714.         for _,admin in pairs(admins) do
  715.             table.insert(localAdmins, admin)
  716.         end
  717.     end
  718.    
  719.     return localAdmins
  720. end
  721. --------------------------------------------
  722. -->              EventHanlder            <--
  723. --------------------------------------------
  724. if not lastID then
  725.     lastID = 0
  726. end
  727. function startChatEventQueue()
  728.     while true do
  729.         chatData, ok, err = getChat()
  730.         if ok then
  731.             lastIDTemp = lastID
  732.             for i=1, 10, 1 do
  733.                 data = chatData[i]
  734.                 if data ~= nil then
  735.                     id = data["id"]
  736.                     if id > lastID then
  737.                         if id > lastIDTemp then
  738.                             lastIDTemp = id
  739.                         end
  740.                        
  741.                         player = data["name"]
  742.                         command = data["message"]
  743.                        
  744.                         if command ~= nil and string.starts(command, "!") then
  745.                             command = string.sub(command, 2)
  746.                             lastID = lastIDTemp
  747.                             os.queueEvent("chatEvent", player, command)
  748.                         end
  749.                     end    
  750.                 end
  751.             end
  752.             lastID = lastIDTemp
  753.         end
  754.         sleep(1)
  755.     end
  756. end
  757.  
  758.  
  759. --*--*--*--*--*--*--*--*--*--*--*--*--*--*--
  760. --*             RBG TO COLOUR            *--
  761. --*          by CrazedProgrammer         *--
  762. --*    (Used in the printImageFromURL)   *--
  763. --*   https://pastebin.com/BCSWghjR/rgb  *--
  764. --*--*--*--*--*--*--*--*--*--*--*--*--*--*--
  765. local hex = {"F0F0F0", "F2B233", "E57FD8", "99B2F2", "DEDE6C", "7FCC19", "F2B2CC", "4C4C4C", "999999", "4C99B2", "B266E5", "3366CC", "7F664C", "57A64E", "CC4C4C", "191919"}
  766. local rgb = {}
  767. for i=1,16,1 do
  768.   rgb[i] = {tonumber(string.sub(hex[i],1, 2), 16), tonumber(string.sub(hex[i],3, 4), 16), tonumber(string.sub(hex[i], 5, 6), 16)}
  769. end
  770.  
  771. colors.fromRGB = function (r, g, b)
  772.   local dist = 1e100
  773.   local d = 1e100
  774.   local color = -1
  775.   for i=1,16,1 do
  776.     d = math.sqrt((math.max(rgb[i][1], r) - math.min(rgb[i][1], r)) ^ 2 + (math.max(rgb[i][2], g) - math.min(rgb[i][2], g)) ^ 2 + (math.max(rgb[i][3], b) - math.min(rgb[i][3], b)) ^ 2)
  777.     if d < dist then
  778.       dist = d
  779.       color = i - 1
  780.     end
  781.   end
  782.   return 2 ^ color
  783. end
  784.  
  785. colours.fromRGB = colors.fromRGB
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top