Search in sources :

Example 6 with Motion

use of com.github.dirtpowered.dirtmv.data.protocol.objects.Motion 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)

Aggregations

Motion (com.github.dirtpowered.dirtmv.data.protocol.objects.Motion)6 PacketData (com.github.dirtpowered.dirtmv.data.protocol.PacketData)5 TypeHolder (com.github.dirtpowered.dirtmv.data.protocol.TypeHolder)5 PacketTranslator (com.github.dirtpowered.dirtmv.data.translator.PacketTranslator)5 ServerSession (com.github.dirtpowered.dirtmv.network.server.ServerSession)5 ItemStack (com.github.dirtpowered.dirtmv.data.protocol.objects.ItemStack)4 WatchableObject (com.github.dirtpowered.dirtmv.data.protocol.objects.WatchableObject)3 SneakyThrows (lombok.SneakyThrows)3 EntityType (com.github.dirtpowered.dirtmv.data.entity.EntityType)2 Location (com.github.dirtpowered.dirtmv.data.protocol.objects.Location)2 ProtocolStorage (com.github.dirtpowered.dirtmv.data.user.ProtocolStorage)2 List (java.util.List)2 Configuration (com.github.dirtpowered.dirtmv.api.Configuration)1 ObjectType (com.github.dirtpowered.dirtmv.data.entity.ObjectType)1 SpawnableObject (com.github.dirtpowered.dirtmv.data.entity.SpawnableObject)1 NettyInputWrapper (com.github.dirtpowered.dirtmv.data.protocol.io.NettyInputWrapper)1 NettyOutputWrapper (com.github.dirtpowered.dirtmv.data.protocol.io.NettyOutputWrapper)1 MetadataType (com.github.dirtpowered.dirtmv.data.protocol.objects.MetadataType)1 V1_3_4ChunkBulk (com.github.dirtpowered.dirtmv.data.protocol.objects.V1_3_4ChunkBulk)1 PlayerListEntry (com.github.dirtpowered.dirtmv.data.protocol.objects.tablist.PlayerListEntry)1