Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 52 additions & 1 deletion EXILED/Exiled.API/Features/Core/UserSettings/SettingBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,13 +259,43 @@ public static IEnumerable<SettingBase> Register(IEnumerable<SettingBase> setting
return result;
}

/// <summary>
/// Registers all settings from the specified collection to player.
/// </summary>
/// <param name="settings">A collection of settings to register.</param>
/// <param name="player">A player that will receive settings.</param>
/// <returns>A <see cref="IEnumerable{T}"/> of <see cref="SettingBase"/> instances that were successfully registered.</returns>
/// <remarks>This method is used to sync new settings with players.</remarks>
public static IEnumerable<SettingBase> Register(IEnumerable<SettingBase> settings, Player player)
{
IEnumerable<IGrouping<HeaderSetting, SettingBase>> grouped = settings.Where(s => s != null).GroupBy(s => s.Header);

List<SettingBase> result = new();

// Group settings by headers
foreach (IGrouping<HeaderSetting, SettingBase> grouping in grouped)
{
if (grouping.Key != null)
result.Add(grouping.Key);

result.AddRange(grouping);
}

ServerSpecificSettingsSync.DefinedSettings = (ServerSpecificSettingsSync.DefinedSettings ?? Array.Empty<ServerSpecificSettingBase>()).Concat(result.Select(s => s.Base)).ToArray();
Settings.AddRange(result);

SendToPlayer(player);

return result;
}

/// <summary>
/// Removes settings from players.
/// </summary>
/// <param name="predicate">Determines which players will receive this update.</param>
/// <param name="settings">Settings to remove. If <c>null</c>, all settings will be removed.</param>
/// <returns>A <see cref="IEnumerable{T}"/> of <see cref="SettingBase"/> instances that were successfully removed.</returns>
/// <remarks>This method is used to unsync settings from players. Using it with <see cref="Register"/> provides an opportunity to update synced settings.</remarks>
/// <remarks>This method is used to unsync settings from players. Using it with <see cref="Register(IEnumerable{SettingBase},Func{Player,bool})"/> provides an opportunity to update synced settings.</remarks>
public static IEnumerable<SettingBase> Unregister(Func<Player, bool> predicate = null, IEnumerable<SettingBase> settings = null)
{
List<ServerSpecificSettingBase> list = ListPool<ServerSpecificSettingBase>.Pool.Get(ServerSpecificSettingsSync.DefinedSettings);
Expand All @@ -283,6 +313,27 @@ public static IEnumerable<SettingBase> Unregister(Func<Player, bool> predicate =
return list2;
}

/// <summary>
/// Removes settings from players.
/// </summary>
/// <param name="player">Determines which player will receive this update.</param>
/// <param name="settings">Settings to remove. If <c>null</c>, all settings will be removed.</param>
/// <returns>A <see cref="IEnumerable{T}"/> of <see cref="SettingBase"/> instances that were successfully removed.</returns>
/// <remarks>This method is used to unsync settings from players. Using it with <see cref="Register(IEnumerable{SettingBase},Player)"/> provides an opportunity to update synced settings.</remarks>
public static IEnumerable<SettingBase> Unregister(Player player, IEnumerable<SettingBase> settings = null)
{
List<ServerSpecificSettingBase> list = ListPool<ServerSpecificSettingBase>.Pool.Get(ServerSpecificSettingsSync.DefinedSettings);
List<SettingBase> list2 = new((settings ?? Settings).Where(setting => list.Remove(setting.Base)));

ServerSpecificSettingsSync.DefinedSettings = list.ToArray();

SendToPlayer(player);

ListPool<ServerSpecificSettingBase>.Pool.Return(list);

return list2;
}

/// <summary>
/// Returns a string representation of this <see cref="SettingBase"/>.
/// </summary>
Expand Down