Search in sources :

Example 41 with ListTag

use of net.minecraft.nbt.ListTag 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)

Example 42 with ListTag

use of net.minecraft.nbt.ListTag in project SpongeCommon by SpongePowered.

the class BlockTypeItemStackData method get.

// @formatter:on
private static Set<BlockType> get(final ItemStack stack, final String nbtKey) {
    final CompoundTag tag = stack.getTag();
    if (tag == null) {
        return null;
    }
    final ListTag list = tag.getList(nbtKey, Constants.NBT.TAG_STRING);
    if (list.isEmpty()) {
        return null;
    }
    return NBTStreams.toStrings(list).map(ResourceLocation::tryParse).filter(Objects::nonNull).map(key -> (BlockType) Registry.BLOCK.getOptional(key).orElse(null)).filter(Objects::nonNull).collect(Collectors.toSet());
}
Also used : ResourceLocation(net.minecraft.resources.ResourceLocation) ListTag(net.minecraft.nbt.ListTag) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 43 with ListTag

use of net.minecraft.nbt.ListTag in project MinecraftForge by MinecraftForge.

the class IForgeMobEffectInstance method writeCurativeItems.

default void writeCurativeItems(CompoundTag nbt) {
    ListTag list = new ListTag();
    getCurativeItems().forEach(s -> list.add(s.save(new CompoundTag())));
    nbt.put("CurativeItems", list);
}
Also used : ListTag(net.minecraft.nbt.ListTag) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 44 with ListTag

use of net.minecraft.nbt.ListTag in project Denizen-For-Bukkit by DenizenScript.

the class ItemHelperImpl method getLore.

@Override
public List<String> getLore(ItemTag item) {
    if (!item.getItemMeta().hasLore()) {
        return null;
    }
    net.minecraft.world.item.ItemStack nmsItemStack = CraftItemStack.asNMSCopy(item.getItemStack());
    ListTag list = ((net.minecraft.nbt.CompoundTag) nmsItemStack.getTag().get("display")).getList("Lore", 8);
    List<String> outList = new ArrayList<>();
    for (int i = 0; i < list.size(); i++) {
        BaseComponent[] lineComponent = ComponentSerializer.parse(list.getString(i));
        outList.add(FormattedTextHelper.stringify(lineComponent, ChatColor.WHITE));
    }
    return outList;
}
Also used : BaseComponent(net.md_5.bungee.api.chat.BaseComponent) ListTag(net.minecraft.nbt.ListTag)

Example 45 with ListTag

use of net.minecraft.nbt.ListTag in project Denizen-For-Bukkit by DenizenScript.

the class ItemHelperImpl method setLore.

@Override
public void setLore(ItemTag item, List<String> lore) {
    net.minecraft.world.item.ItemStack nmsItemStack = CraftItemStack.asNMSCopy(item.getItemStack());
    net.minecraft.nbt.CompoundTag tag = nmsItemStack.getOrCreateTag();
    net.minecraft.nbt.CompoundTag display = tag.getCompound("display");
    if (!tag.contains("display")) {
        tag.put("display", display);
    }
    if (lore == null || lore.isEmpty()) {
        display.put("Lore", null);
    } else {
        ListTag tagList = new ListTag();
        for (String line : lore) {
            tagList.add(net.minecraft.nbt.StringTag.valueOf(ComponentSerializer.toString(FormattedTextHelper.parse(line, ChatColor.WHITE))));
        }
        display.put("Lore", tagList);
    }
    item.setItemStack(CraftItemStack.asBukkitCopy(nmsItemStack));
}
Also used : com.denizenscript.denizen.nms.util.jnbt(com.denizenscript.denizen.nms.util.jnbt) ListTag(net.minecraft.nbt.ListTag)

Aggregations

ListTag (net.minecraft.nbt.ListTag)52 CompoundTag (net.minecraft.nbt.CompoundTag)38 ItemStack (net.minecraft.world.item.ItemStack)10 TagCompound (de.keyle.knbt.TagCompound)8 TagList (de.keyle.knbt.TagList)8 List (java.util.List)7 IntArrayTag (net.minecraft.nbt.IntArrayTag)7 ResourceLocation (net.minecraft.resources.ResourceLocation)7 TagString (de.keyle.knbt.TagString)6 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)5 StringTag (net.minecraft.nbt.StringTag)5 ByteArrayTag (net.minecraft.nbt.ByteArrayTag)4 IntTag (net.minecraft.nbt.IntTag)4 Tag (net.minecraft.nbt.Tag)4 ByteTag (net.minecraft.nbt.ByteTag)3 DoubleTag (net.minecraft.nbt.DoubleTag)3 FloatTag (net.minecraft.nbt.FloatTag)3 LongTag (net.minecraft.nbt.LongTag)3 ShortTag (net.minecraft.nbt.ShortTag)3