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

The following examples show how to use com.github.steveice10.opennbt.tag.builtin.CompoundTag#get() . 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: 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 2
Source File: BannerBlockEntityTranslator.java    From Geyser with MIT License 6 votes vote down vote up
@Override
public List<Tag<?>> translateTag(CompoundTag tag, BlockState blockState) {
    List<Tag<?>> tags = new ArrayList<>();

    int bannerColor = BlockStateValues.getBannerColor(blockState);
    if (bannerColor != -1) {
        tags.add(new IntTag("Base", 15 - bannerColor));
    }

    if (tag.contains("Patterns")) {
        ListTag patterns = tag.get("Patterns");
        tags.add(BannerTranslator.convertBannerPattern(patterns));
    }

    if (tag.contains("CustomName")) {
        tags.add(new StringTag("CustomName", (String) tag.get("CustomName").getValue()));
    }

    return tags;
}
 
Example 3
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 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: 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 6
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 7
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 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: BannerTranslator.java    From Geyser with MIT License 6 votes vote down vote up
@Override
public ItemData translateToBedrock(ItemStack itemStack, ItemEntry itemEntry) {
    if (itemStack.getNbt() == null) return super.translateToBedrock(itemStack, itemEntry);

    ItemData itemData = super.translateToBedrock(itemStack, itemEntry);

    CompoundTag blockEntityTag = itemStack.getNbt().get("BlockEntityTag");
    if (blockEntityTag.contains("Patterns")) {
        ListTag patterns = blockEntityTag.get("Patterns");

        CompoundTagBuilder builder = itemData.getTag().toBuilder();
        builder.tag(convertBannerPattern(patterns));

        itemData = ItemData.of(itemData.getId(), itemData.getDamage(), itemData.getCount(), builder.buildRootTag());
    }

    return itemData;
}
 
Example 10
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 11
Source File: EndGatewayBlockEntityTranslator.java    From Geyser with MIT License 5 votes vote down vote up
private int getExitPortalCoordinate(CompoundTag tag, String axis) {
    // Return 0 if it doesn't exist, otherwise give proper value
    if (tag.get("ExitPortal") != null) {
        LinkedHashMap compoundTag = (LinkedHashMap) tag.get("ExitPortal").getValue();
        com.github.steveice10.opennbt.tag.builtin.IntTag intTag = (com.github.steveice10.opennbt.tag.builtin.IntTag) compoundTag.get(axis);
        return intTag.getValue();
    } return 0;
}
 
Example 12
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 13
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 14
Source File: LeatherArmorTranslator.java    From Geyser with MIT License 5 votes vote down vote up
@Override
public void translateToBedrock(CompoundTag itemTag, ItemEntry itemEntry) {
    if (!itemTag.contains("display")) {
        return;
    }
    CompoundTag displayTag = itemTag.get("display");
    if (displayTag.contains("color")) {
        IntTag color = displayTag.get("color");
        if (color != null) {
            itemTag.put(new IntTag("customColor", color.getValue()));
            displayTag.remove("color");
        }
    }
}
 
Example 15
Source File: MapItemTranslator.java    From Geyser with MIT License 5 votes vote down vote up
@Override
public void translateToJava(CompoundTag itemTag, ItemEntry itemEntry) {
    IntTag tag = itemTag.get("map_name_index");
    if (tag != null) {
        itemTag.put(new IntTag("map", tag.getValue()));
        itemTag.remove("map_name_index");
        itemTag.remove("map_uuid");
    }
}
 
Example 16
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 17
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 18
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 19
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 20
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;
}