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])