diff --git a/EXILED/Exiled.API/Features/Toys/AdminToy.cs b/EXILED/Exiled.API/Features/Toys/AdminToy.cs index 4a82030c8c..014af3f309 100644 --- a/EXILED/Exiled.API/Features/Toys/AdminToy.cs +++ b/EXILED/Exiled.API/Features/Toys/AdminToy.cs @@ -7,17 +7,14 @@ namespace Exiled.API.Features.Toys { + using System; using System.Collections.Generic; - using System.Linq; using AdminToys; - using Enums; - using Exiled.API.Interfaces; using Footprinting; - using InventorySystem.Items; + using Interfaces; using Mirror; - using UnityEngine; /// @@ -183,6 +180,52 @@ public static AdminToy Get(AdminToyBase adminToyBase) public static T Get(AdminToyBase adminToyBase) where T : AdminToy => Get(adminToyBase) as T; + /// + /// Creates a new . + /// + /// The position of the . + /// The rotation of the . + /// The scale of the . + /// Whether the should be initially spawned. + /// The specific type of to instantiate (e.g., , ). + /// The new . + /// Thrown if no prefab with a component exists in . + public static AdminToy Create(Vector3? position, Vector3? rotation, Vector3? scale, bool spawn) + where T : AdminToyBase + { + if (PrefabCache.Prefab == null) + { + T t = default(T); + using (Dictionary.ValueCollection.Enumerator enumerator = NetworkClient.prefabs.Values.GetEnumerator()) + { + while (enumerator.MoveNext()) + { + if (enumerator.Current != null && enumerator.Current.TryGetComponent(out t)) + { + break; + } + } + } + + if (t == null) + { + throw new InvalidOperationException(string.Format("No prefab in NetworkClient.prefabs has component type {0}", typeof(T))); + } + + PrefabCache.Prefab = t; + } + + T t2 = UnityEngine.Object.Instantiate(PrefabCache.Prefab); + t2.transform.position = position ?? Vector3.zero; + t2.transform.rotation = Quaternion.Euler(rotation ?? Vector3.zero); + t2.transform.localScale = scale ?? Vector3.one; + + if (spawn) + NetworkServer.Spawn(t2.gameObject); + + return Get(t2); + } + /// /// Spawns the toy into the game. Use to remove it. /// @@ -201,5 +244,11 @@ public void Destroy() BaseToAdminToy.Remove(AdminToyBase); NetworkServer.Destroy(AdminToyBase.gameObject); } + + private static class PrefabCache + where T : AdminToyBase + { + public static T Prefab { get; set; } + } } } \ No newline at end of file diff --git a/EXILED/Exiled.API/Features/Toys/Capybara.cs b/EXILED/Exiled.API/Features/Toys/Capybara.cs index 23e3e07f23..6d2ecc9970 100644 --- a/EXILED/Exiled.API/Features/Toys/Capybara.cs +++ b/EXILED/Exiled.API/Features/Toys/Capybara.cs @@ -9,7 +9,9 @@ namespace Exiled.API.Features.Toys { using AdminToys; using Enums; - using Exiled.API.Interfaces; + using Interfaces; + using Mirror; + using UnityEngine; /// /// A wrapper class for . @@ -34,12 +36,33 @@ internal Capybara(CapybaraToy capybaraToy) public CapybaraToy Base { get; } /// - /// Gets or sets a value indicating whether the capybara can be collided with. + /// Gets or sets a value indicating whether the capybara can be collided with. Only server side. /// public bool Collidable { - get => Base.Network_collisionsEnabled; - set => Base.Network_collisionsEnabled = value; + get => Base.CollisionsEnabled; + set => Base.CollisionsEnabled = value; + } + + /// + /// Creates a new . + /// + /// The position of the . + /// The rotation of the . + /// The scale of the . + /// Whether the should be initially spawned. + /// The new . + public static Capybara Create(Vector3? posititon, Vector3? rotation, Vector3? scale, bool spawn) + { + Capybara capybara = new Capybara(Object.Instantiate(Prefab)); + capybara.Position = posititon ?? Vector3.zero; + capybara.Rotation = Quaternion.Euler(rotation ?? Vector3.zero); + capybara.Scale = scale ?? Vector3.one; + + if (spawn) + capybara.Spawn(); + + return capybara; } } } diff --git a/EXILED/Exiled.API/Features/Toys/Text.cs b/EXILED/Exiled.API/Features/Toys/Text.cs index 7c3317cd22..f317d8896e 100644 --- a/EXILED/Exiled.API/Features/Toys/Text.cs +++ b/EXILED/Exiled.API/Features/Toys/Text.cs @@ -9,7 +9,7 @@ namespace Exiled.API.Features.Toys { using AdminToys; using Enums; - using Exiled.API.Interfaces; + using Interfaces; using UnityEngine; /// @@ -20,9 +20,9 @@ public class Text : AdminToy, IWrapper /// /// Initializes a new instance of the class. /// - /// The of the toy. - internal Text(TextToy speakerToy) - : base(speakerToy, AdminToyType.TextToy) => Base = speakerToy; + /// The of the toy. + internal Text(TextToy textToy) + : base(textToy, AdminToyType.TextToy) => Base = textToy; /// /// Gets the prefab. @@ -51,5 +51,30 @@ public Vector2 DisplaySize get => Base.Network_displaySize; set => Base.Network_displaySize = value; } + + /// + /// Creates a new . + /// + /// The text to shown . + /// The size of the . + /// The position of the . + /// The rotation of the . + /// The scale of the . + /// Whether the should be initially spawned. + /// The new . + public static Text Create(string newText, Vector2? displaySize, Vector3? posititon, Vector3? rotation, Vector3? scale, bool spawn) + { + Text text = new Text(Object.Instantiate(Prefab)); + text.Position = posititon ?? Vector3.zero; + text.Rotation = Quaternion.Euler(rotation ?? Vector3.zero); + text.Scale = scale ?? Vector3.one; + + text.TextFormat = newText ?? string.Empty; + text.DisplaySize = displaySize ?? TextToy.DefaultDisplaySize; + + if (spawn) + text.Spawn(); + return text; + } } }