com.github.steveice10.opennbt.tag.builtin.Tag Java Examples

The following examples show how to use com.github.steveice10.opennbt.tag.builtin.Tag. 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: BookPagesTranslator.java    From Geyser with MIT License 6 votes vote down vote up
@Override
public void translateToJava(CompoundTag itemTag, ItemEntry itemEntry) {
    if (!itemTag.contains("pages")) {
        return;
    }
    List<Tag> pages = new ArrayList<>();
    ListTag pagesTag = itemTag.get("pages");
    for (Tag tag : pagesTag.getValue()) {
        if (!(tag instanceof CompoundTag))
            continue;

        CompoundTag pageTag = (CompoundTag) tag;

        StringTag textTag = pageTag.get("text");
        pages.add(new StringTag(MessageUtils.getJavaMessage(textTag.getValue())));
    }

    itemTag.remove("pages");
    itemTag.put(new ListTag("pages", pages));
}
 
Example #2
Source File: BlockEntityProvider.java    From ViaVersion with MIT License 6 votes vote down vote up
/**
 * Transforms the BlockEntities to blocks!
 *
 * @param user       UserConnection instance
 * @param position   Block Position - WARNING: Position is null when called from a chunk
 * @param tag        BlockEntity NBT
 * @param sendUpdate send a block change update
 * @return new block id
 * @throws Exception Gotta throw that exception
 */
public int transform(UserConnection user, Position position, CompoundTag tag, boolean sendUpdate) throws Exception {
    Tag idTag = tag.get("id");
    if (idTag == null) return -1;

    String id = (String) idTag.getValue();
    BlockEntityHandler handler = handlers.get(id);
    if (handler == null) {
        if (Via.getManager().isDebug()) {
            Via.getPlatform().getLogger().warning("Unhandled BlockEntity " + id + " full tag: " + tag);
        }
        return -1;
    }

    int newBlock = handler.transform(user, tag);

    if (sendUpdate && newBlock != -1) {
        sendBlockChange(user, position, newBlock);
    }

    return newBlock;
}
 
Example #3
Source File: BedHandler.java    From ViaVersion with MIT License 6 votes vote down vote up
@Override
public int transform(UserConnection user, CompoundTag tag) {
    BlockStorage storage = user.get(BlockStorage.class);
    Position position = new Position((int) getLong(tag.get("x")), (short) getLong(tag.get("y")), (int) getLong(tag.get("z")));

    if (!storage.contains(position)) {
        Via.getPlatform().getLogger().warning("Received an bed color update packet, but there is no bed! O_o " + tag);
        return -1;
    }

    //                                              RED_BED + FIRST_BED
    int blockId = storage.get(position).getOriginal() - 972 + 748;

    Tag color = tag.get("color");
    if (color != null) {
        blockId += (((Number) color.getValue()).intValue() * 16);
    }

    return blockId;
}
 
Example #4
Source File: SkullHandler.java    From ViaVersion with MIT License 6 votes vote down vote up
@Override
public int transform(UserConnection user, CompoundTag tag) {
    BlockStorage storage = user.get(BlockStorage.class);
    Position position = new Position((int) getLong(tag.get("x")), (short) getLong(tag.get("y")), (int) getLong(tag.get("z")));

    if (!storage.contains(position)) {
        Via.getPlatform().getLogger().warning("Received an head update packet, but there is no head! O_o " + tag);
        return -1;
    }

    int id = storage.get(position).getOriginal();
    if (id >= SKULL_WALL_START && id <= SKULL_END) {
        Tag skullType = tag.get("SkullType");
        if (skullType != null) {
            id += ((Number) tag.get("SkullType").getValue()).intValue() * 20;
        }
        if (tag.contains("Rot")) {
            id += ((Number) tag.get("Rot").getValue()).intValue();
        }
    } else {
        Via.getPlatform().getLogger().warning("Why does this block have the skull block entity? " + tag);
        return -1;
    }

    return id;
}
 
Example #5
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 #6
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 #7
Source File: ItemRewriter.java    From ViaVersion with MIT License 6 votes vote down vote up
public static void rewriteBookToServer(Item item) {
    int id = item.getIdentifier();
    if (id != 387) {
        return;
    }
    CompoundTag tag = item.getTag();
    ListTag pages = tag.get("pages");
    if (pages == null) { // is this even possible?
        return;
    }
    for (int i = 0; i < pages.size(); i++) {
        Tag pageTag = pages.get(i);
        if (!(pageTag instanceof StringTag)) {
            continue;
        }
        StringTag stag = (StringTag) pageTag;
        String value = stag.getValue();
        if (value.replaceAll(" ", "").isEmpty()) {
            value = "\"" + fixBookSpaceChars(value) + "\"";
        } else {
            value = fixBookSpaceChars(value);
        }
        stag.setValue(value);
    }
}
 
Example #8
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 #9
Source File: TagStringReader.java    From ViaVersion with MIT License 6 votes vote down vote up
public Tag tag() throws StringTagParseException {
    final char startToken = this.buffer.skipWhitespace().peek();
    switch (startToken) {
        case Tokens.COMPOUND_BEGIN:
            return this.compound();
        case Tokens.ARRAY_BEGIN:
            if (this.buffer.peek(2) == ';') { // we know we're an array tag
                return this.array(this.buffer.peek(1));
            } else {
                return this.list();
            }
        case Tokens.SINGLE_QUOTE:
        case Tokens.DOUBLE_QUOTE:
            // definitely a string tag
            this.buffer.advance();
            return new StringTag("", unescape(this.buffer.takeUntil(startToken).toString()));
        default: // scalar
            return this.scalar();
    }
}
 
Example #10
Source File: TagStringReader.java    From ViaVersion with MIT License 6 votes vote down vote up
/**
 * Similar to a list tag in syntax, but returning a single array tag rather than a list of tags.
 *
 * @return array-typed tag
 */
public Tag array(final char elementType) throws StringTagParseException {
    this.buffer.expect(Tokens.ARRAY_BEGIN)
            .expect(elementType)
            .expect(Tokens.ARRAY_SIGNATURE_SEPARATOR);

    if (elementType == Tokens.TYPE_BYTE) {
        return new ByteArrayTag("", this.byteArray());
    } else if (elementType == Tokens.TYPE_INT) {
        return new IntArrayTag("", this.intArray());
    } else if (elementType == Tokens.TYPE_LONG) {
        return new LongArrayTag("", this.longArray());
    } else {
        throw this.buffer.makeError("Type " + elementType + " is not a valid element type in an array!");
    }
}
 
Example #11
Source File: TagStringReader.java    From ViaVersion with MIT License 6 votes vote down vote up
public ListTag list() throws StringTagParseException {
    final ListTag listTag = new ListTag("");
    this.buffer.expect(Tokens.ARRAY_BEGIN);
    final boolean prefixedIndex = this.buffer.peek() == '0' && this.buffer.peek(1) == ':';
    while (this.buffer.hasMore()) {
        if (prefixedIndex) {
            this.buffer.takeUntil(':');
        }

        final Tag next = this.tag();
        // TODO: validate type
        listTag.add(next);
        if (this.separatorOrCompleteWith(Tokens.ARRAY_END)) {
            return listTag;
        }
    }
    throw this.buffer.makeError("Reached end of file without end of list tag!");
}
 
Example #12
Source File: TagStringReader.java    From ViaVersion with MIT License 6 votes vote down vote up
public CompoundTag compound() throws StringTagParseException {
    this.buffer.expect(Tokens.COMPOUND_BEGIN);
    final CompoundTag compoundTag = new CompoundTag("");
    while (this.buffer.hasMore()) {
        final String key = this.key();
        final Tag tag = this.tag();
        // Doesn't get around this with the steveice lib :/
        try {
            if (!NAME_FIELD.isAccessible()) {
                NAME_FIELD.setAccessible(true);
            }
            NAME_FIELD.set(tag, key);
        } catch (IllegalAccessException e) {
            throw new IllegalArgumentException(e);
        }

        compoundTag.put(tag);
        if (this.separatorOrCompleteWith(Tokens.COMPOUND_END)) {
            return compoundTag;
        }
    }
    throw this.buffer.makeError("Unterminated compound tag!");
}
 
Example #13
Source File: BookPagesTranslator.java    From Geyser with MIT License 6 votes vote down vote up
@Override
public void translateToBedrock(CompoundTag itemTag, ItemEntry itemEntry) {
    if (!itemTag.contains("pages")) {
        return;
    }
    List<Tag> pages = new ArrayList<>();
    ListTag pagesTag = itemTag.get("pages");
    for (Tag tag : pagesTag.getValue()) {
        if (!(tag instanceof StringTag))
            continue;

        StringTag textTag = (StringTag) tag;

        CompoundTag pageTag = new CompoundTag("");
        pageTag.put(new StringTag("photoname", ""));
        pageTag.put(new StringTag("text", MessageUtils.getBedrockMessageLenient(textTag.getValue())));
        pages.add(pageTag);
    }

    itemTag.remove("pages");
    itemTag.put(new ListTag("pages", pages));
}
 
Example #14
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 #15
Source File: PotionTranslator.java    From Geyser with MIT License 5 votes vote down vote up
@Override
public ItemData translateToBedrock(ItemStack itemStack, ItemEntry itemEntry) {
    if (itemStack.getNbt() == null) return super.translateToBedrock(itemStack, itemEntry);
    Tag potionTag = itemStack.getNbt().get("Potion");
    if (potionTag instanceof StringTag) {
        Potion potion = Potion.getByJavaIdentifier(((StringTag) potionTag).getValue());
        if (potion != null) {
            return ItemData.of(itemEntry.getBedrockId(), potion.getBedrockId(), itemStack.getAmount(), translateNbtToBedrock(itemStack.getNbt()));
        }
        GeyserConnector.getInstance().getLogger().debug("Unknown java potion: " + potionTag.getValue());
    }
    return super.translateToBedrock(itemStack, itemEntry);
}
 
Example #16
Source File: CommandBlockHandler.java    From ViaVersion with MIT License 5 votes vote down vote up
@Override
public int transform(UserConnection user, CompoundTag tag) {
    Tag name = tag.get("CustomName");
    if (name instanceof StringTag) {
        ((StringTag) name).setValue(ChatRewriter.legacyTextToJson(((StringTag) name).getValue()).toString());
    }
    Tag out = tag.get("LastOutput");
    if (out instanceof StringTag) {
        JsonElement value = GsonUtil.getJsonParser().parse(((StringTag) out).getValue());
        ChatRewriter.processTranslate(value);
        ((StringTag) out).setValue(value.toString());
    }
    return -1;
}
 
Example #17
Source File: EnchantedBookTranslator.java    From Geyser with MIT License 5 votes vote down vote up
@Override
public void translateToBedrock(CompoundTag itemTag, ItemEntry itemEntry) {
    if (!itemTag.contains("StoredEnchantments")) {
        return;
    }
    Tag enchTag = itemTag.get("StoredEnchantments");
    if (enchTag instanceof ListTag) {
        enchTag = new ListTag("Enchantments", ((ListTag) enchTag).getValue());
        itemTag.remove("StoredEnchantments");
        itemTag.put(enchTag);
    }
}
 
Example #18
Source File: EntityIdRewriter.java    From ViaVersion with MIT License 5 votes vote down vote up
private static boolean hasEntityTag(Item item) {
    if (item == null || item.getIdentifier() != 383) return false; // Monster Egg

    CompoundTag tag = item.getTag();
    if (tag == null) return false;

    Tag entityTag = tag.get("EntityTag");
    return entityTag instanceof CompoundTag && ((CompoundTag) entityTag).get("id") instanceof StringTag;
}
 
Example #19
Source File: EntityIdRewriter.java    From ViaVersion with MIT License 5 votes vote down vote up
public static void toServerItem(Item item, boolean backwards) {
    if (!hasEntityTag(item)) return;

    CompoundTag entityTag = item.getTag().get("EntityTag");
    Tag idTag = entityTag.get("id");
    if (idTag instanceof StringTag) {
        StringTag id = (StringTag) idTag;
        String newName = backwards ? oldToNewNames.get(id.getValue()) : oldToNewNames.inverse().get(id.getValue());
        if (newName != null) {
            id.setValue(newName);
        }
    }
}
 
Example #20
Source File: EntityIdRewriter.java    From ViaVersion with MIT License 5 votes vote down vote up
public static void toClientSpawner(CompoundTag tag, boolean backwards) {
    if (tag == null) return;

    Tag spawnDataTag = tag.get("SpawnData");
    if (spawnDataTag != null) {
        toClient((CompoundTag) spawnDataTag, backwards);
    }
}
 
Example #21
Source File: EntityIdRewriter.java    From ViaVersion with MIT License 5 votes vote down vote up
public static void toClient(CompoundTag tag, boolean backwards) {
    Tag idTag = tag.get("id");
    if (idTag instanceof StringTag) {
        StringTag id = (StringTag) idTag;
        String newName = backwards ? oldToNewNames.inverse().get(id.getValue()) : oldToNewNames.get(id.getValue());
        if (newName != null) {
            id.setValue(newName);
        }
    }
}
 
Example #22
Source File: WorldPackets.java    From ViaVersion with MIT License 5 votes vote down vote up
private static void handleBlockEntity(CompoundTag compoundTag) {
    StringTag idTag = compoundTag.get("id");
    if (idTag == null) return;

    String id = idTag.getValue();
    if (id.equals("minecraft:conduit")) {
        Tag targetUuidTag = compoundTag.remove("target_uuid");
        if (!(targetUuidTag instanceof StringTag)) return;

        // target_uuid -> Target
        UUID targetUuid = UUID.fromString((String) targetUuidTag.getValue());
        compoundTag.put(new IntArrayTag("Target", UUIDIntArrayType.uuidToIntArray(targetUuid)));
    } else if (id.equals("minecraft:skull") && compoundTag.get("Owner") instanceof CompoundTag) {
        CompoundTag ownerTag = compoundTag.remove("Owner");
        StringTag ownerUuidTag = ownerTag.remove("Id");
        if (ownerUuidTag != null) {
            UUID ownerUuid = UUID.fromString(ownerUuidTag.getValue());
            ownerTag.put(new IntArrayTag("Id", UUIDIntArrayType.uuidToIntArray(ownerUuid)));
        }

        // Owner -> SkullOwner
        CompoundTag skullOwnerTag = new CompoundTag("SkullOwner");
        for (Tag tag : ownerTag) {
            skullOwnerTag.put(tag);
        }
        compoundTag.put(skullOwnerTag);
    }
}
 
Example #23
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 #24
Source File: BannerTranslator.java    From Geyser with MIT License 5 votes vote down vote up
/**
 * Convert a list of patterns from Java nbt to Bedrock nbt
 *
 * @param patterns The patterns to convert
 * @return The new converted patterns
 */
public static com.nukkitx.nbt.tag.ListTag convertBannerPattern(ListTag patterns) {
    List<com.nukkitx.nbt.tag.CompoundTag> tagsList = new ArrayList<>();
    for (com.github.steveice10.opennbt.tag.builtin.Tag patternTag : patterns.getValue()) {
        com.nukkitx.nbt.tag.CompoundTag newPatternTag = getBedrockBannerPattern((CompoundTag) patternTag);
        if (newPatternTag != null) {
            tagsList.add(newPatternTag);
        }
    }

    return new com.nukkitx.nbt.tag.ListTag<>("Patterns", com.nukkitx.nbt.tag.CompoundTag.class, tagsList);
}
 
Example #25
Source File: BannerTranslator.java    From Geyser with MIT License 5 votes vote down vote up
/**
 * Convert a list of patterns from Bedrock nbt to Java nbt
 *
 * @param patterns The patterns to convert
 * @return The new converted patterns
 */
public static ListTag convertBannerPattern(com.nukkitx.nbt.tag.ListTag<?> patterns) {
    List<Tag> tagsList = new ArrayList<>();
    for (Object patternTag : patterns.getValue()) {
        CompoundTag newPatternTag = getJavaBannerPattern((com.nukkitx.nbt.tag.CompoundTag) patternTag);
        tagsList.add(newPatternTag);
    }

    return new ListTag("Patterns", tagsList);
}
 
Example #26
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 #27
Source File: BannerTranslator.java    From Geyser with MIT License 5 votes vote down vote up
/**
 * Convert the Bedrock edition banner pattern nbt to Java edition
 *
 * @param pattern Bedorck edition pattern nbt
 * @return The Java edition format pattern nbt
 */
public static CompoundTag getJavaBannerPattern(com.nukkitx.nbt.tag.CompoundTag pattern) {
    Map<String, Tag> tags = new HashMap<>();
    tags.put("Color", new IntTag("Color", 15 - pattern.getInt("Color")));
    tags.put("Pattern", new StringTag("Pattern", pattern.getString("Pattern")));

    return new CompoundTag("", tags);
}
 
Example #28
Source File: TagStringReader.java    From ViaVersion with MIT License 5 votes vote down vote up
private int[] intArray() throws StringTagParseException {
    final IntStream.Builder builder = IntStream.builder();
    while (this.buffer.hasMore()) {
        final Tag value = this.tag();
        if (!(value instanceof IntTag)) {
            throw this.buffer.makeError("All elements of an int array must be ints!");
        }
        builder.add(((IntTag) value).getValue());
        if (this.separatorOrCompleteWith(Tokens.ARRAY_END)) {
            return builder.build().toArray();
        }
    }
    throw this.buffer.makeError("Reached end of document without array close");
}
 
Example #29
Source File: ChunkDataPacket.java    From Cleanstone with MIT License 5 votes vote down vote up
public ChunkDataPacket(int chunkX, int chunkZ, boolean groundUpContinuous,
                       BlockDataStorage blockDataStorage, Tag blockEntities) {
    this.chunkX = chunkX;
    this.chunkZ = chunkZ;
    this.groundUpContinuous = groundUpContinuous;
    this.blockDataStorage = blockDataStorage;
    this.blockEntities = blockEntities;
}
 
Example #30
Source File: UpdateTradePacket.java    From Cleanstone with MIT License 5 votes vote down vote up
public UpdateTradePacket(byte windowID, byte windowType, int unknown0, int unknown1, boolean isWilling, long traderEntityID, long playerEntityID, String displayName, Tag namedTag) {
    this.windowID = windowID;
    this.windowType = windowType;
    this.unknown0 = unknown0;
    this.unknown1 = unknown1;
    this.isWilling = isWilling;
    this.traderEntityID = traderEntityID;
    this.playerEntityID = playerEntityID;
    this.displayName = displayName;
    this.namedTag = namedTag;
}