Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Whitelist && update ReservedSlot extensions #2160

Merged
merged 11 commits into from
Oct 24, 2023
69 changes: 61 additions & 8 deletions Exiled.API/Features/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace Exiled.API.Features
using Exiled.API.Structs;
using Extensions;
using Footprinting;
using GameCore;
using global::Scp914;
using Hints;
using Interactables.Interobjects;
Expand All @@ -51,6 +52,7 @@ namespace Exiled.API.Features
using PlayerRoles.Spectating;
using PlayerRoles.Voice;
using PlayerStatsSystem;
using PluginAPI.Core;
using RelativePositioning;
using RemoteAdmin;
using RoundRestarting;
Expand Down Expand Up @@ -414,10 +416,17 @@ public float InfoViewRange
/// <summary>
/// Gets a value indicating whether or not the player has a reserved slot.
/// </summary>
/// <seealso cref="GiveReservedSlot"/>
/// <seealso cref="AddReservedSlot(string)"/>
/// <seealso cref="GiveReservedSlot(bool)"/>
/// <seealso cref="AddReservedSlot(string, bool)"/>
public bool HasReservedSlot => ReservedSlot.HasReservedSlot(UserId, out _);

/// <summary>
/// Gets a value indicating whether or not the player is in whitelist.
/// </summary>
/// <seealso cref="GrantWhitelist(bool)"/>
/// <seealso cref="AddToWhitelist(string, bool)"/>
public bool IsWhitelisted => WhiteList.IsWhitelisted(UserId);

/// <summary>
/// Gets a value indicating whether or not the player has Remote Admin access.
/// </summary>
Expand Down Expand Up @@ -1402,24 +1411,68 @@ public static Player Get(string args)
/// <summary>
/// Adds a player's UserId to the list of reserved slots.
/// </summary>
/// <remarks>This method does not permanently give a user a reserved slot. The slot will be removed if the reserved slots are reloaded.</remarks>
/// <param name="userId">The UserId of the player to add.</param>
/// <param name="isPermanent"> Whether or not to add a <see langword="userId"/> permanently. It will write a <see langword="userId"/> to UserIDReservedSlots.txt file.</param>
/// <returns><see langword="true"/> if the slot was successfully added, or <see langword="false"/> if the provided UserId already has a reserved slot.</returns>
/// <seealso cref="GiveReservedSlot()"/>
public static bool AddReservedSlot(string userId) => ReservedSlot.Users.Add(userId);
/// <seealso cref="GiveReservedSlot(bool)"/>
public static bool AddReservedSlot(string userId, bool isPermanent = false)
{
if (isPermanent && !ReservedSlots.HasReservedSlot(userId))
{
string filePath = ConfigSharing.Paths[3] + "UserIDReservedSlots.txt";
List<string> list = FileManager.ReadAllLines(filePath).ToList();
list.Add(userId ?? string.Empty);
FileManager.WriteToFile(list, filePath);
}

return ReservedSlot.Users.Add(userId);
}

/// <summary>
/// Adds a player's UserId to the whitelist.
/// </summary>
/// <param name="userId">The UserId of the player to add.</param>
/// <param name="isPermanent"> Whether or not to add a <see langword="userId"/> permanently. It will write a <see langword="userId"/> to UserIDWhitelist.txt file.</param>
/// <returns><see langword="true"/> if the record was successfully added, or <see langword="false"/> if the provided UserId already is in whitelist.</returns>
/// <seealso cref="GrantWhitelist(bool)"/>
public static bool AddToWhitelist(string userId, bool isPermanent = false)
{
if (isPermanent && !WhiteList.IsOnWhitelist(userId))
{
string filePath = ConfigSharing.Paths[2] + "UserIDWhitelist.txt";
List<string> list = FileManager.ReadAllLines(filePath).ToList();
list.Add(userId ?? string.Empty);
FileManager.WriteToFile(list, filePath);
}

return WhiteList.Users.Add(userId);
}

/// <summary>
/// Reloads the reserved slot list, clearing all reserved slot changes made with add/remove methods and reverting to the reserved slots files.
/// </summary>
public static void ReloadReservedSlots() => ReservedSlot.Reload();

/// <summary>
/// Reloads the whitelist, clearing all whitelist changes made with add/remove methods and reverting to the whitelist files.
/// </summary>
public static void ReloadWhitelist() => WhiteList.Reload();

/// <summary>
/// Adds the player's UserId to the list of reserved slots.
/// </summary>
/// <remarks>This method does not permanently give a user a reserved slot. The slot will be removed if the reserved slots are reloaded.</remarks>
/// <param name="isPermanent"> Whether or not to add a <see langword="userId"/> permanently. It will write a <see langword="userId"/> to UserIDReservedSlots.txt file.</param>
/// <returns><see langword="true"/> if the slot was successfully added, or <see langword="false"/> if the player already has a reserved slot.</returns>
/// <seealso cref="AddReservedSlot(string)"/>
public bool GiveReservedSlot() => AddReservedSlot(UserId);
/// <seealso cref="AddReservedSlot(string, bool)"/>
public bool GiveReservedSlot(bool isPermanent = false) => AddReservedSlot(UserId, isPermanent);

/// <summary>
/// Adds the player's UserId to the whitelist.
/// </summary>
/// <param name="isPermanent"> Whether or not to add a <see langword="userId"/> permanently. It will write a <see langword="userId"/> to UserIDWhitelist.txt file.</param>
/// <returns><see langword="true"/> if the record was successfully added, or <see langword="false"/> if the provided UserId already is in whitelist.</returns>
/// <seealso cref="AddToWhitelist(string, bool)"/>
public bool GrantWhitelist(bool isPermanent = false) => AddToWhitelist(UserId, isPermanent);

/// <summary>
/// Tries to add <see cref="RoleTypeId"/> to FriendlyFire rules.
Expand Down