Search in sources :

Example 16 with ListTag

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

the class BlockTypeItemStackData method set.

private static boolean set(final ItemStack stack, final String nbtKey, final Set<? extends BlockType> value) {
    if (value.isEmpty()) {
        stack.removeTagKey(nbtKey);
        return true;
    }
    final CompoundTag tag = stack.getOrCreateTag();
    final ListTag list = value.stream().map(type -> Registry.BLOCK.getKey((Block) type).toString()).collect(NBTCollectors.toStringTagList());
    tag.put(nbtKey, list);
    return true;
}
Also used : ResourceLocation(net.minecraft.resources.ResourceLocation) Constants(org.spongepowered.common.util.Constants) Set(java.util.Set) NBTStreams(org.spongepowered.common.util.NBTStreams) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) Registry(net.minecraft.core.Registry) Keys(org.spongepowered.api.data.Keys) CompoundTag(net.minecraft.nbt.CompoundTag) NBTCollectors(org.spongepowered.common.util.NBTCollectors) DataProviderRegistrator(org.spongepowered.common.data.provider.DataProviderRegistrator) BlockType(org.spongepowered.api.block.BlockType) Block(net.minecraft.world.level.block.Block) ItemStack(net.minecraft.world.item.ItemStack) ListTag(net.minecraft.nbt.ListTag) Block(net.minecraft.world.level.block.Block) ListTag(net.minecraft.nbt.ListTag) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 17 with ListTag

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

the class BookPagesItemStackData method get.

// @formatter:on
private static List<Enchantment> get(final ItemStack holder, final String nbtKey) {
    final CompoundTag tag = holder.getTag();
    if (tag == null || !tag.contains(nbtKey, Constants.NBT.TAG_LIST)) {
        return new ArrayList<>();
    }
    final ListTag list = tag.getList(nbtKey, Constants.NBT.TAG_COMPOUND);
    return NBTStreams.toCompounds(list).map(BookPagesItemStackData::enchantmentFromNbt).filter(Objects::nonNull).collect(Collectors.toList());
}
Also used : ArrayList(java.util.ArrayList) ListTag(net.minecraft.nbt.ListTag) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 18 with ListTag

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

the class NBTTranslator method setInternal.

@SuppressWarnings({ "unchecked", "rawtypes" })
private static void setInternal(Tag base, byte type, DataView view, String key) {
    checkNotNull(base);
    checkNotNull(view);
    checkNotNull(key);
    checkArgument(!key.isEmpty());
    checkArgument(type > Constants.NBT.TAG_END && type <= Constants.NBT.TAG_INT_ARRAY);
    switch(type) {
        case Constants.NBT.TAG_BYTE:
            if (key.contains(NBTTranslator.BOOLEAN_IDENTIFIER)) {
                view.set(of(key.replace(NBTTranslator.BOOLEAN_IDENTIFIER, "")), (((ByteTag) base).getAsByte() != 0));
            } else {
                view.set(of(key), ((ByteTag) base).getAsByte());
            }
            break;
        case Constants.NBT.TAG_SHORT:
            view.set(of(key), ((ShortTag) base).getAsShort());
            break;
        case Constants.NBT.TAG_INT:
            view.set(of(key), ((IntTag) base).getAsInt());
            break;
        case Constants.NBT.TAG_LONG:
            view.set(of(key), ((LongTag) base).getAsLong());
            break;
        case Constants.NBT.TAG_FLOAT:
            view.set(of(key), ((FloatTag) base).getAsFloat());
            break;
        case Constants.NBT.TAG_DOUBLE:
            view.set(of(key), ((DoubleTag) base).getAsDouble());
            break;
        case Constants.NBT.TAG_BYTE_ARRAY:
            view.set(of(key), ((ByteArrayTag) base).getAsByteArray());
            break;
        case Constants.NBT.TAG_STRING:
            view.set(of(key), base.getAsString());
            break;
        case Constants.NBT.TAG_LIST:
            ListTag list = (ListTag) base;
            byte listType = list.getElementType();
            int count = list.size();
            List objectList = Lists.newArrayListWithCapacity(count);
            for (final Tag inbt : list) {
                objectList.add(NBTTranslator.fromTagBase(inbt, listType));
            }
            view.set(of(key), objectList);
            break;
        case Constants.NBT.TAG_COMPOUND:
            DataView internalView = view.createView(of(key));
            CompoundTag compound = (CompoundTag) base;
            for (String internalKey : compound.getAllKeys()) {
                Tag internalBase = compound.get(internalKey);
                byte internalType = internalBase.getId();
                // Basically.... more recursion.
                // Reasoning: This avoids creating a new DataContainer which would
                // then be copied in to the owning DataView anyways. We can internally
                // set the actual data directly to the child view instead.
                NBTTranslator.setInternal(internalBase, internalType, internalView, internalKey);
            }
            break;
        case Constants.NBT.TAG_INT_ARRAY:
            view.set(of(key), ((IntArrayTag) base).getAsIntArray());
            break;
        case Constants.NBT.TAG_LONG_ARRAY:
            view.set(of(key), ((LongArrayTag) base).getAsLongArray());
            break;
        default:
            throw new IllegalArgumentException("Unknown NBT type " + type);
    }
}
Also used : DataView(org.spongepowered.api.data.persistence.DataView) List(java.util.List) LongArrayTag(net.minecraft.nbt.LongArrayTag) DoubleTag(net.minecraft.nbt.DoubleTag) Tag(net.minecraft.nbt.Tag) StringTag(net.minecraft.nbt.StringTag) ByteTag(net.minecraft.nbt.ByteTag) IntTag(net.minecraft.nbt.IntTag) IntArrayTag(net.minecraft.nbt.IntArrayTag) FloatTag(net.minecraft.nbt.FloatTag) ByteArrayTag(net.minecraft.nbt.ByteArrayTag) LongTag(net.minecraft.nbt.LongTag) ShortTag(net.minecraft.nbt.ShortTag) CompoundTag(net.minecraft.nbt.CompoundTag) ListTag(net.minecraft.nbt.ListTag) ListTag(net.minecraft.nbt.ListTag) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 19 with ListTag

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

the class NBTTranslator method fromTagBase.

@SuppressWarnings({ "unchecked", "rawtypes" })
private static Object fromTagBase(Tag base, byte type) {
    switch(type) {
        case Constants.NBT.TAG_BYTE:
            return ((ByteTag) base).getAsByte();
        case Constants.NBT.TAG_SHORT:
            return (((ShortTag) base)).getAsShort();
        case Constants.NBT.TAG_INT:
            return ((IntTag) base).getAsInt();
        case Constants.NBT.TAG_LONG:
            return ((LongTag) base).getAsLong();
        case Constants.NBT.TAG_FLOAT:
            return ((FloatTag) base).getAsFloat();
        case Constants.NBT.TAG_DOUBLE:
            return ((DoubleTag) base).getAsDouble();
        case Constants.NBT.TAG_BYTE_ARRAY:
            return ((ByteArrayTag) base).getAsByteArray();
        case Constants.NBT.TAG_STRING:
            return base.getAsString();
        case Constants.NBT.TAG_LIST:
            ListTag list = (ListTag) base;
            byte listType = list.getElementType();
            int count = list.size();
            List objectList = Lists.newArrayListWithCapacity(count);
            for (Tag inbt : list) {
                objectList.add(NBTTranslator.fromTagBase(inbt, listType));
            }
            return objectList;
        case Constants.NBT.TAG_COMPOUND:
            return NBTTranslator.getViewFromCompound((CompoundTag) base);
        case Constants.NBT.TAG_INT_ARRAY:
            return ((IntArrayTag) base).getAsIntArray();
        case Constants.NBT.TAG_LONG_ARRAY:
            return ((LongArrayTag) base).getAsLongArray();
        default:
            return null;
    }
}
Also used : IntArrayTag(net.minecraft.nbt.IntArrayTag) DoubleTag(net.minecraft.nbt.DoubleTag) ListTag(net.minecraft.nbt.ListTag) FloatTag(net.minecraft.nbt.FloatTag) ByteTag(net.minecraft.nbt.ByteTag) LongArrayTag(net.minecraft.nbt.LongArrayTag) List(java.util.List) LongArrayTag(net.minecraft.nbt.LongArrayTag) DoubleTag(net.minecraft.nbt.DoubleTag) Tag(net.minecraft.nbt.Tag) StringTag(net.minecraft.nbt.StringTag) ByteTag(net.minecraft.nbt.ByteTag) IntTag(net.minecraft.nbt.IntTag) IntArrayTag(net.minecraft.nbt.IntArrayTag) FloatTag(net.minecraft.nbt.FloatTag) ByteArrayTag(net.minecraft.nbt.ByteArrayTag) LongTag(net.minecraft.nbt.LongTag) ShortTag(net.minecraft.nbt.ShortTag) CompoundTag(net.minecraft.nbt.CompoundTag) ListTag(net.minecraft.nbt.ListTag) IntTag(net.minecraft.nbt.IntTag) LongTag(net.minecraft.nbt.LongTag) ByteArrayTag(net.minecraft.nbt.ByteArrayTag)

Example 20 with ListTag

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

the class EntityKoaBase method readAdditionalSaveData.

@Override
public void readAdditionalSaveData(CompoundTag compound) {
    super.readAdditionalSaveData(compound);
    if (compound.contains("home_X")) {
        this.restrictTo(new BlockPos(compound.getInt("home_X"), compound.getInt("home_Y"), compound.getInt("home_Z")), MAX_HOME_DISTANCE);
    }
    if (compound.contains("fireplace_X")) {
        this.setFirelacePos(new BlockPos(compound.getInt("fireplace_X"), compound.getInt("fireplace_Y"), compound.getInt("fireplace_Z")));
    }
    lastTimeFished = compound.getLong("lastTimeFished");
    if (compound.contains("koa_inventory", 9)) {
        ListTag nbttaglist = compound.getList("koa_inventory", 10);
        for (int i = 0; i < nbttaglist.size(); ++i) {
            CompoundTag nbttagcompound = nbttaglist.getCompound(i);
            int j = nbttagcompound.getByte("Slot") & 255;
            this.inventory.setItem(j, ItemStack.of(nbttagcompound));
        }
    }
    this.villageID = compound.getInt("village_id");
    // backwards compat
    if (!compound.contains("village_dimension")) {
        this.villageDimension = level.dimension();
    } else {
        this.villageDimension = ResourceKey.create(Registry.DIMENSION_REGISTRY, new ResourceLocation(compound.getString("village_dim_id")));
    }
    if (compound.contains("role_id")) {
        this.getEntityData().set(ROLE, compound.getInt("role_id"));
    } else {
        rollDiceRole();
    }
    if (compound.contains("gender_id")) {
        this.getEntityData().set(GENDER, compound.getInt("gender_id"));
    } else {
        rollDiceGender();
    }
    this.lastTradeTime = compound.getLong("lastTradeTime");
    for (int i = 0; i < MAX_DRUMS; i++) {
        if (compound.contains("drum_" + i + "_X")) {
            this.listPosDrums.add(new BlockPos(compound.getInt("drum_" + i + "_X"), compound.getInt("drum_" + i + "_Y"), compound.getInt("drum_" + i + "_Z")));
        }
    }
    druggedTime = compound.getInt("druggedTime");
    updateUniqueEntityAI();
}
Also used : ResourceLocation(net.minecraft.resources.ResourceLocation) BlockPos(net.minecraft.core.BlockPos) ListTag(net.minecraft.nbt.ListTag) CompoundTag(net.minecraft.nbt.CompoundTag)

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