From 3f074df697fef80a1d104423bcd2556d91b73597 Mon Sep 17 00:00:00 2001 From: Sandalot <20023123+Sandalot@users.noreply.github.com> Date: Mon, 4 Dec 2023 05:30:50 +1100 Subject: [PATCH 01/12] Add setClipboardText(s) Add function to set clipboard text --- .../core/custom/cl_clipboard.lua | 5 +++++ .../core/custom/clipboard.lua | 14 ++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 lua/entities/gmod_wire_expression2/core/custom/cl_clipboard.lua create mode 100644 lua/entities/gmod_wire_expression2/core/custom/clipboard.lua diff --git a/lua/entities/gmod_wire_expression2/core/custom/cl_clipboard.lua b/lua/entities/gmod_wire_expression2/core/custom/cl_clipboard.lua new file mode 100644 index 0000000000..f99d685d0f --- /dev/null +++ b/lua/entities/gmod_wire_expression2/core/custom/cl_clipboard.lua @@ -0,0 +1,5 @@ +E2Helper.Descriptions["setClipboardText(s)"] = "Adds the given string to the computers clipboard. Can not exceed 65532 characters" + +net.Receive("wire_expression2_set_clipboard_text", function(len, ply) + SetClipboardText(net.ReadString()) +end) \ No newline at end of file diff --git a/lua/entities/gmod_wire_expression2/core/custom/clipboard.lua b/lua/entities/gmod_wire_expression2/core/custom/clipboard.lua new file mode 100644 index 0000000000..2d4bf25839 --- /dev/null +++ b/lua/entities/gmod_wire_expression2/core/custom/clipboard.lua @@ -0,0 +1,14 @@ +util.AddNetworkString( "wire_expression2_set_clipboard_text" ) + +__e2setcost(100) +e2function void setClipboardText(string text) + -- The maximum allowed length of a single written string is 65532 characters while using net.WriteString + -- See https://wiki.facepunch.com/gmod/net.WriteString + + -- This is probably more than sufficient for most people + if string.len(text) > 65532 then return self:throw("Exceeded maximum string length for clipboard text", 0) end + + net.Start("wire_expression2_set_clipboard_text") + net.WriteString(text) + net.Send(self.player) +end \ No newline at end of file From 9b2c24df759463f43dc267353ec9baf6e505574a Mon Sep 17 00:00:00 2001 From: Sandalot <20023123+Sandalot@users.noreply.github.com> Date: Mon, 4 Dec 2023 18:22:37 +1100 Subject: [PATCH 02/12] Moved setClipboardText(s) into debug Moved setClipboardText(s) from its own extension to debug. Also moved e2helper description to a more appropriate location --- .../gmod_wire_expression2/core/cl_debug.lua | 4 ++++ .../core/custom/cl_clipboard.lua | 5 ----- .../core/custom/clipboard.lua | 14 -------------- lua/entities/gmod_wire_expression2/core/debug.lua | 15 +++++++++++++++ lua/wire/client/e2descriptions.lua | 1 + 5 files changed, 20 insertions(+), 19 deletions(-) delete mode 100644 lua/entities/gmod_wire_expression2/core/custom/cl_clipboard.lua delete mode 100644 lua/entities/gmod_wire_expression2/core/custom/clipboard.lua diff --git a/lua/entities/gmod_wire_expression2/core/cl_debug.lua b/lua/entities/gmod_wire_expression2/core/cl_debug.lua index e1ef2ec636..86b6d66d60 100644 --- a/lua/entities/gmod_wire_expression2/core/cl_debug.lua +++ b/lua/entities/gmod_wire_expression2/core/cl_debug.lua @@ -27,4 +27,8 @@ end) net.Receive("wire_expression2_print", function(len, ply) chat.AddText(net.ReadString()) +end) + +net.Receive("wire_expression2_set_clipboard_text", function(len, ply) + SetClipboardText(net.ReadString()) end) \ No newline at end of file diff --git a/lua/entities/gmod_wire_expression2/core/custom/cl_clipboard.lua b/lua/entities/gmod_wire_expression2/core/custom/cl_clipboard.lua deleted file mode 100644 index f99d685d0f..0000000000 --- a/lua/entities/gmod_wire_expression2/core/custom/cl_clipboard.lua +++ /dev/null @@ -1,5 +0,0 @@ -E2Helper.Descriptions["setClipboardText(s)"] = "Adds the given string to the computers clipboard. Can not exceed 65532 characters" - -net.Receive("wire_expression2_set_clipboard_text", function(len, ply) - SetClipboardText(net.ReadString()) -end) \ No newline at end of file diff --git a/lua/entities/gmod_wire_expression2/core/custom/clipboard.lua b/lua/entities/gmod_wire_expression2/core/custom/clipboard.lua deleted file mode 100644 index 2d4bf25839..0000000000 --- a/lua/entities/gmod_wire_expression2/core/custom/clipboard.lua +++ /dev/null @@ -1,14 +0,0 @@ -util.AddNetworkString( "wire_expression2_set_clipboard_text" ) - -__e2setcost(100) -e2function void setClipboardText(string text) - -- The maximum allowed length of a single written string is 65532 characters while using net.WriteString - -- See https://wiki.facepunch.com/gmod/net.WriteString - - -- This is probably more than sufficient for most people - if string.len(text) > 65532 then return self:throw("Exceeded maximum string length for clipboard text", 0) end - - net.Start("wire_expression2_set_clipboard_text") - net.WriteString(text) - net.Send(self.player) -end \ No newline at end of file diff --git a/lua/entities/gmod_wire_expression2/core/debug.lua b/lua/entities/gmod_wire_expression2/core/debug.lua index fbb4185647..4c08a7109b 100644 --- a/lua/entities/gmod_wire_expression2/core/debug.lua +++ b/lua/entities/gmod_wire_expression2/core/debug.lua @@ -416,3 +416,18 @@ e2function void entity:printColorDriver(array arr) printColorArray(self.entity, driver, false, arr) end + +util.AddNetworkString( "wire_expression2_set_clipboard_text" ) + +__e2setcost(100) +e2function void setClipboardText(string text) + -- The maximum allowed length of a single written string is 65532 characters while using net.WriteString + -- See https://wiki.facepunch.com/gmod/net.WriteString + + -- This is probably more than sufficient for most people + if string.len(text) > 65532 then return self:throw("Exceeded maximum string length for clipboard text", 0) end + + net.Start("wire_expression2_set_clipboard_text") + net.WriteString(text) + net.Send(self.player) +end \ No newline at end of file diff --git a/lua/wire/client/e2descriptions.lua b/lua/wire/client/e2descriptions.lua index 090810a117..d7f583cbd6 100644 --- a/lua/wire/client/e2descriptions.lua +++ b/lua/wire/client/e2descriptions.lua @@ -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 computers clipboard" -- Time E2Helper.Descriptions["tickClk()"] = "Returns 1 if the current execution was caused by \"runOnTick\"" From 110ff3165f8516fdab14d05e45a3832e75fcb630 Mon Sep 17 00:00:00 2001 From: Sandalot <20023123+Sandalot@users.noreply.github.com> Date: Mon, 4 Dec 2023 18:26:24 +1100 Subject: [PATCH 03/12] 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 --- lua/entities/gmod_wire_expression2/core/debug.lua | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lua/entities/gmod_wire_expression2/core/debug.lua b/lua/entities/gmod_wire_expression2/core/debug.lua index 4c08a7109b..a8afc0da90 100644 --- a/lua/entities/gmod_wire_expression2/core/debug.lua +++ b/lua/entities/gmod_wire_expression2/core/debug.lua @@ -418,14 +418,11 @@ e2function void entity:printColorDriver(array 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) __e2setcost(100) e2function void setClipboardText(string text) - -- The maximum allowed length of a single written string is 65532 characters while using net.WriteString - -- See https://wiki.facepunch.com/gmod/net.WriteString - - -- This is probably more than sufficient for most people - if string.len(text) > 65532 then return self:throw("Exceeded maximum string length for clipboard text", 0) end + if #text > clipboard_character_limit:GetInt() then return nil end net.Start("wire_expression2_set_clipboard_text") net.WriteString(text) From d11966652b3d9760f001a7acbfc9c790134577ac Mon Sep 17 00:00:00 2001 From: Sandalot <20023123+Sandalot@users.noreply.github.com> Date: Mon, 4 Dec 2023 18:48:57 +1100 Subject: [PATCH 04/12] Add setClipboardText cooldown Added a cooldown convar and fixed limit test --- .../gmod_wire_expression2/core/debug.lua | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/lua/entities/gmod_wire_expression2/core/debug.lua b/lua/entities/gmod_wire_expression2/core/debug.lua index a8afc0da90..c5c2a304a1 100644 --- a/lua/entities/gmod_wire_expression2/core/debug.lua +++ b/lua/entities/gmod_wire_expression2/core/debug.lua @@ -419,12 +419,23 @@ 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) __e2setcost(100) e2function void setClipboardText(string text) - if #text > clipboard_character_limit:GetInt() then return nil end + print(self.entity:EntIndex()) + local timerid = "wire_expression2_clipboard_cooldown_" .. self.entity:EntIndex() + if not timer.Exists(timerid) then + if #text > clipboard_character_limit:GetInt() then + return self:throw("setClipboardText exceeding string limit of " .. clipboard_character_limit:GetInt() .. " characters", nil) + end - net.Start("wire_expression2_set_clipboard_text") - net.WriteString(text) - net.Send(self.player) + timer.Create( timerid, clipboard_cooldown:GetInt(), 1, function() timer.Remove(timerid) end) + + net.Start("wire_expression2_set_clipboard_text") + net.WriteString(text) + net.Send(self.player) + else + return self:throw("setClipboardText cooldown!", nil) + end end \ No newline at end of file From 23a4177fd0c27a50d76c00633bbdf4624c953e45 Mon Sep 17 00:00:00 2001 From: Sandalot <20023123+Sandalot@users.noreply.github.com> Date: Mon, 4 Dec 2023 18:56:29 +1100 Subject: [PATCH 05/12] Correcting linting mistakes --- lua/entities/gmod_wire_expression2/core/debug.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lua/entities/gmod_wire_expression2/core/debug.lua b/lua/entities/gmod_wire_expression2/core/debug.lua index c5c2a304a1..ab4d0e5e2d 100644 --- a/lua/entities/gmod_wire_expression2/core/debug.lua +++ b/lua/entities/gmod_wire_expression2/core/debug.lua @@ -426,12 +426,12 @@ e2function void setClipboardText(string text) print(self.entity:EntIndex()) local timerid = "wire_expression2_clipboard_cooldown_" .. self.entity:EntIndex() if not timer.Exists(timerid) then - if #text > clipboard_character_limit:GetInt() then - return self:throw("setClipboardText exceeding string limit of " .. clipboard_character_limit:GetInt() .. " characters", nil) + if #text > clipboard_character_limit:GetInt() then + return self:throw("setClipboardText exceeding string limit of " .. clipboard_character_limit:GetInt() .. " characters", nil) end timer.Create( timerid, clipboard_cooldown:GetInt(), 1, function() timer.Remove(timerid) end) - + net.Start("wire_expression2_set_clipboard_text") net.WriteString(text) net.Send(self.player) From 94cb61f9af73d6bae58053c956ff3332357ce383 Mon Sep 17 00:00:00 2001 From: Sandalot <20023123+Sandalot@users.noreply.github.com> Date: Tue, 5 Dec 2023 12:10:49 +1100 Subject: [PATCH 06/12] Removed print oops forgot to remove that --- lua/entities/gmod_wire_expression2/core/debug.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/lua/entities/gmod_wire_expression2/core/debug.lua b/lua/entities/gmod_wire_expression2/core/debug.lua index ab4d0e5e2d..c8e01ef977 100644 --- a/lua/entities/gmod_wire_expression2/core/debug.lua +++ b/lua/entities/gmod_wire_expression2/core/debug.lua @@ -423,7 +423,6 @@ local clipboard_cooldown = CreateConVar("wire_expression2_clipboard_cooldown", 1 __e2setcost(100) e2function void setClipboardText(string text) - print(self.entity:EntIndex()) local timerid = "wire_expression2_clipboard_cooldown_" .. self.entity:EntIndex() if not timer.Exists(timerid) then if #text > clipboard_character_limit:GetInt() then From 5b4e6c442b1e4ddc47feabd469752839772e9c48 Mon Sep 17 00:00:00 2001 From: Sandalot <20023123+Sandalot@users.noreply.github.com> Date: Tue, 5 Dec 2023 12:19:16 +1100 Subject: [PATCH 07/12] Changed E2 helper description Change to some less confusing --- lua/wire/client/e2descriptions.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/wire/client/e2descriptions.lua b/lua/wire/client/e2descriptions.lua index d7f583cbd6..46b59838b4 100644 --- a/lua/wire/client/e2descriptions.lua +++ b/lua/wire/client/e2descriptions.lua @@ -929,7 +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 computers clipboard" +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\"" From 1075ca2cee1496cb6f97199f3564ad7bd2c04c5e Mon Sep 17 00:00:00 2001 From: Sandalot <20023123+Sandalot@users.noreply.github.com> Date: Tue, 5 Dec 2023 13:52:34 +1100 Subject: [PATCH 08/12] Add setClipboardText toggle convar --- lua/entities/gmod_wire_expression2/core/debug.lua | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lua/entities/gmod_wire_expression2/core/debug.lua b/lua/entities/gmod_wire_expression2/core/debug.lua index c8e01ef977..9dc6ee052d 100644 --- a/lua/entities/gmod_wire_expression2/core/debug.lua +++ b/lua/entities/gmod_wire_expression2/core/debug.lua @@ -420,9 +420,12 @@ 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) +local clipboard_allow_toggle = CreateConVar("wire_expression2_clipboard_allow", 0, FCVAR_ARCHIVE, "Cooldown for setClipboardText in seconds", 0, 1) __e2setcost(100) e2function void setClipboardText(string text) + if clipboard_allow_toggle:GetInt() == 0 then return self:throw("setClipboardText is disabled by this server", nil) end + local timerid = "wire_expression2_clipboard_cooldown_" .. self.entity:EntIndex() if not timer.Exists(timerid) then if #text > clipboard_character_limit:GetInt() then From 2a732bb790964595f0272d0d0129413a37570a54 Mon Sep 17 00:00:00 2001 From: Sandalot <20023123+Sandalot@users.noreply.github.com> Date: Tue, 5 Dec 2023 14:34:46 +1100 Subject: [PATCH 09/12] Change convar check to use boolean minor nitpicks, I agree its nicer. --- lua/entities/gmod_wire_expression2/core/debug.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/entities/gmod_wire_expression2/core/debug.lua b/lua/entities/gmod_wire_expression2/core/debug.lua index 9dc6ee052d..0f2e34cb04 100644 --- a/lua/entities/gmod_wire_expression2/core/debug.lua +++ b/lua/entities/gmod_wire_expression2/core/debug.lua @@ -424,7 +424,7 @@ local clipboard_allow_toggle = CreateConVar("wire_expression2_clipboard_allow", __e2setcost(100) e2function void setClipboardText(string text) - if clipboard_allow_toggle:GetInt() == 0 then return self:throw("setClipboardText is disabled by this server", nil) end + if clipboard_allow_toggle:GetBool() then return self:throw("setClipboardText is disabled by this server", nil) end local timerid = "wire_expression2_clipboard_cooldown_" .. self.entity:EntIndex() if not timer.Exists(timerid) then From 87498b5531e80c0cd52c2e235049fadf6af9039e Mon Sep 17 00:00:00 2001 From: Sandalot <20023123+Sandalot@users.noreply.github.com> Date: Tue, 5 Dec 2023 18:04:05 +1100 Subject: [PATCH 10/12] Change clipboard access toggle from server side to client side --- lua/entities/gmod_wire_expression2/core/cl_debug.lua | 2 ++ lua/entities/gmod_wire_expression2/core/debug.lua | 7 +++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lua/entities/gmod_wire_expression2/core/cl_debug.lua b/lua/entities/gmod_wire_expression2/core/cl_debug.lua index 86b6d66d60..c1bf5d5f56 100644 --- a/lua/entities/gmod_wire_expression2/core/cl_debug.lua +++ b/lua/entities/gmod_wire_expression2/core/cl_debug.lua @@ -29,6 +29,8 @@ 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) \ No newline at end of file diff --git a/lua/entities/gmod_wire_expression2/core/debug.lua b/lua/entities/gmod_wire_expression2/core/debug.lua index 0f2e34cb04..887cb52ee3 100644 --- a/lua/entities/gmod_wire_expression2/core/debug.lua +++ b/lua/entities/gmod_wire_expression2/core/debug.lua @@ -420,11 +420,14 @@ 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) -local clipboard_allow_toggle = CreateConVar("wire_expression2_clipboard_allow", 0, FCVAR_ARCHIVE, "Cooldown for setClipboardText in seconds", 0, 1) __e2setcost(100) e2function void setClipboardText(string text) - if clipboard_allow_toggle:GetBool() then return self:throw("setClipboardText is disabled by this server", nil) end + + local clipboard_allow = self.player:GetInfoNum("wire_expression2_clipboard_allow", 0) + if clipboard_allow == 0 then + return self:throw("setClipboardText is not enabled. You need to change the convar \"wire_expression2_clipboard_allow\" to enable it", nil) + end local timerid = "wire_expression2_clipboard_cooldown_" .. self.entity:EntIndex() if not timer.Exists(timerid) then From f4f6adcf5f9163020962576e9b51c4cd6cc1e8ca Mon Sep 17 00:00:00 2001 From: Sandalot <20023123+Sandalot@users.noreply.github.com> Date: Sun, 10 Dec 2023 02:13:08 +1100 Subject: [PATCH 11/12] Changed setClipboardText cooldown Removed timer as suggested and added table to track when last called. --- .../gmod_wire_expression2/core/debug.lua | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/lua/entities/gmod_wire_expression2/core/debug.lua b/lua/entities/gmod_wire_expression2/core/debug.lua index 887cb52ee3..a1a22d0eeb 100644 --- a/lua/entities/gmod_wire_expression2/core/debug.lua +++ b/lua/entities/gmod_wire_expression2/core/debug.lua @@ -420,6 +420,11 @@ 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) +Expression2ClipboardLastCalled = {} + +registerCallback("destruct",function(self) + Expression2ClipboardLastCalled[self.entity:EntIndex()] = nil +end) __e2setcost(100) e2function void setClipboardText(string text) @@ -429,18 +434,18 @@ e2function void setClipboardText(string text) return self:throw("setClipboardText is not enabled. You need to change the convar \"wire_expression2_clipboard_allow\" to enable it", nil) end - local timerid = "wire_expression2_clipboard_cooldown_" .. self.entity:EntIndex() - if not timer.Exists(timerid) then - if #text > clipboard_character_limit:GetInt() then - return self:throw("setClipboardText exceeding string limit of " .. clipboard_character_limit:GetInt() .. " characters", nil) - end + if #text > clipboard_character_limit:GetInt() then + return self:throw("setClipboardText exceeding string limit of " .. clipboard_character_limit:GetInt() .. " characters", nil) + end - timer.Create( timerid, clipboard_cooldown:GetInt(), 1, function() timer.Remove(timerid) end) + local lastCooldown = type(Expression2ClipboardLastCalled[self.entity:EntIndex()]) == "number" and math.floor(Expression2ClipboardLastCalled[self.entity:EntIndex()]) or 0 + if math.floor(CurTime()) < lastCooldown + clipboard_cooldown:GetInt() then + return self:throw("setClipboardText exceeded " .. clipboard_cooldown:GetInt() .. " second cooldown", nil) + else + Expression2ClipboardLastCalled[self.entity:EntIndex()] = math.floor(CurTime()) net.Start("wire_expression2_set_clipboard_text") net.WriteString(text) net.Send(self.player) - else - return self:throw("setClipboardText cooldown!", nil) end end \ No newline at end of file From 3afc5a16687852edc86376e475b7df3657f3c757 Mon Sep 17 00:00:00 2001 From: Vurv <56230599+Vurv78@users.noreply.github.com> Date: Sat, 9 Dec 2023 12:46:18 -0800 Subject: [PATCH 12/12] Simplify --- .../gmod_wire_expression2/core/debug.lua | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/lua/entities/gmod_wire_expression2/core/debug.lua b/lua/entities/gmod_wire_expression2/core/debug.lua index a1a22d0eeb..ab9efd6697 100644 --- a/lua/entities/gmod_wire_expression2/core/debug.lua +++ b/lua/entities/gmod_wire_expression2/core/debug.lua @@ -420,17 +420,17 @@ 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) -Expression2ClipboardLastCalled = {} + +-- TODO: Make an E2Lib.RegisterChipTable function that is essentially WireLib.RegisterPlayerTable, but handles chips. +local ClipboardCooldown = {} registerCallback("destruct",function(self) - Expression2ClipboardLastCalled[self.entity:EntIndex()] = nil + ClipboardCooldown[self.entity] = nil end) __e2setcost(100) e2function void setClipboardText(string text) - - local clipboard_allow = self.player:GetInfoNum("wire_expression2_clipboard_allow", 0) - if clipboard_allow == 0 then + 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 @@ -438,14 +438,14 @@ e2function void setClipboardText(string text) return self:throw("setClipboardText exceeding string limit of " .. clipboard_character_limit:GetInt() .. " characters", nil) end - local lastCooldown = type(Expression2ClipboardLastCalled[self.entity:EntIndex()]) == "number" and math.floor(Expression2ClipboardLastCalled[self.entity:EntIndex()]) or 0 - - if math.floor(CurTime()) < lastCooldown + clipboard_cooldown:GetInt() then - return self:throw("setClipboardText exceeded " .. clipboard_cooldown:GetInt() .. " second cooldown", nil) - else - Expression2ClipboardLastCalled[self.entity:EntIndex()] = math.floor(CurTime()) - net.Start("wire_expression2_set_clipboard_text") - net.WriteString(text) - net.Send(self.player) + 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 \ No newline at end of file