Search in sources :

Example 1 with WorldSound

use of com.github.dirtpowered.dirtmv.network.versions.Release39To29.sound.WorldSound 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 WorldSound

use of com.github.dirtpowered.dirtmv.network.versions.Release39To29.sound.WorldSound in project DirtMultiversion by DirtPowered.

the class WorldEntityEvent method playSoundAt.

public static void playSoundAt(ServerSession session, Location loc, WorldSound sound) {
    EntityTracker tracker = session.getStorage().get(EntityTracker.class);
    if (tracker.isEntityTracked(-999)) {
        Location l = tracker.getEntity(-999).getLocation();
        playSoundAt(session, loc, l, new Sound(sound.getSoundName(), 0.75F, 1.0F));
    }
}
Also used : Sound(com.github.dirtpowered.dirtmv.network.versions.Release39To29.sound.Sound) WorldSound(com.github.dirtpowered.dirtmv.network.versions.Release39To29.sound.WorldSound) Location(com.github.dirtpowered.dirtmv.data.protocol.objects.Location)

Example 3 with WorldSound

use of com.github.dirtpowered.dirtmv.network.versions.Release39To29.sound.WorldSound in project DirtMultiversion by DirtPowered.

the class WorldEntityEvent method playSoundAt.

public static void playSoundAt(ServerSession session, Location loc, WorldSound sound, float vol, float pitch) {
    EntityTracker tracker = session.getStorage().get(EntityTracker.class);
    if (tracker.isEntityTracked(-999)) {
        Location l = tracker.getEntity(-999).getLocation();
        playSoundAt(session, loc, l, new Sound(sound.getSoundName(), vol, pitch));
    }
}
Also used : Sound(com.github.dirtpowered.dirtmv.network.versions.Release39To29.sound.Sound) WorldSound(com.github.dirtpowered.dirtmv.network.versions.Release39To29.sound.WorldSound) Location(com.github.dirtpowered.dirtmv.data.protocol.objects.Location)

Aggregations

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