LabAPI icon indicating copy to clipboard operation
LabAPI copied to clipboard

[BUG]: Missing setter overrides for ChamberMax, ChamberedAmmo, and Cocked in ParticleDisruptorItem cause console errors

Open efrosim opened this issue 6 months ago • 1 comments

Before You Report

  • [x] I have searched for existing reports of this bug, and this bug report is unique.

Version

1.1.6

Description

The ParticleDisruptorItem class overrides the ChamberMax, ChamberedAmmo, and Cocked properties but only provides a get accessor for them. Because the base class (FirearmItem) has both get and set for these properties, attempting to set any of them on a ParticleDisruptorItem falls back to the base class setter.

The base class setters attempt to cast the ActionModule to AutomaticActionModule, which fails for the Disruptor (since it uses DisruptorActionModule), resulting in an internal LabAPI error logged to the server console.

To Reproduce

  1. Obtain a ParticleDisruptorItem instance.
  2. Attempt to set its ChamberMax, ChamberedAmmo, or Cocked property (e.g., disruptor.ChamberMax = 5; or disruptor.Cocked = true;).
  3. Check the server console. You will see the following error: [ERROR] [LabApi] Unable to set <PropertyName> as this firearm's IActionModule is not valid.

Expected Behavior

Setting these properties on a Particle Disruptor should either be ignored silently (since it's a special weapon with fixed mechanics) or throw a clear NotSupportedException without falling back to the incompatible base class logic.

Additional Information

Proposed Fix

In LabApi\Features\Wrappers\Items\Firearm\SpecialFirearms\ParticleDisruptorItem.cs:

Add setters that throw a NotSupportedException to override the base class behavior and clearly indicate invalid operations.

Proposed fix:

    public override int ChamberedAmmo
    {
        get
        {
            if (ActionModule is DisruptorActionModule actionModule)
            {
                return actionModule.IsLoaded ? 1 : 0;
            }
            return 0;
        }
        set => throw new NotSupportedException("ChamberedAmmo cannot be modified for the Particle Disruptor.");
    }

    public override int ChamberMax
    {
        get => 1;
        set => throw new NotSupportedException("ChamberMax cannot be modified for the Particle Disruptor.");
    }

    public override bool Cocked
    {
        get
        {
            if (ActionModule is DisruptorActionModule actionModule)
            {
                return actionModule.IsLoaded;
            }
            return false;
        }
        set => throw new NotSupportedException("Cocked state cannot be modified for the Particle Disruptor.");
    }

efrosim avatar May 14 '26 07:05 efrosim

We prefer to not throw exceptions unless absolutely uncessary due to how Unity works in regards to exceptions

davidsebesta1 avatar May 25 '26 16:05 davidsebesta1