Search in sources :

Example 6 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;
                short blockId = data.read(Type.SHORT, 3);
                if (shouldCache(blockId)) {
                    blockStorage.setBlockAt(chunkX, chunkZ, x, y, z, blockId);
                }
            }
            return data;
        }
    });
    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<>();
                boolean skyLight = true;
                if (session.getStorage().hasObject(DimensionTracker.class)) {
                    DimensionTracker tracker = session.getStorage().get(DimensionTracker.class);
                    skyLight = tracker.getDimension() == 0;
                }
                // create chunk storage
                V1_2RChunkStorage storage = new V1_2RChunkStorage(skyLight, 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 & 127;
                        int z = j >> 4 & 15;
                        int blockId = blockArray[j] & 255;
                        if (shouldCache(blockId)) {
                            blockStorage.setBlockAt(chunk.getChunkX(), chunk.getChunkZ(), x, y, z, blockId);
                        }
                    }
                }
            }
            return data;
        }
    });
}
Also used : ServerSession(com.github.dirtpowered.dirtmv.network.server.ServerSession) 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) DimensionTracker(com.github.dirtpowered.dirtmv.network.versions.Release28To23.chunk.DimensionTracker) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) ExtendedBlockStorage(com.github.dirtpowered.dirtmv.data.chunk.storage.ExtendedBlockStorage) V1_2MultiBlockArray(com.github.dirtpowered.dirtmv.data.protocol.objects.V1_2MultiBlockArray) 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)

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