Search in sources :

Example 1 with PotionType

use of org.spongepowered.api.item.potion.PotionType in project Sponge by SpongePowered.

the class PotionItemStackData method register.

// @formatter:off
public static void register(final DataProviderRegistrator registrator) {
    registrator.asMutable(ItemStack.class).create(Keys.COLOR).get(h -> Color.ofRgb(PotionUtils.getColor(h))).set((h, v) -> {
        final CompoundTag tag = h.getOrCreateTag();
        tag.putInt(Constants.Item.CUSTOM_POTION_COLOR, v.rgb());
    }).delete(h -> h.removeTagKey(Constants.Item.CUSTOM_POTION_COLOR)).supports(h -> h.getItem() == Items.POTION || h.getItem() == Items.SPLASH_POTION || h.getItem() == Items.LINGERING_POTION).create(Keys.POTION_EFFECTS).get(h -> {
        final List<MobEffectInstance> effects = PotionUtils.getMobEffects(h);
        return effects.isEmpty() ? null : ImmutableList.copyOf((List<PotionEffect>) (Object) effects);
    }).set((h, v) -> {
        final CompoundTag tag = h.getOrCreateTag();
        final ListTag list = v.stream().map(effect -> {
            final CompoundTag potionTag = new CompoundTag();
            ((MobEffectInstance) effect).save(potionTag);
            return potionTag;
        }).collect(NBTCollectors.toTagList());
        tag.put(Constants.Item.CUSTOM_POTION_EFFECTS, list);
    }).delete(h -> h.removeTagKey(Constants.Item.CUSTOM_POTION_EFFECTS)).supports(h -> h.getItem() == Items.POTION || h.getItem() == Items.SPLASH_POTION || h.getItem() == Items.LINGERING_POTION || h.getItem() == Items.TIPPED_ARROW).create(Keys.POTION_TYPE).get(h -> (PotionType) PotionUtils.getPotion(h)).set((h, v) -> {
        h.getOrCreateTag();
        PotionUtils.setPotion(h, (Potion) v);
    }).delete(h -> {
        if (h.hasTag()) {
            PotionUtils.setPotion(h, Potions.EMPTY);
        }
    }).supports(h -> h.getItem() == Items.POTION || h.getItem() == Items.SPLASH_POTION || h.getItem() == Items.LINGERING_POTION || h.getItem() == Items.TIPPED_ARROW);
}
Also used : PotionUtils(net.minecraft.world.item.alchemy.PotionUtils) Items(net.minecraft.world.item.Items) Constants(org.spongepowered.common.util.Constants) MobEffectInstance(net.minecraft.world.effect.MobEffectInstance) Potions(net.minecraft.world.item.alchemy.Potions) Potion(net.minecraft.world.item.alchemy.Potion) Keys(org.spongepowered.api.data.Keys) List(java.util.List) CompoundTag(net.minecraft.nbt.CompoundTag) ImmutableList(com.google.common.collect.ImmutableList) NBTCollectors(org.spongepowered.common.util.NBTCollectors) DataProviderRegistrator(org.spongepowered.common.data.provider.DataProviderRegistrator) PotionEffect(org.spongepowered.api.effect.potion.PotionEffect) Color(org.spongepowered.api.util.Color) ItemStack(net.minecraft.world.item.ItemStack) ListTag(net.minecraft.nbt.ListTag) PotionType(org.spongepowered.api.item.potion.PotionType) MobEffectInstance(net.minecraft.world.effect.MobEffectInstance) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) PotionType(org.spongepowered.api.item.potion.PotionType) ItemStack(net.minecraft.world.item.ItemStack) ListTag(net.minecraft.nbt.ListTag) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 2 with PotionType

use of org.spongepowered.api.item.potion.PotionType in project SpongeCommon by SpongePowered.

the class PotionItemStackData method register.

// @formatter:off
public static void register(final DataProviderRegistrator registrator) {
    registrator.asMutable(ItemStack.class).create(Keys.COLOR).get(h -> Color.ofRgb(PotionUtils.getColor(h))).set((h, v) -> {
        final CompoundTag tag = h.getOrCreateTag();
        tag.putInt(Constants.Item.CUSTOM_POTION_COLOR, v.rgb());
    }).delete(h -> h.removeTagKey(Constants.Item.CUSTOM_POTION_COLOR)).supports(h -> h.getItem() == Items.POTION || h.getItem() == Items.SPLASH_POTION || h.getItem() == Items.LINGERING_POTION).create(Keys.POTION_EFFECTS).get(h -> {
        final List<MobEffectInstance> effects = PotionUtils.getMobEffects(h);
        return effects.isEmpty() ? null : ImmutableList.copyOf((List<PotionEffect>) (Object) effects);
    }).set((h, v) -> {
        final CompoundTag tag = h.getOrCreateTag();
        final ListTag list = v.stream().map(effect -> {
            final CompoundTag potionTag = new CompoundTag();
            ((MobEffectInstance) effect).save(potionTag);
            return potionTag;
        }).collect(NBTCollectors.toTagList());
        tag.put(Constants.Item.CUSTOM_POTION_EFFECTS, list);
    }).delete(h -> h.removeTagKey(Constants.Item.CUSTOM_POTION_EFFECTS)).supports(h -> h.getItem() == Items.POTION || h.getItem() == Items.SPLASH_POTION || h.getItem() == Items.LINGERING_POTION || h.getItem() == Items.TIPPED_ARROW).create(Keys.POTION_TYPE).get(h -> (PotionType) PotionUtils.getPotion(h)).set((h, v) -> {
        h.getOrCreateTag();
        PotionUtils.setPotion(h, (Potion) v);
    }).delete(h -> {
        if (h.hasTag()) {
            PotionUtils.setPotion(h, Potions.EMPTY);
        }
    }).supports(h -> h.getItem() == Items.POTION || h.getItem() == Items.SPLASH_POTION || h.getItem() == Items.LINGERING_POTION || h.getItem() == Items.TIPPED_ARROW);
}
Also used : PotionUtils(net.minecraft.world.item.alchemy.PotionUtils) Items(net.minecraft.world.item.Items) Constants(org.spongepowered.common.util.Constants) MobEffectInstance(net.minecraft.world.effect.MobEffectInstance) Potions(net.minecraft.world.item.alchemy.Potions) Potion(net.minecraft.world.item.alchemy.Potion) Keys(org.spongepowered.api.data.Keys) List(java.util.List) CompoundTag(net.minecraft.nbt.CompoundTag) ImmutableList(com.google.common.collect.ImmutableList) NBTCollectors(org.spongepowered.common.util.NBTCollectors) DataProviderRegistrator(org.spongepowered.common.data.provider.DataProviderRegistrator) PotionEffect(org.spongepowered.api.effect.potion.PotionEffect) Color(org.spongepowered.api.util.Color) ItemStack(net.minecraft.world.item.ItemStack) ListTag(net.minecraft.nbt.ListTag) PotionType(org.spongepowered.api.item.potion.PotionType) MobEffectInstance(net.minecraft.world.effect.MobEffectInstance) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) PotionType(org.spongepowered.api.item.potion.PotionType) ItemStack(net.minecraft.world.item.ItemStack) ListTag(net.minecraft.nbt.ListTag) CompoundTag(net.minecraft.nbt.CompoundTag)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)2 List (java.util.List)2 CompoundTag (net.minecraft.nbt.CompoundTag)2 ListTag (net.minecraft.nbt.ListTag)2 MobEffectInstance (net.minecraft.world.effect.MobEffectInstance)2 ItemStack (net.minecraft.world.item.ItemStack)2 Items (net.minecraft.world.item.Items)2 Potion (net.minecraft.world.item.alchemy.Potion)2 PotionUtils (net.minecraft.world.item.alchemy.PotionUtils)2 Potions (net.minecraft.world.item.alchemy.Potions)2 Keys (org.spongepowered.api.data.Keys)2 PotionEffect (org.spongepowered.api.effect.potion.PotionEffect)2 PotionType (org.spongepowered.api.item.potion.PotionType)2 Color (org.spongepowered.api.util.Color)2 DataProviderRegistrator (org.spongepowered.common.data.provider.DataProviderRegistrator)2 Constants (org.spongepowered.common.util.Constants)2 NBTCollectors (org.spongepowered.common.util.NBTCollectors)2