Skip to content

fix: respect CustomWeapon ClipSize and preserve ammo on drop (#817)#819

Open
intjiraya wants to merge 3 commits into
ExMod-Team:devfrom
intjiraya:fix/customweapon-ammo
Open

fix: respect CustomWeapon ClipSize and preserve ammo on drop (#817)#819
intjiraya wants to merge 3 commits into
ExMod-Team:devfrom
intjiraya:fix/customweapon-ammo

Conversation

@intjiraya
Copy link
Copy Markdown

@intjiraya intjiraya commented May 8, 2026

Description

Describe the changes

Fixes three defects in CustomWeapon that caused custom weapon configuration to be silently lost (issue #817).

What is the current behavior? (You can also link to an open issue here)

Closes #817.

  • ClipSize larger than the stock magazine is silently truncated. firearm.MagazineAmmo = ClipSize writes to MagazineModule.SyncData but the base game still caps the magazine at AmmoMax = _defaultCapacity + attachmentMod in OnAdded, ServerLoadAmmoFromInventory, and OnAttachmentsApplied. Nothing in CustomWeapon raised _defaultCapacity, so reload / give / respawn truncate back to stock.
  • After the player dies or escapes, the dropped pickup's ammo is reset to ClipSize. The drop path (CustomItem.OnInternalOwnerDying -> Spawn(Vector3, Item, Player?)) unconditionally wrote firearm.MagazineAmmo = ClipSize, discarding the player's actual round count.
  • CustomWeapon.Spawn(Vector3, Player?) returns null for every firearm. The type guard introduced in fix: Item leaks #763 is inverted relative to its log message: if (Type.IsWeapon(false)) returns null when the type IS a firearm.

What is the new behavior? (if this is a feature change)

  • Lift Firearm.MaxMagazineAmmo (which writes _defaultCapacity) to ClipSize in Spawn, Spawn(Item), Give, and OnInternalReloaded so the custom value survives every base-game clamp.
  • Drop the MagazineAmmo = ClipSize write in Spawn(Vector3, Item, Player?) so the dropped pickup preserves whatever ammo the player actually had on death/escape. Initial-fill semantics stay in the no-Item overload and in Give.
  • Negate the firearm-type guard in Spawn(Vector3, Player?) so it matches its log message and the method actually runs for firearms.

Does this PR introduce a breaking change? (What changes might users need to make in their application due to this PR?)

No.

Other information:

N/A.


Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentations

Submission checklist

  • I have checked the project can be compiled
  • I have tested my changes and it worked as expected

Patches (if there are any changes related to Harmony patches)

  • I have checked no IL patching errors in the console

Other

  • Still requires more testing

@intjiraya
Copy link
Copy Markdown
Author

@microsievert

Comment thread EXILED/Exiled.CustomItems/API/Features/CustomWeapon.cs
@Someone-193
Copy link
Copy Markdown

Someone-193 commented May 9, 2026

The IsWeapon check was added in #763 by MS

@MS-crew
Copy link
Copy Markdown

MS-crew commented May 9, 2026

The IsWeapon check was added in #763 by MS

68747470733a2f2f6d65646961322e67697068792e636f6d2f6d656469612f76312e59326c6b505463354d4749334e6a4578656d46704d5468366132316a647a5636637a6b7a646d45326548686963474a6b5a576332613352774e57787a62475271616a643464695a6c634431324d563970626e526c636d35686246396e61575a66596e6c666157516d593351395a772f47666757364b33506a4b76746e633136494b2f67697068792e676966

:(

@intjiraya
Copy link
Copy Markdown
Author

I’ll build it tomorrow, check for possible errors, verify that the changes are correct, and make the PR ready for review 🤓

@intjiraya intjiraya marked this pull request as ready for review May 10, 2026 11:59
Copilot AI review requested due to automatic review settings May 10, 2026 11:59
@intjiraya
Copy link
Copy Markdown
Author

fahhh

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes CustomWeapon firearm spawning/giving/reloading so custom ClipSize is respected by the underlying firearm magazine cap, and so dropped weapons preserve the actual remaining ammo (issue #817).

Changes:

  • Fixes an inverted firearm-type guard in CustomWeapon.Spawn(Vector3, Player?) so firearms are actually spawned.
  • Raises the firearm magazine maximum to ClipSize across spawn/give/reload flows to prevent base-game clamping back to default capacity.
  • Preserves actual magazine ammo when spawning a pickup from an existing firearm item (death/escape drops) by removing the unconditional refill to ClipSize.

Comment thread EXILED/Exiled.CustomItems/API/Features/CustomWeapon.cs
@intjiraya intjiraya requested a review from louis1706 May 10, 2026 12:11
@louis1706
Copy link
Copy Markdown

Pull request overview

This PR fixes CustomWeapon firearm spawning/giving/reloading so custom ClipSize is respected by the underlying firearm magazine cap, and so dropped weapons preserve the actual remaining ammo (issue #817).

Changes:

  • Fixes an inverted firearm-type guard in CustomWeapon.Spawn(Vector3, Player?) so firearms are actually spawned.
  • Raises the firearm magazine maximum to ClipSize across spawn/give/reload flows to prevent base-game clamping back to default capacity.
  • Preserves actual magazine ammo when spawning a pickup from an existing firearm item (death/escape drops) by removing the unconditional refill to ClipSize.

Bro i disabled you in setting why are you there

Copy link
Copy Markdown

@louis1706 louis1706 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than this comment i will approved it (i will approve it if there a good reason would still try to found to make it work without setting it at every reload)

Comment thread EXILED/Exiled.CustomItems/API/Features/CustomWeapon.cs Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants