Search in sources :

Example 1 with ExtendedBlockStorage

use of com.github.dirtpowered.dirtmv.data.chunk.storage.ExtendedBlockStorage 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 2 with ExtendedBlockStorage

use of com.github.dirtpowered.dirtmv.data.chunk.storage.ExtendedBlockStorage 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)

Example 3 with ExtendedBlockStorage

use of com.github.dirtpowered.dirtmv.data.chunk.storage.ExtendedBlockStorage in project DirtMultiversion by DirtPowered.

the class V1_3ToV1_8ChunkTranslator method getChunkData.

public byte[] getChunkData() {
    ExtendedBlockStorage[] columnStorage = chunkStorage.getColumnStorage();
    List<ExtendedBlockStorage> blockStorages = new ArrayList<>();
    boolean skyLight = chunkStorage.isSkylight();
    byte[] biomes = chunkStorage.getBiomeData();
    int columnBits = 0;
    for (int i = 0; i < columnStorage.length; ++i) {
        ExtendedBlockStorage extendedblockstorage = columnStorage[i];
        boolean f = extendedblockstorage != null && oldChunk != columnStorage[i].isEmpty();
        if (extendedblockstorage != null && (this.bitmapValue & 1 << i) != 0 && (!this.groundUp || f)) {
            columnBits |= 1 << i;
            blockStorages.add(extendedblockstorage);
        }
    }
    byte[] data = new byte[ChunkUtils.calculateDataSize(Integer.bitCount(columnBits), skyLight)];
    int totalSize = 0;
    for (int i = 0; i < blockStorages.size(); i++) {
        ExtendedBlockStorage blockStorage = blockStorages.get(i);
        byte[] blockArray = blockStorage.getBlockLSBArray();
        for (int j = 0; j < blockArray.length; ++j) {
            int x = j & 15;
            int y = (j >> 8);
            int z = j >> 4 & 15;
            int blockId = blockArray[j] & 255;
            int blockData = blockStorage.getBlockMetadataArray().getNibble(x, y, z);
            if (DataFixers.shouldCache(blockId)) {
                ProtocolStorage storage = session.getStorage();
                PortalFrameCache portalFrameCache = storage.get(PortalFrameCache.class);
                y = +i * 16 & 255;
                int xPos = x + (chunkStorage.getChunkX() << 4);
                int zPos = z + (chunkStorage.getChunkZ() << 4);
                portalFrameCache.setBlockAt(chunkStorage.getChunkX(), chunkStorage.getChunkZ(), xPos, y, zPos, blockId);
                blockData = DataFixers.getCorrectedDataFor(portalFrameCache, xPos, y, zPos, blockId, blockData);
            } else {
                blockData = DataFixers.fixInvalidData(blockId, blockData);
            }
            char c = (char) (blockId << 4 | blockData);
            data[totalSize++] = (byte) (c & 255);
            data[totalSize++] = (byte) (c >> 8 & 255);
        }
    }
    for (ExtendedBlockStorage blockStorage : blockStorages) {
        totalSize = writeData(blockStorage.getBlockLightArray().getData(), data, totalSize);
    }
    if (skyLight) {
        for (ExtendedBlockStorage blockStorage : blockStorages) {
            totalSize = writeData(blockStorage.getSkylightArray().getData(), data, totalSize);
        }
    }
    if (this.groundUp) {
        totalSize = writeData(biomes, data, totalSize);
    }
    byte[] chunk = new byte[totalSize];
    System.arraycopy(data, 0, chunk, 0, totalSize);
    return chunk;
}
Also used : ProtocolStorage(com.github.dirtpowered.dirtmv.data.user.ProtocolStorage) ArrayList(java.util.ArrayList) ExtendedBlockStorage(com.github.dirtpowered.dirtmv.data.chunk.storage.ExtendedBlockStorage)

Aggregations

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