Skip to content

Commit

Permalink
More <T> implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
louis1706 committed Aug 1, 2024
1 parent f2b225d commit db53ca7
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 5 deletions.
34 changes: 34 additions & 0 deletions EXILED/Exiled.API/Features/Items/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,40 @@ ItemType.KeycardGuard or ItemType.KeycardJanitor or ItemType.KeycardO5 or ItemTy
_ => new Item(type),
};

/// <summary>
/// Creates a new <see cref="Item"/> with the proper inherited subclass.
/// <para>
/// Based on the <paramref name="type"/>, the returned <see cref="Item"/> can be casted into a subclass to gain more control over the object.
/// <br />- Usable items (Adrenaline, Medkit, Painkillers, SCP-207, SCP-268, and SCP-500) should be casted to the <see cref="Usable"/> class.
/// <br />- All valid ammo should be casted to the <see cref="Ammo"/> class.
/// <br />- All valid firearms (not including the Micro HID) should be casted to the <see cref="Firearm"/> class.
/// <br />- All valid keycards should be casted to the <see cref="Keycard"/> class.
/// <br />- All valid armor should be casted to the <see cref="Armor"/> class.
/// <br />- Explosive grenades and SCP-018 should be casted to the <see cref="ExplosiveGrenade"/> class.
/// <br />- Flash grenades should be casted to the <see cref="FlashGrenade"/> class.
/// </para>
/// <para>
/// <br />The following have their own respective classes:
/// <br />- Flashlights can be casted to <see cref="Flashlight"/>.
/// <br />- Radios can be casted to <see cref="Radio"/>.
/// <br />- The Micro HID can be casted to <see cref="MicroHid"/>.
/// <br />- SCP-244 A and B variants can be casted to <see cref="Scp244"/>.
/// <br />- SCP-330 can be casted to <see cref="Scp330"/>.
/// <br />- SCP-2176 can be casted to the <see cref="Scp2176"/> class.
/// <br />- SCP-1576 can be casted to the <see cref="Scp1576"/> class.
/// <br />- Jailbird can be casted to the <see cref="Jailbird"/> class.
/// </para>
/// <para>
/// Items that are not listed above do not have a subclass, and can only use the base <see cref="Item"/> class.
/// </para>
/// </summary>
/// <param name="type">The <see cref="ItemType"/> of the item to create.</param>
/// <param name="owner">The <see cref="Player"/> who owns the item by default.</param>
/// <typeparam name="T">The specified <see cref="Item"/> type.</typeparam>
/// <returns>The <see cref="Item"/> created. This can be cast as a subclass.</returns>
public static Item Create<T>(ItemType type, Player owner = null)
where T : Item => Create(type, owner) as T;

/// <summary>
/// Gives this item to a <see cref="Player"/>.
/// </summary>
Expand Down
43 changes: 43 additions & 0 deletions EXILED/Exiled.API/Features/Pickups/Pickup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,36 @@ public static IEnumerable<T> Get<T>(IEnumerable<GameObject> gameObjects)
_ => new Pickup(type),
};

/// <summary>
/// Creates and returns a new <see cref="Pickup"/> with the proper inherited subclass.
/// <para>
/// Based on the <paramref name="type"/>, the returned <see cref="Pickup"/> can be cast into a subclass to gain more control over the object.
/// <br />- All valid ammo should be cast to the <see cref="AmmoPickup"/> class.
/// <br />- All valid firearms (not including the Micro HID) should be cast to the <see cref="FirearmPickup"/> class.
/// <br />- All valid keycards should be cast to the <see cref="KeycardPickup"/> class.
/// <br />- All valid armor should be cast to the <see cref="BodyArmorPickup"/> class.
/// <br />- All grenades and throwables (not including SCP-018 and SCP-2176) should be cast to the <see cref="GrenadePickup"/> class.
/// </para>
/// <para>
/// <br />The following have their own respective classes:
/// <br />- Radios can be cast to <see cref="RadioPickup"/>.
/// <br />- The Micro HID can be cast to <see cref="MicroHIDPickup"/>.
/// <br />- SCP-244 A and B variants can be cast to <see cref="Scp244Pickup"/>.
/// <br />- SCP-330 can be cast to <see cref="Scp330Pickup"/>.
/// <br />- SCP-018 can be cast to <see cref="Projectiles.Scp018Projectile"/>.
/// <br />- SCP-2176 can be cast to <see cref="Projectiles.Scp2176Projectile"/>.
/// </para>
/// <para>
/// Items that are not listed above do not have a subclass, and can only use the base <see cref="Pickup"/> class.
/// </para>
/// </summary>
/// <param name="type">The <see cref="ItemType"/> of the pickup.</param>
/// <typeparam name="T">The specified <see cref="Pickup"/> type.</typeparam>
/// <returns>The created <see cref="Pickup"/>.</returns>
/// <seealso cref="Projectile.Create(Enums.ProjectileType)"/>
public static Pickup Create<T>(ItemType type)
where T : Pickup => Create(type) as T;

/// <summary>
/// Creates and spawns a <see cref="Pickup"/>.
/// </summary>
Expand All @@ -507,6 +537,19 @@ public static IEnumerable<T> Get<T>(IEnumerable<GameObject> gameObjects)
/// <seealso cref="Projectile.CreateAndSpawn(Enums.ProjectileType, Vector3, Quaternion, bool, Player)"/>
public static Pickup CreateAndSpawn(ItemType type, Vector3 position, Quaternion rotation, Player previousOwner = null) => Create(type).Spawn(position, rotation, previousOwner);

/// <summary>
/// Creates and spawns a <see cref="Pickup"/>.
/// </summary>
/// <param name="type">The <see cref="ItemType"/> of the pickup.</param>
/// <param name="position">The position to spawn the <see cref="Pickup"/> at.</param>
/// <param name="rotation">The rotation to spawn the <see cref="Pickup"/>.</param>
/// <param name="previousOwner">An optional previous owner of the item.</param>
/// <typeparam name="T">The specified <see cref="Pickup"/> type.</typeparam>
/// <returns>The <see cref="Pickup"/>. See documentation of <see cref="Create(ItemType)"/> for more information on casting.</returns>
/// <seealso cref="Projectile.CreateAndSpawn(Enums.ProjectileType, Vector3, Quaternion, bool, Player)"/>
public static Pickup CreateAndSpawn<T>(ItemType type, Vector3 position, Quaternion rotation, Player previousOwner = null)
where T : Pickup => CreateAndSpawn(type, position, rotation, previousOwner) as T;

/// <summary>
/// Spawns a <see cref="Pickup"/>.
/// </summary>
Expand Down
43 changes: 38 additions & 5 deletions EXILED/Exiled.API/Features/Pickups/Projectiles/Projectile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,37 @@ internal Projectile(ItemType type)
/// Projectile that are not listed will cause an Exception.
/// </para>
/// </summary>
/// <param name="projectiletype">The <see cref="ProjectileType"/> of the pickup.</param>
/// <returns>The created <see cref="Pickup"/>.</returns>
/// <param name="projectiletype">The <see cref="Enums.ProjectileType"/> of the projectile.</param>
/// <returns>The created <see cref="Projectile"/>.</returns>
public static Projectile Create(ProjectileType projectiletype) => projectiletype switch
{
ProjectileType.Scp018 => new Scp018Projectile(),
ProjectileType.Flashbang => new FlashbangProjectile(),
ProjectileType.Scp2176 => new Scp2176Projectile(),
ProjectileType.FragGrenade => new ExplosionGrenadeProjectile(ItemType.GrenadeHE),
_ => throw new System.Exception($"ProjectileType does not contain a valid value : {projectiletype}"),
_ => throw new Exception($"ProjectileType does not contain a valid value : {projectiletype}"),
};

/// <summary>
/// Creates and returns a new <see cref="Projectile"/> with the proper inherited subclass.
/// <para>
/// Based on the <paramref name="projectiletype"/>, the returned <see cref="Projectile"/> can be casted into a subclass to gain more control over the object.
/// <br />The following have their own respective classes:
/// <br />- FragGrenade can be casted to <see cref="ExplosionGrenadeProjectile"/>.
/// <br />- Flashbang can be casted to <see cref="FlashbangProjectile"/>.
/// <br />- Scp018 A and B variants can be casted to <see cref="Scp018Projectile"/>.
/// <br />- Scp2176 can be casted to <see cref="Scp2176Projectile"/>.
/// </para>
/// <para>
/// Projectile that are not listed will cause an Exception.
/// </para>
/// </summary>
/// <param name="projectiletype">The <see cref="Enums.ProjectileType"/> of the projectile.</param>
/// <typeparam name="T">The specified <see cref="Projectile"/> type.</typeparam>
/// <returns>The created <see cref="Projectile"/>.</returns>
public static Projectile Create<T>(ProjectileType projectiletype)
where T : Projectile => Create(projectiletype) as T;

/// <summary>
/// Spawns a <see cref="Projectile"/>.
/// </summary>
Expand All @@ -110,14 +130,27 @@ public static Projectile Spawn(Projectile pickup, Vector3 position, Quaternion r
/// <summary>
/// Creates and spawns a <see cref="Projectile"/>.
/// </summary>
/// <param name="type">The <see cref="ItemType"/> of the pickup.</param>
/// <param name="type">The <see cref="ProjectileType"/> of the projectile.</param>
/// <param name="position">The position to spawn the <see cref="Projectile"/> at.</param>
/// <param name="rotation">The rotation to spawn the <see cref="Projectile"/>.</param>
/// <param name="shouldBeActive">Whether the <see cref="Projectile"/> should be in active state after spawn.</param>
/// <param name="previousOwner">An optional previous owner of the item.</param>
/// <returns>The <see cref="Projectile"/>. See documentation of <see cref="Pickup.Create(ItemType)"/> for more information on casting.</returns>
/// <returns>The <see cref="Projectile"/>. See documentation of <see cref="Create(ProjectileType)"/> for more information on casting.</returns>
public static Projectile CreateAndSpawn(ProjectileType type, Vector3 position, Quaternion rotation, bool shouldBeActive = true, Player previousOwner = null) => Create(type).Spawn(position, rotation, shouldBeActive, previousOwner);

/// <summary>
/// Creates and spawns a <see cref="Projectile"/>.
/// </summary>
/// <param name="type">The <see cref="ProjectileType"/> of the projectile.</param>
/// <param name="position">The position to spawn the <see cref="Projectile"/> at.</param>
/// <param name="rotation">The rotation to spawn the <see cref="Projectile"/>.</param>
/// <param name="shouldBeActive">Whether the <see cref="Projectile"/> should be in active state after spawn.</param>
/// <param name="previousOwner">An optional previous owner of the item.</param>
/// <typeparam name="T">The specified <see cref="Projectile"/> type.</typeparam>
/// <returns>The <see cref="Projectile"/>. See documentation of <see cref="Create(ProjectileType)"/> for more information on casting.</returns>
public static Projectile CreateAndSpawn<T>(ProjectileType type, Vector3 position, Quaternion rotation, bool shouldBeActive = true, Player previousOwner = null)
where T : Projectile => CreateAndSpawn(type, position, rotation, shouldBeActive, previousOwner) as T;

/// <summary>
/// Activates the current <see cref="Projectile"/>.
/// </summary>
Expand Down

0 comments on commit db53ca7

Please sign in to comment.