#giving an item stack nbt

10 messages · Page 1 of 1 (latest)

rigid steeple
#

so, i want to detect if a player used a water bottle on a custom block, but i have no idea how to make it so the item stack for potion has nbt for water bottle

lyric wren
rigid steeple
#

yeah but won't that react to every type of potion?

lyric wren
#

yes, you are right, i'm getting the Potion component type

#
    ItemStack item = player.getMainHandStack();
    PotionContentsComponent potionContentsComponent = item.getOrDefault(DataComponentTypes.POTION_CONTENTS, PotionContentsComponent.DEFAULT);
    if (item != null && item.getItem() instanceof PotionItem && !potionContentsComponent.hasEffects()) {
        System.out.println("Water bottle used");
    }
    return ActionResult.PASS;
});```
rigid steeple
#

thank you very much, i'll try that out

lyric wren
# rigid steeple thank you very much, i'll try that out

Simple way I think but only checks for Water Bottle only, not Mundane, Awkward etc

    ItemStack item = player.getMainHandStack();
    PotionContentsComponent potionContentsComponent = item.get(DataComponentTypes.POTION_CONTENTS);
    if (potionContentsComponent != null && potionContentsComponent.matches(Potions.WATER)) {
        System.out.println("Water bottle used");
    }
    return ActionResult.PASS;
});```
rigid steeple
#

and what about when i would like to give the player a water potion too? how would that work?

lyric wren
#

you mean how to create a water bottle stack?

lyric wren
#
    ItemStack itemStack = new ItemStack(Items.POTION);
    itemStack.set(DataComponentTypes.POTION_CONTENTS, new PotionContentsComponent(Potions.WATER));
    return itemStack;
}```