-
Notifications
You must be signed in to change notification settings - Fork 0
/
Util.cs
165 lines (138 loc) · 5.53 KB
/
Util.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using Exiled.API.Features;
using Exiled.CustomRoles.API;
using Exiled.CustomRoles.API.Features;
using Exiled.Permissions.Extensions;
using PlayerRoles;
using PlayerRoles.FirstPersonControl;
using System;
using System.Collections.Generic;
using System.Linq;
namespace ModTools
{
public static class Util
{
/// <summary>
/// Given a stirng, capitalize the first character and make the rest lowercase.
/// </summary>
/// <param name="input">A string in any case</param>
/// <returns>The string with only the first character capitalized</returns>
public static string SentenceCase(this string input)
{
if (string.IsNullOrEmpty(input))
{
return "";
}
return char.ToUpper(input[0]) + input.Substring(1).ToLower();
}
public static bool EnableModMode(this Player player, out string response, bool retainPosition = false)
{
if (!player.CheckPermission(PlayerPermissions.ForceclassSelf))
{
response = Plugin.Singleton.Translation.NoForceClassPerm;
return false;
}
foreach (CustomRole role in player.GetCustomRoles())
role.RemoveRole(player);
// A list of descriptions of changes that we'll send to the user
var changes = new List<string>();
// Forceclass tends not to work when overwatch is enabled
player.IsOverwatchEnabled = false;
var spawnFlags = retainPosition ? RoleSpawnFlags.AssignInventory : RoleSpawnFlags.All;
player.Role.Set(RoleTypeId.Tutorial, Exiled.API.Enums.SpawnReason.ForceClass, spawnFlags);
player.BadgeHidden = false;
changes.Add(Plugin.Singleton.Translation.TagShown);
if (player.CheckPermission(PlayerPermissions.Noclip))
{
FpcNoclip.PermitPlayer(player.ReferenceHub);
changes.Add(Plugin.Singleton.Translation.NoclipEnabled);
}
if (player.CheckPermission(PlayerPermissions.FacilityManagement))
{
player.IsBypassModeEnabled = true;
changes.Add(Plugin.Singleton.Translation.BypassEnabled);
}
if (player.CheckPermission(PlayerPermissions.PlayersManagement))
{
player.IsGodModeEnabled = true;
changes.Add(Plugin.Singleton.Translation.GodmodeEnabled);
}
response = String.Join(", ", changes).SentenceCase();
player.Broadcast(new Exiled.API.Features.Broadcast(
Plugin.Singleton.Translation.BroadcastHeader + response
));
return true;
}
public static bool DisableModMode(this Player player, out string response)
{
// A list of descriptions of changes that we'll send to the user
var changes = new List<string>();
// Note: it would be possible to check whether e.g. noclip is already enabled
// (rather than just whether the user has permission); however, I've elected
// not to so in order for staff to not have to guess or remember whether it's
// still enabled. For instance, a moderator might want to use it to double check
// that noclip is indeed disabled.
if (player.CheckPermission(PlayerPermissions.Noclip))
{
FpcNoclip.UnpermitPlayer(player.ReferenceHub);
changes.Add(Plugin.Singleton.Translation.NoclipDisabled);
}
if (player.CheckPermission(PlayerPermissions.FacilityManagement))
{
player.IsBypassModeEnabled = false;
changes.Add(Plugin.Singleton.Translation.BypassDisabled);
}
if (player.CheckPermission(PlayerPermissions.PlayersManagement))
{
player.IsGodModeEnabled = false;
changes.Add(Plugin.Singleton.Translation.GodmodeDisabled);
}
if (changes.IsEmpty())
{
response = Plugin.Singleton.Translation.InsufficientPermissions;
return false;
}
response = String.Join(", ", changes).SentenceCase();
player.Broadcast(new Exiled.API.Features.Broadcast(
Plugin.Singleton.Translation.BroadcastHeader + response
));
return true;
}
/// <summary>
/// Find who the player is currently spectating, or null if this is not possible
///
/// Note: Nullable types would be preferable but aren't available before C# 8.0
/// </summary>
/// <param name="player">The spectator</param>
/// <returns>The target of the spectator</returns>
public static Player? FindSpectatingTargetOrNull(this Player player)
{
try
{
return Player.List.First(p => p != player && p.CurrentSpectatingPlayers.Contains(player));
}
catch
{
return null;
}
}
public static string S(int count)
{
return count switch
{
1 => "",
_ => "s"
};
}
public static void Toggle<T>(this List<T> list, T elem)
{
if (!list.Remove(elem))
{
list.Add(elem);
}
}
public static void Toggle(this ref bool val)
{
val = !val;
}
}
}