Skip to content

Commit

Permalink
fix: Fixes Custom Weapon Reload (#418)
Browse files Browse the repository at this point in the history
fix custom weapon reloading issues

Co-authored-by: Yamato <[email protected]>
  • Loading branch information
TtroubleTT and louis1706 authored Jan 30, 2025
1 parent aec6506 commit 2c89b59
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions EXILED/Exiled.CustomItems/API/Features/CustomWeapon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ namespace Exiled.CustomItems.API.Features
using System;
using System.Linq;

using Exiled.API.Enums;
using Exiled.API.Extensions;
using Exiled.API.Features;
using Exiled.API.Features.DamageHandlers;
using Exiled.API.Features.Items;
using Exiled.API.Features.Pickups;
using Exiled.Events.EventArgs.Player;

using InventorySystem;
using InventorySystem.Items.Firearms.Attachments;
using InventorySystem.Items.Firearms.Attachments.Components;
using InventorySystem.Items.Firearms.Modules;
Expand Down Expand Up @@ -230,9 +231,24 @@ private void OnInternalReloaded(ReloadedWeaponEventArgs ev)
if (ClipSize > 0)
{
int ammoChambered = ((AutomaticActionModule)ev.Firearm.Base.Modules.FirstOrDefault(x => x is AutomaticActionModule))?.SyncAmmoChambered ?? 0;
int ammodrop = -(ClipSize - ev.Firearm.MagazineAmmo) - ammoChambered;
ev.Firearm.MagazineAmmo = ClipSize - ammoChambered;
ev.Player.AddAmmo(ev.Firearm.AmmoType, (ushort)Mathf.Clamp(ammodrop, ushort.MinValue, ushort.MaxValue));
int ammoToGive = ClipSize - ammoChambered;

AmmoType ammoType = ev.Firearm.AmmoType;
int firearmAmmo = ev.Firearm.MagazineAmmo;
int ammoDrop = -(ClipSize - firearmAmmo - ammoChambered);

int ammoInInventory = ev.Player.Ammo[ammoType.GetItemType()] + firearmAmmo;
if (ammoToGive < ammoInInventory)
{
ev.Firearm.MagazineAmmo = ammoToGive;
int newAmmo = ev.Player.Inventory.GetCurAmmo(ammoType.GetItemType()) + ammoDrop;
ev.Player.Inventory.ServerSetAmmo(ammoType.GetItemType(), newAmmo);
}
else
{
ev.Firearm.MagazineAmmo = ammoInInventory;
ev.Player.Inventory.ServerSetAmmo(ammoType.GetItemType(), 0);
}
}

OnReloaded(ev);
Expand Down

0 comments on commit 2c89b59

Please sign in to comment.