Search in sources :

Example 1 with NumberTag

use of com.github.steveice10.opennbt.tag.builtin.NumberTag in project ViaVersion by ViaVersion.

the class InventoryPackets method handleItemToClient.

@Override
public Item handleItemToClient(Item item) {
    if (item == null)
        return null;
    CompoundTag tag = item.tag();
    // Save original id
    int originalId = (item.identifier() << 16 | item.data() & 0xFFFF);
    int rawId = (item.identifier() << 4 | item.data() & 0xF);
    // NBT Additions
    if (isDamageable(item.identifier())) {
        if (tag == null)
            item.setTag(tag = new CompoundTag());
        tag.put("Damage", new IntTag(item.data()));
    }
    if (item.identifier() == 358) {
        // map
        if (tag == null)
            item.setTag(tag = new CompoundTag());
        tag.put("map", new IntTag(item.data()));
    }
    // NBT Changes
    if (tag != null) {
        // Invert banner/shield color id
        boolean banner = item.identifier() == 425;
        if (banner || item.identifier() == 442) {
            if (tag.get("BlockEntityTag") instanceof CompoundTag) {
                CompoundTag blockEntityTag = tag.get("BlockEntityTag");
                if (blockEntityTag.get("Base") instanceof IntTag) {
                    IntTag base = blockEntityTag.get("Base");
                    // Set banner item id according to nbt
                    if (banner) {
                        rawId = 6800 + base.asInt();
                    }
                    base.setValue(15 - base.asInt());
                }
                if (blockEntityTag.get("Patterns") instanceof ListTag) {
                    for (Tag pattern : (ListTag) blockEntityTag.get("Patterns")) {
                        if (pattern instanceof CompoundTag) {
                            IntTag c = ((CompoundTag) pattern).get("Color");
                            // Invert color id
                            c.setValue(15 - c.asInt());
                        }
                    }
                }
            }
        }
        // Display Name now uses JSON
        if (tag.get("display") instanceof CompoundTag) {
            CompoundTag display = tag.get("display");
            if (display.get("Name") instanceof StringTag) {
                StringTag name = display.get("Name");
                display.put(NBT_TAG_NAME + "|Name", new StringTag(name.getValue()));
                name.setValue(ChatRewriter.legacyTextToJsonString(name.getValue(), true));
            }
        }
        // ench is now Enchantments and now uses identifiers
        if (tag.get("ench") instanceof ListTag) {
            ListTag ench = tag.get("ench");
            ListTag enchantments = new ListTag(CompoundTag.class);
            for (Tag enchEntry : ench) {
                NumberTag idTag;
                if (enchEntry instanceof CompoundTag && (idTag = ((CompoundTag) enchEntry).get("id")) != null) {
                    CompoundTag enchantmentEntry = new CompoundTag();
                    short oldId = idTag.asShort();
                    String newId = Protocol1_13To1_12_2.MAPPINGS.getOldEnchantmentsIds().get(oldId);
                    if (newId == null) {
                        newId = "viaversion:legacy/" + oldId;
                    }
                    enchantmentEntry.put("id", new StringTag(newId));
                    enchantmentEntry.put("lvl", new ShortTag(((NumberTag) ((CompoundTag) enchEntry).get("lvl")).asShort()));
                    enchantments.add(enchantmentEntry);
                }
            }
            tag.remove("ench");
            tag.put("Enchantments", enchantments);
        }
        if (tag.get("StoredEnchantments") instanceof ListTag) {
            ListTag storedEnch = tag.get("StoredEnchantments");
            ListTag newStoredEnch = new ListTag(CompoundTag.class);
            for (Tag enchEntry : storedEnch) {
                if (enchEntry instanceof CompoundTag) {
                    CompoundTag enchantmentEntry = new CompoundTag();
                    short oldId = ((NumberTag) ((CompoundTag) enchEntry).get("id")).asShort();
                    String newId = Protocol1_13To1_12_2.MAPPINGS.getOldEnchantmentsIds().get(oldId);
                    if (newId == null) {
                        newId = "viaversion:legacy/" + oldId;
                    }
                    enchantmentEntry.put("id", new StringTag(newId));
                    enchantmentEntry.put("lvl", new ShortTag(((NumberTag) ((CompoundTag) enchEntry).get("lvl")).asShort()));
                    newStoredEnch.add(enchantmentEntry);
                }
            }
            tag.remove("StoredEnchantments");
            tag.put("StoredEnchantments", newStoredEnch);
        }
        if (tag.get("CanPlaceOn") instanceof ListTag) {
            ListTag old = tag.get("CanPlaceOn");
            ListTag newCanPlaceOn = new ListTag(StringTag.class);
            // There will be data losing
            tag.put(NBT_TAG_NAME + "|CanPlaceOn", ConverterRegistry.convertToTag(ConverterRegistry.convertToValue(old)));
            for (Tag oldTag : old) {
                Object value = oldTag.getValue();
                String oldId = value.toString().replace("minecraft:", "");
                String numberConverted = BlockIdData.numberIdToString.get(Ints.tryParse(oldId));
                if (numberConverted != null) {
                    oldId = numberConverted;
                }
                String[] newValues = BlockIdData.blockIdMapping.get(oldId.toLowerCase(Locale.ROOT));
                if (newValues != null) {
                    for (String newValue : newValues) {
                        newCanPlaceOn.add(new StringTag(newValue));
                    }
                } else {
                    newCanPlaceOn.add(new StringTag(oldId.toLowerCase(Locale.ROOT)));
                }
            }
            tag.put("CanPlaceOn", newCanPlaceOn);
        }
        if (tag.get("CanDestroy") instanceof ListTag) {
            ListTag old = tag.get("CanDestroy");
            ListTag newCanDestroy = new ListTag(StringTag.class);
            // There will be data losing
            tag.put(NBT_TAG_NAME + "|CanDestroy", ConverterRegistry.convertToTag(ConverterRegistry.convertToValue(old)));
            for (Tag oldTag : old) {
                Object value = oldTag.getValue();
                String oldId = value.toString().replace("minecraft:", "");
                String numberConverted = BlockIdData.numberIdToString.get(Ints.tryParse(oldId));
                if (numberConverted != null) {
                    oldId = numberConverted;
                }
                String[] newValues = BlockIdData.blockIdMapping.get(oldId.toLowerCase(Locale.ROOT));
                if (newValues != null) {
                    for (String newValue : newValues) {
                        newCanDestroy.add(new StringTag(newValue));
                    }
                } else {
                    newCanDestroy.add(new StringTag(oldId.toLowerCase(Locale.ROOT)));
                }
            }
            tag.put("CanDestroy", newCanDestroy);
        }
        // Handle SpawnEggs
        if (item.identifier() == 383) {
            if (tag.get("EntityTag") instanceof CompoundTag) {
                CompoundTag entityTag = tag.get("EntityTag");
                if (entityTag.get("id") instanceof StringTag) {
                    StringTag identifier = entityTag.get("id");
                    rawId = SpawnEggRewriter.getSpawnEggId(identifier.getValue());
                    if (rawId == -1) {
                        // Bat fallback
                        rawId = 25100288;
                    } else {
                        entityTag.remove("id");
                        if (entityTag.isEmpty())
                            tag.remove("EntityTag");
                    }
                } else {
                    // Fallback to bat
                    rawId = 25100288;
                }
            } else {
                // Fallback to bat
                rawId = 25100288;
            }
        }
        if (tag.isEmpty()) {
            item.setTag(tag = null);
        }
    }
    if (!Protocol1_13To1_12_2.MAPPINGS.getItemMappings().containsKey(rawId)) {
        if (!isDamageable(item.identifier()) && item.identifier() != 358) {
            // Map
            if (tag == null)
                item.setTag(tag = new CompoundTag());
            // Data will be lost, saving original id
            tag.put(NBT_TAG_NAME, new IntTag(originalId));
        }
        if (item.identifier() == 31 && item.data() == 0) {
            // Shrub was removed
            // Dead Bush
            rawId = 32 << 4;
        } else if (Protocol1_13To1_12_2.MAPPINGS.getItemMappings().containsKey(rawId & ~0xF)) {
            // Remove data
            rawId &= ~0xF;
        } else {
            if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) {
                Via.getPlatform().getLogger().warning("Failed to get 1.13 item for " + item.identifier());
            }
            // Stone
            rawId = 16;
        }
    }
    item.setIdentifier(Protocol1_13To1_12_2.MAPPINGS.getItemMappings().get(rawId));
    item.setData((short) 0);
    return item;
}
Also used : StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) ListTag(com.github.steveice10.opennbt.tag.builtin.ListTag) ShortTag(com.github.steveice10.opennbt.tag.builtin.ShortTag) NumberTag(com.github.steveice10.opennbt.tag.builtin.NumberTag) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) ListTag(com.github.steveice10.opennbt.tag.builtin.ListTag) NumberTag(com.github.steveice10.opennbt.tag.builtin.NumberTag) IntTag(com.github.steveice10.opennbt.tag.builtin.IntTag) StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) Tag(com.github.steveice10.opennbt.tag.builtin.Tag) ShortTag(com.github.steveice10.opennbt.tag.builtin.ShortTag) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) IntTag(com.github.steveice10.opennbt.tag.builtin.IntTag)

Example 2 with NumberTag

use of com.github.steveice10.opennbt.tag.builtin.NumberTag in project ViaVersion by ViaVersion.

the class WorldPackets method register.

public static void register(Protocol protocol) {
    // Outgoing packets
    protocol.registerClientbound(ClientboundPackets1_12_1.SPAWN_PAINTING, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // 1 - Entity UUID
            map(Type.UUID);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    PaintingProvider provider = Via.getManager().getProviders().get(PaintingProvider.class);
                    String motive = wrapper.read(Type.STRING);
                    Optional<Integer> id = provider.getIntByIdentifier(motive);
                    if (!id.isPresent() && (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug())) {
                        Via.getPlatform().getLogger().warning("Could not find painting motive: " + motive + " falling back to default (0)");
                    }
                    wrapper.write(Type.VAR_INT, id.orElse(0));
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_12_1.BLOCK_ENTITY_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Location
            map(Type.POSITION);
            // 1 - Action
            map(Type.UNSIGNED_BYTE);
            // 2 - NBT data
            map(Type.NBT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    Position position = wrapper.get(Type.POSITION, 0);
                    short action = wrapper.get(Type.UNSIGNED_BYTE, 0);
                    CompoundTag tag = wrapper.get(Type.NBT, 0);
                    BlockEntityProvider provider = Via.getManager().getProviders().get(BlockEntityProvider.class);
                    int newId = provider.transform(wrapper.user(), position, tag, true);
                    if (newId != -1) {
                        BlockStorage storage = wrapper.user().get(BlockStorage.class);
                        BlockStorage.ReplacementData replacementData = storage.get(position);
                        if (replacementData != null) {
                            replacementData.setReplacement(newId);
                        }
                    }
                    if (action == 5) {
                        // Set type of flower in flower pot
                        // Removed
                        wrapper.cancel();
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_12_1.BLOCK_ACTION, new PacketRemapper() {

        @Override
        public void registerMap() {
            // Location
            map(Type.POSITION);
            // Action Id
            map(Type.UNSIGNED_BYTE);
            // Action param
            map(Type.UNSIGNED_BYTE);
            // Block Id - /!\ NOT BLOCK STATE ID
            map(Type.VAR_INT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    Position pos = wrapper.get(Type.POSITION, 0);
                    short action = wrapper.get(Type.UNSIGNED_BYTE, 0);
                    short param = wrapper.get(Type.UNSIGNED_BYTE, 1);
                    int blockId = wrapper.get(Type.VAR_INT, 0);
                    if (blockId == 25)
                        blockId = 73;
                    else if (blockId == 33)
                        blockId = 99;
                    else if (blockId == 29)
                        blockId = 92;
                    else if (blockId == 54)
                        blockId = 142;
                    else if (blockId == 146)
                        blockId = 305;
                    else if (blockId == 130)
                        blockId = 249;
                    else if (blockId == 138)
                        blockId = 257;
                    else if (blockId == 52)
                        blockId = 140;
                    else if (blockId == 209)
                        blockId = 472;
                    else if (blockId >= 219 && blockId <= 234)
                        blockId = blockId - 219 + 483;
                    if (blockId == 73) {
                        // Note block
                        // block change
                        PacketWrapper blockChange = wrapper.create(0x0B);
                        blockChange.write(Type.POSITION, pos);
                        blockChange.write(Type.VAR_INT, 249 + (action * 24 * 2) + (param * 2));
                        blockChange.send(Protocol1_13To1_12_2.class);
                    }
                    wrapper.set(Type.VAR_INT, 0, blockId);
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_12_1.BLOCK_CHANGE, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.POSITION);
            map(Type.VAR_INT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    Position position = wrapper.get(Type.POSITION, 0);
                    int newId = toNewId(wrapper.get(Type.VAR_INT, 0));
                    UserConnection userConnection = wrapper.user();
                    if (Via.getConfig().isServersideBlockConnections()) {
                        ConnectionData.updateBlockStorage(userConnection, position.x(), position.y(), position.z(), newId);
                        newId = ConnectionData.connect(userConnection, position, newId);
                    }
                    wrapper.set(Type.VAR_INT, 0, checkStorage(wrapper.user(), position, newId));
                    if (Via.getConfig().isServersideBlockConnections()) {
                        // Workaround for packet order issue
                        wrapper.send(Protocol1_13To1_12_2.class);
                        wrapper.cancel();
                        ConnectionData.update(userConnection, position);
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_12_1.MULTI_BLOCK_CHANGE, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Chunk X
            map(Type.INT);
            // 1 - Chunk Z
            map(Type.INT);
            // 2 - Records
            map(Type.BLOCK_CHANGE_RECORD_ARRAY);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int chunkX = wrapper.get(Type.INT, 0);
                    int chunkZ = wrapper.get(Type.INT, 1);
                    UserConnection userConnection = wrapper.user();
                    BlockChangeRecord[] records = wrapper.get(Type.BLOCK_CHANGE_RECORD_ARRAY, 0);
                    // Convert ids
                    for (BlockChangeRecord record : records) {
                        int newBlock = toNewId(record.getBlockId());
                        Position position = new Position(record.getSectionX() + (chunkX * 16), record.getY(), record.getSectionZ() + (chunkZ * 16));
                        if (Via.getConfig().isServersideBlockConnections()) {
                            ConnectionData.updateBlockStorage(userConnection, position.x(), position.y(), position.z(), newBlock);
                        }
                        record.setBlockId(checkStorage(wrapper.user(), position, newBlock));
                    }
                    if (Via.getConfig().isServersideBlockConnections()) {
                        for (BlockChangeRecord record : records) {
                            int blockState = record.getBlockId();
                            Position position = new Position(record.getSectionX() + (chunkX * 16), record.getY(), record.getSectionZ() + (chunkZ * 16));
                            ConnectionHandler handler = ConnectionData.getConnectionHandler(blockState);
                            if (handler != null) {
                                blockState = handler.connect(userConnection, position, blockState);
                                record.setBlockId(blockState);
                            }
                        }
                        // Workaround for packet order issue
                        wrapper.send(Protocol1_13To1_12_2.class);
                        wrapper.cancel();
                        for (BlockChangeRecord record : records) {
                            Position position = new Position(record.getSectionX() + (chunkX * 16), record.getY(), record.getSectionZ() + (chunkZ * 16));
                            ConnectionData.update(userConnection, position);
                        }
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_12_1.EXPLOSION, new PacketRemapper() {

        @Override
        public void registerMap() {
            if (!Via.getConfig().isServersideBlockConnections())
                return;
            // X
            map(Type.FLOAT);
            // Y
            map(Type.FLOAT);
            // Z
            map(Type.FLOAT);
            // Radius
            map(Type.FLOAT);
            // Record Count
            map(Type.INT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    UserConnection userConnection = wrapper.user();
                    int x = (int) Math.floor(wrapper.get(Type.FLOAT, 0));
                    int y = (int) Math.floor(wrapper.get(Type.FLOAT, 1));
                    int z = (int) Math.floor(wrapper.get(Type.FLOAT, 2));
                    int recordCount = wrapper.get(Type.INT, 0);
                    Position[] records = new Position[recordCount];
                    for (int i = 0; i < recordCount; i++) {
                        Position position = new Position(x + wrapper.passthrough(Type.BYTE), (short) (y + wrapper.passthrough(Type.BYTE)), z + wrapper.passthrough(Type.BYTE));
                        records[i] = position;
                        // Set to air
                        ConnectionData.updateBlockStorage(userConnection, position.x(), position.y(), position.z(), 0);
                    }
                    // Workaround for packet order issue
                    wrapper.send(Protocol1_13To1_12_2.class);
                    wrapper.cancel();
                    for (int i = 0; i < recordCount; i++) {
                        ConnectionData.update(userConnection, records[i]);
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_12_1.UNLOAD_CHUNK, new PacketRemapper() {

        @Override
        public void registerMap() {
            if (Via.getConfig().isServersideBlockConnections()) {
                handler(new PacketHandler() {

                    @Override
                    public void handle(PacketWrapper wrapper) throws Exception {
                        int x = wrapper.passthrough(Type.INT);
                        int z = wrapper.passthrough(Type.INT);
                        ConnectionData.blockConnectionProvider.unloadChunk(wrapper.user(), x, z);
                    }
                });
            }
        }
    });
    protocol.registerClientbound(ClientboundPackets1_12_1.NAMED_SOUND, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.STRING);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    String sound = wrapper.get(Type.STRING, 0).replace("minecraft:", "");
                    String newSoundId = NamedSoundRewriter.getNewId(sound);
                    wrapper.set(Type.STRING, 0, newSoundId);
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_12_1.CHUNK_DATA, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
                    BlockStorage storage = wrapper.user().get(BlockStorage.class);
                    Chunk1_9_3_4Type type = new Chunk1_9_3_4Type(clientWorld);
                    Chunk1_13Type type1_13 = new Chunk1_13Type(clientWorld);
                    Chunk chunk = wrapper.read(type);
                    wrapper.write(type1_13, chunk);
                    for (int i = 0; i < chunk.getSections().length; i++) {
                        ChunkSection section = chunk.getSections()[i];
                        if (section == null)
                            continue;
                        for (int p = 0; p < section.getPaletteSize(); p++) {
                            int old = section.getPaletteEntry(p);
                            int newId = toNewId(old);
                            section.setPaletteEntry(p, newId);
                        }
                        boolean willSaveToStorage = false;
                        for (int p = 0; p < section.getPaletteSize(); p++) {
                            int newId = section.getPaletteEntry(p);
                            if (storage.isWelcome(newId)) {
                                willSaveToStorage = true;
                                break;
                            }
                        }
                        boolean willSaveConnection = false;
                        if (Via.getConfig().isServersideBlockConnections() && ConnectionData.needStoreBlocks()) {
                            for (int p = 0; p < section.getPaletteSize(); p++) {
                                int newId = section.getPaletteEntry(p);
                                if (ConnectionData.isWelcome(newId)) {
                                    willSaveConnection = true;
                                    break;
                                }
                            }
                        }
                        if (willSaveToStorage) {
                            for (int y = 0; y < 16; y++) {
                                for (int z = 0; z < 16; z++) {
                                    for (int x = 0; x < 16; x++) {
                                        int block = section.getFlatBlock(x, y, z);
                                        if (storage.isWelcome(block)) {
                                            storage.store(new Position((x + (chunk.getX() << 4)), (short) (y + (i << 4)), (z + (chunk.getZ() << 4))), block);
                                        }
                                    }
                                }
                            }
                        }
                        if (willSaveConnection) {
                            for (int y = 0; y < 16; y++) {
                                for (int z = 0; z < 16; z++) {
                                    for (int x = 0; x < 16; x++) {
                                        int block = section.getFlatBlock(x, y, z);
                                        if (ConnectionData.isWelcome(block)) {
                                            ConnectionData.blockConnectionProvider.storeBlock(wrapper.user(), x + (chunk.getX() << 4), y + (i << 4), z + (chunk.getZ() << 4), block);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    // Rewrite biome id 255 to plains
                    if (chunk.isBiomeData()) {
                        int latestBiomeWarn = Integer.MIN_VALUE;
                        for (int i = 0; i < 256; i++) {
                            int biome = chunk.getBiomeData()[i];
                            if (!VALID_BIOMES.contains(biome)) {
                                if (// is it generated naturally? *shrug*
                                biome != 255 && latestBiomeWarn != biome) {
                                    if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) {
                                        Via.getPlatform().getLogger().warning("Received invalid biome id " + biome);
                                    }
                                    latestBiomeWarn = biome;
                                }
                                // Plains
                                chunk.getBiomeData()[i] = 1;
                            }
                        }
                    }
                    // Rewrite BlockEntities to normal blocks
                    BlockEntityProvider provider = Via.getManager().getProviders().get(BlockEntityProvider.class);
                    final Iterator<CompoundTag> iterator = chunk.getBlockEntities().iterator();
                    while (iterator.hasNext()) {
                        CompoundTag tag = iterator.next();
                        int newId = provider.transform(wrapper.user(), null, tag, false);
                        if (newId != -1) {
                            int x = ((NumberTag) tag.get("x")).asInt();
                            int y = ((NumberTag) tag.get("y")).asInt();
                            int z = ((NumberTag) tag.get("z")).asInt();
                            Position position = new Position(x, (short) y, z);
                            // Store the replacement blocks for blockupdates
                            BlockStorage.ReplacementData replacementData = storage.get(position);
                            if (replacementData != null) {
                                replacementData.setReplacement(newId);
                            }
                            chunk.getSections()[y >> 4].setFlatBlock(x & 0xF, y & 0xF, z & 0xF, newId);
                        }
                        final Tag idTag = tag.get("id");
                        if (idTag instanceof StringTag) {
                            // No longer block entities
                            final String id = ((StringTag) idTag).getValue();
                            if (id.equals("minecraft:noteblock") || id.equals("minecraft:flower_pot")) {
                                iterator.remove();
                            }
                        }
                    }
                    if (Via.getConfig().isServersideBlockConnections()) {
                        ConnectionData.connectBlocks(wrapper.user(), chunk);
                        // Workaround for packet order issue
                        wrapper.send(Protocol1_13To1_12_2.class);
                        wrapper.cancel();
                        for (int i = 0; i < chunk.getSections().length; i++) {
                            ChunkSection section = chunk.getSections()[i];
                            if (section == null)
                                continue;
                            ConnectionData.updateChunkSectionNeighbours(wrapper.user(), chunk.getX(), chunk.getZ(), i);
                        }
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_12_1.SPAWN_PARTICLE, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Particle ID
            map(Type.INT);
            // 1 - Long Distance
            map(Type.BOOLEAN);
            // 2 - X
            map(Type.FLOAT);
            // 3 - Y
            map(Type.FLOAT);
            // 4 - Z
            map(Type.FLOAT);
            // 5 - Offset X
            map(Type.FLOAT);
            // 6 - Offset Y
            map(Type.FLOAT);
            // 7 - Offset Z
            map(Type.FLOAT);
            // 8 - Particle Data
            map(Type.FLOAT);
            // 9 - Particle Count
            map(Type.INT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int particleId = wrapper.get(Type.INT, 0);
                    // Get the data (Arrays are overrated)
                    int dataCount = 0;
                    // Particles with 1 data [BlockCrack,BlockDust,FallingDust]
                    if (particleId == 37 || particleId == 38 || particleId == 46)
                        dataCount = 1;
                    else // Particles with 2 data [IconCrack]
                    if (particleId == 36)
                        dataCount = 2;
                    Integer[] data = new Integer[dataCount];
                    for (int i = 0; i < data.length; i++) data[i] = wrapper.read(Type.VAR_INT);
                    Particle particle = ParticleRewriter.rewriteParticle(particleId, data);
                    // Cancel if null or completely removed
                    if (particle == null || particle.getId() == -1) {
                        wrapper.cancel();
                        return;
                    }
                    // Handle reddust particle color
                    if (particle.getId() == 11) {
                        int count = wrapper.get(Type.INT, 1);
                        float speed = wrapper.get(Type.FLOAT, 6);
                        // Only handle for count = 0
                        if (count == 0) {
                            wrapper.set(Type.INT, 1, 1);
                            wrapper.set(Type.FLOAT, 6, 0f);
                            List<Particle.ParticleData> arguments = particle.getArguments();
                            for (int i = 0; i < 3; i++) {
                                // RGB values are represented by the X/Y/Z offset
                                float colorValue = wrapper.get(Type.FLOAT, i + 3) * speed;
                                if (colorValue == 0 && i == 0) {
                                    // https://minecraft.gamepedia.com/User:Alphappy/reddust
                                    colorValue = 1;
                                }
                                arguments.get(i).setValue(colorValue);
                                wrapper.set(Type.FLOAT, i + 3, 0f);
                            }
                        }
                    }
                    wrapper.set(Type.INT, 0, particle.getId());
                    for (Particle.ParticleData particleData : particle.getArguments()) wrapper.write(particleData.getType(), particleData.getValue());
                }
            });
        }
    });
}
Also used : StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Chunk1_9_3_4Type(com.viaversion.viaversion.protocols.protocol1_9_1_2to1_9_3_4.types.Chunk1_9_3_4Type) Particle(com.viaversion.viaversion.api.type.types.Particle) BlockChangeRecord(com.viaversion.viaversion.api.minecraft.BlockChangeRecord) BlockStorage(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage.BlockStorage) Chunk1_13Type(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.types.Chunk1_13Type) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) Position(com.viaversion.viaversion.api.minecraft.Position) PaintingProvider(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.providers.PaintingProvider) Chunk(com.viaversion.viaversion.api.minecraft.chunks.Chunk) UserConnection(com.viaversion.viaversion.api.connection.UserConnection) ConnectionHandler(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.blockconnections.ConnectionHandler) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) NumberTag(com.github.steveice10.opennbt.tag.builtin.NumberTag) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) NumberTag(com.github.steveice10.opennbt.tag.builtin.NumberTag) StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) Tag(com.github.steveice10.opennbt.tag.builtin.Tag) BlockEntityProvider(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.providers.BlockEntityProvider) ChunkSection(com.viaversion.viaversion.api.minecraft.chunks.ChunkSection)

Example 3 with NumberTag

use of com.github.steveice10.opennbt.tag.builtin.NumberTag in project ViaVersion by ViaVersion.

the class BedHandler method transform.

@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 += (((NumberTag) color).asInt() * 16);
    }
    return blockId;
}
Also used : BlockStorage(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage.BlockStorage) Position(com.viaversion.viaversion.api.minecraft.Position) Tag(com.github.steveice10.opennbt.tag.builtin.Tag) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) NumberTag(com.github.steveice10.opennbt.tag.builtin.NumberTag)

Example 4 with NumberTag

use of com.github.steveice10.opennbt.tag.builtin.NumberTag in project ViaVersion by ViaVersion.

the class InventoryPackets method oldToNewAttributes.

public static void oldToNewAttributes(Item item) {
    if (item.tag() == null)
        return;
    ListTag attributes = item.tag().get("AttributeModifiers");
    if (attributes == null)
        return;
    for (Tag tag : attributes) {
        CompoundTag attribute = (CompoundTag) tag;
        rewriteAttributeName(attribute, "AttributeName", false);
        rewriteAttributeName(attribute, "Name", false);
        Tag leastTag = attribute.get("UUIDLeast");
        if (leastTag != null) {
            Tag mostTag = attribute.get("UUIDMost");
            int[] uuidIntArray = UUIDIntArrayType.bitsToIntArray(((NumberTag) leastTag).asLong(), ((NumberTag) mostTag).asLong());
            attribute.put("UUID", new IntArrayTag(uuidIntArray));
        }
    }
}
Also used : IntArrayTag(com.github.steveice10.opennbt.tag.builtin.IntArrayTag) ListTag(com.github.steveice10.opennbt.tag.builtin.ListTag) IntArrayTag(com.github.steveice10.opennbt.tag.builtin.IntArrayTag) NumberTag(com.github.steveice10.opennbt.tag.builtin.NumberTag) StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) Tag(com.github.steveice10.opennbt.tag.builtin.Tag) LongTag(com.github.steveice10.opennbt.tag.builtin.LongTag) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) ListTag(com.github.steveice10.opennbt.tag.builtin.ListTag) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag)

Example 5 with NumberTag

use of com.github.steveice10.opennbt.tag.builtin.NumberTag in project ViaVersion by ViaVersion.

the class InventoryPackets method handleItemToServer.

@Override
public Item handleItemToServer(Item item) {
    if (item == null)
        return null;
    Integer rawId = null;
    boolean gotRawIdFromTag = false;
    CompoundTag tag = item.tag();
    // Use tag to get original ID and data
    if (tag != null) {
        // Check for valid tag
        if (tag.get(NBT_TAG_NAME) instanceof IntTag) {
            rawId = ((NumberTag) tag.get(NBT_TAG_NAME)).asInt();
            // Remove the tag
            tag.remove(NBT_TAG_NAME);
            gotRawIdFromTag = true;
        }
    }
    if (rawId == null) {
        int oldId = Protocol1_13To1_12_2.MAPPINGS.getItemMappings().inverse().get(item.identifier());
        if (oldId != -1) {
            // Handle spawn eggs
            Optional<String> eggEntityId = SpawnEggRewriter.getEntityId(oldId);
            if (eggEntityId.isPresent()) {
                rawId = 383 << 16;
                if (tag == null)
                    item.setTag(tag = new CompoundTag());
                if (!tag.contains("EntityTag")) {
                    CompoundTag entityTag = new CompoundTag();
                    entityTag.put("id", new StringTag(eggEntityId.get()));
                    tag.put("EntityTag", entityTag);
                }
            } else {
                rawId = (oldId >> 4) << 16 | oldId & 0xF;
            }
        }
    }
    if (rawId == null) {
        if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) {
            Via.getPlatform().getLogger().warning("Failed to get 1.12 item for " + item.identifier());
        }
        // Stone
        rawId = 0x10000;
    }
    item.setIdentifier((short) (rawId >> 16));
    item.setData((short) (rawId & 0xFFFF));
    // NBT changes
    if (tag != null) {
        if (isDamageable(item.identifier())) {
            if (tag.get("Damage") instanceof IntTag) {
                if (!gotRawIdFromTag) {
                    item.setData((short) (int) tag.get("Damage").getValue());
                }
                tag.remove("Damage");
            }
        }
        if (item.identifier() == 358) {
            // map
            if (tag.get("map") instanceof IntTag) {
                if (!gotRawIdFromTag) {
                    item.setData((short) (int) tag.get("map").getValue());
                }
                tag.remove("map");
            }
        }
        if (item.identifier() == 442 || item.identifier() == 425) {
            // shield / banner
            if (tag.get("BlockEntityTag") instanceof CompoundTag) {
                CompoundTag blockEntityTag = tag.get("BlockEntityTag");
                if (blockEntityTag.get("Base") instanceof IntTag) {
                    IntTag base = blockEntityTag.get("Base");
                    // invert color id
                    base.setValue(15 - base.asInt());
                }
                if (blockEntityTag.get("Patterns") instanceof ListTag) {
                    for (Tag pattern : (ListTag) blockEntityTag.get("Patterns")) {
                        if (pattern instanceof CompoundTag) {
                            IntTag c = ((CompoundTag) pattern).get("Color");
                            // Invert color id
                            c.setValue(15 - c.asInt());
                        }
                    }
                }
            }
        }
        // Display Name now uses JSON
        if (tag.get("display") instanceof CompoundTag) {
            CompoundTag display = tag.get("display");
            if (display.get("Name") instanceof StringTag) {
                StringTag name = display.get("Name");
                StringTag via = display.remove(NBT_TAG_NAME + "|Name");
                name.setValue(via != null ? via.getValue() : ChatRewriter.jsonToLegacyText(name.getValue()));
            }
        }
        // ench is now Enchantments and now uses identifiers
        if (tag.get("Enchantments") instanceof ListTag) {
            ListTag enchantments = tag.get("Enchantments");
            ListTag ench = new ListTag(CompoundTag.class);
            for (Tag enchantmentEntry : enchantments) {
                if (enchantmentEntry instanceof CompoundTag) {
                    CompoundTag enchEntry = new CompoundTag();
                    String newId = (String) ((CompoundTag) enchantmentEntry).get("id").getValue();
                    Short oldId = Protocol1_13To1_12_2.MAPPINGS.getOldEnchantmentsIds().inverse().get(newId);
                    if (oldId == null && newId.startsWith("viaversion:legacy/")) {
                        oldId = Short.valueOf(newId.substring(18));
                    }
                    if (oldId != null) {
                        enchEntry.put("id", new ShortTag(oldId));
                        enchEntry.put("lvl", new ShortTag(((NumberTag) ((CompoundTag) enchantmentEntry).get("lvl")).asShort()));
                        ench.add(enchEntry);
                    }
                }
            }
            tag.remove("Enchantments");
            tag.put("ench", ench);
        }
        if (tag.get("StoredEnchantments") instanceof ListTag) {
            ListTag storedEnch = tag.get("StoredEnchantments");
            ListTag newStoredEnch = new ListTag(CompoundTag.class);
            for (Tag enchantmentEntry : storedEnch) {
                if (enchantmentEntry instanceof CompoundTag) {
                    CompoundTag enchEntry = new CompoundTag();
                    String newId = (String) ((CompoundTag) enchantmentEntry).get("id").getValue();
                    Short oldId = Protocol1_13To1_12_2.MAPPINGS.getOldEnchantmentsIds().inverse().get(newId);
                    if (oldId == null && newId.startsWith("viaversion:legacy/")) {
                        oldId = Short.valueOf(newId.substring(18));
                    }
                    if (oldId != null) {
                        enchEntry.put("id", new ShortTag(oldId));
                        enchEntry.put("lvl", new ShortTag(((NumberTag) ((CompoundTag) enchantmentEntry).get("lvl")).asShort()));
                        newStoredEnch.add(enchEntry);
                    }
                }
            }
            tag.remove("StoredEnchantments");
            tag.put("StoredEnchantments", newStoredEnch);
        }
        if (tag.get(NBT_TAG_NAME + "|CanPlaceOn") instanceof ListTag) {
            tag.put("CanPlaceOn", ConverterRegistry.convertToTag(ConverterRegistry.convertToValue(tag.get(NBT_TAG_NAME + "|CanPlaceOn"))));
            tag.remove(NBT_TAG_NAME + "|CanPlaceOn");
        } else if (tag.get("CanPlaceOn") instanceof ListTag) {
            ListTag old = tag.get("CanPlaceOn");
            ListTag newCanPlaceOn = new ListTag(StringTag.class);
            for (Tag oldTag : old) {
                Object value = oldTag.getValue();
                String[] newValues = BlockIdData.fallbackReverseMapping.get(value instanceof String ? ((String) value).replace("minecraft:", "") : null);
                if (newValues != null) {
                    for (String newValue : newValues) {
                        newCanPlaceOn.add(new StringTag(newValue));
                    }
                } else {
                    newCanPlaceOn.add(oldTag);
                }
            }
            tag.put("CanPlaceOn", newCanPlaceOn);
        }
        if (tag.get(NBT_TAG_NAME + "|CanDestroy") instanceof ListTag) {
            tag.put("CanDestroy", ConverterRegistry.convertToTag(ConverterRegistry.convertToValue(tag.get(NBT_TAG_NAME + "|CanDestroy"))));
            tag.remove(NBT_TAG_NAME + "|CanDestroy");
        } else if (tag.get("CanDestroy") instanceof ListTag) {
            ListTag old = tag.get("CanDestroy");
            ListTag newCanDestroy = new ListTag(StringTag.class);
            for (Tag oldTag : old) {
                Object value = oldTag.getValue();
                String[] newValues = BlockIdData.fallbackReverseMapping.get(value instanceof String ? ((String) value).replace("minecraft:", "") : null);
                if (newValues != null) {
                    for (String newValue : newValues) {
                        newCanDestroy.add(new StringTag(newValue));
                    }
                } else {
                    newCanDestroy.add(oldTag);
                }
            }
            tag.put("CanDestroy", newCanDestroy);
        }
    }
    return item;
}
Also used : StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) ListTag(com.github.steveice10.opennbt.tag.builtin.ListTag) ShortTag(com.github.steveice10.opennbt.tag.builtin.ShortTag) NumberTag(com.github.steveice10.opennbt.tag.builtin.NumberTag) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) ListTag(com.github.steveice10.opennbt.tag.builtin.ListTag) NumberTag(com.github.steveice10.opennbt.tag.builtin.NumberTag) IntTag(com.github.steveice10.opennbt.tag.builtin.IntTag) StringTag(com.github.steveice10.opennbt.tag.builtin.StringTag) Tag(com.github.steveice10.opennbt.tag.builtin.Tag) ShortTag(com.github.steveice10.opennbt.tag.builtin.ShortTag) CompoundTag(com.github.steveice10.opennbt.tag.builtin.CompoundTag) IntTag(com.github.steveice10.opennbt.tag.builtin.IntTag)

Aggregations

CompoundTag (com.github.steveice10.opennbt.tag.builtin.CompoundTag)10 NumberTag (com.github.steveice10.opennbt.tag.builtin.NumberTag)10 Tag (com.github.steveice10.opennbt.tag.builtin.Tag)8 StringTag (com.github.steveice10.opennbt.tag.builtin.StringTag)7 ListTag (com.github.steveice10.opennbt.tag.builtin.ListTag)5 Position (com.viaversion.viaversion.api.minecraft.Position)5 IntTag (com.github.steveice10.opennbt.tag.builtin.IntTag)4 BlockStorage (com.viaversion.viaversion.protocols.protocol1_13to1_12_2.storage.BlockStorage)4 ShortTag (com.github.steveice10.opennbt.tag.builtin.ShortTag)3 IntArrayTag (com.github.steveice10.opennbt.tag.builtin.IntArrayTag)2 LongTag (com.github.steveice10.opennbt.tag.builtin.LongTag)2 PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)2 ByteArrayTag (com.github.steveice10.opennbt.tag.builtin.ByteArrayTag)1 ByteTag (com.github.steveice10.opennbt.tag.builtin.ByteTag)1 DoubleTag (com.github.steveice10.opennbt.tag.builtin.DoubleTag)1 FloatTag (com.github.steveice10.opennbt.tag.builtin.FloatTag)1 LongArrayTag (com.github.steveice10.opennbt.tag.builtin.LongArrayTag)1 Via (com.viaversion.viaversion.api.Via)1 UserConnection (com.viaversion.viaversion.api.connection.UserConnection)1 EntityTracker (com.viaversion.viaversion.api.data.entity.EntityTracker)1