Search in sources :

Example 1 with ListTag

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

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

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

Example 4 with ListTag

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

the class ForgeChunkManager method writeForgeForcedChunks.

/**
 * Writes the forge forced chunks into the NBT compound. Format is List{modid, List{ChunkPos, List{BlockPos}, List{UUID}}}
 *
 * @apiNote Internal
 */
public static void writeForgeForcedChunks(CompoundTag nbt, TicketTracker<BlockPos> blockForcedChunks, TicketTracker<UUID> entityForcedChunks) {
    if (!blockForcedChunks.isEmpty() || !entityForcedChunks.isEmpty()) {
        Map<String, Long2ObjectMap<CompoundTag>> forcedEntries = new HashMap<>();
        writeForcedChunkOwners(forcedEntries, blockForcedChunks, "Blocks", Tag.TAG_COMPOUND, (pos, forcedBlocks) -> forcedBlocks.add(NbtUtils.writeBlockPos(pos)));
        writeForcedChunkOwners(forcedEntries, entityForcedChunks, "Entities", Tag.TAG_INT_ARRAY, (uuid, forcedEntities) -> forcedEntities.add(NbtUtils.createUUID(uuid)));
        ListTag forcedChunks = new ListTag();
        for (Map.Entry<String, Long2ObjectMap<CompoundTag>> entry : forcedEntries.entrySet()) {
            CompoundTag forcedEntry = new CompoundTag();
            forcedEntry.putString("Mod", entry.getKey());
            ListTag modForced = new ListTag();
            modForced.addAll(entry.getValue().values());
            forcedEntry.put("ModForced", modForced);
            forcedChunks.add(forcedEntry);
        }
        nbt.put("ForgeForced", forcedChunks);
    }
}
Also used : HashMap(java.util.HashMap) Long2ObjectOpenHashMap(it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap) Long2ObjectMap(it.unimi.dsi.fastutil.longs.Long2ObjectMap) ListTag(net.minecraft.nbt.ListTag) HashMap(java.util.HashMap) Long2ObjectOpenHashMap(it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap) Map(java.util.Map) Long2ObjectMap(it.unimi.dsi.fastutil.longs.Long2ObjectMap) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 5 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)20 CompoundTag (net.minecraft.nbt.CompoundTag)14 TagCompound (de.keyle.knbt.TagCompound)8 TagList (de.keyle.knbt.TagList)8 TagString (de.keyle.knbt.TagString)6 TagShort (de.keyle.knbt.TagShort)4 ByteArrayTag (net.minecraft.nbt.ByteArrayTag)4 IntArrayTag (net.minecraft.nbt.IntArrayTag)4 Dynamic (com.mojang.serialization.Dynamic)2 MyPetBaby (de.Keyle.MyPet.api.entity.MyPetBaby)2 MyAxolotl (de.Keyle.MyPet.api.entity.types.MyAxolotl)2 MyBee (de.Keyle.MyPet.api.entity.types.MyBee)2 MyCat (de.Keyle.MyPet.api.entity.types.MyCat)2 MyCreeper (de.Keyle.MyPet.api.entity.types.MyCreeper)2 MyEnderman (de.Keyle.MyPet.api.entity.types.MyEnderman)2 MyGoat (de.Keyle.MyPet.api.entity.types.MyGoat)2 MyHorse (de.Keyle.MyPet.api.entity.types.MyHorse)2 MyIronGolem (de.Keyle.MyPet.api.entity.types.MyIronGolem)2 MyLlama (de.Keyle.MyPet.api.entity.types.MyLlama)2 MyMagmaCube (de.Keyle.MyPet.api.entity.types.MyMagmaCube)2