Skip to content

RelatedItem::CheckContained did not check the TargetSlot in the correct Inventory #16606

@Regalis11

Description

@Regalis11

Discussed in #15923

Originally posted by zhu-rengong April 19, 2025

Disclaimers

  • I have searched the issue tracker to check if the issue has already been reported.
  • My issue happened while using mods.

What happened?

Related code:

foreach (var container in parentItem.GetComponents<Items.Components.ItemContainer>())
{
if (TargetSlot > -1 && RequireOrMatchOnEmpty)
{
var itemInSlot = container.Inventory.GetItemAt(TargetSlot);
if (RequireEmpty) { return itemInSlot == null; }
if (MatchOnEmpty && itemInSlot == null) { return true; }
}
foreach (Item contained in container.Inventory.AllItems)
{
if (TargetSlot > -1 && parentItem.OwnInventory.FindIndex(contained) != TargetSlot) { continue; }
if ((!ExcludeBroken || contained.Condition > 0.0f) && (!ExcludeFullCondition || !contained.IsFullCondition) && MatchesItem(contained)) { return true; }
if (CheckContained(contained)) { return true; }
}
}

Solution:

  1. Change parentItem.OwnInventory.FindIndex to container.Inventory.FindIndex, otherwise it will always return false
  2. Consider an offset for TargetSlot to ensure it does not confuse different ItemContainers

Diff code:
image

Reproduction steps

  1. spawn an item (smg2 used for test), it auto fill a magazine and checks the item in slot 0 when firing
  <Item name="" identifier="smg2" category="Weapon" cargocontaineridentifier="metalcrate" tags="smallautoweapon,smallitem,weapon,gun,gunsmith,provocativetohumanai,mountableweapon" Scale="0.5" impactsoundtag="impact_metal_light">
    <InventoryIcon texture="Content/Items/InventoryIconAtlas.png" sourcerect="832,830,64,64" origin="0.5,0.5" />
    <Sprite texture="Content/Items/Weapons/weapons_new.png" sourcerect="0,121,144,80" depth="0.55" origin="0.5,0.5" />
    <Body width="140" height="60" density="25" />
    <Holdable slots="Any,RightHand+LeftHand" controlpose="true" holdpos="40,-10" aimpos="45,-10" handle1="-30,-15" handle2="26,5" holdangle="-35" msg="ItemMsgPickUpSelect" />
    <RangedWeapon reload="0.19" weapondamagemodifier="1.3" penetration="0.15" holdtrigger="true" barrelpos="64,9" spread="10" unskilledspread="16" combatPriority="80" drawhudwhenequipped="true" crosshairscale="0.2">
      <Crosshair texture="Content/Items/Weapons/Crosshairs.png" sourcerect="0,256,256,256" />
      <CrosshairPointer texture="Content/Items/Weapons/Crosshairs.png" sourcerect="256,256,256,256" />
      <ParticleEmitter particle="muzzleflash" particleamount="1" velocitymin="0" velocitymax="0" />
      <Sound file="Content/Items/Weapons/SMGsingleShot1.ogg" type="OnUse" range="3000" selectionmode="Random" />
      <Sound file="Content/Items/Weapons/SMGsingleShot2.ogg" type="OnUse" range="3000" />
      <Sound file="Content/Items/Weapons/SMGsingleShot3.ogg" type="OnUse" range="3000" />
      <Sound file="Content/Items/Weapons/SMGsingleShot4.ogg" type="OnUse" range="3000" />
      <Sound file="Content/Items/Weapons/SMGsingleShot5.ogg" type="OnUse" range="3000" />
      <Sound file="Content/Items/Weapons/SMGsingleShot6.ogg" type="OnUse" range="3000" />
      <StatusEffect type="OnUse" target="This">
        <ParticleEmitter particle="casingfirearm" particleamount="1" anglemin="90" anglemax="150" velocitymin="50" velocitymax="250" CopyEntityAngle="true" />
        <Explosion range="150.0" force="1.5" shockwave="false" smoke="false" flames="false" sparks="false" underwaterbubble="false" camerashake="12.0" />
      </StatusEffect>
      <!--"Use" contained magazine to spawn projectiles.-->
      <StatusEffect type="OnUse" target="Contained">
        <Use />
      </StatusEffect>
      <RequiredItems items="smgammo" type="Contained" targetslot="0" msg="ItemMsgAmmoRequired" />
      <RequiredSkill identifier="weapons" level="50" />
    </RangedWeapon>
    <ItemContainer capacity="1" maxstacksize="1" hideitems="false" containedstateindicatorslot="0" containedstateindicatorstyle="bullet" containedspritedepth="0.56">
      <Containable items="none" />
      <SlotIcon slotindex="1" texture="Content/UI/StatusMonitorUI.png" sourcerect="320,448,64,64" origin="0.5,0.5" />
      <SubContainer capacity="1" maxstacksize="1">
        <Containable items="flashlight" hide="false" itempos="22,-1" setactive="true" />
      </SubContainer>
    </ItemContainer>
    <ItemContainer spawnwithid="smgmagazine" capacity="1" maxstacksize="1" hideitems="false" containedstateindicatorslot="0" containedstateindicatorstyle="bullet" containedspritedepth="0.56">
      <Containable items="smgammo" hide="false" itempos="4,-10" rotation="-30" />
    </ItemContainer>
    <aitarget sightrange="2000" soundrange="4000" fadeouttime="5" />
    <Quality>
      <QualityStat stattype="FirepowerMultiplier" value="0.1" />
    </Quality>
    <SkillRequirementHint identifier="weapons" level="50" />
  </Item>
  1. you can't shoot, it shows "Ammo Required"

Bug prevalence

Happens every now and then

Single player or multiplayer?

Single player

-

No response

Version

v1.8.7.0 (Calm Before the Storm update)

-

No response

Which operating system did you encounter this bug on?

Windows

Relevant error messages and crash reports

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugSomething isn't workingCodeProgramming taskTicketThe issue has been converted into an issue report.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions