1. This forum is ARCHIVED! Visit the new Cloud Sixteen forums, codename Eden, at https://eden.cloudsixteen.com. These forums will remain archived for a few months before being closed down, so try to encourage plugin developers to begin moving their content across to the new forums.
Dismiss Notice
Hi Guest, you need a Steam account to register and post on these forums. Login with Steam at the top of the forums to get started!
Dismiss Notice
Hi Guest, do you want to buy HL2RP or another Clockwork schema? Visit the Cloud Sixteen Store to get started!

[RELEASE] DermaRequest Library

Discussion in 'Development' started by Spencer, Dec 30, 2012.

  1. Spencer

    Spencer Spencer Sharkey

    We wanted a way to easily query players using the Derma_StringRequest function and receive the data server-side in a couple of commands, as apposed to using arguments.

    Here's a library that you can easily add to your schema. Just make a shared library file and paste it in (ex: libraries\sh_dermarequest.lua)

    Code:
    Schema.dermaRequest = Clockwork.kernel:NewLibrary("DermaRequest");
    
    if (SERVER) then
    
    	Schema.dermaRequest.hooks = {}
    
    	function Schema.dermaRequest:RequestString(player, title, question, default, Callback)
    		local rID = self:GenerateID()
    		Clockwork.datastream:Start(player, "dermaRequest_stringQuery", {id = rID, title = title, question = question, default = default})
    		self.hooks[rID] = {Callback = Callback, player = player}
    	end
    
    	function Schema.dermaRequest:Message(player, message, title, button)
    		Clockwork.datastream:Start(player, "dermaRequest_message", {message = message, title = title or nil, button = button or nil})
    	end
    
    	
    	Clockwork.datastream:Hook("dermaRequestCallback", function(player, data)
    		if (!Schema.dermaRequest:Validate(player, data)) then return end
    		Schema.dermaRequest.hooks[data.id].Callback(data.recv)
    		Schema.dermaRequest.hooks[data.id] = nil
    	end)
    
    	function Schema.dermaRequest:Validate(player, data)
    		if (data.id and data.recv and self.hooks[data.id] and self.hooks[data.id].player == player) then
    			return true
    		end
    		return false
    	end
    
    else
    
    	Clockwork.datastream:Hook("dermaRequest_stringQuery", function(data)
    		Derma_StringRequest(data.title, data.question, data.default, function(recv)
    			Schema.dermaRequest:Send(data.id, recv)
    		end)
    	end)
    
    	Clockwork.datastream:Hook("dermaRequest_message", function(data)
    		local title = data.title or nil
    		local button = data.button or nil
    		Derma_Message(data.message, data.title, data.button)
    	end)
    	
    	function Schema.dermaRequest:Send(id, recv)
    		Clockwork.datastream:Start("dermaRequestCallback", {id = id, recv = recv})
    	end
    
    end
    
    local REQUEST_INDEX = 0
    function Schema.dermaRequest:GenerateID()
    	REQUEST_INDEX = REQUEST_INDEX + 1
    	return os.time() + REQUEST_INDEX
    end
    
    Usage is really simple, here's an example of a command that changes the player's name but asks you for the name rather than using an argument. It was made for our property-menu command library I made that I might release later RELEASED HERE. The library can still have it's uses outside of commands of course.

    Code:
    local COMMAND = Clockwork.command:New("SFName");
    COMMAND.tip = "Set a user's Name.";
    COMMAND.text = "<string Name>";
    COMMAND.flags = CMD_DEFAULT;
    COMMAND.access = "o";
    
    -- Called when the command has been run.
    function COMMAND:OnRun(player, arguments)
            local entity = Clockwork.player:FindByID(arguments[1])
    	if (entity) then
    		Schema.dermaRequest:RequestString(player, "Name", "What should their new name be?", "", function(text)
    			name = text;
    			Clockwork.kernel:ServerLog(player:Name().." set "..entity:Name().."'s name to "..name..".");
    			
    			Clockwork.player:SetName(entity, name);
    		end)
    	else
    		Clockwork.player:Notify(player, entity.." is not a valid character!");
    	end;
    end;
    
    COMMAND:Register()
    
    Enojoy!

    P.S. There's also a Schema.dermaRequest:Message(player, message, [title, button])
     
  2. Neat, you are such a nice person
     
  3. Razor

    Razor Guest

    Very good, I was making something like this when I noticed i coudln't use Derma_ stuff.
    Well, guess I was ninjad.
     
  4. kurozael

    kurozael Cloud Sixteen Director Staff Member Administrator Investor

  5. jamiecross

    jamiecross HTML and VB coder

    Thanks alot spencer.
     

Previous Readers (Total: 0)