Skip to content

Commit

Permalink
Add setClipboardText(s) (#2922)
Browse files Browse the repository at this point in the history
* Add setClipboardText(s)

Add function to set clipboard text

* Moved setClipboardText(s) into debug

Moved setClipboardText(s) from its own extension to debug.

Also moved e2helper description to a more appropriate location

* Add setClipboardText limit convar and fixed error

Added a convar to limit the amount of characters the server is willing to send.

Made use of `#` operator

Limit check now returns nil

* Add setClipboardText cooldown

Added a cooldown convar and fixed limit test

* Correcting linting mistakes

* Removed print

 oops forgot to remove that

* Changed E2 helper description

Change to some less confusing

* Add setClipboardText toggle convar

* Change convar check to use boolean

minor nitpicks, I agree its nicer.

* Change clipboard access toggle from server side to client side

* Changed setClipboardText cooldown

Removed timer as suggested and added table to track when last called.

* Simplify

---------

Co-authored-by: Vurv <[email protected]>
  • Loading branch information
Sandalot and Vurv78 authored Dec 11, 2023
1 parent 33d3fac commit 77e1423
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lua/entities/gmod_wire_expression2/core/cl_debug.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,10 @@ end)

net.Receive("wire_expression2_print", function(len, ply)
chat.AddText(net.ReadString())
end)

CreateClientConVar("wire_expression2_clipboard_allow", 0, true, true, "Allow E2 to set your clipboard text", 0, 1)

net.Receive("wire_expression2_set_clipboard_text", function(len, ply)
SetClipboardText(net.ReadString())
end)
33 changes: 33 additions & 0 deletions lua/entities/gmod_wire_expression2/core/debug.lua
Original file line number Diff line number Diff line change
Expand Up @@ -416,3 +416,36 @@ e2function void entity:printColorDriver(array arr)

printColorArray(self.entity, driver, false, arr)
end

util.AddNetworkString( "wire_expression2_set_clipboard_text" )
local clipboard_character_limit = CreateConVar("wire_expression2_clipboard_character_limit", 512, FCVAR_ARCHIVE, "Maximum character that can be copied into a players clipboard", 0, 65532)
local clipboard_cooldown = CreateConVar("wire_expression2_clipboard_cooldown", 1, FCVAR_ARCHIVE, "Cooldown for setClipboardText in seconds", 0, nil)


-- TODO: Make an E2Lib.RegisterChipTable function that is essentially WireLib.RegisterPlayerTable, but handles chips.
local ClipboardCooldown = {}
registerCallback("destruct",function(self)
ClipboardCooldown[self.entity] = nil
end)

__e2setcost(100)
e2function void setClipboardText(string text)
if self.player:GetInfoNum("wire_expression2_clipboard_allow", 0) == 0 then
return self:throw("setClipboardText is not enabled. You need to change the convar \"wire_expression2_clipboard_allow\" to enable it", nil)
end

if #text > clipboard_character_limit:GetInt() then
return self:throw("setClipboardText exceeding string limit of " .. clipboard_character_limit:GetInt() .. " characters", nil)
end

local cooldown, now = ClipboardCooldown[self.entity], CurTime()
if cooldown and now < cooldown then
return self:throw("You must wait " .. clipboard_cooldown:GetInt() .. " second(s) before calling setClipboardText again.", nil)
end

ClipboardCooldown[self.entity] = now + clipboard_cooldown:GetInt()

net.Start("wire_expression2_set_clipboard_text")
net.WriteString(text)
net.Send(self.player)
end
1 change: 1 addition & 0 deletions lua/wire/client/e2descriptions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,7 @@ E2Helper.Descriptions["printColorDriver(e:...)"] = "Like printColor but prints t
E2Helper.Descriptions["printColorDriver(e:r)"] = "Like printColorDriver but takes an array containing all the parameters"
E2Helper.Descriptions["printTable(t)"] = "Prints a table like the lua function PrintTable does, except to the chat area"
E2Helper.Descriptions["printTable(r)"] = "Prints an array like the lua function PrintTable does, except to the chat area"
E2Helper.Descriptions["setClipboardText(s)"] = "Adds the given string to the chip owners clipboard"

-- Time
E2Helper.Descriptions["tickClk()"] = "Returns 1 if the current execution was caused by \"runOnTick\""
Expand Down

0 comments on commit 77e1423

Please sign in to comment.