Skip to content

Commit 5d2d310

Browse files
authored
fix: respect CustomWeapon ClipSize and preserve ammo on drop (#819)
* fix: respect CustomWeapon ClipSize and preserve ammo on drop (#817) * fix(CustomItems): re-assert CustomWeapon ClipSize capacity in OnAcquired instead of on every reload
1 parent 510b312 commit 5d2d310

1 file changed

Lines changed: 23 additions & 4 deletions

File tree

EXILED/Exiled.CustomItems/API/Features/CustomWeapon.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,17 @@ public override ItemType Type
7171
/// <inheritdoc />
7272
public override Pickup? Spawn(Vector3 position, Player? previousOwner = null)
7373
{
74-
if (Type.IsWeapon(false))
74+
if (!Type.IsWeapon(false))
7575
{
7676
Log.Warn($"{nameof(Spawn)}: Item is not Firearm.");
7777
return null;
7878
}
7979

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

82+
if (ClipSize > 0)
83+
firearm.MaxMagazineAmmo = ClipSize;
84+
8285
if (!Attachments.IsEmpty())
8386
firearm.AddAttachment(Attachments);
8487

@@ -112,12 +115,12 @@ public override ItemType Type
112115
{
113116
if (item is Firearm firearm)
114117
{
118+
if (ClipSize > 0)
119+
firearm.MaxMagazineAmmo = ClipSize;
120+
115121
if (!Attachments.IsEmpty())
116122
firearm.AddAttachment(Attachments);
117123

118-
if (ClipSize > 0)
119-
firearm.MagazineAmmo = ClipSize;
120-
121124
int ammo = firearm.MagazineAmmo;
122125
Log.Debug($"{nameof(Name)}.{nameof(Spawn)}: Spawning weapon with {ammo} ammo.");
123126
Pickup? pickup = firearm.CreatePickup(position);
@@ -141,6 +144,9 @@ public override void Give(Player player, bool displayMessage = true)
141144

142145
if (item is Firearm firearm)
143146
{
147+
if (ClipSize > 0)
148+
firearm.MaxMagazineAmmo = ClipSize;
149+
144150
if (!Attachments.IsEmpty())
145151
firearm.AddAttachment(Attachments);
146152

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

163+
/// <inheritdoc/>
164+
protected override void OnAcquired(Player player, Item item, bool displayMessage)
165+
{
166+
// MaxMagazineAmmo writes to MagazineModule._defaultCapacity, which is per-instance state and is not
167+
// carried over when the item is re-created (e.g. dropped and picked back up). Re-assert it here so the
168+
// custom ClipSize survives every base-game capacity clamp (reload, attachment changes) no matter how
169+
// the weapon entered the inventory.
170+
if (ClipSize > 0 && item is Firearm firearm)
171+
firearm.MaxMagazineAmmo = ClipSize;
172+
173+
base.OnAcquired(player, item, displayMessage);
174+
}
175+
157176
/// <inheritdoc/>
158177
protected override void SubscribeEvents()
159178
{

0 commit comments

Comments
 (0)