Search in sources :

Example 6 with CompoundTag

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

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

the class FluidStack method readFromPacket.

public static FluidStack readFromPacket(FriendlyByteBuf buf) {
    Fluid fluid = buf.readRegistryId();
    int amount = buf.readVarInt();
    CompoundTag tag = buf.readNbt();
    if (fluid == Fluids.EMPTY)
        return EMPTY;
    return new FluidStack(fluid, amount, tag);
}
Also used : Fluid(net.minecraft.world.level.material.Fluid) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 8 with CompoundTag

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

the class FluidStack method getOrCreateChildTag.

public CompoundTag getOrCreateChildTag(String childName) {
    getOrCreateTag();
    CompoundTag child = tag.getCompound(childName);
    if (!tag.contains(childName, Tag.TAG_COMPOUND)) {
        tag.put(childName, child);
    }
    return child;
}
Also used : CompoundTag(net.minecraft.nbt.CompoundTag)

Example 9 with CompoundTag

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

the class CraftingHelper method getItemStack.

public static ItemStack getItemStack(JsonObject json, boolean readNBT, boolean disallowsAirInRecipe) {
    String itemName = GsonHelper.getAsString(json, "item");
    ResourceLocation itemKey = new ResourceLocation(itemName);
    if (!ForgeRegistries.ITEMS.containsKey(itemKey))
        throw new JsonSyntaxException("Unknown item '" + itemName + "'");
    Item item = ForgeRegistries.ITEMS.getValue(itemKey);
    if (disallowsAirInRecipe && item == Items.AIR)
        throw new JsonSyntaxException("Invalid item: " + itemName);
    if (readNBT && json.has("nbt")) {
        // Lets hope this works? Needs test
        try {
            JsonElement element = json.get("nbt");
            CompoundTag nbt;
            if (element.isJsonObject())
                nbt = TagParser.parseTag(GSON.toJson(element));
            else
                nbt = TagParser.parseTag(GsonHelper.convertToString(element, "nbt"));
            CompoundTag tmp = new CompoundTag();
            if (nbt.contains("ForgeCaps")) {
                tmp.put("ForgeCaps", nbt.get("ForgeCaps"));
                nbt.remove("ForgeCaps");
            }
            tmp.put("tag", nbt);
            tmp.putString("id", itemName);
            tmp.putInt("Count", GsonHelper.getAsInt(json, "count", 1));
            return ItemStack.of(tmp);
        } catch (CommandSyntaxException e) {
            throw new JsonSyntaxException("Invalid NBT Entry: " + e.toString());
        }
    }
    return new ItemStack(item, GsonHelper.getAsInt(json, "count", 1));
}
Also used : Item(net.minecraft.world.item.Item) JsonSyntaxException(com.google.gson.JsonSyntaxException) JsonElement(com.google.gson.JsonElement) ResourceLocation(net.minecraft.resources.ResourceLocation) ItemStack(net.minecraft.world.item.ItemStack) CompoundTag(net.minecraft.nbt.CompoundTag) CommandSyntaxException(com.mojang.brigadier.exceptions.CommandSyntaxException)

Example 10 with CompoundTag

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

the class FakeBlockHelper method handleMapChunkPacket.

public static ClientboundLevelChunkPacket handleMapChunkPacket(ClientboundLevelChunkPacket originalPacket, List<FakeBlock> blocks) {
    try {
        ClientboundLevelChunkPacket packet = new ClientboundLevelChunkPacket(DenizenNetworkManagerImpl.copyPacket(originalPacket));
        copyPacketPaperPatch(packet, originalPacket);
        // TODO: properly update HeightMap?
        BitSet bitmask = packet.getAvailableSections();
        FriendlyByteBuf serial = originalPacket.getReadBuffer();
        FriendlyByteBuf outputSerial = new FriendlyByteBuf(Unpooled.buffer(serial.readableBytes()));
        List<net.minecraft.nbt.CompoundTag> blockEntities = new ArrayList<>(packet.getBlockEntitiesTags());
        BLOCKENTITIES_MAPCHUNK.set(packet, blockEntities);
        ListIterator<CompoundTag> iterator = blockEntities.listIterator();
        while (iterator.hasNext()) {
            net.minecraft.nbt.CompoundTag blockEnt = iterator.next();
            int x = blockEnt.getInt("x");
            int y = blockEnt.getInt("y");
            int z = blockEnt.getInt("z");
            for (FakeBlock block : blocks) {
                LocationTag loc = block.location;
                if (loc.getBlockX() == x && loc.getBlockY() == y && loc.getBlockZ() == z && block.material != null) {
                    iterator.remove();
                    break;
                }
            }
        }
        for (FakeBlock block : blocks) {
            if (block.material != null) {
                LocationTag loc = block.location;
                net.minecraft.nbt.CompoundTag newCompound = new net.minecraft.nbt.CompoundTag();
                newCompound.putInt("x", loc.getBlockX());
                newCompound.putInt("y", loc.getBlockY());
                newCompound.putInt("z", loc.getBlockZ());
                newCompound.putString("id", block.material.getMaterial().getKey().toString());
                blockEntities.add(newCompound);
            }
        }
        for (int y = 0; y < 16; y++) {
            if (bitmask.get(y)) {
                int blockCount = serial.readShort();
                int width = serial.readUnsignedByte();
                int paletteLen = serial.readVarInt();
                int[] palette = new int[paletteLen];
                for (int p = 0; p < paletteLen; p++) {
                    palette[p] = serial.readVarInt();
                }
                int dataLen = serial.readVarInt();
                long[] blockListHelper = new long[dataLen];
                for (int i = 0; i < blockListHelper.length; i++) {
                    blockListHelper[i] = serial.readLong();
                }
                outputSerial.writeShort(blockCount);
                if (!anyBlocksInSection(blocks, y)) {
                    outputSerial.writeByte(width);
                    outputSerial.writeVarInt(paletteLen);
                    for (int p = 0; p < paletteLen; p++) {
                        outputSerial.writeVarInt(palette[p]);
                    }
                    outputSerial.writeLongArray(blockListHelper);
                    continue;
                }
                char dataBitsF = (char) (64 / width);
                int expectedLength = (4096 + dataBitsF - 1) / dataBitsF;
                if (blockListHelper.length != expectedLength) {
                    // This chunk is too-complex and is using non-standard chunk format. For now, just ignore it.
                    return originalPacket;
                // TODO: Add support for processing very-complex chunks (DataPaletteHash might be responsible for the unique format?)
                }
                BitStorage bits = new BitStorage(width, 4096, blockListHelper);
                int minY = y << 4;
                int maxY = (y << 4) + 16;
                for (FakeBlock block : blocks) {
                    if (block.material != null) {
                        int blockY = block.location.getBlockY();
                        if (blockY >= minY && blockY < maxY) {
                            int blockX = block.location.getBlockX();
                            int blockZ = block.location.getBlockZ();
                            blockX -= (blockX >> 4) * 16;
                            blockY -= (blockY >> 4) * 16;
                            blockZ -= (blockZ >> 4) * 16;
                            int blockIndex = blockArrayIndex(blockX, blockY, blockZ);
                            BlockState replacementData = getNMSState(block);
                            int globalPaletteIndex = indexInPalette(replacementData);
                            int subPaletteId = getPaletteSubId(palette, globalPaletteIndex);
                            if (subPaletteId == -1) {
                                int[] newPalette = new int[paletteLen + 1];
                                if (paletteLen >= 0)
                                    System.arraycopy(palette, 0, newPalette, 0, paletteLen);
                                newPalette[paletteLen] = globalPaletteIndex;
                                subPaletteId = paletteLen;
                                paletteLen++;
                                palette = newPalette;
                                int newWidth = Mth.ceillog2(paletteLen);
                                if (newWidth > width) {
                                    BitStorage newBits = new BitStorage(newWidth, 4096);
                                    for (int i = 0; i < bits.getSize(); i++) {
                                        newBits.getAndSet(i, bits.get(i));
                                    }
                                    bits = newBits;
                                    width = newWidth;
                                }
                            }
                            bits.getAndSet(blockIndex, subPaletteId);
                        }
                    }
                }
                outputSerial.writeByte(width);
                outputSerial.writeVarInt(paletteLen);
                for (int p = 0; p < palette.length; p++) {
                    outputSerial.writeVarInt(palette[p]);
                }
                outputSerial.writeLongArray(bits.getRaw());
            }
        }
        int[] biomes = packet.getBiomes();
        if (biomes != null) {
            outputSerial.writeVarIntArray(biomes);
        }
        byte[] outputBytes = outputSerial.array();
        DATA_MAPCHUNK.set(packet, outputBytes);
        return packet;
    } catch (Exception ex) {
        Debug.echoError(ex);
    }
    return null;
}
Also used : FriendlyByteBuf(net.minecraft.network.FriendlyByteBuf) BitSet(java.util.BitSet) ArrayList(java.util.ArrayList) FakeBlock(com.denizenscript.denizen.utilities.blocks.FakeBlock) LocationTag(com.denizenscript.denizen.objects.LocationTag) CompoundTag(net.minecraft.nbt.CompoundTag) BlockState(net.minecraft.world.level.block.state.BlockState) ClientboundLevelChunkPacket(net.minecraft.network.protocol.game.ClientboundLevelChunkPacket) CompoundTag(net.minecraft.nbt.CompoundTag) BitStorage(net.minecraft.util.BitStorage)

Aggregations

CompoundTag (net.minecraft.nbt.CompoundTag)31 ListTag (net.minecraft.nbt.ListTag)14 TagCompound (de.keyle.knbt.TagCompound)12 TagList (de.keyle.knbt.TagList)8 TagString (de.keyle.knbt.TagString)6 TagShort (de.keyle.knbt.TagShort)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 ByteArrayTag (net.minecraft.nbt.ByteArrayTag)4 ResourceLocation (net.minecraft.resources.ResourceLocation)4 ItemStack (org.bukkit.inventory.ItemStack)4 CraftItemStack (org.bukkit.craftbukkit.v1_18_R1.inventory.CraftItemStack)3 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