Jump to content

GravityCube

VIP
  • Posts

    96
  • Joined

  • Last visited

  • Days Won

    2

Posts posted by GravityCube

  1. ComputerCraft Tools.

    I made this post to facilitate the access to simple tools that I made or I found on internet to make different kind of programs.

    peripheral.find This function helps you to find every peripheral connected to the computer of a certain type (sType) or a custom filter (fnFilter). Source.


      Example:

    Spoiler
    mon = peripheral.find(“monitor”) instead of mon = peripheral.wrap(“monitor_1”)

     

      Function:

    Spoiler
    peripheral.find = function ( sType, fnFilter )
        if type( sType ) ~= "string" then
            error( "bad argument #1 (expected string, got " .. type( sType ) .. ")", 2 )
        end
        if fnFilter ~= nil and type( fnFilter ) ~= "function" then
            error( "bad argument #2 (expected function, got " .. type( fnFilter ) .. ")", 2 )
        end
        local tResults = {}
        for n,sName in ipairs( peripheral.getNames() ) do
            if peripheral.getType( sName ) == sType then
                local wrapped = peripheral.wrap( sName )
                if fnFilter == nil or fnFilter( sName, wrapped ) then
                    table.insert( tResults, wrapped )
                end
            end
        end
        return table.unpack( tResults )
    end

     

     

     

     

    string.random This function gives you a random string with a certain amount of characters. Source: @XMedders


      Function:

    Spoiler
    -- RANDOM STRING
    local charset = {}
    -- qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890
    for i = 48,  57 do table.insert(charset, string.char(i)) end
    for i = 65,  90 do table.insert(charset, string.char(i)) end
    for i = 97, 122 do table.insert(charset, string.char(i)) end
    
    math.randomseed(os.time()*100)
    function randomString(length)
    	if length > 0 then
    		return string.random(length - 1) .. charset[math.random(1, #charset)]
    	else
    		return ""
    	end
    end
    string.random = randomString

     

     

     

     

    string.split With this you can split a string into a table with a delimiter.

      Example:

    Spoiler
    eString = "Hello World"
    eTable = string.split(eString, " ")
    
    print(eTable[1])
    --OUTPUT: Hello
    
    print(eTable[2])
    --OUTPUT: World

     


      Function:

    Spoiler
    string.split = function (self, delimiter)
    	result = {};
    	for match in (self..delimiter):gmatch("(.-)"..delimiter) do
    		table.insert(result, match);
    	end
    	return result;
    end

     

     

     

     

    http.getStringWithTimeout This is a custom http.get that have a timeout for the requests if the website doesnt response so your program doesnt get stuck.

    Function:

    Spoiler
    function http.getStringWithTimeout(url, headers, timeout)
    	if timeout == nil then
    		local response = http.get(url, headers)
    		local responseString = response.readAll()
    		response.close()
    		return responseString
    	end
    	
    	--seconds to ticks
    	timeout = timeout*20
    	
    	http.request(url, nil, headers)
    	
    	local requesting = true
    	
    	local localReloj = 0
    	local nextTimeEventID = os.startTimer(0.1)
    	while requesting do
    		
    		--Wait for event.
    		tEvent = {os.pullEvent()}
    		
    		if "timer" == tEvent[1] then
    			if tEvent[2] == nextTimeEventID then
    				if timeout < localReloj then
    					return nil
    				end
    				localReloj = localReloj + 2
    				nextTimeEventID = os.startTimer(0.1)
    			end
    		else
    			--Success.
    			if tEvent[1] == "http_success" and url == tEvent[2] then
    				local response = tEvent[3]
    				local responseString = response.readAll()
    				response.close()
    				return responseString
    			end
    			--Failure
    			if tEvent[1] == "http_failure" and url == tEvent[2] then
    				return nil
    			end
    			
    			nextTimeEventID = os.startTimer(0.1)
    		end
    		
    	end
    end

     

     

     

    GCAPI

    To be honest I'm too lazy to explain all the function of the API so I'm going to give you  easy example programs for you to try. I disabled some stuff like the rule34 ;)

      Download API

        > pastebin get XJAdkfJ8 gcapi

     

      Images for Monitor This program display a image from a URL from internet.

    Spoiler
    os.loadAPI("gcapi")
     
    if fs.exists('gcAPIImage') then
        gcapi.printImageFromFile('gcAPIImage', nil, true)
    end
    while true do  
        write("Enter the url: ")
        gcapi.printImageFromURL(gcapi.split(read()," ")[1], nil, true)
    end

     

     

    Images for glasses This is almost the same that the one for monitors but with commands for the glasses and higher resolution

    Spoiler
    os.loadAPI("gcapi")
    playerImages = {}
    
    reloj = 2
    relojAnterior = 0
    globalReset = 0
    
    errorMessages={} --{[1] = {["player"]=jugador, ["id"]=idMessage, ["resetTime"] = reloj}}
    
    --------------------------------------------
    -->               Version                <--
    --------------------------------------------
    
    
    --------------------------------------------
    -->                Tools                 <--
    --------------------------------------------
    function hasPermissions(player)
    	return gcapi.hasPermissions(player)
    end
    
    --------------------------------------------
    -->               Glasses                <--
    --------------------------------------------
    function hasGlasses(player)
    	local gb = gcapi.getGlasses()
    	if gb == nil then return false end
    	
    	for k,v in pairs(gb.getUsers()) do
    		if player == v then
    			return true
    		end
    	end
    	return false
    end
    
    
    --DONT USE THIS FUNCTION
    local function displayMessage(player, message, color)
    	
    	local gb = gcapi.getGlasses()
    	
    	if color == nil then
    		color = 0xFFFFFF
    	end
    	
    	if not hasGlasses(player) then
    		return 0 
    	end
    	
    	glasses = gb.getUserSurface(player)
    	
    	messageO = glasses.addText(10,65,message,color)
    	
    	return messageO.getId()
    end
    
    function displayError(player, message)
    	local gb = gcapi.getGlasses()
    	local id = displayMessage(player,message,0xffcc00)
    	local a = {["player"]=player,["id"]=id, ["resetTime"] = reloj+120}
    	table.insert(errorMessages,a)
    	print("<"..player.."> Error: " .. message)
    end
    
    function toggleErrorMessages()
    	
    	local gb = gcapi.getGlasses()
    	
    	if math.floor(reloj/10) ~= math.floor(relojAnterior/10) then
    		local deletionList = {}
    		for i=#errorMessages,1,-1 do
    			local tablita = errorMessages[i]
    			
    			local player = tablita["player"]
    			local id = tablita["id"]
    			local resetTime = tablita["resetTime"]
    			
    			if resetTime < reloj then
    				table.insert(deletionList, i)
    			else
    				if hasGlasses(player) then
    					glasses = gb.getUserSurface(player)
    					message = glasses.getById(id)
    					if message ~= nil then
    						if message.getColor() == 0xff0c00 then
    							message.setColor(0xffcc00)
    						else
    							message.setColor(0xff0c00)						
    						end
    					else
    						table.insert(deletionList, i)
    					end
    				end
    			end
    		end
    		
    		for _,i in pairs(deletionList) do
    			local id = errorMessages[i]["id"]
    			local player = errorMessages[i]["player"]
    			errorMessages[_] = nil
    			deleteMessage(id, player, gb)
    		end
    	end
    end
    
    function deleteMessage(id, player, gb)
    	if hasGlasses(player) then
    		glasses = gb.getUserSurface(player)
    		message = glasses.getById(id)
    		if message ~= nil then
    			message.delete()
    		end
    	end
    end
    
    --------------------------------------------
    -->          Command executor            <--
    --------------------------------------------
    function executeCommand(player, co)
    	co = co .. " "
    	command = {}
    	command = gcapi.split(co," ")
    	
    	
    	if (command[1] == nil or command[2] == nil) then
    		return false
    	end
    	
    	if command[1] == "image" then
    		gb = gcapi.getGlasses()
    		glasses = gb.getUserSurface(player)
    	
    		if not hasGlasses(player) then
    			return false
    		end
    		resetTime = 60
    		if tonumber(command[3]) then
    			resetTime = math.floor(math.abs(tonumber(command[3])*20))
    		end
    		
    		maxSize = 200
    		if tonumber(command[4]) then
    			maxSize = math.floor(math.abs(tonumber(command[4])))
    			if maxSize > 200 then
    				maxSize = 200
    			end
    		end
    		
    		url = command[2]
    		
    		ok, err = gcapi.displayImageFromURL(url, glasses, maxSize, 10, 10)
    		if (ok) then
    			playerImages[player] = reloj + resetTime
    		else
    			displayError(player, err)
    		end
    		
    	end
    	
    	if command[1] == "rule34" then
    		gb = gcapi.getGlasses()
    		glasses = gb.getUserSurface(player)
    	
    		if not hasGlasses(player) then
    			return false
    		end
    		resetTime = 60
    		if tonumber(command[3]) then
    			resetTime = math.floor(math.abs(tonumber(command[3])*20))
    		end
    		
    		maxSize = 200
    		if tonumber(command[4]) then
    			maxSize = math.floor(math.abs(tonumber(command[4])))
    			if maxSize > 200 then
    				maxSize = 200
    			end
    		end
    		
    		tag = command[2]
    		
    		ok, err = gcapi.displayImageFromRule34(tag, glasses, maxSize, 10, 10)
    		
    		if (ok) then
    			playerImages[player] = reloj + resetTime
    		else
    			displayError(player, err)
    		end
    		
    	end
    	
    	if command[1] == "imageplayer" and hasPermissions(player) then
    		if (command[3] == nil) then
    			return false
    		end
    		
    		player2 = command[2]
    		
    		gb = gcapi.getGlasses()
    		
    		glasses = gb
    		if player2 ~= "all" then
    			if not hasGlasses(player2) then
    				displayError(player, "Player dont have the glasses")
    				return false
    			end
    			glasses = gb.getUserSurface(player2)
    		end
    		
    		resetTime = 60
    		if tonumber(command[4]) then
    			resetTime = math.floor(math.abs(tonumber(command[4])*20))
    		end
    		
    		maxSize = 200
    		if tonumber(command[5]) then
    			maxSize = math.floor(math.abs(tonumber(command[5])))
    			if maxSize > 200 then
    				maxSize = 200
    			end
    		end
    		
    		url = command[3]
    		
    		ok, err = gcapi.displayImageFromURL(url, glasses, maxSize, 10, 10)
    		if (ok) then
    			if player2 == "all" then
    				globalReset = reloj + resetTime
    			else
    				playerImages[player2] = reloj + resetTime
    			end
    		else
    			displayError(player, err)
    		end
    	end
    end
    
    --------------------------------------------
    -->            Main program              <--
    --------------------------------------------
    
    nextTimeEventID = os.startTimer(0.1)
    while true do
    	
    	--Wait for event.
    	tEvent = {os.pullEvent()}
    	
    	--TIMER
    	if "timer" == tEvent[1] then
    		if tEvent[2] == nextTimeEventID then
    			relojAnterior = reloj
    			reloj = reloj + 2
    			
    			--Periodic Reset
    			if math.floor(reloj/20) ~= math.floor(relojAnterior/20) then
    			
    				gb = gcapi.getGlasses()
    				
    				listForDeletion = {}
    				for player, resetTime in pairs(playerImages) do
    					if resetTime < reloj then
    						if hasGlasses(player) then
    							glasses = gb.getUserSurface(player)
    							glasses.clear()
    							table.insert(listForDeletion, player)
    						end
    					end
    				end
    				
    				for _,player in pairs(listForDeletion) do
    					playerImages[player] = nil
    				end
    				
    				if globalReset < reloj and globalReset > 0 then
    					gb.clear()
    					globalReset = 0
    				end
    			end
    			
    			toggleErrorMessages()
    						
    			relojAnterior = reloj
    			
    			nextTimeEventID = os.startTimer(0.1)
    		end
    	else
    		--Process Event.
    		if tEvent[1] == "chat_command" then
    			co = tostring(tEvent[2])
    			player = tostring(tEvent[3])
    			print(player..": "..co)
    			executeCommand(player,co)
    		end
    		
    		nextTimeEventID = os.startTimer(0.1)
    	end
    end

     

     

    Note: If you have any question, you found a bug or need something extra just comment this post :)

×
×
  • Create New...

Important Information

By using this site you agree to the following Terms of Use, Guidelines and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.