Skip to content

Commit

Permalink
feat: Pr133 Usable change (#403)
Browse files Browse the repository at this point in the history
* PR-133 UsableItem

* No breaking change

* Remove breaking change
  • Loading branch information
louis1706 authored Jan 19, 2025
1 parent 5f5b4b7 commit a898dbf
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 16 deletions.
2 changes: 1 addition & 1 deletion EXILED/Exiled.API/Features/Items/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ public static T Get<T>(ushort serial)
/// <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)
public static Item Create<T>(ItemType type, Player owner = null) // TODO modify return type to "T"
where T : Item => Create(type, owner) as T;

/// <summary>
Expand Down
21 changes: 16 additions & 5 deletions EXILED/Exiled.API/Features/Items/Usable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Exiled.API.Features.Items
{
using Exiled.API.Extensions;
using Exiled.API.Features.Pickups;
using Exiled.API.Interfaces;

Expand Down Expand Up @@ -126,13 +127,23 @@ public override Pickup CreatePickup(Vector3 position, Quaternion? rotation = nul
/// <summary>
/// Uses the item.
/// </summary>
/// <exception cref="System.InvalidOperationException">The <see cref="Item.Owner"/> of the item cannot be <see langword="null"/>.</exception>
public virtual void Use()
public virtual void Use() => Use(Owner);

/// <summary>
/// Uses the item.
/// </summary>
/// <param name="owner">Target <see cref="Player"/> to use an <see cref="Usable"/>.</param>
public virtual void Use(Player owner = null)
{
if (Owner is null)
throw new System.InvalidOperationException("The Owner of the item cannot be null.");
Player oldOwner = Owner;
owner ??= Owner;

Base.Owner = owner.ReferenceHub;
Base.ServerOnUsingCompleted();

typeof(UsableItemsController).InvokeStaticEvent(nameof(UsableItemsController.ServerOnUsingCompleted), new object[] { owner.ReferenceHub, Base });

Owner.UseItem(this);
Base.Owner = oldOwner.ReferenceHub;
}

/// <inheritdoc/>
Expand Down
2 changes: 1 addition & 1 deletion EXILED/Exiled.API/Features/Pickups/Pickup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ public static IEnumerable<T> Get<T>(IEnumerable<GameObject> gameObjects)
/// <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)
public static Pickup Create<T>(ItemType type) // TODO modify return type to "T"
where T : Pickup => Create(type) as T;

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ internal Projectile(ItemType type)
/// <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)
public static Projectile Create<T>(ProjectileType projectiletype) // TODO modify return type to "T"
where T : Projectile => Create(projectiletype) as T;

/// <summary>
Expand Down
15 changes: 7 additions & 8 deletions EXILED/Exiled.API/Features/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2206,6 +2206,12 @@ public void Heal(float amount, bool overrideMaxHealth = false)
/// <returns><see langword="true"/> if item was used successfully. Otherwise, <see langword="false"/>.</returns>
public bool UseItem(ItemType usableItem) => UseItem(Item.Create(usableItem));

/// <summary>
/// Forces the player to use an item.
/// </summary>
/// <param name="usable">The item to be used.</param>
public void UseItem(Usable usable) => usable?.Use(this);

/// <summary>
/// Forces the player to use an item.
/// </summary>
Expand All @@ -2216,14 +2222,7 @@ public bool UseItem(Item item)
if (item is not Usable usableItem)
return false;

usableItem.Base.Owner = referenceHub;
usableItem.Base.ServerOnUsingCompleted();

typeof(UsableItemsController).InvokeStaticEvent(nameof(UsableItemsController.ServerOnUsingCompleted), new object[] { referenceHub, usableItem.Base });

if (usableItem.Base is not null)
usableItem.Destroy();

UseItem(usableItem);
return true;
}

Expand Down

0 comments on commit a898dbf

Please sign in to comment.