zedwick / VillagerItemCollector

A Minecraft datapack which allows villagers to pick up items when the gamerule mobGriefing is set to false.
GNU General Public License v3.0
2 stars 2 forks source link

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

Closed zedwick closed 5 years ago

zedwick commented 5 years ago

https://github.com/zedwick/VillagerItemCollector/blob/e9c7010b1a76e0cff29b3d25c57d04db9dba44b2/VillagerItemCollector/data/villager_item_collector/functions/villager_food_item_carrot.mcfunction#L12

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.

Dax23333 commented 5 years ago

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.