Skip to content

Commit

Permalink
AdminToy.List (#18)
Browse files Browse the repository at this point in the history
* AdminToy.List

* Better AdminToy::Get()

* Update EXILED/Exiled.API/Features/Toys/AdminToy.cs

Co-authored-by: IRacle <79921583+IRacle1@users.noreply.github.com>

* TODO

* Fix Error

* Fix2

---------

Co-authored-by: IRacle <79921583+IRacle1@users.noreply.github.com>
  • Loading branch information
louis1706 and IRacle1 authored Aug 2, 2024
1 parent 6ebeac5 commit 5653733
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
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();
}
}
}

0 comments on commit 5653733

Please sign in to comment.