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

The following examples show how to use com.github.steveice10.opennbt.tag.builtin.StringTag. 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: CrossbowTranslator.java    From Geyser with MIT License 6 votes vote down vote up
@Override
public void translateToBedrock(CompoundTag itemTag, ItemEntry itemEntry) {
    if (itemTag.get("ChargedProjectiles") != null) {
        ListTag chargedProjectiles = itemTag.get("ChargedProjectiles");
        if (!chargedProjectiles.getValue().isEmpty()) {
            CompoundTag projectile = (CompoundTag) chargedProjectiles.getValue().get(0);

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

            // Not sure what this is for
            newProjectile.put(new ByteTag("Damage", (byte) 0));

            itemTag.put(newProjectile);
        }
    }
}
 
Example #2
Source File: EntityPackets.java    From ViaVersion with MIT License 6 votes vote down vote up
private static CompoundTag createNetherEntry() {
    CompoundTag tag = new CompoundTag("");
    tag.put(new ByteTag("piglin_safe", (byte) 1));
    tag.put(new ByteTag("natural", (byte) 0));
    tag.put(new FloatTag("ambient_light", 0.1F));
    tag.put(new StringTag("infiniburn", "minecraft:infiniburn_nether"));
    tag.put(new ByteTag("respawn_anchor_works", (byte) 1));
    tag.put(new ByteTag("has_skylight", (byte) 0));
    tag.put(new ByteTag("bed_works", (byte) 0));
    tag.put(new LongTag("fixed_time", 18000));
    tag.put(new ByteTag("has_raids", (byte) 0));
    tag.put(new StringTag("name", "minecraft:the_nether"));
    tag.put(new IntTag("logical_height", 128));
    tag.put(new ByteTag("shrunk", (byte) 1));
    tag.put(new ByteTag("ultrawarm", (byte) 1));
    tag.put(new ByteTag("has_ceiling", (byte) 1));
    return tag;
}
 
Example #3
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 #4
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 #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: 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 #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;
    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 #8
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 #9
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 #10
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 #11
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 #12
Source File: SpawnerHandler.java    From ViaVersion with MIT License 5 votes vote down vote up
@Override
public int transform(UserConnection user, CompoundTag tag) {
    if (tag.contains("SpawnData") && tag.get("SpawnData") instanceof CompoundTag) {
        CompoundTag data = tag.get("SpawnData");
        if (data.contains("id") && data.get("id") instanceof StringTag) {
            StringTag s = data.get("id");
            s.setValue(EntityNameRewriter.rewrite(s.getValue()));
        }

    }

    // Always return -1 because the block is still the same id
    return -1;
}
 
Example #13
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 #14
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 #15
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 #16
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 #17
Source File: ItemRewriter_1_11_TO_1_10.java    From ChatItem with GNU General Public License v3.0 5 votes vote down vote up
static void toClient(Item item) {
    if (hasEntityTag(item)) {
        CompoundTag entityTag = item.getTag().get("EntityTag");
        if (entityTag.get("id") instanceof StringTag) {
            StringTag id = entityTag.get("id");
            if (oldToNewNames.containsKey(id.getValue())) {
                id.setValue(oldToNewNames.get(id.getValue()));
            }
        }
    }
}
 
Example #18
Source File: ItemRewriter_1_11_TO_1_10.java    From ChatItem with GNU General Public License v3.0 5 votes vote down vote up
static void reverseToClient(Item item) {
    if (hasEntityTag(item)) {
        CompoundTag entityTag = item.getTag().get("EntityTag");
        if (entityTag.get("id") instanceof StringTag) {
            StringTag id = entityTag.get("id");
            if (oldToNewNames.containsKey(id.getValue())) {
                id.setValue(oldToNewNames.get(id.getValue()));
            }
        }
    }
}
 
Example #19
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 #20
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 #21
Source File: ItemRewriter_1_11_TO_1_10.java    From ChatItem with GNU General Public License v3.0 5 votes vote down vote up
private static boolean hasEntityTag(Item item) {
    if (item != null && item.getId().equals("minecraft:spawn_egg")) { // Monster Egg
        CompoundTag tag = item.getTag();
        if (tag != null && tag.contains("EntityTag") && tag.get("EntityTag") instanceof CompoundTag) {
            if (((CompoundTag) tag.get("EntityTag")).get("id") instanceof StringTag) {
                return true;
            }
        }
    }
    return false;
}
 
Example #22
Source File: EntityPackets.java    From ViaVersion with MIT License 5 votes vote down vote up
private static void addSharedOverwaldEntries(CompoundTag tag) {
    tag.put(new ByteTag("piglin_safe", (byte) 0));
    tag.put(new ByteTag("natural", (byte) 1));
    tag.put(new FloatTag("ambient_light", 0));
    tag.put(new StringTag("infiniburn", "minecraft:infiniburn_overworld"));
    tag.put(new ByteTag("respawn_anchor_works", (byte) 0));
    tag.put(new ByteTag("has_skylight", (byte) 1));
    tag.put(new ByteTag("bed_works", (byte) 1));
    tag.put(new ByteTag("has_raids", (byte) 1));
    tag.put(new IntTag("logical_height", 256));
    tag.put(new ByteTag("shrunk", (byte) 0));
    tag.put(new ByteTag("ultrawarm", (byte) 0));
}
 
Example #23
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 #24
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 #25
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 #26
Source File: TagStringWriter.java    From ViaVersion with MIT License 5 votes vote down vote up
public TagStringWriter writeTag(final Tag tag) throws IOException {
    if (tag instanceof CompoundTag) {
        return this.writeCompound((CompoundTag) tag);
    } else if (tag instanceof ListTag) {
        return this.writeList((ListTag) tag);
    } else if (tag instanceof ByteArrayTag) {
        return this.writeByteArray((ByteArrayTag) tag);
    } else if (tag instanceof IntArrayTag) {
        return this.writeIntArray((IntArrayTag) tag);
    } else if (tag instanceof LongArrayTag) {
        return this.writeLongArray((LongArrayTag) tag);
    } else if (tag instanceof StringTag) {
        return this.value(((StringTag) tag).getValue(), Tokens.EOF);
    } else if (tag instanceof ByteTag) {
        return this.value(Byte.toString(((ByteTag) tag).getValue()), Tokens.TYPE_BYTE);
    } else if (tag instanceof ShortTag) {
        return this.value(Short.toString(((ShortTag) tag).getValue()), Tokens.TYPE_SHORT);
    } else if (tag instanceof IntTag) {
        return this.value(Integer.toString(((IntTag) tag).getValue()), Tokens.TYPE_INT);
    } else if (tag instanceof LongTag) {
        return this.value(Long.toString(((LongTag) tag).getValue()), Tokens.TYPE_LONG);
    } else if (tag instanceof FloatTag) {
        return this.value(Float.toString(((FloatTag) tag).getValue()), Tokens.TYPE_FLOAT);
    } else if (tag instanceof DoubleTag) {
        return this.value(Double.toString(((DoubleTag) tag).getValue()), Tokens.TYPE_DOUBLE);
    } else {
        throw new IOException("Unknown tag type: " + tag.getClass().getSimpleName());
        // unknown!
    }
}
 
Example #27
Source File: PotionTranslator.java    From Geyser with MIT License 5 votes vote down vote up
@Override
public ItemStack translateToJava(ItemData itemData, ItemEntry itemEntry) {
    Potion potion = Potion.getByBedrockId(itemData.getDamage());
    ItemStack itemStack = super.translateToJava(itemData, itemEntry);
    if (potion != null) {
        StringTag potionTag = new StringTag("Potion", potion.getJavaIdentifier());
        itemStack.getNbt().put(potionTag);
    }
    return itemStack;
}
 
Example #28
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 #29
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 #30
Source File: MapItemTranslator.java    From Geyser with MIT License 5 votes vote down vote up
@Override
public void translateToBedrock(CompoundTag itemTag, ItemEntry itemEntry) {
    IntTag mapId = itemTag.get("map");

    if (mapId != null) {
        itemTag.put(new StringTag("map_uuid", mapId.getValue().toString()));
        itemTag.put(new IntTag("map_name_index", mapId.getValue()));
        itemTag.put(new ByteTag("map_display_players", (byte) 1));
        itemTag.remove("map");
    }
}