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

AdminToy.List #18

Merged
merged 6 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
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
7 changes: 1 addition & 6 deletions EXILED/Exiled.API/Features/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,6 @@ public static class Map
/// </summary>
internal static readonly List<PocketDimensionTeleport> TeleportsValue = new(8);

/// <summary>
/// A list of <see cref="AdminToy"/>s on the map.
/// </summary>
internal static readonly List<AdminToy> ToysValue = new();

private static TantrumEnvironmentalHazard tantrumPrefab;
private static Scp939AmnesticCloudInstance amnesticCloudPrefab;

Expand Down Expand Up @@ -130,7 +125,7 @@ DecontaminationController.Singleton.NetworkDecontaminationOverride is Decontamin
/// <summary>
/// Gets all <see cref="AdminToy"/> objects.
/// </summary>
public static ReadOnlyCollection<AdminToy> Toys { get; } = ToysValue.AsReadOnly();
public static ReadOnlyCollection<AdminToy> Toys => AdminToy.BaseToAdminToy.Values.ToList().AsReadOnly(); // TODO: Obsolete it and make people use AdminToy.List

/// <summary>
/// Gets or sets the current seed of the map.
Expand Down
33 changes: 30 additions & 3 deletions EXILED/Exiled.API/Features/Toys/AdminToy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@

namespace Exiled.API.Features.Toys
{
using System.Collections.Generic;
using System.Linq;

using AdminToys;

using Enums;
using Exiled.API.Interfaces;
using Footprinting;
using InventorySystem.Items;
using Mirror;

using UnityEngine;
Expand All @@ -23,6 +25,11 @@ namespace Exiled.API.Features.Toys
/// </summary>
public abstract class AdminToy : IWorldSpace
{
/// <summary>
/// A dictionary of all <see cref="AdminToys.AdminToyBase"/>'s that have been converted into <see cref="AdminToy"/>.
/// </summary>
internal static readonly Dictionary<AdminToyBase, AdminToy> BaseToAdminToy = new(new ComponentsEqualityComparer());

/// <summary>
/// Initializes a new instance of the <see cref="AdminToy"/> class.
/// </summary>
Expand All @@ -33,9 +40,14 @@ internal AdminToy(AdminToyBase toyAdminToyBase, AdminToyType type)
AdminToyBase = toyAdminToyBase;
ToyType = type;

Map.ToysValue.Add(this);
BaseToAdminToy.Add(toyAdminToyBase, this);
}

/// <summary>
/// Gets a list of all <see cref="AdminToy"/>'s on the server.
/// </summary>
public static IReadOnlyCollection<AdminToy> List => BaseToAdminToy.Values;

/// <summary>
/// Gets the original <see cref="AdminToys.AdminToyBase"/>.
/// </summary>
Expand Down Expand Up @@ -130,7 +142,22 @@ public bool IsStatic
/// </summary>
/// <param name="adminToyBase">The <see cref="AdminToys.AdminToyBase"/> instance.</param>
/// <returns>The corresponding <see cref="AdminToy"/> instance.</returns>
public static AdminToy Get(AdminToyBase adminToyBase) => Map.Toys.FirstOrDefault(x => x.AdminToyBase == adminToyBase);
public static AdminToy Get(AdminToyBase adminToyBase)
{
if (adminToyBase == null)
return null;

if (BaseToAdminToy.TryGetValue(adminToyBase, out AdminToy adminToy))
return adminToy;

return adminToyBase switch
{
LightSourceToy lightSourceToy => new Light(lightSourceToy),
PrimitiveObjectToy primitiveObjectToy => new Primitive(primitiveObjectToy),
ShootingTarget shootingTarget => new ShootingTargetToy(shootingTarget),
_ => throw new System.NotImplementedException()
};
}

/// <summary>
/// Spawns the toy into the game. Use <see cref="UnSpawn"/> to remove it.
Expand All @@ -147,7 +174,7 @@ public bool IsStatic
/// </summary>
public void Destroy()
{
Map.ToysValue.Remove(this);
BaseToAdminToy.Remove(AdminToyBase);
NetworkServer.Destroy(AdminToyBase.gameObject);
}
}
Expand Down
4 changes: 2 additions & 2 deletions EXILED/Exiled.Events/Handlers/Internal/SceneUnloaded.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace Exiled.Events.Handlers.Internal
{
using API.Features;

using Exiled.API.Features.Toys;
using UnityEngine.SceneManagement;

#pragma warning disable SA1611 // Element parameters should be documented
Expand All @@ -35,7 +35,7 @@ public static void OnSceneUnloaded(Scene _)
{
Player.UserIdsCache.Clear();
Player.Dictionary.Clear();
Map.ToysValue.Clear();
AdminToy.BaseToAdminToy.Clear();
}
}
}
Loading