Search in sources :

Example 1 with CompoundTag

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

the class FluidHandlerItemStackSimple method setFluid.

protected void setFluid(FluidStack fluid) {
    if (!container.hasTag()) {
        container.setTag(new CompoundTag());
    }
    CompoundTag fluidTag = new CompoundTag();
    fluid.writeToNBT(fluidTag);
    container.getTag().put(FLUID_NBT_KEY, fluidTag);
}
Also used : CompoundTag(net.minecraft.nbt.CompoundTag)

Example 2 with CompoundTag

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

the class ItemStackHandler method serializeNBT.

@Override
public CompoundTag serializeNBT() {
    ListTag nbtTagList = new ListTag();
    for (int i = 0; i < stacks.size(); i++) {
        if (!stacks.get(i).isEmpty()) {
            CompoundTag itemTag = new CompoundTag();
            itemTag.putInt("Slot", i);
            stacks.get(i).save(itemTag);
            nbtTagList.add(itemTag);
        }
    }
    CompoundTag nbt = new CompoundTag();
    nbt.put("Items", nbtTagList);
    nbt.putInt("Size", stacks.size());
    return nbt;
}
Also used : ListTag(net.minecraft.nbt.ListTag) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 3 with CompoundTag

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

the class ItemStackHandler method deserializeNBT.

@Override
public void deserializeNBT(CompoundTag nbt) {
    setSize(nbt.contains("Size", Tag.TAG_INT) ? nbt.getInt("Size") : stacks.size());
    ListTag tagList = nbt.getList("Items", Tag.TAG_COMPOUND);
    for (int i = 0; i < tagList.size(); i++) {
        CompoundTag itemTags = tagList.getCompound(i);
        int slot = itemTags.getInt("Slot");
        if (slot >= 0 && slot < stacks.size()) {
            stacks.set(slot, ItemStack.of(itemTags));
        }
    }
    onLoad();
}
Also used : ListTag(net.minecraft.nbt.ListTag) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 4 with CompoundTag

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

the class FluidHandlerItemStack method setFluid.

protected void setFluid(FluidStack fluid) {
    if (!container.hasTag()) {
        container.setTag(new CompoundTag());
    }
    CompoundTag fluidTag = new CompoundTag();
    fluid.writeToNBT(fluidTag);
    container.getTag().put(FLUID_NBT_KEY, fluidTag);
}
Also used : CompoundTag(net.minecraft.nbt.CompoundTag)

Example 5 with CompoundTag

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

the class ForgeChunkManager method writeForcedChunkOwners.

private static <T extends Comparable<? super T>> void writeForcedChunkOwners(Map<String, Long2ObjectMap<CompoundTag>> forcedEntries, Map<TicketOwner<T>, LongSet> forcedChunks, String listKey, int listType, BiConsumer<T, ListTag> ownerWriter) {
    for (Map.Entry<TicketOwner<T>, LongSet> entry : forcedChunks.entrySet()) {
        Long2ObjectMap<CompoundTag> modForced = forcedEntries.computeIfAbsent(entry.getKey().modId, modId -> new Long2ObjectOpenHashMap<>());
        for (long chunk : entry.getValue()) {
            CompoundTag modEntry = modForced.computeIfAbsent(chunk, chunkPos -> {
                CompoundTag baseEntry = new CompoundTag();
                baseEntry.putLong("Chunk", chunkPos);
                return baseEntry;
            });
            ListTag ownerList = modEntry.getList(listKey, listType);
            ownerWriter.accept(entry.getKey().owner, ownerList);
            // Note: As getList returns a new list in the case the data is of the wrong type,
            // we need to mimic was vanilla does in various places and put our list back in
            // the CompoundNBT regardless.
            modEntry.put(listKey, ownerList);
        }
    }
}
Also used : LongSet(it.unimi.dsi.fastutil.longs.LongSet) HashMap(java.util.HashMap) Long2ObjectOpenHashMap(it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap) Map(java.util.Map) Long2ObjectMap(it.unimi.dsi.fastutil.longs.Long2ObjectMap) ListTag(net.minecraft.nbt.ListTag) CompoundTag(net.minecraft.nbt.CompoundTag)

Aggregations

CompoundTag (net.minecraft.nbt.CompoundTag)112 ListTag (net.minecraft.nbt.ListTag)33 ItemStack (net.minecraft.world.item.ItemStack)17 TagCompound (de.keyle.knbt.TagCompound)12 ResourceLocation (net.minecraft.resources.ResourceLocation)10 Nullable (org.checkerframework.checker.nullness.qual.Nullable)10 Keys (org.spongepowered.api.data.Keys)9 DataProviderRegistrator (org.spongepowered.common.data.provider.DataProviderRegistrator)9 Constants (org.spongepowered.common.util.Constants)9 TagList (de.keyle.knbt.TagList)8 List (java.util.List)8 IntArrayTag (net.minecraft.nbt.IntArrayTag)8 Tag (net.minecraft.nbt.Tag)8 ResourceKey (org.spongepowered.api.ResourceKey)8 DataContainer (org.spongepowered.api.data.persistence.DataContainer)8 DataView (org.spongepowered.api.data.persistence.DataView)8 ArrayList (java.util.ArrayList)7 ByteArrayTag (net.minecraft.nbt.ByteArrayTag)7 TagString (de.keyle.knbt.TagString)6 StringTag (net.minecraft.nbt.StringTag)5