Search in sources :

Example 1 with V1_2Chunk

use of com.github.dirtpowered.dirtmv.data.protocol.objects.V1_2Chunk in project DirtMultiversion by DirtPowered.

the class V1_2_3RChunkDataType method write.

@Override
public void write(TypeHolder typeHolder, PacketOutput packetOutput) {
    V1_2Chunk chunk = (V1_2Chunk) typeHolder.getObject();
    packetOutput.writeInt(chunk.getChunkX());
    packetOutput.writeInt(chunk.getChunkZ());
    packetOutput.writeBoolean(chunk.isGroundUp());
    packetOutput.writeShort(chunk.getPrimaryBitmap() & 65535);
    packetOutput.writeShort(chunk.getAdditionalBitmap() & 65535);
    packetOutput.writeInt(chunk.getCompressedDataSize());
    if (getType() == Type.V1_2_CHUNK)
        packetOutput.writeInt(0);
    packetOutput.writeBytes(chunk.getData(), chunk.getCompressedDataSize());
}
Also used : V1_2Chunk(com.github.dirtpowered.dirtmv.data.protocol.objects.V1_2Chunk)

Example 2 with V1_2Chunk

use of com.github.dirtpowered.dirtmv.data.protocol.objects.V1_2Chunk in project DirtMultiversion by DirtPowered.

the class WorldPackets method registerTranslators.

@Override
public void registerTranslators() {
    // block change
    addTranslator(0x35, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            BlockStorage blockStorage = session.getStorage().get(BlockStorage.class);
            if (blockStorage.getVersion() == getTo()) {
                int x = data.read(Type.INT, 0);
                byte y = data.read(Type.BYTE, 1);
                int z = data.read(Type.INT, 2);
                int chunkX = x >> 4;
                int chunkZ = z >> 4;
                byte blockId = data.read(Type.BYTE, 3);
                if (shouldCache(blockId)) {
                    blockStorage.setBlockAt(chunkX, chunkZ, x, y, z, blockId);
                }
            }
            return PacketUtil.createPacket(0x35, new TypeHolder[] { data.read(0), data.read(1), data.read(2), set(Type.SHORT, data.read(Type.BYTE, 3).shortValue()), data.read(4) });
        }
    });
    // multi block change
    addTranslator(0x34, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            BlockStorage blockStorage = session.getStorage().get(BlockStorage.class);
            if (blockStorage.getVersion() == getTo()) {
                int chunkX = data.read(Type.INT, 0);
                int chunkZ = data.read(Type.INT, 1);
                V1_2MultiBlockArray blocks = data.read(Type.V1_2MULTIBLOCK_ARRAY, 2);
                DataInput dis = new DataInputStream(new ByteArrayInputStream(blocks.getData()));
                for (int i = 0; i < blocks.getRecordCount(); i++) {
                    try {
                        short pos = dis.readShort();
                        int x = pos >> 12 & 15;
                        int y = pos & 255;
                        int z = pos >> 8 & 15;
                        int xPos = x + (chunkX << 4);
                        int zPos = z + (chunkZ << 4);
                        int blockId = dis.readShort() >> 4 & 4095;
                        if (shouldCache(blockId)) {
                            blockStorage.setBlockAt(chunkX, chunkZ, xPos, y, zPos, blockId);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return data;
        }
    });
    // chunk data
    addTranslator(0x33, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            BlockStorage blockStorage = session.getStorage().get(BlockStorage.class);
            V1_2Chunk chunk = data.read(Type.V1_2_CHUNK, 0);
            if (blockStorage.getVersion() == getTo()) {
                List<ExtendedBlockStorage> parts = new ArrayList<>();
                V1_2RChunkStorage storage;
                if (chunk.getStorage() != null) {
                    storage = chunk.getStorage();
                } else {
                    // create chunk storage
                    storage = new V1_2RChunkStorage(true, true, chunk.getChunkX(), chunk.getChunkZ());
                    storage.readChunk(chunk.isGroundUp(), chunk.getPrimaryBitmap(), chunk.getUncompressedData());
                    // cache for later use (r1.8 -> r1.7)
                    chunk.setStorage(storage);
                }
                ExtendedBlockStorage[] columns = storage.getColumnStorage();
                for (int i = 0; i < columns.length; ++i) {
                    ExtendedBlockStorage e = columns[i];
                    boolean f = e != null && !columns[i].isEmpty();
                    if (e != null && (chunk.getPrimaryBitmap() & 1 << i) != 0 && (!chunk.isGroundUp() || f)) {
                        parts.add(e);
                    }
                }
                for (int i = 0; i < parts.size(); ++i) {
                    byte[] blockArray = parts.get(i).getBlockLSBArray();
                    for (int j = 0; j < blockArray.length; ++j) {
                        int x = j & 15;
                        int y = (j >> 8) + i * 16 & 255;
                        int z = j >> 4 & 15;
                        int blockId = blockArray[j] & 255;
                        if (shouldCache(blockId)) {
                            blockStorage.setBlockAt(chunk.getChunkX(), chunk.getChunkZ(), x, y, z, blockId);
                        }
                    }
                }
            }
            return PacketUtil.createPacket(0x33, new TypeHolder[] { set(Type.V1_3_CHUNK, chunk) });
        }
    });
    // pre chunk
    addTranslator(0x32, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            ProtocolStorage storage = session.getStorage();
            byte mode = data.read(Type.BYTE, 2);
            if (mode == 1) {
                return cancel();
            }
            int chunkX = data.read(Type.INT, 0);
            int chunkZ = data.read(Type.INT, 1);
            OpenChestTracker chestTracker = storage.get(OpenChestTracker.class);
            for (BlockLocation entry : chestTracker.getChestStates().keySet()) {
                int x = entry.getX() >> 4;
                int z = entry.getZ() >> 4;
                if (chunkX == x && chunkZ == z) {
                    chestTracker.remove(entry);
                }
            }
            BlockStorage blockStorage = storage.get(BlockStorage.class);
            if (blockStorage.getVersion() == getTo()) {
                blockStorage.removeChunk(chunkX, chunkZ);
            }
            V1_2Chunk chunk = new V1_2Chunk(chunkX, chunkZ, true, (short) 0, (short) 0, 0, new byte[0], new byte[0], null);
            return PacketUtil.createPacket(0x33, new TypeHolder[] { set(Type.V1_3_CHUNK, chunk) });
        }
    });
    // play noteblock
    addTranslator(0x36, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            int x = data.read(Type.INT, 0);
            int y = data.read(Type.SHORT, 1);
            int z = data.read(Type.INT, 2);
            byte type = data.read(Type.BYTE, 3);
            byte pitch = data.read(Type.BYTE, 4);
            ProtocolStorage storage = session.getStorage();
            short blockId = (short) storage.get(BlockStorage.class).getBlockAt(x, y, z);
            WorldSound worldSound;
            OpenChestTracker chestTracker = storage.get(OpenChestTracker.class);
            switch(type) {
                case 0:
                    if (blockId == 33 || blockId == 29) {
                        worldSound = WorldSound.PISTON_OUT;
                    } else {
                        worldSound = WorldSound.NOTE_HARP;
                    }
                    break;
                case 1:
                    if (blockId == 54) {
                        BlockLocation loc = new BlockLocation(x, y, z);
                        if (pitch == 1) {
                            if (!chestTracker.getState(loc)) {
                                chestTracker.setOpen(loc, true);
                                worldSound = WorldSound.CHEST_OPEN;
                                pitch = 12;
                            } else {
                                worldSound = WorldSound.NO_SOUND;
                            }
                        } else {
                            if (chestTracker.getState(loc)) {
                                worldSound = WorldSound.CHEST_CLOSE;
                                chestTracker.setOpen(loc, false);
                            } else {
                                worldSound = WorldSound.NO_SOUND;
                            }
                        }
                    } else if (blockId == 33 || blockId == 29) {
                        worldSound = WorldSound.PISTON_IN;
                    } else {
                        worldSound = WorldSound.NOTE_CLICK;
                    }
                    break;
                case 2:
                    worldSound = WorldSound.NOTE_SNARE;
                    break;
                case 3:
                    worldSound = WorldSound.NOTE_HAT;
                    break;
                case 4:
                    worldSound = WorldSound.NOTE_BASS_ATTACK;
                    break;
                default:
                    worldSound = WorldSound.NOTE_HARP;
                    break;
            }
            float correctedPitch = (float) (0.5f * (Math.pow(2, pitch / 12.0f)));
            WorldEntityEvent.playSoundAt(session, new Location(x, y, z), worldSound, 3.0f, correctedPitch);
            return PacketUtil.createPacket(0x36, new TypeHolder[] { data.read(0), data.read(1), data.read(2), data.read(3), data.read(4), set(Type.SHORT, blockId) });
        }
    });
}
Also used : ServerSession(com.github.dirtpowered.dirtmv.network.server.ServerSession) ProtocolStorage(com.github.dirtpowered.dirtmv.data.user.ProtocolStorage) PacketTranslator(com.github.dirtpowered.dirtmv.data.translator.PacketTranslator) V1_2Chunk(com.github.dirtpowered.dirtmv.data.protocol.objects.V1_2Chunk) V1_2RChunkStorage(com.github.dirtpowered.dirtmv.data.chunk.storage.V1_2RChunkStorage) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) ExtendedBlockStorage(com.github.dirtpowered.dirtmv.data.chunk.storage.ExtendedBlockStorage) BlockLocation(com.github.dirtpowered.dirtmv.data.protocol.objects.BlockLocation) OpenChestTracker(com.github.dirtpowered.dirtmv.network.versions.Release39To29.sound.OpenChestTracker) WorldSound(com.github.dirtpowered.dirtmv.network.versions.Release39To29.sound.WorldSound) V1_2MultiBlockArray(com.github.dirtpowered.dirtmv.data.protocol.objects.V1_2MultiBlockArray) TypeHolder(com.github.dirtpowered.dirtmv.data.protocol.TypeHolder) DataInput(java.io.DataInput) ExtendedBlockStorage(com.github.dirtpowered.dirtmv.data.chunk.storage.ExtendedBlockStorage) BlockStorage(com.github.dirtpowered.dirtmv.network.versions.Beta17To14.storage.BlockStorage) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) List(java.util.List) PacketData(com.github.dirtpowered.dirtmv.data.protocol.PacketData) BlockLocation(com.github.dirtpowered.dirtmv.data.protocol.objects.BlockLocation) Location(com.github.dirtpowered.dirtmv.data.protocol.objects.Location)

Example 3 with V1_2Chunk

use of com.github.dirtpowered.dirtmv.data.protocol.objects.V1_2Chunk in project DirtMultiversion by DirtPowered.

the class ProtocolRelease47To5 method registerTranslators.

@Override
public void registerTranslators() {
    // status ping
    addTranslator(0x00, ProtocolState.STATUS, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            String json = data.read(Type.V1_7_STRING, 0);
            ServerPing serverPing = DirtMultiVersion.GSON.fromJson(json, ServerPing.class);
            ServerPing.Version versionObj = new ServerPing.Version();
            versionObj.setName("1.8.x");
            versionObj.setProtocol(47);
            serverPing.setVersion(versionObj);
            return PacketUtil.createPacket(0x00, new TypeHolder[] { set(Type.V1_7_STRING, serverPing.toString()) });
        }
    });
    // keep alive
    addTranslator(0x00, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x00, new TypeHolder[] { set(Type.VAR_INT, data.read(Type.INT, 0)) });
        }
    });
    // join game
    addTranslator(0x01, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x01, new TypeHolder[] { data.read(0), data.read(1), data.read(2), data.read(3), data.read(4), data.read(5), set(Type.BOOLEAN, false) });
        }
    });
    // login success
    addTranslator(0x02, ProtocolState.LOGIN, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @SneakyThrows
        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            String username = data.read(Type.STRING, 1);
            // block connection thread until profile is fetched
            UUID uuid = GameProfileFetcher.getProfile(username).get().getId();
            String uniqueId = uuid.toString();
            session.getUserData().setUniqueId(uuid);
            // set compression
            enableCompression(session);
            return PacketUtil.createPacket(0x02, new TypeHolder[] { set(Type.V1_7_STRING, uniqueId), set(Type.V1_7_STRING, username) });
        }
    });
    // spawn position
    addTranslator(0x05, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            int x = data.read(Type.INT, 0);
            int y = data.read(Type.INT, 1);
            int z = data.read(Type.INT, 2);
            return PacketUtil.createPacket(0x05, new TypeHolder[] { set(Type.LONG, toBlockPosition(x, y, z)) });
        }
    });
    // update health
    addTranslator(0x06, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x06, new TypeHolder[] { data.read(0), set(Type.VAR_INT, (int) data.read(Type.SHORT, 1)), data.read(2) });
        }
    });
    // chat
    addTranslator(0x02, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x02, new TypeHolder[] { data.read(0), set(Type.BYTE, (byte) 0) });
        }
    });
    // chunk data
    addTranslator(0x21, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            V1_2Chunk chunk = data.read(Type.V1_3_CHUNK, 0);
            int chunkX = chunk.getChunkX();
            int chunkZ = chunk.getChunkZ();
            short bitmap = chunk.getPrimaryBitmap();
            boolean groundUp = chunk.isGroundUp();
            if (groundUp && bitmap == 0) {
                V1_8Chunk emptyChunk = new V1_8Chunk(chunkX, chunkZ, true, bitmap, new byte[0]);
                ProtocolStorage storage = session.getStorage();
                PortalFrameCache portalFrameCache = storage.get(PortalFrameCache.class);
                portalFrameCache.removeChunk(chunkX, chunkZ);
                return PacketUtil.createPacket(0x21, new TypeHolder[] { new TypeHolder<>(Type.V1_8R_CHUNK, emptyChunk) });
            }
            V1_3ToV1_8ChunkTranslator chunkTransformer;
            if (chunk.getStorage() != null) {
                // use existing chunk storage (pre 1.2 servers)
                chunkTransformer = new V1_3ToV1_8ChunkTranslator(session, chunk.getStorage(), groundUp, bitmap);
            } else {
                ProtocolStorage storage = session.getStorage();
                boolean skyLight = true;
                if (storage.hasObject(DimensionTracker.class)) {
                    DimensionTracker tracker = storage.get(DimensionTracker.class);
                    skyLight = tracker.getDimension() == 0;
                }
                chunkTransformer = new V1_3ToV1_8ChunkTranslator(session, chunk.getUncompressedData(), bitmap, skyLight, groundUp);
            }
            V1_8Chunk newChunk = new V1_8Chunk(chunkX, chunkZ, groundUp, bitmap, chunkTransformer.getChunkData());
            return PacketUtil.createPacket(0x21, new TypeHolder[] { new TypeHolder<>(Type.V1_8R_CHUNK, newChunk) });
        }
    });
    // chunk bulk
    addTranslator(0x26, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            V1_3_4ChunkBulk oldChunkBulk = data.read(Type.V1_4CHUNK_BULK, 0);
            int[] x = oldChunkBulk.getColumnX();
            int[] z = oldChunkBulk.getColumnZ();
            int columnAmount = oldChunkBulk.getChunks().length;
            V1_3ToV1_8ChunkTranslator[] bulks = new V1_3ToV1_8ChunkTranslator[columnAmount];
            for (int i = 0; i < columnAmount; i++) {
                bulks[i] = new V1_3ToV1_8ChunkTranslator(session, oldChunkBulk.getChunks()[i], oldChunkBulk.getPrimaryBitmaps()[i], oldChunkBulk.isSkylight(), true);
            }
            V1_8ChunkBulk.Chunk[] chunks = new V1_8ChunkBulk.Chunk[columnAmount];
            for (int i = 0; i < columnAmount; i++) {
                chunks[i] = new V1_8ChunkBulk.Chunk();
                chunks[i].setData(bulks[i].getChunkData());
                chunks[i].setDataSize(oldChunkBulk.getPrimaryBitmaps()[i]);
            }
            V1_8ChunkBulk chunkBulk = new V1_8ChunkBulk(oldChunkBulk.isSkylight(), x, z, chunks);
            return PacketUtil.createPacket(0x26, new TypeHolder[] { set(Type.V1_8R_CHUNK_BULK, chunkBulk) });
        }
    });
    // multi block change
    addTranslator(0x22, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            V1_2MultiBlockArray blockArray = data.read(Type.V1_2MULTIBLOCK_ARRAY, 2);
            DataInput dis = new DataInputStream(new ByteArrayInputStream(blockArray.getData()));
            BlockChangeRecord[] blockChangeRecords = new BlockChangeRecord[blockArray.getRecordCount()];
            for (int i = 0; i < blockArray.getRecordCount(); i++) {
                try {
                    short pos = dis.readShort();
                    short packedBlock = dis.readShort();
                    int blockId = packedBlock >> 4;
                    int blockData = packedBlock & 15;
                    if (DataFixers.shouldCache(blockId)) {
                        ProtocolStorage storage = session.getStorage();
                        PortalFrameCache portalFrameCache = storage.get(PortalFrameCache.class);
                        int x = pos >> 12 & 15;
                        int y = pos & 255;
                        int z = pos >> 8 & 15;
                        int chunkX = data.read(Type.INT, 0);
                        int chunkZ = data.read(Type.INT, 1);
                        int xPos = x + (chunkX << 4);
                        int zPos = z + (chunkZ << 4);
                        portalFrameCache.setBlockAt(chunkX, chunkZ, xPos, y, zPos, blockId);
                        blockData = DataFixers.getCorrectedDataFor(portalFrameCache, xPos, y, zPos, blockId, blockData);
                    } else {
                        blockData = DataFixers.fixInvalidData(blockId, blockData);
                    }
                    blockChangeRecords[i] = new BlockChangeRecord(pos, (short) (blockId & 4095) << 4 | blockData & 15);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return PacketUtil.createPacket(0x22, new TypeHolder[] { data.read(0), data.read(1), set(Type.V1_8R_MULTIBLOCK_ARRAY, blockChangeRecords) });
        }
    });
    // block change
    addTranslator(0x23, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            int x = data.read(Type.INT, 0);
            short y = data.read(Type.UNSIGNED_BYTE, 1);
            int z = data.read(Type.INT, 2);
            int blockId = data.read(Type.VAR_INT, 3);
            int blockData = data.read(Type.UNSIGNED_BYTE, 4);
            if (DataFixers.shouldCache(blockId)) {
                ProtocolStorage storage = session.getStorage();
                PortalFrameCache portalFrameCache = storage.get(PortalFrameCache.class);
                portalFrameCache.setBlockAt(x >> 4, z >> 4, x, y, z, blockId);
                blockData = DataFixers.getCorrectedDataFor(portalFrameCache, x, y, z, blockId, blockData);
            } else {
                blockData = DataFixers.fixInvalidData(blockId, blockData);
            }
            return PacketUtil.createPacket(0x23, new TypeHolder[] { set(Type.LONG, toBlockPosition(x, y, z)), set(Type.VAR_INT, blockId << 4 | blockData & 15) });
        }
    });
    // block action
    addTranslator(0x24, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            int x = data.read(Type.INT, 0);
            short y = data.read(Type.SHORT, 1);
            int z = data.read(Type.INT, 2);
            return PacketUtil.createPacket(0x24, new TypeHolder[] { set(Type.LONG, toBlockPosition(x, y, z)), data.read(3), data.read(4), data.read(5) });
        }
    });
    // block break animation
    addTranslator(0x25, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            int x = data.read(Type.INT, 1);
            int y = data.read(Type.INT, 2);
            int z = data.read(Type.INT, 3);
            return PacketUtil.createPacket(0x25, new TypeHolder[] { data.read(0), set(Type.LONG, toBlockPosition(x, y, z)), data.read(4) });
        }
    });
    // effect
    addTranslator(0x28, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            int x = data.read(Type.INT, 1);
            byte y = data.read(Type.BYTE, 2);
            int z = data.read(Type.INT, 3);
            int effectType = data.read(Type.INT, 0);
            if (effectType == 2006) {
                double dist = Math.min(0.2F + data.read(Type.INT, 4) / 15.0F, 10.0F);
                if (dist > 2.5D) {
                    dist = 2.5D;
                }
                int i = (int) (150.0D * dist);
                return PacketUtil.createPacket(0x2A, new TypeHolder[] { set(Type.INT, 38), set(Type.BOOLEAN, false), set(Type.FLOAT, (float) x + 0.5f), set(Type.FLOAT, (float) y + 1.0f), set(Type.FLOAT, (float) z + 0.5f), set(Type.FLOAT, 0f), set(Type.FLOAT, 0f), set(Type.FLOAT, 0f), set(Type.FLOAT, 0.15000000596046448f), set(Type.INT, i), // stone
                set(Type.VAR_INT, 1) });
            }
            return PacketUtil.createPacket(0x28, new TypeHolder[] { data.read(0), set(Type.LONG, toBlockPosition(x, y, z)), data.read(4), data.read(5) });
        }
    });
    // update sign
    addTranslator(0x33, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            int x = data.read(Type.INT, 0);
            short y = data.read(Type.SHORT, 1);
            int z = data.read(Type.INT, 2);
            String[] lines = new String[4];
            for (int i = 0; i < 4; i++) {
                lines[i] = ChatUtils.legacyToJsonString(data.read(Type.V1_7_STRING, 3 + i));
            }
            return PacketUtil.createPacket(0x33, new TypeHolder[] { set(Type.LONG, toBlockPosition(x, y, z)), set(Type.V1_7_STRING, lines[0]), set(Type.V1_7_STRING, lines[1]), set(Type.V1_7_STRING, lines[2]), set(Type.V1_7_STRING, lines[3]) });
        }
    });
    // sign editor
    addTranslator(0x36, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            int x = data.read(Type.INT, 0);
            int y = data.read(Type.INT, 1);
            int z = data.read(Type.INT, 2);
            return PacketUtil.createPacket(0x36, new TypeHolder[] { set(Type.LONG, toBlockPosition(x, y, z)) });
        }
    });
    // tab list item
    addTranslator(0x38, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            String u = data.read(Type.V1_7_STRING, 0);
            if (u == null) {
                // skip if server is updating ping or something
                return cancel();
            }
            String username = ChatUtils.stripColor(u);
            boolean online = data.read(Type.BOOLEAN, 1);
            UUID uuid = UUID.nameUUIDFromBytes(("fake:" + username).getBytes(Charsets.UTF_8));
            if (username.equals(session.getUserData().getUsername())) {
                uuid = session.getUserData().getUniqueId();
            }
            if (online) {
                TabListEntry tabAddListEntry = new TabListEntry(TabListAction.ADD_PLAYER, new PlayerListEntry[] { new PlayerListEntry(new GameProfile(uuid, username), new Property[0], 0, 0, null) });
                return PacketUtil.createPacket(0x38, new TypeHolder[] { set(Type.TAB_LIST_ENTRY, tabAddListEntry) });
            } else {
                TabListEntry tabRemoveListEntry = new TabListEntry(TabListAction.REMOVE_PLAYER, new PlayerListEntry[] { new PlayerListEntry(new GameProfile(uuid, username)) });
                return PacketUtil.createPacket(0x38, new TypeHolder[] { set(Type.TAB_LIST_ENTRY, tabRemoveListEntry) });
            }
        }
    });
    // set experience
    addTranslator(0x1F, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x1F, new TypeHolder[] { data.read(0), set(Type.VAR_INT, data.read(Type.SHORT, 1).intValue()), set(Type.VAR_INT, data.read(Type.SHORT, 2).intValue()) });
        }
    });
    // use bed
    addTranslator(0x0A, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            int x = data.read(Type.INT, 1);
            byte y = data.read(Type.BYTE, 2);
            int z = data.read(Type.INT, 3);
            return PacketUtil.createPacket(0x0A, new TypeHolder[] { set(Type.VAR_INT, data.read(Type.INT, 0)), set(Type.LONG, toBlockPosition(x, y, z)) });
        }
    });
    // collect item
    addTranslator(0x0D, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x0D, new TypeHolder[] { set(Type.VAR_INT, data.read(Type.INT, 0)), set(Type.VAR_INT, data.read(Type.INT, 1)) });
        }
    });
    // custom payload
    addTranslator(0x3F, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @SneakyThrows
        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            String channel = data.read(Type.V1_7_STRING, 0);
            byte[] bytes = data.read(Type.SHORT_BYTE_ARRAY, 1);
            byte[] remappedData;
            NettyOutputWrapper fixedPayload = new NettyOutputWrapper(Unpooled.buffer());
            switch(channel) {
                case "MC|TrList":
                    NettyInputWrapper buf = new NettyInputWrapper(Unpooled.wrappedBuffer(bytes));
                    fixedPayload.writeInt(buf.readInt());
                    int items = buf.readByte();
                    fixedPayload.writeByte(items);
                    for (int i = 0; i < items; i++) {
                        V1_8RProtocol.ITEM.write(set(Type.V1_8R_ITEM, V1_3_1RProtocol.ITEM.read(buf)), fixedPayload);
                        V1_8RProtocol.ITEM.write(set(Type.V1_8R_ITEM, V1_3_1RProtocol.ITEM.read(buf)), fixedPayload);
                        boolean hasAdditionalItem = buf.readBoolean();
                        fixedPayload.writeBoolean(hasAdditionalItem);
                        if (hasAdditionalItem) {
                            V1_8RProtocol.ITEM.write(set(Type.V1_8R_ITEM, V1_3_1RProtocol.ITEM.read(buf)), fixedPayload);
                        }
                        fixedPayload.writeBoolean(buf.readBoolean());
                        fixedPayload.writeInt(0);
                        fixedPayload.writeInt(7);
                    }
                    remappedData = fixedPayload.array();
                    break;
                case "MC|RPack":
                    return cancel();
                case "MC|Brand":
                    V1_7_2RProtocol.STRING.write(set(Type.V1_7_STRING, new String(bytes, StandardCharsets.UTF_8)), fixedPayload);
                    remappedData = fixedPayload.array();
                    break;
                default:
                    remappedData = bytes;
                    break;
            }
            return PacketUtil.createPacket(0x3F, new TypeHolder[] { data.read(0), set(Type.READABLE_BYTES, remappedData) });
        }
    });
    // entity attributes
    addTranslator(0x20, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x20, new TypeHolder[] { set(Type.VAR_INT, data.read(Type.INT, 0)), set(Type.V1_8_ENTITY_ATTRIBUTES, data.read(Type.V1_7_ENTITY_ATTRIBUTES, 1)) });
        }
    });
    // client packets
    // keep alive
    addTranslator(0x00, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x00, new TypeHolder[] { set(Type.INT, data.read(Type.VAR_INT, 0)) });
        }
    });
    // use entity
    addTranslator(0x02, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            OptionalPosition optPos = data.read(Type.V1_8R_USE_ENTITY_OPTIONAL_POSITION, 1);
            int action = optPos.getAction();
            if (action == 2) {
                return cancel();
            }
            return PacketUtil.createPacket(0x02, new TypeHolder[] { set(Type.INT, data.read(Type.VAR_INT, 0)), set(Type.BYTE, (byte) action) });
        }
    });
    // player digging
    addTranslator(0x07, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            long encodedPosition = data.read(Type.LONG, 1);
            int action = data.read(Type.UNSIGNED_BYTE, 0);
            BlockLocation l = fromBlockPosition(encodedPosition);
            ProtocolStorage storage = session.getStorage();
            if (storage.hasObject(BlockMiningTimeFixer.class) && storage.hasObject(BlockStorage.class)) {
                BlockMiningTimeFixer blockMiningTimeFixer = storage.get(BlockMiningTimeFixer.class);
                BlockStorage blockStorage = storage.get(BlockStorage.class);
                switch(action) {
                    case // start digging
                    0:
                        blockMiningTimeFixer.onBlockStartBreaking(l);
                        break;
                    case // cancel digging
                    1:
                        blockMiningTimeFixer.onBlockCancelBreaking(l);
                        break;
                    case // finish digging
                    2:
                        if (HardnessTable.exist(blockStorage.getBlockAt(l.getX(), l.getY(), l.getZ()))) {
                            return cancel();
                        }
                        break;
                }
            }
            return PacketUtil.createPacket(0x07, new TypeHolder[] { data.read(0), set(Type.INT, l.getX()), set(Type.UNSIGNED_BYTE, (short) l.getY()), set(Type.INT, l.getZ()), set(Type.UNSIGNED_BYTE, data.read(Type.BYTE, 2).shortValue()) });
        }
    });
    // place block
    addTranslator(0x08, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            long encodedPosition = data.read(Type.LONG, 0);
            BlockLocation l = fromBlockPosition(encodedPosition);
            ItemStack itemStack = data.read(Type.V1_8R_ITEM, 2);
            // 1.0+ servers are kicking players when placed block is 0. Should be null
            if (itemStack != null && itemStack.getItemId() == 0)
                itemStack = null;
            if (itemStack != null && itemStack.getItemId() == 387) /* written book */
            {
                PacketData payload = PacketUtil.createPacket(0x3F, new TypeHolder[] { set(Type.V1_7_STRING, "MC|BOpen") });
                session.sendPacket(payload, PacketDirection.TO_CLIENT, getFrom());
            }
            return PacketUtil.createPacket(0x08, new TypeHolder[] { set(Type.INT, l.getX()), set(Type.UNSIGNED_BYTE, (short) l.getY()), set(Type.INT, l.getZ()), data.read(1), set(Type.V1_3R_ITEM, itemStack), data.read(3), data.read(4), data.read(5) });
        }
    });
    // animation
    addTranslator(0x0A, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x0A, new TypeHolder[] { set(Type.INT, 0), set(Type.BYTE, (byte) 1) });
        }
    });
    // entity action
    addTranslator(0x0B, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            int action = data.read(Type.VAR_INT, 1);
            return PacketUtil.createPacket(0x0B, new TypeHolder[] { set(Type.INT, data.read(Type.VAR_INT, 0)), set(Type.BYTE, (byte) (action + 1)), set(Type.INT, data.read(Type.VAR_INT, 2)) });
        }
    });
    // update sign
    addTranslator(0x12, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            long encodedPosition = data.read(Type.LONG, 0);
            BlockLocation l = fromBlockPosition(encodedPosition);
            String[] lines = new String[4];
            for (int i = 0; i < 4; i++) {
                String msg = data.read(Type.V1_7_STRING, 1 + i);
                if (msg.startsWith("\"")) {
                    msg = ChatUtils.createChatComponentFromInvalidJson(msg);
                }
                msg = ChatUtils.jsonToLegacy(msg);
                msg = StringUtils.substring(msg, 0, 15);
                lines[i] = msg;
            }
            return PacketUtil.createPacket(0x12, new TypeHolder[] { set(Type.INT, l.getX()), set(Type.SHORT, (short) l.getY()), set(Type.INT, l.getZ()), set(Type.V1_7_STRING, lines[0]), set(Type.V1_7_STRING, lines[1]), set(Type.V1_7_STRING, lines[2]), set(Type.V1_7_STRING, lines[3]) });
        }
    });
    // tab complete
    addTranslator(0x14, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x14, new TypeHolder[] { data.read(0) });
        }
    });
    // client settings
    addTranslator(0x15, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x15, new TypeHolder[] { data.read(0), data.read(1), data.read(2), data.read(3), data.read(4), set(Type.BOOLEAN, false) });
        }
    });
    // custom payload
    addTranslator(0x17, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) throws IOException {
            byte[] bytes = data.read(Type.READABLE_BYTES, 1);
            String channel = data.read(Type.V1_7_STRING, 0);
            if (channel.equals("MC|BEdit") || channel.equals("MC|BSign")) {
                NettyInputWrapper in = new NettyInputWrapper(Unpooled.wrappedBuffer(bytes));
                NettyOutputWrapper out = new NettyOutputWrapper(Unpooled.buffer());
                ItemStack compressedItem = V1_8RProtocol.ITEM.read(in);
                V1_3_1RProtocol.ITEM.write(new TypeHolder<>(Type.V1_3R_ITEM, compressedItem), out);
                bytes = out.array();
            }
            return PacketUtil.createPacket(0x17, new TypeHolder[] { data.read(0), set(Type.SHORT_BYTE_ARRAY, bytes) });
        }
    });
    // player input
    addTranslator(0x0C, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            byte status = data.read(Type.BYTE, 2);
            return PacketUtil.createPacket(0x0C, new TypeHolder[] { data.read(0), data.read(1), set(Type.BOOLEAN, (status & 1) == 1), set(Type.BOOLEAN, (status & 2) == 2) });
        }
    });
    // map data
    addTranslator(0x34, -1, ProtocolState.PLAY, PacketDirection.TO_CLIENT);
    // spawn particle
    addTranslator(0x2A, -1, ProtocolState.PLAY, PacketDirection.TO_CLIENT);
}
Also used : PortalFrameCache(com.github.dirtpowered.dirtmv.network.versions.Release47To5.chunk.PortalFrameCache) V1_3ToV1_8ChunkTranslator(com.github.dirtpowered.dirtmv.network.versions.Release47To5.chunk.V1_3ToV1_8ChunkTranslator) PacketTranslator(com.github.dirtpowered.dirtmv.data.translator.PacketTranslator) V1_2Chunk(com.github.dirtpowered.dirtmv.data.protocol.objects.V1_2Chunk) DimensionTracker(com.github.dirtpowered.dirtmv.network.versions.Release28To23.chunk.DimensionTracker) SneakyThrows(lombok.SneakyThrows) PlayerListEntry(com.github.dirtpowered.dirtmv.data.protocol.objects.tablist.PlayerListEntry) BlockLocation(com.github.dirtpowered.dirtmv.data.protocol.objects.BlockLocation) V1_8Chunk(com.github.dirtpowered.dirtmv.data.protocol.objects.V1_8Chunk) ServerPing(com.github.dirtpowered.dirtmv.network.versions.Release4To78.ping.ServerPing) V1_2MultiBlockArray(com.github.dirtpowered.dirtmv.data.protocol.objects.V1_2MultiBlockArray) TypeHolder(com.github.dirtpowered.dirtmv.data.protocol.TypeHolder) BlockChangeRecord(com.github.dirtpowered.dirtmv.data.protocol.objects.BlockChangeRecord) BlockStorage(com.github.dirtpowered.dirtmv.network.versions.Beta17To14.storage.BlockStorage) DirtMultiVersion(com.github.dirtpowered.dirtmv.DirtMultiVersion) MinecraftVersion(com.github.dirtpowered.dirtmv.data.MinecraftVersion) UUID(java.util.UUID) NettyInputWrapper(com.github.dirtpowered.dirtmv.data.protocol.io.NettyInputWrapper) ServerSession(com.github.dirtpowered.dirtmv.network.server.ServerSession) V1_8ChunkBulk(com.github.dirtpowered.dirtmv.data.protocol.objects.V1_8ChunkBulk) NettyOutputWrapper(com.github.dirtpowered.dirtmv.data.protocol.io.NettyOutputWrapper) OptionalPosition(com.github.dirtpowered.dirtmv.data.protocol.objects.OptionalPosition) ProtocolStorage(com.github.dirtpowered.dirtmv.data.user.ProtocolStorage) TabListEntry(com.github.dirtpowered.dirtmv.data.protocol.objects.tablist.TabListEntry) BlockMiningTimeFixer(com.github.dirtpowered.dirtmv.network.versions.Release47To5.other.BlockMiningTimeFixer) IOException(java.io.IOException) V1_2Chunk(com.github.dirtpowered.dirtmv.data.protocol.objects.V1_2Chunk) V1_8Chunk(com.github.dirtpowered.dirtmv.data.protocol.objects.V1_8Chunk) DataInputStream(java.io.DataInputStream) DataInput(java.io.DataInput) V1_3_4ChunkBulk(com.github.dirtpowered.dirtmv.data.protocol.objects.V1_3_4ChunkBulk) ByteArrayInputStream(java.io.ByteArrayInputStream) GameProfile(com.mojang.authlib.GameProfile) ItemStack(com.github.dirtpowered.dirtmv.data.protocol.objects.ItemStack) PacketData(com.github.dirtpowered.dirtmv.data.protocol.PacketData)

Example 4 with V1_2Chunk

use of com.github.dirtpowered.dirtmv.data.protocol.objects.V1_2Chunk in project DirtMultiversion by DirtPowered.

the class BetaToV1_2ChunkTranslator method translate.

@Override
public PacketData translate(ServerSession session, PacketData data) {
    ProtocolStorage storage = session.getStorage();
    LoadedChunkTracker chunkTracker = storage.get(LoadedChunkTracker.class);
    DimensionTracker dimensionTracker = storage.get(DimensionTracker.class);
    boolean hasSkylight = dimensionTracker.getDimension() == 0;
    V1_3BChunk oldChunk = (V1_3BChunk) data.read(0).getObject();
    boolean groundUp = true;
    if (oldChunk.getXSize() * oldChunk.getYSize() * oldChunk.getZSize() != 32768) {
        groundUp = false;
    }
    int chunkX = oldChunk.getX() >> 4;
    int chunkZ = oldChunk.getZ() >> 4;
    if (groundUp) {
        V1_3BChunkStorage oldChunkStorage = new V1_3BChunkStorage(chunkX, chunkZ);
        V1_2RChunkStorage newChunkStorage = new V1_2RChunkStorage(hasSkylight, true, chunkX, chunkZ);
        oldChunkStorage.setChunkData(oldChunk.getChunk(), oldChunk.getX(), oldChunk.getY(), oldChunk.getZ(), oldChunk.getXSize(), oldChunk.getYSize(), oldChunk.getZSize(), 0, true);
        for (int x = 0; x < 16; x++) {
            for (int y = 0; y < 128; y++) {
                for (int z = 0; z < 16; z++) {
                    int oldBlockId = oldChunkStorage.getBlockId(x, y, z);
                    int oldBlockData = oldChunkStorage.getBlockData(x, y, z);
                    Block replacement = blockDataTransformer.replaceBlock(oldBlockId, oldBlockData);
                    newChunkStorage.setBlockId(x, y, z, replacement.getBlockId());
                    newChunkStorage.setBlockMetadata(x, y, z, replacement.getBlockData());
                    newChunkStorage.setBlockLight(x, y, z, oldChunkStorage.getBlockLight(x, y, z));
                    if (hasSkylight) {
                        newChunkStorage.setSkyLight(x, y, z, oldChunkStorage.getSkyLight(x, y, z));
                    }
                }
            }
        }
        byte[] biomes = new byte[256];
        if (storage.hasObject(OldChunkData.class)) {
            OldChunkData biomeData = storage.get(OldChunkData.class);
            biomes = biomeData.getBiomeDataAt(chunkX, chunkZ, !hasSkylight);
        } else {
            // forest
            Arrays.fill(biomes, (byte) 0x04);
        }
        newChunkStorage.setBiomeData(biomes);
        byte[] compressedData = newChunkStorage.getCompressedData(true, 0xff);
        session.sendPacket(PacketUtil.createPacket(0x33, new TypeHolder[] { new TypeHolder<>(Type.V1_2_CHUNK, new V1_2Chunk(chunkX, chunkZ, true, (short) newChunkStorage.getPrimaryBitmap(), (short) 0, newChunkStorage.getCompressedSize(), compressedData, new byte[0], newChunkStorage)) }), PacketDirection.TO_CLIENT, MinecraftVersion.R1_2_1);
        chunkTracker.setChunkLoaded(chunkX, chunkZ);
        return ServerProtocol.cancel();
    } else {
        if (!chunkTracker.isChunkLoaded(chunkX, chunkZ)) {
            return ServerProtocol.cancel();
        }
        Configuration c = session.getMain().getConfiguration();
        boolean replaceChests = session.getUserData().getClientVersion().getRegistryId() >= 39 && c.replaceChests();
        List<WorldBlock> worldBlocks = getUpdatedBlockList(oldChunk.getX(), oldChunk.getY(), oldChunk.getZ(), oldChunk.getXSize(), oldChunk.getYSize(), oldChunk.getZSize(), oldChunk.getChunk(), replaceChests);
        int records = worldBlocks.size();
        if (worldBlocks.isEmpty()) {
            return ServerProtocol.cancel();
        }
        List<List<WorldBlock>> slicedList = getSlicedData(worldBlocks);
        if (records > MAX_SINGLE_BLOCK_UPDATE_PACKETS) {
            for (List<WorldBlock> slicedData : slicedList) {
                chunkX = slicedData.get(0).getX() >> 4;
                chunkZ = slicedData.get(0).getZ() >> 4;
                int totalDataSize = 4 * slicedData.size();
                ByteArrayOutputStream baos = new ByteArrayOutputStream(totalDataSize);
                DataOutputStream dos = new DataOutputStream(baos);
                V1_2MultiBlockArray blockArray = null;
                try {
                    for (WorldBlock record : slicedData) {
                        dos.writeShort((record.getX() - (chunkX << 4)) << 12 | (record.getZ() - (chunkZ << 4)) << 8 | record.getY());
                        dos.writeShort((record.getBlockId() & 4095) << 4 | record.getBlockData() & 15);
                    }
                    byte[] bytes = baos.toByteArray();
                    blockArray = new V1_2MultiBlockArray(slicedData.size(), bytes.length, bytes);
                    dos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                PacketData multiBlockChange = PacketUtil.createPacket(0x34, new TypeHolder[] { new TypeHolder<>(Type.INT, chunkX), new TypeHolder<>(Type.INT, chunkZ), new TypeHolder<>(Type.V1_2MULTIBLOCK_ARRAY, blockArray) });
                session.sendPacket(multiBlockChange, PacketDirection.TO_CLIENT, MinecraftVersion.R1_2_1);
            }
        } else {
            for (WorldBlock block : worldBlocks) {
                PacketData blockUpdate = PacketUtil.createPacket(0x35, new TypeHolder[] { new TypeHolder<>(Type.INT, block.getX()), new TypeHolder<>(Type.BYTE, (byte) block.getY()), new TypeHolder<>(Type.INT, block.getZ()), new TypeHolder<>(Type.BYTE, (byte) block.getBlockId()), new TypeHolder<>(Type.BYTE, (byte) block.getBlockData()) });
                session.sendPacket(blockUpdate, PacketDirection.TO_CLIENT, MinecraftVersion.R1_2_1);
            }
        }
    }
    return ServerProtocol.cancel();
}
Also used : OldChunkData(com.github.dirtpowered.dirtmv.data.chunk.biome.OldChunkData) ProtocolStorage(com.github.dirtpowered.dirtmv.data.user.ProtocolStorage) V1_3BChunkStorage(com.github.dirtpowered.dirtmv.data.chunk.storage.V1_3BChunkStorage) Configuration(com.github.dirtpowered.dirtmv.api.Configuration) V1_2RChunkStorage(com.github.dirtpowered.dirtmv.data.chunk.storage.V1_2RChunkStorage) V1_2Chunk(com.github.dirtpowered.dirtmv.data.protocol.objects.V1_2Chunk) DataOutputStream(java.io.DataOutputStream) V1_3BChunk(com.github.dirtpowered.dirtmv.data.protocol.objects.V1_3BChunk) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) V1_2MultiBlockArray(com.github.dirtpowered.dirtmv.data.protocol.objects.V1_2MultiBlockArray) TypeHolder(com.github.dirtpowered.dirtmv.data.protocol.TypeHolder) Block(com.github.dirtpowered.dirtmv.data.transformers.block.Block) ArrayList(java.util.ArrayList) List(java.util.List) PacketData(com.github.dirtpowered.dirtmv.data.protocol.PacketData)

Example 5 with V1_2Chunk

use of com.github.dirtpowered.dirtmv.data.protocol.objects.V1_2Chunk in project DirtMultiversion by DirtPowered.

the class V1_2_3RChunkDataType method read.

@Override
public V1_2Chunk read(PacketInput packetInput) throws IOException {
    int chunkX = packetInput.readInt();
    int chunkZ = packetInput.readInt();
    boolean groundUp = packetInput.readBoolean();
    short primaryBitmap = packetInput.readShort();
    short additionalBitmap = packetInput.readShort();
    int compressedDataSize = packetInput.readInt();
    // unused
    if (getType() == Type.V1_2_CHUNK)
        packetInput.readInt();
    byte[] chunk = packetInput.readBytes(compressedDataSize);
    int i = 0;
    for (int j = 0; j < 16; j++) {
        i += primaryBitmap >> j & 1;
    }
    int size = 12288 * i;
    if (groundUp) {
        size += 256;
    }
    byte[] buf = new byte[size];
    Inflater inflater = new Inflater();
    inflater.setInput(chunk, 0, compressedDataSize);
    try {
        inflater.inflate(buf);
    } catch (DataFormatException e) {
        throw new IOException("Bad compressed data format");
    } finally {
        inflater.end();
    }
    return new V1_2Chunk(chunkX, chunkZ, groundUp, primaryBitmap, additionalBitmap, compressedDataSize, chunk, buf, null);
}
Also used : DataFormatException(java.util.zip.DataFormatException) V1_2Chunk(com.github.dirtpowered.dirtmv.data.protocol.objects.V1_2Chunk) Inflater(java.util.zip.Inflater) IOException(java.io.IOException)

Aggregations

V1_2Chunk (com.github.dirtpowered.dirtmv.data.protocol.objects.V1_2Chunk)6 IOException (java.io.IOException)5 PacketData (com.github.dirtpowered.dirtmv.data.protocol.PacketData)4 V1_2MultiBlockArray (com.github.dirtpowered.dirtmv.data.protocol.objects.V1_2MultiBlockArray)4 V1_2RChunkStorage (com.github.dirtpowered.dirtmv.data.chunk.storage.V1_2RChunkStorage)3 TypeHolder (com.github.dirtpowered.dirtmv.data.protocol.TypeHolder)3 PacketTranslator (com.github.dirtpowered.dirtmv.data.translator.PacketTranslator)3 ProtocolStorage (com.github.dirtpowered.dirtmv.data.user.ProtocolStorage)3 ServerSession (com.github.dirtpowered.dirtmv.network.server.ServerSession)3 BlockStorage (com.github.dirtpowered.dirtmv.network.versions.Beta17To14.storage.BlockStorage)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 DataInput (java.io.DataInput)3 DataInputStream (java.io.DataInputStream)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 ExtendedBlockStorage (com.github.dirtpowered.dirtmv.data.chunk.storage.ExtendedBlockStorage)2 BlockLocation (com.github.dirtpowered.dirtmv.data.protocol.objects.BlockLocation)2 DimensionTracker (com.github.dirtpowered.dirtmv.network.versions.Release28To23.chunk.DimensionTracker)2 DirtMultiVersion (com.github.dirtpowered.dirtmv.DirtMultiVersion)1 Configuration (com.github.dirtpowered.dirtmv.api.Configuration)1