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;
}
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();
}
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);
}
}
}
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);
}
}
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));
}
Aggregations