diff --git a/EXILED/Exiled.API/Features/Items/Item.cs b/EXILED/Exiled.API/Features/Items/Item.cs index 7e46a408f..710a031a1 100644 --- a/EXILED/Exiled.API/Features/Items/Item.cs +++ b/EXILED/Exiled.API/Features/Items/Item.cs @@ -357,7 +357,7 @@ public static T Get(ushort serial) /// The who owns the item by default. /// The specified type. /// The created. This can be cast as a subclass. - public static Item Create(ItemType type, Player owner = null) + public static Item Create(ItemType type, Player owner = null) // TODO modify return type to "T" where T : Item => Create(type, owner) as T; /// diff --git a/EXILED/Exiled.API/Features/Items/Usable.cs b/EXILED/Exiled.API/Features/Items/Usable.cs index 65c858b74..b58f6b8dd 100644 --- a/EXILED/Exiled.API/Features/Items/Usable.cs +++ b/EXILED/Exiled.API/Features/Items/Usable.cs @@ -7,6 +7,7 @@ namespace Exiled.API.Features.Items { + using Exiled.API.Extensions; using Exiled.API.Features.Pickups; using Exiled.API.Interfaces; @@ -126,13 +127,23 @@ public override Pickup CreatePickup(Vector3 position, Quaternion? rotation = nul /// /// Uses the item. /// - /// The of the item cannot be . - public virtual void Use() + public virtual void Use() => Use(Owner); + + /// + /// Uses the item. + /// + /// Target to use an . + 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; } /// diff --git a/EXILED/Exiled.API/Features/Pickups/Pickup.cs b/EXILED/Exiled.API/Features/Pickups/Pickup.cs index 77998260a..fa9aca3b2 100644 --- a/EXILED/Exiled.API/Features/Pickups/Pickup.cs +++ b/EXILED/Exiled.API/Features/Pickups/Pickup.cs @@ -532,7 +532,7 @@ public static IEnumerable Get(IEnumerable gameObjects) /// The specified type. /// The created . /// - public static Pickup Create(ItemType type) + public static Pickup Create(ItemType type) // TODO modify return type to "T" where T : Pickup => Create(type) as T; /// diff --git a/EXILED/Exiled.API/Features/Pickups/Projectiles/Projectile.cs b/EXILED/Exiled.API/Features/Pickups/Projectiles/Projectile.cs index 17a0691b9..80a129eb6 100644 --- a/EXILED/Exiled.API/Features/Pickups/Projectiles/Projectile.cs +++ b/EXILED/Exiled.API/Features/Pickups/Projectiles/Projectile.cs @@ -111,7 +111,7 @@ internal Projectile(ItemType type) /// The of the projectile. /// The specified type. /// The created . - public static Projectile Create(ProjectileType projectiletype) + public static Projectile Create(ProjectileType projectiletype) // TODO modify return type to "T" where T : Projectile => Create(projectiletype) as T; /// diff --git a/EXILED/Exiled.API/Features/Player.cs b/EXILED/Exiled.API/Features/Player.cs index c0bc8f40b..ada3c952a 100644 --- a/EXILED/Exiled.API/Features/Player.cs +++ b/EXILED/Exiled.API/Features/Player.cs @@ -2206,6 +2206,12 @@ public void Heal(float amount, bool overrideMaxHealth = false) /// if item was used successfully. Otherwise, . public bool UseItem(ItemType usableItem) => UseItem(Item.Create(usableItem)); + /// + /// Forces the player to use an item. + /// + /// The item to be used. + public void UseItem(Usable usable) => usable?.Use(this); + /// /// Forces the player to use an item. /// @@ -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; }