Skip to content

Commit db53ca7

Browse files
committed
More <T> implementation
1 parent f2b225d commit db53ca7

File tree

3 files changed

+115
-5
lines changed

3 files changed

+115
-5
lines changed

EXILED/Exiled.API/Features/Items/Item.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,40 @@ ItemType.KeycardGuard or ItemType.KeycardJanitor or ItemType.KeycardO5 or ItemTy
286286
_ => new Item(type),
287287
};
288288

289+
/// <summary>
290+
/// Creates a new <see cref="Item"/> with the proper inherited subclass.
291+
/// <para>
292+
/// Based on the <paramref name="type"/>, the returned <see cref="Item"/> can be casted into a subclass to gain more control over the object.
293+
/// <br />- Usable items (Adrenaline, Medkit, Painkillers, SCP-207, SCP-268, and SCP-500) should be casted to the <see cref="Usable"/> class.
294+
/// <br />- All valid ammo should be casted to the <see cref="Ammo"/> class.
295+
/// <br />- All valid firearms (not including the Micro HID) should be casted to the <see cref="Firearm"/> class.
296+
/// <br />- All valid keycards should be casted to the <see cref="Keycard"/> class.
297+
/// <br />- All valid armor should be casted to the <see cref="Armor"/> class.
298+
/// <br />- Explosive grenades and SCP-018 should be casted to the <see cref="ExplosiveGrenade"/> class.
299+
/// <br />- Flash grenades should be casted to the <see cref="FlashGrenade"/> class.
300+
/// </para>
301+
/// <para>
302+
/// <br />The following have their own respective classes:
303+
/// <br />- Flashlights can be casted to <see cref="Flashlight"/>.
304+
/// <br />- Radios can be casted to <see cref="Radio"/>.
305+
/// <br />- The Micro HID can be casted to <see cref="MicroHid"/>.
306+
/// <br />- SCP-244 A and B variants can be casted to <see cref="Scp244"/>.
307+
/// <br />- SCP-330 can be casted to <see cref="Scp330"/>.
308+
/// <br />- SCP-2176 can be casted to the <see cref="Scp2176"/> class.
309+
/// <br />- SCP-1576 can be casted to the <see cref="Scp1576"/> class.
310+
/// <br />- Jailbird can be casted to the <see cref="Jailbird"/> class.
311+
/// </para>
312+
/// <para>
313+
/// Items that are not listed above do not have a subclass, and can only use the base <see cref="Item"/> class.
314+
/// </para>
315+
/// </summary>
316+
/// <param name="type">The <see cref="ItemType"/> of the item to create.</param>
317+
/// <param name="owner">The <see cref="Player"/> who owns the item by default.</param>
318+
/// <typeparam name="T">The specified <see cref="Item"/> type.</typeparam>
319+
/// <returns>The <see cref="Item"/> created. This can be cast as a subclass.</returns>
320+
public static Item Create<T>(ItemType type, Player owner = null)
321+
where T : Item => Create(type, owner) as T;
322+
289323
/// <summary>
290324
/// Gives this item to a <see cref="Player"/>.
291325
/// </summary>

EXILED/Exiled.API/Features/Pickups/Pickup.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,36 @@ public static IEnumerable<T> Get<T>(IEnumerable<GameObject> gameObjects)
496496
_ => new Pickup(type),
497497
};
498498

499+
/// <summary>
500+
/// Creates and returns a new <see cref="Pickup"/> with the proper inherited subclass.
501+
/// <para>
502+
/// Based on the <paramref name="type"/>, the returned <see cref="Pickup"/> can be cast into a subclass to gain more control over the object.
503+
/// <br />- All valid ammo should be cast to the <see cref="AmmoPickup"/> class.
504+
/// <br />- All valid firearms (not including the Micro HID) should be cast to the <see cref="FirearmPickup"/> class.
505+
/// <br />- All valid keycards should be cast to the <see cref="KeycardPickup"/> class.
506+
/// <br />- All valid armor should be cast to the <see cref="BodyArmorPickup"/> class.
507+
/// <br />- All grenades and throwables (not including SCP-018 and SCP-2176) should be cast to the <see cref="GrenadePickup"/> class.
508+
/// </para>
509+
/// <para>
510+
/// <br />The following have their own respective classes:
511+
/// <br />- Radios can be cast to <see cref="RadioPickup"/>.
512+
/// <br />- The Micro HID can be cast to <see cref="MicroHIDPickup"/>.
513+
/// <br />- SCP-244 A and B variants can be cast to <see cref="Scp244Pickup"/>.
514+
/// <br />- SCP-330 can be cast to <see cref="Scp330Pickup"/>.
515+
/// <br />- SCP-018 can be cast to <see cref="Projectiles.Scp018Projectile"/>.
516+
/// <br />- SCP-2176 can be cast to <see cref="Projectiles.Scp2176Projectile"/>.
517+
/// </para>
518+
/// <para>
519+
/// Items that are not listed above do not have a subclass, and can only use the base <see cref="Pickup"/> class.
520+
/// </para>
521+
/// </summary>
522+
/// <param name="type">The <see cref="ItemType"/> of the pickup.</param>
523+
/// <typeparam name="T">The specified <see cref="Pickup"/> type.</typeparam>
524+
/// <returns>The created <see cref="Pickup"/>.</returns>
525+
/// <seealso cref="Projectile.Create(Enums.ProjectileType)"/>
526+
public static Pickup Create<T>(ItemType type)
527+
where T : Pickup => Create(type) as T;
528+
499529
/// <summary>
500530
/// Creates and spawns a <see cref="Pickup"/>.
501531
/// </summary>
@@ -507,6 +537,19 @@ public static IEnumerable<T> Get<T>(IEnumerable<GameObject> gameObjects)
507537
/// <seealso cref="Projectile.CreateAndSpawn(Enums.ProjectileType, Vector3, Quaternion, bool, Player)"/>
508538
public static Pickup CreateAndSpawn(ItemType type, Vector3 position, Quaternion rotation, Player previousOwner = null) => Create(type).Spawn(position, rotation, previousOwner);
509539

540+
/// <summary>
541+
/// Creates and spawns a <see cref="Pickup"/>.
542+
/// </summary>
543+
/// <param name="type">The <see cref="ItemType"/> of the pickup.</param>
544+
/// <param name="position">The position to spawn the <see cref="Pickup"/> at.</param>
545+
/// <param name="rotation">The rotation to spawn the <see cref="Pickup"/>.</param>
546+
/// <param name="previousOwner">An optional previous owner of the item.</param>
547+
/// <typeparam name="T">The specified <see cref="Pickup"/> type.</typeparam>
548+
/// <returns>The <see cref="Pickup"/>. See documentation of <see cref="Create(ItemType)"/> for more information on casting.</returns>
549+
/// <seealso cref="Projectile.CreateAndSpawn(Enums.ProjectileType, Vector3, Quaternion, bool, Player)"/>
550+
public static Pickup CreateAndSpawn<T>(ItemType type, Vector3 position, Quaternion rotation, Player previousOwner = null)
551+
where T : Pickup => CreateAndSpawn(type, position, rotation, previousOwner) as T;
552+
510553
/// <summary>
511554
/// Spawns a <see cref="Pickup"/>.
512555
/// </summary>

EXILED/Exiled.API/Features/Pickups/Projectiles/Projectile.cs

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,37 @@ internal Projectile(ItemType type)
8383
/// Projectile that are not listed will cause an Exception.
8484
/// </para>
8585
/// </summary>
86-
/// <param name="projectiletype">The <see cref="ProjectileType"/> of the pickup.</param>
87-
/// <returns>The created <see cref="Pickup"/>.</returns>
86+
/// <param name="projectiletype">The <see cref="Enums.ProjectileType"/> of the projectile.</param>
87+
/// <returns>The created <see cref="Projectile"/>.</returns>
8888
public static Projectile Create(ProjectileType projectiletype) => projectiletype switch
8989
{
9090
ProjectileType.Scp018 => new Scp018Projectile(),
9191
ProjectileType.Flashbang => new FlashbangProjectile(),
9292
ProjectileType.Scp2176 => new Scp2176Projectile(),
9393
ProjectileType.FragGrenade => new ExplosionGrenadeProjectile(ItemType.GrenadeHE),
94-
_ => throw new System.Exception($"ProjectileType does not contain a valid value : {projectiletype}"),
94+
_ => throw new Exception($"ProjectileType does not contain a valid value : {projectiletype}"),
9595
};
9696

97+
/// <summary>
98+
/// Creates and returns a new <see cref="Projectile"/> with the proper inherited subclass.
99+
/// <para>
100+
/// Based on the <paramref name="projectiletype"/>, the returned <see cref="Projectile"/> can be casted into a subclass to gain more control over the object.
101+
/// <br />The following have their own respective classes:
102+
/// <br />- FragGrenade can be casted to <see cref="ExplosionGrenadeProjectile"/>.
103+
/// <br />- Flashbang can be casted to <see cref="FlashbangProjectile"/>.
104+
/// <br />- Scp018 A and B variants can be casted to <see cref="Scp018Projectile"/>.
105+
/// <br />- Scp2176 can be casted to <see cref="Scp2176Projectile"/>.
106+
/// </para>
107+
/// <para>
108+
/// Projectile that are not listed will cause an Exception.
109+
/// </para>
110+
/// </summary>
111+
/// <param name="projectiletype">The <see cref="Enums.ProjectileType"/> of the projectile.</param>
112+
/// <typeparam name="T">The specified <see cref="Projectile"/> type.</typeparam>
113+
/// <returns>The created <see cref="Projectile"/>.</returns>
114+
public static Projectile Create<T>(ProjectileType projectiletype)
115+
where T : Projectile => Create(projectiletype) as T;
116+
97117
/// <summary>
98118
/// Spawns a <see cref="Projectile"/>.
99119
/// </summary>
@@ -110,14 +130,27 @@ public static Projectile Spawn(Projectile pickup, Vector3 position, Quaternion r
110130
/// <summary>
111131
/// Creates and spawns a <see cref="Projectile"/>.
112132
/// </summary>
113-
/// <param name="type">The <see cref="ItemType"/> of the pickup.</param>
133+
/// <param name="type">The <see cref="ProjectileType"/> of the projectile.</param>
114134
/// <param name="position">The position to spawn the <see cref="Projectile"/> at.</param>
115135
/// <param name="rotation">The rotation to spawn the <see cref="Projectile"/>.</param>
116136
/// <param name="shouldBeActive">Whether the <see cref="Projectile"/> should be in active state after spawn.</param>
117137
/// <param name="previousOwner">An optional previous owner of the item.</param>
118-
/// <returns>The <see cref="Projectile"/>. See documentation of <see cref="Pickup.Create(ItemType)"/> for more information on casting.</returns>
138+
/// <returns>The <see cref="Projectile"/>. See documentation of <see cref="Create(ProjectileType)"/> for more information on casting.</returns>
119139
public static Projectile CreateAndSpawn(ProjectileType type, Vector3 position, Quaternion rotation, bool shouldBeActive = true, Player previousOwner = null) => Create(type).Spawn(position, rotation, shouldBeActive, previousOwner);
120140

141+
/// <summary>
142+
/// Creates and spawns a <see cref="Projectile"/>.
143+
/// </summary>
144+
/// <param name="type">The <see cref="ProjectileType"/> of the projectile.</param>
145+
/// <param name="position">The position to spawn the <see cref="Projectile"/> at.</param>
146+
/// <param name="rotation">The rotation to spawn the <see cref="Projectile"/>.</param>
147+
/// <param name="shouldBeActive">Whether the <see cref="Projectile"/> should be in active state after spawn.</param>
148+
/// <param name="previousOwner">An optional previous owner of the item.</param>
149+
/// <typeparam name="T">The specified <see cref="Projectile"/> type.</typeparam>
150+
/// <returns>The <see cref="Projectile"/>. See documentation of <see cref="Create(ProjectileType)"/> for more information on casting.</returns>
151+
public static Projectile CreateAndSpawn<T>(ProjectileType type, Vector3 position, Quaternion rotation, bool shouldBeActive = true, Player previousOwner = null)
152+
where T : Projectile => CreateAndSpawn(type, position, rotation, shouldBeActive, previousOwner) as T;
153+
121154
/// <summary>
122155
/// Activates the current <see cref="Projectile"/>.
123156
/// </summary>

0 commit comments

Comments
 (0)