MV Yanfly Core Engine - Max Item note tag doesn't account for equipped items when buying from the shop.

Hello!

So, in my game the party is comprised of a single hero with 6 accessories able to be equipped. For balance reasons, the player is only able to earn 3 of any accessory in the game.
For the following example, for ease, I'll simply call the accessory "Ribbon."

My ribbon has the note tag <Max Item: 3>

The notetag works perfectly in shops, only allowing me to buy 3 Ribbons.
HOWEVER, if I equip all 3 Ribbons, and go back into the store, I can buy another 3 Ribbons.
I understand this is likely due to the plugin not including equipment when considering max items.
I found another thread with a similar issue, and the proposed code was as follows:

Game_Party.prototype.numItems = function(item)
{
var container = this.itemContainer(item);
let numHeld = container ? container[item.id] || 0 : 0;
if (container == this._weapons)
$gameActors._data.forEach(actor =>
{
if (actor)
actor.weapons().forEach(weapon => {if (weapon.id == item.id || weapon.baseItemId && weapon.baseItemId == item.id) numHeld++;});
});
else if (container == this._armors)
$gameActors._data.forEach(actor =>
{
if (actor)
actor.armors().forEach(armor=> {if (armor.id == item.id || armor.baseItemId && armor.baseItemId == item.id) numHeld++;});
});
return numHeld;
};

Whilst this code works in the shop preventing me from owning more than 3, it presents a new unique issue - it resulted in infinite ribbons. If I equipped one, my number of ribbons would actually go up, which makes sense looking at the code. If I had 3 ribbons equipped, and 3 in my inventory, my total ribbons in my inventory would appear as 6, essentially giving me 9 ribbons.

My question is essentially, how can I make the shop follow the max item cap including the equipment my actor has equipped? Or is there a way to modify this code to ONLY be considered when inside the shop?

Any suggestions at all would be immensely appreciated, thank you!
This thread was moved from RPG Maker MV Support to JavaScript/Plugin Support.
I understand this is likely due to the plugin not including equipment when considering max items.
thats not the plugin. thats the base engine iirc. items when theyre equipped are removed from your inventory. when theyre unequipped theyre given back.

My question is essentially, how can I make the shop follow the max item cap including the equipment my actor has equipped? Or is there a way to modify this code to ONLY be considered when inside the shop?
while its possible, have you considered using a plugin that restricts the player to only buying 3 ribbons in the first place like hime's shop manager?
thats not the plugin. thats the base engine iirc. items when theyre equipped are removed from your inventory. when theyre unequipped theyre given back.


while its possible, have you considered using a plugin that restricts the player to only buying 3 ribbons in the first place like hime's shop manager?
I've already installed and tried the plugin but the help text didn't make it clear to me how to achieve this - additionally the player is capable of receiving the items in the store anyway, so simply capping the stock to 3 doesn't fix the issue if the player has 3 equipped anyway 🙂
Hmmm, can't you just limit how many ribbons the player can equip with Yanfly Equip Requirements? I think it's like:

<Custom Equip Requirement Condition>
let count = 0;
user.armors().forEach(function countArmors(armor){if(armor.id == 4){count++;}});
if (count < 3) {
condition = true;
} else {
condition = false;
}
</Custom Equip Requirement Condition>

EDIT: Replace that '4' with the id of your ribbons.
I've already installed and tried the plugin but the help text didn't make it clear to me how to achieve this
the website says that it should be automatic if you use the Shop.open() command before the shop processing command and supply the command with the shop's name, but i havent tried it.
additionally the player is capable of receiving the items in the store anyway, so simply capping the stock to 3 doesn't fix the issue if the player has 3 equipped anyway
did you mean the player can obtain them outside of the shop?

then you'd probably find more luck going with this approach
make the shop follow the max item cap including the equipment my actor has equipped
the way i see it, It'd mean undoing the Game_Party.numItems() change and moving the logic to check actor equipment into Scene_Shop.maxBuy(). i wont be able to actually look at it till much later
Hmmm, can't you just limit how many ribbons the player can equip with Yanfly Equip Requirements? I think it's like:

<Custom Equip Requirement Condition>
let count = 0;
user.armors().forEach(function countArmors(armor){if(armor.id == 4){count++;}});
if (count < 3) {
condition = true;
} else {
condition = false;
}
</Custom Equip Requirement Condition>

EDIT: Replace that '4' with the id of your ribbons.
I could, except I currently have 70 accessories and am likely to have roughly 400 by the end of the game, so if there's a more optimal method I'd be inclined to go torwards that - I also would feel bad having the player buy a 4th ribbon and then find it's completely useless

the website says that it should be automatic if you use the Shop.open() command before the shop processing command and supply the command with the shop's name, but i havent tried it.

did you mean the player can obtain them outside of the shop?

then you'd probably find more luck going with this approach

the way i see it, It'd mean undoing the Game_Party.numItems() change and moving the logic to check actor equipment into Scene_Shop.maxBuy(). i wont be able to actually look at it till much later
Indeed, the player can get most of the items from enemy drops anyway, or purchase from the shop if they'd rather do that.
here is a [fix for it though] as it was asked before.
here is a [fix for it though] as it was asked before.
Ah yeah so that's the code I referenced in my original post, and also what's caused the issues i'm facing (unless I pasted it into the wrong area ofc)
Ah yeah so that's the code I referenced in my original post, and also what's caused the issues i'm facing (unless I pasted it into the wrong area ofc)
When you're referencing something you got from another post, you should always link to that post - that way people know what you're talking about and you won't get duplicate effort/results.

As a side note, when you post code, use the CODE tags - the </> on the edit bar - so that it's legible and the forum doesn't eat your brackets.

Note that's explicitly how the OP of that thread wanted it to work - they wanted the number you see anyplace to reflect the total number of that item that you own, whether it's in your inventory, equipped, etc.

If you want to quickly modify that snippet so that it only applies in the shop scene, then you'd do this:
Code:
Game_Party.prototype.numItems = function(item)
{
    var container = this.itemContainer(item);
    let numHeld = container ? container[item.id] || 0 : 0;
    if (container == this._weapons && SceneManager._scene typeof Scene_Shop)
        $gameActors._data.forEach(actor =>
        {
             if (actor)
                actor.weapons().forEach(weapon => {if (weapon.id == item.id || weapon.baseItemId && weapon.baseItemId == item.id) numHeld++;});
        });
    else if (container == this._armors && SceneManager._scene typeof Scene_Shop)
        $gameActors._data.forEach(actor =>
        {
             if (actor)
                actor.armors().forEach(armor=> {if (armor.id == item.id || armor.baseItemId && armor.baseItemId == item.id) numHeld++;});
        });
    return numHeld;
};

I don't know if this shop scene also displays the number carried anyplace that this would mess with - if that's the case, then what @Robro33 suggested would be a more elegant fix to have it interface directly with the buying method.

But this should cause the shop to count weapons and armor equipped where looking at held items in the main menu will not.

(unless I pasted it into the wrong area ofc)
I don't know what you did, but there should be no "area" that you're pasting it into to have any possible error. Whenever you use little snippets of code like that, you copy the code, paste it into a text editor, save it as a *.js file, include it as a plugin.

In this case, make sure that new plugin is below the one that you're modifying in your plugin manager.
When you're referencing something you got from another post, you should always link to that post - that way people know what you're talking about and you won't get duplicate effort/results.

As a side note, when you post code, use the CODE tags - the </> on the edit bar - so that it's legible and the forum doesn't eat your brackets.

Note that's explicitly how the OP of that thread wanted it to work - they wanted the number you see anyplace to reflect the total number of that item that you own, whether it's in your inventory, equipped, etc.

If you want to quickly modify that snippet so that it only applies in the shop scene, then you'd do this:
Code:
Game_Party.prototype.numItems = function(item)
{
    var container = this.itemContainer(item);
    let numHeld = container ? container[item.id] || 0 : 0;
    if (container == this._weapons && SceneManager._scene typeof Scene_Shop)
        $gameActors._data.forEach(actor =>
        {
             if (actor)
                actor.weapons().forEach(weapon => {if (weapon.id == item.id || weapon.baseItemId && weapon.baseItemId == item.id) numHeld++;});
        });
    else if (container == this._armors && SceneManager._scene typeof Scene_Shop)
        $gameActors._data.forEach(actor =>
        {
             if (actor)
                actor.armors().forEach(armor=> {if (armor.id == item.id || armor.baseItemId && armor.baseItemId == item.id) numHeld++;});
        });
    return numHeld;
};

I don't know if this shop scene also displays the number carried anyplace that this would mess with - if that's the case, then what @Robro33 suggested would be a more elegant fix to have it interface directly with the buying method.

But this should cause the shop to count weapons and armor equipped where looking at held items in the main menu will not.


I don't know what you did, but there should be no "area" that you're pasting it into to have any possible error. Whenever you use little snippets of code like that, you copy the code, paste it into a text editor, save it as a *.js file, include it as a plugin.

In this case, make sure that new plugin is below the one that you're modifying in your plugin manager.
Thank you - good to know about the etiquette for forums, I'm incredibly new (start development last week) and have had a few issues during the process that have been a tad overwhelming. All of your patience is much appreciated.

This solution ended up working after some modifications, and I am eternally grateful for your help. Have a great rest of your day 🙂

Original thread: https://forums.rpgmakerweb.com/threads/yanfly-core-engine-max-item-note-tag-doesnt-account-for-equipped-items-when-buying-from-the-shop.177342/