Java Code Examples for com.github.steveice10.opennbt.tag.builtin.CompoundTag#put()

The following examples show how to use com.github.steveice10.opennbt.tag.builtin.CompoundTag#put() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InventoryPackets.java    From ViaVersion with MIT License 6 votes vote down vote up
public static void toClient(Item item) {
    if (item == null) return;
    item.setIdentifier(getNewItemId(item.getIdentifier()));

    CompoundTag tag;
    if ((tag = item.getTag()) != null) {
        // Display Lore now uses JSON
        Tag displayTag = tag.get("display");
        if (displayTag instanceof CompoundTag) {
            CompoundTag display = (CompoundTag) displayTag;
            Tag loreTag = display.get("Lore");
            if (loreTag instanceof ListTag) {
                ListTag lore = (ListTag) loreTag;
                display.put(ConverterRegistry.convertToTag(NBT_TAG_NAME + "|Lore", ConverterRegistry.convertToValue(lore)));
                for (Tag loreEntry : lore) {
                    if (loreEntry instanceof StringTag) {
                        ((StringTag) loreEntry).setValue(ChatRewriter.legacyTextToJson(((StringTag) loreEntry).getValue()).toString());
                    }
                }
            }
        }
    }
}
 
Example 2
Source File: EntityPackets.java    From ViaVersion with MIT License 6 votes vote down vote up
private static CompoundTag createEndEntry() {
    CompoundTag tag = new CompoundTag("");
    tag.put(new ByteTag("piglin_safe", (byte) 0));
    tag.put(new ByteTag("natural", (byte) 0));
    tag.put(new FloatTag("ambient_light", 0));
    tag.put(new StringTag("infiniburn", "minecraft:infiniburn_end"));
    tag.put(new ByteTag("respawn_anchor_works", (byte) 0));
    tag.put(new ByteTag("has_skylight", (byte) 0));
    tag.put(new ByteTag("bed_works", (byte) 0));
    tag.put(new LongTag("fixed_time", 6000));
    tag.put(new ByteTag("has_raids", (byte) 1));
    tag.put(new StringTag("name", "minecraft:the_end"));
    tag.put(new IntTag("logical_height", 256));
    tag.put(new ByteTag("shrunk", (byte) 0));
    tag.put(new ByteTag("ultrawarm", (byte) 0));
    tag.put(new ByteTag("has_ceiling", (byte) 0));
    return tag;
}
 
Example 3
Source File: VanillaBlockDataCodec.java    From Cleanstone with MIT License 6 votes vote down vote up
private void writeHeightMap(ByteBuf data, VanillaBlockDataStorage storage) {
    BlockDataStorage table = storage.constructTable();
    int[] motionBlocking = new int[BlockDataSection.WIDTH * BlockDataSection.WIDTH];
    for (int sectionY = 0; sectionY < Chunk.HEIGHT / BlockDataSection.HEIGHT; sectionY++) {
        for (int x = 0; x < BlockDataSection.WIDTH; x++) {
            for (int y = 0; y < BlockDataSection.HEIGHT; y++) {
                for (int z = 0; z < BlockDataSection.WIDTH; z++) {
                    BlockState blockState = table.getBlock(x, y + sectionY * BlockDataSection.HEIGHT, z).getState();
                    if (blockState.getBlockType() != VanillaBlockType.AIR)
                        motionBlocking[x + z * 16] = y + sectionY * 16 + 2;
                }
            }
        }
    }
    CompoundTag heightMap = new CompoundTag("");
    heightMap.put(new LongArrayTag("MOTION_BLOCKING", HeightMapUtil.encodeHeightMap(motionBlocking)));
    try {
        ByteBufOutputStream byteBufStream = new ByteBufOutputStream(data);
        DataOutputStream dataOutputStream = new DataOutputStream(byteBufStream);
        NBTIO.writeTag((DataOutput) dataOutputStream, heightMap);
        dataOutputStream.close();
    } catch (IOException e) {
        log.error("Error occurred while serializing heightMap NBT data", e);
    }
}
 
Example 4
Source File: ChunkDataEncoder_v1_14.java    From Cleanstone with MIT License 6 votes vote down vote up
private void writeHeightMap(ByteBuf data, BlockDataStorage storage) throws IOException {
    int[] motionBlocking = new int[Chunk.WIDTH * Chunk.WIDTH];
    for (int sectionY = 0; sectionY < PaletteBlockStateStorage.SECTION_HEIGHT; sectionY++) {
        for (int x = 0; x < Chunk.WIDTH; x++) {
            for (int y = 0; y < Chunk.HEIGHT; y++) {
                for (int z = 0; z < Chunk.WIDTH; z++) {
                    BlockState blockState = storage.getBlock(x, y, z).getState();
                    if (blockState.getBlockType() != VanillaBlockType.AIR)
                        motionBlocking[x + z * 16] = y + sectionY * 16 + 2;
                }
            }
        }
    }
    CompoundTag heightMap = new CompoundTag("");
    heightMap.put(new LongArrayTag("MOTION_BLOCKING", HeightMapUtil.encodeHeightMap(motionBlocking)));

    ByteBufOutputStream byteBufStream = new ByteBufOutputStream(data);
    DataOutputStream dataOutputStream = new DataOutputStream(byteBufStream);
    NBTIO.writeTag((DataOutput) dataOutputStream, heightMap);
    dataOutputStream.close();
}
 
Example 5
Source File: BannerTranslator.java    From Geyser with MIT License 6 votes vote down vote up
@Override
public ItemStack translateToJava(ItemData itemData, ItemEntry itemEntry) {
    if (itemData.getTag() == null) return super.translateToJava(itemData, itemEntry);

    ItemStack itemStack = super.translateToJava(itemData, itemEntry);

    com.nukkitx.nbt.tag.CompoundTag nbtTag = itemData.getTag();
    if (nbtTag.contains("Patterns")) {
        com.nukkitx.nbt.tag.ListTag<?> patterns = nbtTag.get("Patterns");

        CompoundTag blockEntityTag = new CompoundTag("BlockEntityTag");
        blockEntityTag.put(convertBannerPattern(patterns));

        itemStack.getNbt().put(blockEntityTag);
    }

    return itemStack;
}
 
Example 6
Source File: CommandBlockStorage.java    From ViaVersion with MIT License 6 votes vote down vote up
public Optional<CompoundTag> getCommandBlock(Position position) {
    Pair<Integer, Integer> chunkCoords = getChunkCoords(position);

    Map<Position, CompoundTag> blocks = storedCommandBlocks.get(chunkCoords);
    if (blocks == null)
        return Optional.empty();

    CompoundTag tag = blocks.get(position);
    if (tag == null)
        return Optional.empty();

    tag = tag.clone();
    tag.put(new ByteTag("powered", (byte) 0));
    tag.put(new ByteTag("auto", (byte) 0));
    tag.put(new ByteTag("conditionMet", (byte) 0));
    return Optional.of(tag);
}
 
Example 7
Source File: InventoryPackets.java    From ViaVersion with MIT License 6 votes vote down vote up
public static void toClient(Item item) {
    if (item == null) return;

    if (item.getIdentifier() == 771 && item.getTag() != null) {
        CompoundTag tag = item.getTag();
        Tag ownerTag = tag.get("SkullOwner");
        if (ownerTag instanceof CompoundTag) {
            CompoundTag ownerCompundTag = (CompoundTag) ownerTag;
            Tag idTag = ownerCompundTag.get("Id");
            if (idTag instanceof StringTag) {
                UUID id = UUID.fromString((String) idTag.getValue());
                ownerCompundTag.put(new IntArrayTag("Id", UUIDIntArrayType.uuidToIntArray(id)));
            }
        }
    }

    item.setIdentifier(getNewItemId(item.getIdentifier()));
}
 
Example 8
Source File: BasicItemTranslator.java    From Geyser with MIT License 6 votes vote down vote up
@Override
public void translateToJava(CompoundTag itemTag, ItemEntry itemEntry) {
    if (!itemTag.contains("display")) {
        return;
    }
    CompoundTag displayTag = itemTag.get("display");
    if (displayTag.contains("Name")) {
        StringTag nameTag = displayTag.get("Name");
        displayTag.put(new StringTag("Name", toJavaMessage(nameTag)));
    }

    if (displayTag.contains("Lore")) {
        ListTag loreTag = displayTag.get("Lore");
        List<Tag> lore = new ArrayList<>();
        for (Tag tag : loreTag.getValue()) {
            if (!(tag instanceof StringTag)) return;
            lore.add(new StringTag("", "§r" + toJavaMessage((StringTag) tag)));
        }
        displayTag.put(new ListTag("Lore", lore));
    }
}
 
Example 9
Source File: InventoryPackets.java    From ViaVersion with MIT License 6 votes vote down vote up
public static void toServer(Item item) {
    if (item == null) return;

    item.setIdentifier(getOldItemId(item.getIdentifier()));

    if (item.getIdentifier() == 771 && item.getTag() != null) {
        CompoundTag tag = item.getTag();
        Tag ownerTag = tag.get("SkullOwner");
        if (ownerTag instanceof CompoundTag) {
            CompoundTag ownerCompundTag = (CompoundTag) ownerTag;
            Tag idTag = ownerCompundTag.get("Id");
            if (idTag instanceof IntArrayTag) {
                UUID id = UUIDIntArrayType.uuidFromIntArray((int[]) idTag.getValue());
                ownerCompundTag.put(new StringTag("Id", id.toString()));
            }
        }
    }
}
 
Example 10
Source File: FakeTileEntity.java    From ViaVersion with MIT License 5 votes vote down vote up
public static CompoundTag getFromBlock(int x, int y, int z, int block) {
    CompoundTag originalTag = tileEntities.get(block);
    if (originalTag != null) {
        CompoundTag tag = originalTag.clone();
        tag.put(new IntTag("x", x));
        tag.put(new IntTag("y", y));
        tag.put(new IntTag("z", z));
        return tag;
    }
    return null;
}
 
Example 11
Source File: EntityPackets.java    From ViaVersion with MIT License 5 votes vote down vote up
private static CompoundTag createOverworldEntry() {
    CompoundTag tag = new CompoundTag("");
    tag.put(new StringTag("name", "minecraft:overworld"));
    tag.put(new ByteTag("has_ceiling", (byte) 0));
    addSharedOverwaldEntries(tag);
    return tag;
}
 
Example 12
Source File: EntityPackets.java    From ViaVersion with MIT License 5 votes vote down vote up
private static CompoundTag createOverworldCavesEntry() {
    CompoundTag tag = new CompoundTag("");
    tag.put(new StringTag("name", "minecraft:overworld_caves"));
    tag.put(new ByteTag("has_ceiling", (byte) 1));
    addSharedOverwaldEntries(tag);
    return tag;
}
 
Example 13
Source File: EnchantedBookTranslator.java    From Geyser with MIT License 5 votes vote down vote up
@Override
public void translateToJava(CompoundTag itemTag, ItemEntry itemEntry) {
    if (!itemTag.contains("Enchantments")) {
        return;
    }
    Tag enchTag = itemTag.get("Enchantments");
    if (enchTag instanceof ListTag) {
        enchTag = new ListTag("StoredEnchantments", ((ListTag) enchTag).getValue());
        itemTag.remove("Enchantments");
        itemTag.put(enchTag);
    }
}
 
Example 14
Source File: BlockEntityTranslator.java    From Geyser with MIT License 5 votes vote down vote up
protected CompoundTag getConstantJavaTag(String javaId, int x, int y, int z) {
    CompoundTag tag = new CompoundTag("");
    tag.put(new IntTag("x", x));
    tag.put(new IntTag("y", y));
    tag.put(new IntTag("z", z));
    tag.put(new StringTag("id", javaId));
    return tag;
}
 
Example 15
Source File: CrossbowTranslator.java    From Geyser with MIT License 5 votes vote down vote up
@Override
public void translateToJava(CompoundTag itemTag, ItemEntry itemEntry) {
    if (itemTag.get("chargedItem") != null) {
        CompoundTag chargedItem = itemTag.get("chargedItem");

        CompoundTag newProjectile = new CompoundTag("");
        newProjectile.put(new ByteTag("Count", (byte) chargedItem.get("Count").getValue()));
        newProjectile.put(new StringTag("id", (String) chargedItem.get("Name").getValue()));

        ListTag chargedProjectiles = new ListTag("ChargedProjectiles");
        chargedProjectiles.add(newProjectile);

        itemTag.put(chargedProjectiles);
    }
}
 
Example 16
Source File: InventoryPackets.java    From ViaVersion with MIT License 5 votes vote down vote up
public static void toServer(Item item) {
    if (item == null) return;
    item.setIdentifier(getOldItemId(item.getIdentifier()));

    CompoundTag tag;
    if ((tag = item.getTag()) != null) {
        // Display Name now uses JSON
        Tag displayTag = tag.get("display");
        if (displayTag instanceof CompoundTag) {
            CompoundTag display = (CompoundTag) displayTag;
            Tag loreTag = display.get("Lore");
            if (loreTag instanceof ListTag) {
                ListTag lore = (ListTag) loreTag;
                ListTag via = display.get(NBT_TAG_NAME + "|Lore");
                if (via != null) {
                    display.put(ConverterRegistry.convertToTag("Lore", ConverterRegistry.convertToValue(via)));
                } else {
                    for (Tag loreEntry : lore) {
                        if (loreEntry instanceof StringTag) {
                            ((StringTag) loreEntry).setValue(
                                    ChatRewriter.jsonTextToLegacy(
                                            ((StringTag) loreEntry).getValue()
                                    )
                            );
                        }
                    }
                }
                display.remove(NBT_TAG_NAME + "|Lore");
            }
        }
    }
}
 
Example 17
Source File: SignBlockEntityTranslator.java    From Geyser with MIT License 5 votes vote down vote up
@Override
public CompoundTag getDefaultJavaTag(String javaId, int x, int y, int z) {
    CompoundTag tag = getConstantJavaTag(javaId, x, y, z);
    tag.put(new com.github.steveice10.opennbt.tag.builtin.StringTag("Text1", "{\"text\":\"\"}"));
    tag.put(new com.github.steveice10.opennbt.tag.builtin.StringTag("Text2", "{\"text\":\"\"}"));
    tag.put(new com.github.steveice10.opennbt.tag.builtin.StringTag("Text3", "{\"text\":\"\"}"));
    tag.put(new com.github.steveice10.opennbt.tag.builtin.StringTag("Text4", "{\"text\":\"\"}"));
    return tag;
}
 
Example 18
Source File: CampfireBlockEntityTranslator.java    From Geyser with MIT License 4 votes vote down vote up
@Override
public CompoundTag getDefaultJavaTag(String javaId, int x, int y, int z) {
    CompoundTag tag = getConstantJavaTag(javaId, x, y, z);
    tag.put(new ListTag("Items"));
    return tag;
}
 
Example 19
Source File: BannerBlockEntityTranslator.java    From Geyser with MIT License 4 votes vote down vote up
@Override
public CompoundTag getDefaultJavaTag(String javaId, int x, int y, int z) {
    CompoundTag tag = getConstantJavaTag(javaId, x, y, z);
    tag.put(new ListTag("Patterns"));
    return tag;
}
 
Example 20
Source File: FakeTileEntity.java    From ViaVersion with MIT License 4 votes vote down vote up
private static void register(Integer material, String name) {
    CompoundTag comp = new CompoundTag("");
    comp.put(new StringTag(name));
    tileEntities.put(material, comp);
}