MV revisit "Show dropped loot count instead of each individual items?" for counting bug

Original thread (https://forums.rpgmakerweb.com/index.php?posts/1492593/) was closed by @caethyril or somebody, and two Reports on it were rejected, telling me to open a new thread, so here goes:

I was able to insert a tweak to display the icon for each dropped loot item, but the posted plugin code does seem to have an obscure bug. Although I initially blamed the wrong thing in the Database, I did more testing and changed my tune.
Apparently, when the item's class/category (Items/Weapons/Armors) is different but item.id value is the same, the counts are added together for display.
For example, using the default Database, if your Enemy is set to drop 1 Potion (Items #0001) and 1 Sword (Weapons #0001) and 1 Shield (Armors #0001), all at 1/1 chance, and your "Obtain Item" message is "%1 ×%2 found!" then what gets displayed by the plugin after defeating one (1) such Enemy is this:
(icon176)Potion ×3 found!
(icon97)Sword ×3 found!
(icon128)Shield ×3 found!
When you look in the inventory, there are only 1 of each of those loot drops. The item.id is apparently 1 for all 3 of them, so this code
JavaScript:
} else {    // Patch core-script

    BattleManager.displayDropItems = function() {
        var drops = [], count = [];
        for (var item of this._rewards.items) {
            if (drops.indexOf(item) < 0) drops.push(item);
            count[item.id] = (count[item.id] || 0) + 1;
        }
        if (drops.length > 0) {
            $gameMessage.newPage();
            for (var item of drops) {
                $gameMessage.add(TextManager.obtainItem.format(`\\I[${item.iconIndex}]${item.name}`, count[item.id]));
            }
        }
    };
}
thinks they should add together. (I've made further changes to use const, .includes and other recent Javascript thingies in another version of the plugin, but the same thing occurs.)

As I asked in my second Report, which apologized for blaming something else in the Database in the first one, is there something more unique than item.id that can be used here? While this only occurs if the same ID value is used for different categories, it's worth fixing to avoid players asking about the "missing" loot items.
is there something more unique than item.id that can be used here?
you could use the item's name and id as a kind of key-value pair for its drop count. You wouldnt even need to make an object for it and just have your array indices be strings.
Code:
count[item.id+item.name] = (count[item.id+item.name] || 0) + 1;
...
$gameMessage.add(TextManager.obtainItem.format(`\\I[${item.iconIndex}]${item.name}`, count[item.id+item.name]));
(dont shoot me, i didnt know you could have string indices until a few days ago and i want to justify using it 🤣)
Ah, I see! Nice catch~ 👋

(I agree it's better to start a new thread to sort out issues. We can always merge or link it later if that seems more appropriate.)

I'd suggest separate sub-structures for Item, Weapon, and Armor count, e.g.
Spoiler: plugin code v3
JavaScript:
/*:
 * @target MV
 * @plugindesc Show one message per drop item, with item count.
 * @author Caethyril
 * @url https://forums.rpgmakerweb.com/threads/174569/
 * @orderAfter MOG_BattleResult
 * @help Database -> Terms -> Obtain Item:
 *    %2 will now be replaced with the item count.
 * E.g. use "%1 x%2 found!" to show "Potion x2 found!".
 * 
 * If using MOG_BattleResult, load this plugin after Mog's.
 * This plugin will use the Obtain Item message for each drop.
 * 
 * Terms of use: free to use and/or modify for any project.
 *
 * - Now counts by type as well as ID.
 */
;void (function() {
"use strict";
  /**
   * @param {$dataItem|$dataWeapon|$dataArmor} item Database item reference.
   * @returns {number} Type ID, used for indexing reward counts.
   */
  const typeId = function(item) {
    if (DataManager.isItem(item))
      return 1;
    if (DataManager.isWeapon(item))
      return 2;
    if (DataManager.isArmor(item))
      return 3;
    return 0;  // unknown type
  };

  /**
   * @param {$dataItem|$dataWeapon|$dataArmor} item Database item reference.
   * @param {object[]} count Drop item quantity data, indexed by type and item ID.
   * @returns {string} Display text for item drop.
   */
  const text = function(item, count) {
    const qty = count[typeId(item)][item.id];
    return TextManager.obtainItem.format("\\I[" + item.iconIndex + "]" + item.name, qty);
  };

  // Override - for core script rewards.
  void (function() { if ("Imported" in window && Imported.MOG_BattleResult) return;
    BattleManager.displayDropItems = function() {
      /** Array of drop items with duplicates removed. */
      const drops = [];
      /** Unknown, Item, Weapon, and Armor drop counts, indexed by type & ID. */
      const count = [{},{},{},{}];
      for (const item of this._rewards.items) {
        const type = typeId(item);
        if (drops.indexOf(item) < 0) {
          drops.push(item);
          count[type][item.id] = 1;
        } else
          ++count[type][item.id];
      }
      // Display drop text.
      if (drops.length) {
        $gameMessage.newPage();
        for (const item of drops)
          $gameMessage.add(text(item, count));
      }
    };
  })();

  // Override - for MOG_BattleResult.
  void (function() { if (!("Imported" in window && Imported.MOG_BattleResult)) return;
    const alias = BattleManager.getResultData;
    BattleManager.getResultData = function() {
      alias.apply(this, arguments);
      /** Unknown, Item, Weapon, and Armor drop counts, indexed by type & ID. */
      const count = $gameTemp._bResult[3].reduce(function(a, c) {
        const t = typeId[c];
        a[t][c.id] = (a[t][c.id] || 0) + 1;
        return a;
      }, [{},{},{},{}]);
      // Modify item name to change Mog's drop reward display.
      $gameTemp._bResult[3] = $gameTemp._bResult[3].filter(function(item, n, arr) {
        return arr.indexOf(item) === n;  // filter out duplicate references
      }).map(function(item) {
        var o = Object.assign({}, item);  // shallow clone to avoid mutating database
        o.name = text(item, count);
        return o;
      });
    };
  })();

})();
I did a quick test of the core script part of this and it seemed OK. 👋

Alternatively:
i didnt know you could have string indices until a few days ago
An array is a kind of object designed for use with number-type indices. All objects permit bracket notation for accessing properties, e.g. obj["propName"]. However, a simple for...of on an array will only iterate its numeric indices, e.g.
JavaScript:
var a = [1,2,3];
a["hi"] = 4;
for (var x of a) console.log(x);  // 1 2 3
So with your method I believe you'd also need to change how it iterates the drops, e.g.
JavaScript:
for (var x of Object.values(a)) console.log(x);  // 1 2 3 4
The V3 code seems to have fixed the issue for the core script part. Thanks!

I have no idea what some of it does, but I tested in 3 different projects. Plus ran JSHint on it with no more than the usual complaints about undefined variables from the core scripts. And I guess the leading semicolon is a defensive measure.
Great! I've added a link to this thread to my post in the previous thread. 👋

Original thread: https://forums.rpgmakerweb.com/threads/revisit-show-dropped-loot-count-instead-of-each-individual-items-for-counting-bug.174569/