Search in sources :

Example 36 with PacketData

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

the class InventoryPackets method registerTranslators.

@Override
public void registerTranslators() {
    // set slot
    addTranslator(0x2F, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            ProtocolStorage storage = session.getStorage();
            short windowId = data.read(Type.UNSIGNED_BYTE, 0);
            short slot = data.read(Type.SHORT, 1);
            int windowType = 0;
            if (storage.hasObject(WindowTypeTracker.class)) {
                WindowTypeTracker windowTracker = storage.get(WindowTypeTracker.class);
                windowType = windowTracker.getWindowType(windowId);
            }
            ItemStack originalItem = data.read(Type.V1_3R_ITEM, 2);
            if (originalItem == null)
                return new PacketData(0x2F, data.getObjects());
            ItemStack itemStack = itemRemapper.replaceItem(originalItem);
            if (windowType == 4) {
                if (slot >= 1) {
                    slot++;
                }
            }
            // quick-bar cache
            if (storage.hasObject(QuickBarTracker.class)) {
                QuickBarTracker quickBarTracker = storage.get(QuickBarTracker.class);
                quickBarTracker.setItem(slot, itemStack);
            }
            return PacketUtil.createPacket(0x2F, new TypeHolder[] { data.read(0), set(Type.SHORT, slot), set(Type.V1_8R_ITEM, itemStack) });
        }
    });
    // window items
    addTranslator(0x30, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            ProtocolStorage storage = session.getStorage();
            short windowId = data.read(Type.UNSIGNED_BYTE, 0);
            ItemStack[] itemArray = data.read(Type.V1_3R_ITEM_ARRAY, 1);
            int windowType = 0;
            if (storage.hasObject(WindowTypeTracker.class)) {
                WindowTypeTracker windowTracker = storage.get(WindowTypeTracker.class);
                windowType = windowTracker.getWindowType(windowId);
            }
            if (windowType == 4) {
                ItemStack[] copy = itemArray;
                itemArray = new ItemStack[copy.length + 1];
                itemArray[0] = copy[0];
                int slot = 0;
                while (slot < copy.length - 1) {
                    itemArray[slot + 2] = copy[slot + 1];
                    slot++;
                }
                // lapis
                itemArray[1] = new ItemStack(351, 3, 4, null);
            }
            for (int i = 0; i < itemArray.length; i++) {
                ItemStack originalItem = itemArray[i];
                ItemStack item = originalItem;
                if (originalItem != null) {
                    item = itemRemapper.replaceItem(originalItem);
                }
                itemArray[i] = item;
                // quick-bar cache
                if (storage.hasObject(QuickBarTracker.class)) {
                    QuickBarTracker quickBarTracker = storage.get(QuickBarTracker.class);
                    quickBarTracker.setItem(i, item);
                }
            }
            return PacketUtil.createPacket(0x30, new TypeHolder[] { data.read(0), set(Type.V1_8R_ITEM_ARRAY, itemArray) });
        }
    });
    // open window
    addTranslator(0x2D, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            ProtocolStorage storage = session.getStorage();
            short windowId = data.read(Type.UNSIGNED_BYTE, 0);
            short type = data.read(Type.UNSIGNED_BYTE, 1);
            String title = data.read(Type.V1_7_STRING, 2);
            short slots = data.read(Type.UNSIGNED_BYTE, 3);
            // non storage inventories have always 0 slots
            slots = InventoryUtils.isNonStorageInventory(type) ? 0 : slots;
            // cache window type (for enchanting tables)
            if (storage.hasObject(WindowTypeTracker.class)) {
                WindowTypeTracker windowTracker = storage.get(WindowTypeTracker.class);
                windowTracker.setWindowTypeFor(windowId, type);
            }
            return PacketUtil.createPacket(0x2D, new TypeHolder[] { data.read(0), set(Type.V1_7_STRING, InventoryUtils.getNamedTypeFromId(type)), set(Type.V1_7_STRING, InventoryUtils.addTranslateComponent(type == 6 ? "entity.Villager.name" : title)), set(Type.UNSIGNED_BYTE, slots) });
        }
    });
    // window click
    addTranslator(0x0E, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            ProtocolStorage storage = session.getStorage();
            byte windowId = data.read(Type.BYTE, 0);
            short slot = data.read(Type.SHORT, 1);
            int windowType = 0;
            if (storage.hasObject(WindowTypeTracker.class)) {
                WindowTypeTracker windowTracker = storage.get(WindowTypeTracker.class);
                windowType = windowTracker.getWindowType(windowId);
            }
            if (windowType == 4) {
                if (slot > 1) {
                    slot--;
                }
            }
            if (storage.hasObject(QuickBarTracker.class)) {
                QuickBarTracker cache = storage.get(QuickBarTracker.class);
                boolean shiftClick = data.read(Type.BYTE, 4) == 1;
                updateLocalInventory(cache, slot, shiftClick);
            }
            return PacketUtil.createPacket(0x0E, new TypeHolder[] { data.read(0), set(Type.SHORT, slot), data.read(2), data.read(3), data.read(4), set(Type.V1_3R_ITEM, data.read(Type.V1_8R_ITEM, 5)) });
        }
    });
    // creative item get
    addTranslator(0x10, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            ItemStack item = data.read(Type.V1_8R_ITEM, 1);
            boolean notNull = item != null;
            if (notNull && !creativeTab.exists(item.getItemId())) {
                // replace all unknown items to stone
                item.setItemId(1);
                item.setData(0);
            }
            return PacketUtil.createPacket(0x10, new TypeHolder[] { data.read(0), set(Type.V1_3R_ITEM, item) });
        }
    });
    // change held slot
    addTranslator(0x09, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            ProtocolStorage storage = session.getStorage();
            if (storage.hasObject(QuickBarTracker.class)) {
                QuickBarTracker quickBarTracker = storage.get(QuickBarTracker.class);
                quickBarTracker.setCurrentHotBarSlot(data.read(Type.SHORT, 0));
            }
            return data;
        }
    });
    // change held slot (server-side)
    addTranslator(0x09, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            ProtocolStorage storage = session.getStorage();
            if (storage.hasObject(QuickBarTracker.class)) {
                QuickBarTracker quickBarTracker = storage.get(QuickBarTracker.class);
                quickBarTracker.setCurrentHotBarSlot(data.read(Type.BYTE, 0));
            }
            return data;
        }
    });
    // window property
    addTranslator(0x31, ProtocolState.PLAY, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            int property = data.read(Type.SHORT, 1);
            switch(property) {
                case 0:
                    property = 2;
                    PacketData windowProperty = PacketUtil.createPacket(0x31, new TypeHolder[] { data.read(0), set(Type.SHORT, (short) 3), set(Type.SHORT, (short) 200) });
                    session.sendPacket(windowProperty, PacketDirection.TO_CLIENT, getFrom());
                    break;
                case 1:
                    property = 0;
                    break;
                case 2:
                    property = 1;
                    break;
            }
            return PacketUtil.createPacket(0x31, new TypeHolder[] { data.read(0), set(Type.SHORT, (short) property), data.read(2) });
        }
    });
}
Also used : WindowTypeTracker(com.github.dirtpowered.dirtmv.network.versions.Release47To5.inventory.WindowTypeTracker) TypeHolder(com.github.dirtpowered.dirtmv.data.protocol.TypeHolder) ServerSession(com.github.dirtpowered.dirtmv.network.server.ServerSession) ProtocolStorage(com.github.dirtpowered.dirtmv.data.user.ProtocolStorage) PacketTranslator(com.github.dirtpowered.dirtmv.data.translator.PacketTranslator) ItemStack(com.github.dirtpowered.dirtmv.data.protocol.objects.ItemStack) PacketData(com.github.dirtpowered.dirtmv.data.protocol.PacketData) QuickBarTracker(com.github.dirtpowered.dirtmv.network.versions.Release47To5.inventory.QuickBarTracker)

Example 37 with PacketData

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

the class ProtocolRelease60To51 method registerTranslators.

@Override
public void registerTranslators() {
    // handshake
    addTranslator(0x02, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x02, new TypeHolder[] { set(Type.BYTE, (byte) 51), data.read(1), data.read(2), data.read(3) });
        }
    });
    // open window
    addTranslator(0x64, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x64, new TypeHolder[] { data.read(0), data.read(1), data.read(2), data.read(3), // use original title
            set(Type.BYTE, (byte) 0) });
        }
    });
    // window click
    addTranslator(0x66, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            boolean leftClickFlag = false;
            boolean startDragging = false;
            boolean endDragging = false;
            boolean droppingUsingQ = false;
            boolean addSlot = false;
            short slot = data.read(Type.SHORT, 1);
            byte param = data.read(Type.BYTE, 2);
            byte inventoryAction = data.read(Type.BYTE, 4);
            switch(inventoryAction) {
                case 0:
                    leftClickFlag = param == 0;
                    break;
                case 4:
                    droppingUsingQ = param + (slot != -999 ? 2 : 0) == 2;
                    break;
                case 5:
                    startDragging = param == 0;
                    endDragging = param == 2;
                    addSlot = param == 1;
                    break;
            }
            boolean leftClick = leftClickFlag || startDragging || addSlot || endDragging;
            boolean clickingOutside = slot == -999 && inventoryAction != 5;
            boolean usingShift = inventoryAction == 1;
            int mouseClick = leftClick ? 0 : 1;
            if (droppingUsingQ) {
                PacketData closeWindow = PacketUtil.createPacket(0x65, new TypeHolder[] { set(Type.BYTE, (byte) 0) });
                session.sendPacket(closeWindow, PacketDirection.TO_CLIENT, getFrom());
                return cancel();
            }
            if (slot < 0 && !clickingOutside)
                return cancel();
            return PacketUtil.createPacket(0x66, new TypeHolder[] { data.read(0), data.read(1), set(Type.BYTE, (byte) mouseClick), data.read(3), set(Type.BYTE, (byte) (usingShift ? 1 : 0)), set(Type.V1_3R_ITEM, new ItemStack(34, 0, 0, null)) });
        }
    });
    // vehicle spawn
    addTranslator(0x17, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            Motion motion = data.read(Type.MOTION, 7);
            int throwerId = motion.getThrowerId();
            byte vehicleType = data.read(Type.BYTE, 1);
            byte vehicleFixedType = vehicleType;
            switch(vehicleType) {
                case // rideable minecart
                (byte) 10:
                    throwerId = 0;
                    break;
                case // chest minecart
                (byte) 11:
                    vehicleFixedType = 10;
                    throwerId = 1;
                    break;
                case // furnace minecart
                (byte) 12:
                    vehicleFixedType = 10;
                    throwerId = 2;
                    break;
            }
            motion.setThrowerId(throwerId);
            return PacketUtil.createPacket(0x17, new TypeHolder[] { data.read(0), set(Type.BYTE, vehicleFixedType), data.read(2), data.read(3), data.read(4), data.read(5), data.read(6), set(Type.MOTION, motion) });
        }
    });
    // creative item get
    addTranslator(0x6B, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            ItemStack item = data.read(Type.V1_3R_ITEM, 1);
            boolean notNull = item != null;
            if (notNull && !creativeTab.exists(item.getItemId())) {
                // replace all unknown items to stone
                item.setItemId(1);
                item.setData(0);
            }
            return PacketUtil.createPacket(0x6B, new TypeHolder[] { data.read(0), set(Type.V1_3R_ITEM, item) });
        }
    });
}
Also used : TypeHolder(com.github.dirtpowered.dirtmv.data.protocol.TypeHolder) Motion(com.github.dirtpowered.dirtmv.data.protocol.objects.Motion) ServerSession(com.github.dirtpowered.dirtmv.network.server.ServerSession) PacketTranslator(com.github.dirtpowered.dirtmv.data.translator.PacketTranslator) ItemStack(com.github.dirtpowered.dirtmv.data.protocol.objects.ItemStack) PacketData(com.github.dirtpowered.dirtmv.data.protocol.PacketData)

Example 38 with PacketData

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

the class ProtocolRelease78To74 method registerTranslators.

@Override
public void registerTranslators() {
    // handshake
    addTranslator(0x02, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x02, new TypeHolder[] { set(Type.BYTE, (byte) 74), data.read(1), data.read(2), data.read(3) });
        }
    });
    // kick disconnect
    addTranslator(0xFF, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            if (session.getUserData().getPreNettyProtocolState() != PreNettyProtocolState.STATUS)
                return data;
            String reason = data.read(Type.STRING, 0);
            ServerMotd pingMessage = ServerMotd.deserialize(reason);
            pingMessage.setVersionName("1.6.4");
            pingMessage.setProtocol(session.getUserData().getClientVersion().getRegistryId());
            return PacketUtil.createPacket(0xFF, new TypeHolder[] { set(Type.STRING, ServerMotd.serialize(pingMessage)) });
        }
    });
}
Also used : TypeHolder(com.github.dirtpowered.dirtmv.data.protocol.TypeHolder) ServerSession(com.github.dirtpowered.dirtmv.network.server.ServerSession) PacketTranslator(com.github.dirtpowered.dirtmv.data.translator.PacketTranslator) ServerMotd(com.github.dirtpowered.dirtmv.network.versions.Release73To61.ping.ServerMotd) PacketData(com.github.dirtpowered.dirtmv.data.protocol.PacketData)

Example 39 with PacketData

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

the class GlobalProtocolHandler method registerTranslators.

@Override
public void registerTranslators() {
    // chat (pre-netty)
    addTranslator(0x03, ProtocolState.PRE_NETTY, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            int clientVersion = session.getUserData().getClientVersion().getRegistryId();
            String message = clientVersion <= 10 ? data.read(Type.UTF8_STRING, 0) : data.read(Type.STRING, 0);
            onChat(session, message);
            return data;
        }
    });
    // chat (post-netty)
    addTranslator(0x01, ProtocolState.PLAY, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            String message = data.read(Type.V1_7_STRING, 0);
            onChat(session, message);
            return data;
        }
    });
    // login (pre-netty)
    addTranslator(0x01, ProtocolState.PRE_NETTY, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            int clientVersion = session.getUserData().getClientVersion().getRegistryId();
            String username = clientVersion <= 10 ? data.read(Type.UTF8_STRING, 1) : data.read(Type.STRING, 1);
            session.getUserData().setUsername(username);
            return data;
        }
    });
    // login (post-netty)
    addTranslator(0x00, ProtocolState.LOGIN, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            session.getUserData().setUsername(data.read(Type.V1_7_STRING, 0));
            return data;
        }
    });
}
Also used : ServerSession(com.github.dirtpowered.dirtmv.network.server.ServerSession) PacketTranslator(com.github.dirtpowered.dirtmv.data.translator.PacketTranslator) PacketData(com.github.dirtpowered.dirtmv.data.protocol.PacketData)

Example 40 with PacketData

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

the class WorldEntityEvent method playSoundAt.

private static void playSoundAt(ServerSession session, Location loc, Location target, Sound sound) {
    short correctedPitch = Shorts.constrainToRange((short) (sound.getPitch() * 63.0F), (short) 0, (short) 255);
    float vol = sound.getVolume();
    float range = 16F;
    if (vol > 1.0F) {
        range *= vol;
    }
    // cancel if outside allowed range
    if (target.distanceTo(loc) > range) {
        return;
    }
    PacketData namedSound = PacketUtil.createPacket(0x3E, new TypeHolder[] { new TypeHolder<>(Type.STRING, sound.getSoundName()), new TypeHolder<>(Type.INT, ((int) loc.getX()) * 8), new TypeHolder<>(Type.INT, ((int) loc.getY()) * 8), new TypeHolder<>(Type.INT, ((int) loc.getZ()) * 8), new TypeHolder<>(Type.FLOAT, vol), new TypeHolder<>(Type.UNSIGNED_BYTE, correctedPitch) });
    session.sendPacket(namedSound, PacketDirection.TO_CLIENT, MinecraftVersion.R1_3_1);
}
Also used : PacketData(com.github.dirtpowered.dirtmv.data.protocol.PacketData)

Aggregations

PacketData (com.github.dirtpowered.dirtmv.data.protocol.PacketData)40 TypeHolder (com.github.dirtpowered.dirtmv.data.protocol.TypeHolder)28 PacketTranslator (com.github.dirtpowered.dirtmv.data.translator.PacketTranslator)26 ServerSession (com.github.dirtpowered.dirtmv.network.server.ServerSession)25 ItemStack (com.github.dirtpowered.dirtmv.data.protocol.objects.ItemStack)14 ProtocolStorage (com.github.dirtpowered.dirtmv.data.user.ProtocolStorage)9 List (java.util.List)9 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 MinecraftVersion (com.github.dirtpowered.dirtmv.data.MinecraftVersion)5 Motion (com.github.dirtpowered.dirtmv.data.protocol.objects.Motion)5 V1_2MultiBlockArray (com.github.dirtpowered.dirtmv.data.protocol.objects.V1_2MultiBlockArray)5 SneakyThrows (lombok.SneakyThrows)5 EntityType (com.github.dirtpowered.dirtmv.data.entity.EntityType)4 NettyInputWrapper (com.github.dirtpowered.dirtmv.data.protocol.io.NettyInputWrapper)4 V1_2Chunk (com.github.dirtpowered.dirtmv.data.protocol.objects.V1_2Chunk)4 WatchableObject (com.github.dirtpowered.dirtmv.data.protocol.objects.WatchableObject)4 DirtMultiVersion (com.github.dirtpowered.dirtmv.DirtMultiVersion)3 V1_2RChunkStorage (com.github.dirtpowered.dirtmv.data.chunk.storage.V1_2RChunkStorage)3 BlockLocation (com.github.dirtpowered.dirtmv.data.protocol.objects.BlockLocation)3