Skip to content

Commit 38b87b6

Browse files
feat: add quiet mode
1 parent 8874903 commit 38b87b6

File tree

2 files changed

+147
-58
lines changed

2 files changed

+147
-58
lines changed

README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,23 @@ Allow players to automatically try and unlock locked code locks with their code.
1515

1616
## Chat Commands
1717

18+
### Core Commands
19+
1820
*You* refers to the player running the command.
1921

2022
- `/code 1234` - Sets your code to 1234.
2123
- `/code pick` - Opens the code lock input for you to enter a code.
2224
- `/code random` - Sets your code to a random code.
2325
- `/code remove` - Removes your set code (and guest code).
2426

25-
The same commands are also avalibale in a guest code version:
27+
The core commands are also avalibale in a guest code version. e.g.
2628

2729
- `/code guest 5678` - Sets your guest code to 5678.
2830

31+
### Other Commands
32+
33+
- `/code quiet` - Toggle quiet mode. In this mode less messages will be displayed and your code will be hidden.
34+
2935
## Console Commands
3036

3137
Requires `autocode.admin` to use.

src/AutoCode.cs

+140-57
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class AutoCode : RustPlugin
2020
private Data data;
2121
private Dictionary<BasePlayer, TempCodeLockInfo> tempCodeLocks;
2222

23+
private const string HiddenCode = "****";
24+
2325
#region Hooks
2426

2527
void Init()
@@ -117,14 +119,13 @@ void OnEntitySpawned(CodeLock codeLock)
117119
// Lock the lock.
118120
codeLock.SetFlag(BaseEntity.Flags.Locked, true);
119121

120-
// Don't display code if in streamer mode.
121-
if (!player.net.connection.info.GetBool("global.streamermode"))
122+
if (!settings.quietMode)
122123
{
123124
Message(
124125
player,
125126
string.Format(
126127
lang.GetMessage("CodeAutoLocked", this, player.UserIDString),
127-
settings.code
128+
Utils.ShouldHideCode(player, settings) ? HiddenCode : settings.code
128129
)
129130
);
130131
}
@@ -159,24 +160,32 @@ object CanUseLockedEntity(BasePlayer player, CodeLock codeLock)
159160
protected override void LoadDefaultMessages()
160161
{
161162
lang.RegisterMessages(new Dictionary<string, string>
162-
{
163-
{ "NoPermission", "You don't have permission." },
164-
{ "CodeAutoLocked", "Code lock placed with code {0}." },
165-
{ "CodeUpdated", "Your new code is {0}." },
166-
{ "GuestCodeUpdated", "Your new guest code is {0}." },
167-
{ "CodeRemoved", "Your code has been removed." },
168-
{ "GuestCodeRemoved", "Your guest code has been removed." },
169-
{ "InvalidArgsTooMany", "Too many arguments supplied." },
170-
{ "Info", "Code: {0}\nGuest Code: {1}\n\nUsage: {2}" },
171-
{ "NotSet", "Not set." },
172-
{ "SyntaxError", "Syntax Error: expected command in the form:\n{0}" },
173-
{ "SpamPrevention", "Too many recent code sets. Please wait {0} and try again." },
174-
{ "InvalidArguments", "Invalid arguments supplied." },
175-
{ "ErrorNoPlayerFound", "Error: No player found." },
176-
{ "ErrorMoreThanOnePlayerFound", "Error: More than one player found." },
177-
{ "ResettingAllLockOuts", "Resetting lock outs for all players." },
178-
{ "ResettingLockOut", "Resetting lock outs for {0}." },
179-
}, this);
163+
{
164+
{ "Help", "Usage:\n{0}" },
165+
{ "Description", "Automatically set the code on code locks you place.\n\nCode: {0}\nGuest Code: {1}\nQuiet Mode: {2}" },
166+
{ "NoPermission", "You don't have permission." },
167+
{ "CodeAutoLocked", "Code lock placed with code {0}." },
168+
{ "CodeAutoLockedWithGuest", "Code lock placed with code {0} and guest code {1}." },
169+
{ "CodeUpdated", "Your auto-code has changed to {0}." },
170+
{ "CodeUpdatedHidden", "New auto-code set." },
171+
{ "GuestCodeUpdated", "Your guest auto-code has changed to {0}." },
172+
{ "GuestCodeUpdatedHidden", "New guest auto-code set." },
173+
{ "CodeRemoved", "Your auto-code has been removed." },
174+
{ "GuestCodeRemoved", "Your guest auto-code has been removed." },
175+
{ "InvalidArgsTooMany", "Too many arguments supplied." },
176+
{ "NotSet", "Not set" },
177+
{ "SyntaxError", "Syntax Error: expected command in the form:\n{0}" },
178+
{ "SpamPrevention", "Too many recent auto-code sets. Please wait {0} and try again." },
179+
{ "InvalidArguments", "Invalid arguments supplied." },
180+
{ "ErrorNoPlayerFound", "Error: No player found." },
181+
{ "ErrorMoreThanOnePlayerFound", "Error: More than one player found." },
182+
{ "ResettingAllLockOuts", "Resetting lock outs for all players." },
183+
{ "ResettingLockOut", "Resetting lock outs for {0}." },
184+
{ "QuietModeEnable", "Quiet mode now enabled.\nLess messages will be shown and your auto-code will be hidden." },
185+
{ "QuietModeDisable", "Quiet mode now disabled." },
186+
{ "Enabled", "Enabled" },
187+
{ "Disabled", "Disabled" }
188+
}, this);
180189
}
181190

182191
#endregion
@@ -210,7 +219,9 @@ public string GetCode(BasePlayer player, bool guest = false)
210219
/// <param name="player">The player to set the code for.</param>
211220
/// <param name="code">The code to set for the given player.</param>
212221
/// <param name="guest">If true, the guest code will be set instead of the main code.</param>
213-
public void SetCode(BasePlayer player, string code, bool guest = false)
222+
/// <param name="quiet">If true, no output message will be shown to the player.</param>
223+
/// <param name="hideCode">If true, the new code won't be displayed to the user. Has no effect if quiet is true.</param>
224+
public void SetCode(BasePlayer player, string code, bool guest = false, bool quiet = false, bool hideCode = false)
214225
{
215226
if (!data.Inst.playerSettings.ContainsKey(player.userID))
216227
{
@@ -255,15 +266,18 @@ public void SetCode(BasePlayer player, string code, bool guest = false)
255266
// Locked out?
256267
if (lockedOut)
257268
{
258-
Message(
259-
player,
260-
string.Format(
261-
lang.GetMessage("SpamPrevention", this, player.UserIDString),
262-
TimeSpan.FromSeconds(Math.Ceiling(settings.lockedOutUntil - currentTime)).ToString(@"d\d\ h\h\ mm\m\ ss\s").TrimStart(' ', 'd', 'h', 'm', 's', '0'),
263-
config.Options.SpamPrevention.LockOutTime,
264-
config.Options.SpamPrevention.WindowTime
265-
)
266-
);
269+
if (!quiet)
270+
{
271+
Message(
272+
player,
273+
string.Format(
274+
lang.GetMessage("SpamPrevention", this, player.UserIDString),
275+
TimeSpan.FromSeconds(Math.Ceiling(settings.lockedOutUntil - currentTime)).ToString(@"d\d\ h\h\ mm\m\ ss\s").TrimStart(' ', 'd', 'h', 'm', 's', '0'),
276+
config.Options.SpamPrevention.LockOutTime,
277+
config.Options.SpamPrevention.WindowTime
278+
)
279+
);
280+
}
267281
return;
268282
}
269283

@@ -286,15 +300,18 @@ public void SetCode(BasePlayer player, string code, bool guest = false)
286300

287301
settings.lastSet = currentTime;
288302

289-
// Don't display code if in streamer mode.
290-
if (!player.net.connection.info.GetBool("global.streamermode"))
303+
if (!quiet)
291304
{
305+
hideCode = hideCode || Utils.ShouldHideCode(player, settings);
306+
292307
Message(
293308
player,
294-
string.Format(
295-
lang.GetMessage(guest ? "GuestCodeUpdated" : "CodeUpdated", this, player.UserIDString),
296-
code
297-
)
309+
hideCode
310+
? lang.GetMessage(guest ? "GuestCodeUpdatedHidden" : "CodeUpdatedHidden", this, player.UserIDString)
311+
: string.Format(
312+
lang.GetMessage(guest ? "GuestCodeUpdated" : "CodeUpdated", this, player.UserIDString),
313+
code
314+
)
298315
);
299316
}
300317
}
@@ -314,7 +331,8 @@ public void ToggleEnabled(BasePlayer player)
314331
/// </summary>
315332
/// <param name="player">The player to remove the code of.</param>
316333
/// <param name="guest">If true, the guest code will be removed instead of the main code.</param>
317-
public void RemoveCode(BasePlayer player, bool guest = false)
334+
/// <param name="quiet">If true, no output message will be shown to the player.</param>
335+
public void RemoveCode(BasePlayer player, bool guest = false, bool quiet = false)
318336
{
319337
if (!data.Inst.playerSettings.ContainsKey(player.userID))
320338
{
@@ -332,10 +350,13 @@ public void RemoveCode(BasePlayer player, bool guest = false)
332350
// Remove the guest code both then removing the main code and when just removing the guest code.
333351
settings.guestCode = null;
334352

335-
Message(
336-
player,
337-
lang.GetMessage(guest ? "GuestCodeRemoved" : "CodeRemoved", this, player.UserIDString)
338-
);
353+
if (!quiet)
354+
{
355+
Message(
356+
player,
357+
lang.GetMessage(guest ? "GuestCodeRemoved" : "CodeRemoved", this, player.UserIDString)
358+
);
359+
}
339360
}
340361

341362
[ObsoleteAttribute("This method is deprecated. Call IsValidCode instead.", false)]
@@ -426,6 +447,27 @@ public void OpenCodeLockUI(BasePlayer player, bool guest = false)
426447
});
427448
}
428449

450+
/// <summary>
451+
/// Remove all lock outs caused by spam protection.
452+
/// </summary>
453+
/// <param name="player">The player to toggle quiet mode for.</param>
454+
/// <param name="quiet">If true, no output message will be shown to the player.</param>
455+
public void ToggleQuietMode(BasePlayer player, bool quiet = false)
456+
{
457+
// Load the player's settings.
458+
Data.Structure.PlayerSettings settings = data.Inst.playerSettings[player.userID];
459+
460+
settings.quietMode = !settings.quietMode;
461+
462+
if (!quiet)
463+
{
464+
Message(
465+
player,
466+
lang.GetMessage(settings.quietMode ? "QuietModeEnable" : "QuietModeDisable", this, player.UserIDString)
467+
);
468+
}
469+
}
470+
429471
/// <summary>
430472
/// Reset (remove) all lock outs caused by spam protection.
431473
/// </summary>
@@ -719,6 +761,7 @@ public class PlayerSettings
719761
{
720762
public string code = null;
721763
public string guestCode = null;
764+
public bool quietMode = false;
722765
public double lastSet = 0;
723766
public int timesSetInSpamWindow = 0;
724767
public double lockedOutUntil = 0;
@@ -771,6 +814,7 @@ private class Commands
771814
public string PickCode = "pick";
772815
public string RandomCode = "random";
773816
public string RemoveCode = "remove";
817+
public string QuietMode = "quiet";
774818

775819
public Commands(AutoCode plugin)
776820
{
@@ -924,6 +968,27 @@ private void HandleUse(BasePlayer player, string label, string[] args)
924968
return;
925969
}
926970

971+
// Toggle quiet mode.
972+
if (operation == QuietMode)
973+
{
974+
if (guest)
975+
{
976+
SyntaxError(player, label, args);
977+
return;
978+
}
979+
if (args.Length > 1)
980+
{
981+
plugin.Message(
982+
player,
983+
string.Format(plugin.lang.GetMessage("InvalidArgsTooMany", plugin, player.UserIDString), label)
984+
);
985+
return;
986+
}
987+
988+
plugin.ToggleQuietMode(player);
989+
return;
990+
}
991+
927992
// Remove?
928993
if (operation == RemoveCode)
929994
{
@@ -952,7 +1017,14 @@ private void HandleUse(BasePlayer player, string label, string[] args)
9521017
return;
9531018
}
9541019

955-
plugin.SetCode(player, plugin.GenerateRandomCode(), guest);
1020+
var hideCode = false;
1021+
if (plugin.data.Inst.playerSettings.ContainsKey(player.userID))
1022+
{
1023+
Data.Structure.PlayerSettings settings = plugin.data.Inst.playerSettings[player.userID];
1024+
hideCode = settings.quietMode;
1025+
}
1026+
1027+
plugin.SetCode(player, plugin.GenerateRandomCode(), guest, false, hideCode);
9561028
return;
9571029
}
9581030

@@ -982,18 +1054,19 @@ private void ShowInfo(BasePlayer player, string label, string[] args)
9821054
{
9831055
string code = null;
9841056
string guestCode = null;
1057+
bool quietMode = false;
9851058

9861059
if (plugin.data.Inst.playerSettings.ContainsKey(player.userID))
9871060
{
9881061
Data.Structure.PlayerSettings settings = plugin.data.Inst.playerSettings[player.userID];
9891062
code = settings.code;
9901063
guestCode = settings.guestCode;
1064+
quietMode = settings.quietMode;
9911065

992-
// Hide codes for those in streamer mode.
993-
if (player.net.connection.info.GetBool("global.streamermode"))
1066+
if (Utils.ShouldHideCode(player, settings))
9941067
{
995-
code = code == null ? null : "****";
996-
guestCode = guestCode == null ? null : "****";
1068+
code = code == null ? null : HiddenCode;
1069+
guestCode = guestCode == null ? null : HiddenCode;
9971070
}
9981071
}
9991072

@@ -1028,16 +1101,18 @@ private void SyntaxError(BasePlayer player, string label, string[] args)
10281101
/// <returns></returns>
10291102
private string UsageInfo(string label)
10301103
{
1031-
return string.Format("/{0} {1}", label, HelpGetAllUseCommandArguments());
1032-
}
1033-
1034-
/// <summary>
1035-
/// Get all the arguments that can be supplied to the "use" command.
1036-
/// </summary>
1037-
/// <returns></returns>
1038-
private string HelpGetAllUseCommandArguments()
1039-
{
1040-
return string.Format("[{0}] {1}", Guest, string.Join("|", new string[] { "1234", RandomCode, PickCode, RemoveCode }));
1104+
return string.Format(
1105+
"/{0} <{1}>",
1106+
label,
1107+
string.Join("|", new string[] {
1108+
string.Format(
1109+
"[{0}] <{1}>",
1110+
Guest,
1111+
string.Join("|", new string[] { "1234", PickCode, RandomCode, RemoveCode })
1112+
),
1113+
QuietMode
1114+
})
1115+
);
10411116
}
10421117
}
10431118

@@ -1051,6 +1126,14 @@ private static class Utils
10511126
/// </summary>
10521127
/// <returns>The number of seconds that have passed since 1970-01-01.</returns>
10531128
public static double CurrentTime() => DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds;
1129+
1130+
/// <summary>
1131+
/// Should the code for the given player be hidden in messages?
1132+
/// </summary>
1133+
public static bool ShouldHideCode(BasePlayer player, Data.Structure.PlayerSettings settings)
1134+
{
1135+
return settings.quietMode || player.net.connection.info.GetBool("global.streamermode");
1136+
}
10541137
}
10551138

10561139
/// <summary>

0 commit comments

Comments
 (0)