egametang/ET · error

invalid item count

Error message

invalid item count

What it means

Thrown by ItemHelper.AddItem when count <= 0. AddItem is the server entry point for granting items; a non-positive count has no valid meaning (you cannot add zero or negative items), so it fails fast. Note the sibling RemoveItem returns false on count <= 0 rather than throwing, so AddItem is the stricter of the two.

Source

Thrown at Packages/cn.etetet.item/Scripts/Hotfix/Server/ItemHelper.cs:32

        public static void SetCapacity(ItemComponent self, int capacity)
        {
            self.SetCapacity(capacity);
            NotifyCapacityChange(self);
        }

        /// <summary>
        /// 添加物品到背包
        /// </summary>
        /// <param name="self">物品组件</param>
        /// <param name="configId">物品配置ID</param>
        /// <param name="count">物品数量</param>
        /// <param name="reason">道具变化原因</param>
        /// <returns>是否添加成功</returns>
        public static void AddItem(ItemComponent self, int configId, int count, ItemChangeReason reason)
        {
            if (count <= 0)
            {
                throw new Exception("invalid item count");
            }

            ItemConfigCategory itemConfigCategory = self.Fiber().GetSingleton<ItemConfigCategory>();
            ItemConfig itemConfig = itemConfigCategory.Get(configId);
            if (itemConfig == null)
            {
                throw new Exception($"item config not found: {configId}");
            }

            Log.Debug($"add item: configId={configId}, count={count}, reason={reason}");

            int maxStack = itemConfig.MaxStack;
            int remainCount = count;
            List<long> updatedItemIds = new();

            // 如果物品可堆叠,先尝试叠加到已有物品
            if (maxStack > 1)
            {

View on GitHub (pinned to 5cab01f7a8)

Solutions

  1. Guard the caller: if (count <= 0) return; before invoking AddItem (mirror RemoveItem's behavior).
  2. Fix the source of count so reward tables/loot rolls never yield <= 0.
  3. Validate client-supplied counts at the handler boundary, not in the core helper.

Example fix

// before
ItemHelper.AddItem(comp, configId, rewardCount, reason); // rewardCount may be 0

// after
if (rewardCount <= 0) return;
ItemHelper.AddItem(comp, configId, rewardCount, reason);
Defensive patterns

Strategy: validation

Validate before calling

if (count <= 0) return; // mirror RemoveItem's tolerant behavior
ItemHelper.AddItem(comp, configId, count, reason);

Prevention

When it happens

Trigger: Calling ItemHelper.AddItem(self, configId, count, reason) with count <= 0. Common when count comes from a reward table, a loot roll, or a client request that yielded zero.

Common situations: A reward/drop formula produces 0 (e.g. Random.Next(min,max) with min==max); a config row with Count=0 used as a placeholder; a client request that was not validated before reaching the server helper.

Related errors


AI-assisted analysis of egametang/ET@5cab01f7a8 (2026-08-13). Data as JSON: /api/errors/36b1028f4289befb. Report an issue: GitHub.