Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions EXILED/Exiled.CustomItems/API/Features/CustomWeapon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,17 @@ public override ItemType Type
/// <inheritdoc />
public override Pickup? Spawn(Vector3 position, Player? previousOwner = null)
{
if (Type.IsWeapon(false))
if (!Type.IsWeapon(false))
Comment thread
intjiraya marked this conversation as resolved.
{
Log.Warn($"{nameof(Spawn)}: Item is not Firearm.");
return null;
}

Firearm firearm = Item.Create<Firearm>(Type);

if (ClipSize > 0)
firearm.MaxMagazineAmmo = ClipSize;
Comment thread
louis1706 marked this conversation as resolved.

if (!Attachments.IsEmpty())
firearm.AddAttachment(Attachments);

Expand Down Expand Up @@ -112,12 +115,12 @@ public override ItemType Type
{
if (item is Firearm firearm)
{
if (ClipSize > 0)
firearm.MaxMagazineAmmo = ClipSize;

if (!Attachments.IsEmpty())
firearm.AddAttachment(Attachments);

if (ClipSize > 0)
firearm.MagazineAmmo = ClipSize;

int ammo = firearm.MagazineAmmo;
Log.Debug($"{nameof(Name)}.{nameof(Spawn)}: Spawning weapon with {ammo} ammo.");
Pickup? pickup = firearm.CreatePickup(position);
Expand All @@ -141,6 +144,9 @@ public override void Give(Player player, bool displayMessage = true)

if (item is Firearm firearm)
{
if (ClipSize > 0)
firearm.MaxMagazineAmmo = ClipSize;

if (!Attachments.IsEmpty())
firearm.AddAttachment(Attachments);

Expand All @@ -154,6 +160,19 @@ public override void Give(Player player, bool displayMessage = true)
OnAcquired(player, item, displayMessage);
}

/// <inheritdoc/>
protected override void OnAcquired(Player player, Item item, bool displayMessage)
{
// MaxMagazineAmmo writes to MagazineModule._defaultCapacity, which is per-instance state and is not
// carried over when the item is re-created (e.g. dropped and picked back up). Re-assert it here so the
// custom ClipSize survives every base-game capacity clamp (reload, attachment changes) no matter how
// the weapon entered the inventory.
if (ClipSize > 0 && item is Firearm firearm)
firearm.MaxMagazineAmmo = ClipSize;

base.OnAcquired(player, item, displayMessage);
}

/// <inheritdoc/>
protected override void SubscribeEvents()
{
Expand Down
Loading