zedwick / VillagerItemCollector

A Minecraft datapack which allows villagers to pick up items when the gamerule mobGriefing is set to false.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Accessing Inventory[1] does not always return slot villager.1 if it does not yet have an item in it.

zedwick opened this issue · comments

execute as @e[tag=vic_current] store result score @s vic_carrotc run data get entity @s Inventory[1].Count 1

The NBT path Inventory returns an ordered array of items in the villager's inventory, but as it does not return empty slots, accessing an item via Inventory[1] will not consistently return the item in slot 1.

[23:28:31] [main/INFO]: [CHAT] [Carrot] Pre
[23:28:31] [main/INFO]: [CHAT] Villager has [{id:"minecraft:bread",Count:52b},{id:"minecraft:potato",Count:3b},{id:"minecraft:beetroot",Count:4b}] and item is {id:"minecraft:carrot",Count:19b}
[23:28:31] [main/INFO]: [CHAT] [Carrot] Post
[23:28:31] [main/INFO]: [CHAT] Villager has [{id:"minecraft:bread",Count:52b},{id:"minecraft:carrot",Count:22b},{id:"minecraft:potato",Count:3b},{id:"minecraft:beetroot",Count:4b}] and item is

In the above example, the villager had no carrots in slot 1, and 3 potato in slot 2. Inventory[1] returned the potato in slot 2 (Count:3b) and added that up with the carrots from the item stack (Count:19b), and finally added 22 carrots to slot 1.

There does not seem to be an alternative for reading the item from a specific slot.

May need to check all items in the Inventory to see if any match the item type we are looking for.

Fixed by changing item counting to use Inventory[{"id":"minecraft:potato"}].Count rather than Inventory[2].

Counts number of a paticular item type rather than the number of items in a 'specific' slot.