Search in sources :

Example 6 with PacketData

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

the class PacketUtil method readPacket.

public static PacketData readPacket(MinecraftVersion version, PacketInput buffer) throws IOException {
    Preconditions.checkNotNull(version, "Version not provided");
    short packetId = buffer.readUnsignedByte();
    BaseProtocol protocol = ProtocolRegistry.getProtocolFromVersion(version);
    Preconditions.checkNotNull(protocol, "Protocol %s is not registered", version);
    DataType[] parts = protocol.dataTypes[packetId];
    String packetMapping = PreNettyPacketNames.getPacketName(packetId);
    String protocolName = protocol.getClass().getSimpleName();
    Preconditions.checkNotNull(parts, "Unknown packet id %s (%s) in protocol %s", packetId, packetMapping, protocolName);
    TypeHolder[] typeHolders = new TypeHolder[parts.length];
    int i = 0;
    while (i < parts.length) {
        DataType dataType = parts[i];
        typeHolders[i] = new TypeHolder(dataType.getType(), dataType.read(buffer));
        i++;
    }
    return new PacketData(packetId, typeHolders);
}
Also used : TypeHolder(com.github.dirtpowered.dirtmv.data.protocol.TypeHolder) DataType(com.github.dirtpowered.dirtmv.data.protocol.DataType) BaseProtocol(com.github.dirtpowered.dirtmv.data.protocol.BaseProtocol) PacketData(com.github.dirtpowered.dirtmv.data.protocol.PacketData)

Example 7 with PacketData

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

the class ServerSession method sendDisconnectPacket.

private void sendDisconnectPacket(String message) {
    PacketData kickPacket;
    if (userData.getClientVersion().isNettyProtocol()) {
        boolean state = userData.getProtocolState() == ProtocolState.LOGIN;
        kickPacket = PacketUtil.createPacket(state ? 0x00 : 0x40, new TypeHolder[] { new TypeHolder<>(Type.V1_7_STRING, ChatUtils.legacyToJsonString(message)) });
    } else {
        kickPacket = PacketUtil.createPacket(0xFF, new TypeHolder[] { new TypeHolder<>(Type.STRING, message) });
    }
    sendPacket(kickPacket);
}
Also used : TypeHolder(com.github.dirtpowered.dirtmv.data.protocol.TypeHolder) PacketData(com.github.dirtpowered.dirtmv.data.protocol.PacketData)

Example 8 with PacketData

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

the class PacketDecoder method decode.

@Override
protected void decode(ChannelHandlerContext context, ByteBuf buffer, List<Object> list) throws IOException {
    boolean flag = packetDirection == PacketDirection.TO_CLIENT;
    setUserProtocol(flag, buffer);
    PacketInput inputBuffer = new NettyInputWrapper(buffer);
    PacketData packet = PacketUtil.readPacket(flag ? main.getConfiguration().getServerVersion() : userData.getClientVersion(), inputBuffer);
    setProtocolState(packet);
    list.add(packet);
}
Also used : PacketInput(com.github.dirtpowered.dirtmv.data.protocol.io.model.PacketInput) NettyInputWrapper(com.github.dirtpowered.dirtmv.data.protocol.io.NettyInputWrapper) PacketData(com.github.dirtpowered.dirtmv.data.protocol.PacketData)

Example 9 with PacketData

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

the class NettyPacketDecoder method decode.

@Override
protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
    if (byteBuf.readableBytes() == 0)
        return;
    PacketInput inputBuffer = new NettyInputWrapper(byteBuf);
    int i = inputBuffer.readVarInt();
    boolean flag = packetDirection == PacketDirection.TO_CLIENT;
    ProtocolState protocolState = userData.getProtocolState();
    if (protocolState == ProtocolState.HANDSHAKE) {
        int readerIndex = byteBuf.readerIndex();
        int protocol = inputBuffer.readVarInt();
        byteBuf.readerIndex(readerIndex);
        userData.setClientVersion(MinecraftVersion.fromNettyProtocolId(protocol));
    }
    PacketData packet;
    if (flag) {
        packet = readModernPacket(main.getConfiguration().getServerVersion(), protocolState, inputBuffer, packetDirection, i);
    } else {
        packet = readModernPacket(userData.getClientVersion(), protocolState, inputBuffer, packetDirection, i);
    }
    int readableBytes = byteBuf.readableBytes();
    if (readableBytes > 0) {
        byteBuf.skipBytes(readableBytes);
        Logger.warn("skipping {} bytes for packet id: {}, direction: {}", readableBytes, i, packetDirection);
    } else {
        list.add(packet);
    }
}
Also used : PacketInput(com.github.dirtpowered.dirtmv.data.protocol.io.model.PacketInput) NettyInputWrapper(com.github.dirtpowered.dirtmv.data.protocol.io.NettyInputWrapper) ProtocolState(com.github.dirtpowered.dirtmv.data.translator.ProtocolState) PacketData(com.github.dirtpowered.dirtmv.data.protocol.PacketData)

Example 10 with PacketData

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

the class ProtocolBeta11To10 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.UTF8_STRING, data.read(Type.STRING, 0)) });
        }
    });
    // handshake
    addTranslator(0x02, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x02, new TypeHolder[] { set(Type.STRING, data.read(Type.UTF8_STRING, 0)) });
        }
    });
    // login
    addTranslator(0x01, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x01, new TypeHolder[] { set(Type.INT, 10), set(Type.UTF8_STRING, data.read(Type.STRING, 1)), set(Type.UTF8_STRING, "-"), data.read(2), data.read(3) });
        }
    });
    // login
    addTranslator(0x01, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x01, new TypeHolder[] { data.read(0), set(Type.STRING, data.read(Type.UTF8_STRING, 1)), data.read(3), data.read(4) });
        }
    });
    // chat
    addTranslator(0x03, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x03, new TypeHolder[] { set(Type.STRING, data.read(Type.UTF8_STRING, 0)) });
        }
    });
    // chat
    addTranslator(0x03, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x03, new TypeHolder[] { set(Type.UTF8_STRING, data.read(Type.STRING, 0)) });
        }
    });
    // kick disconnect
    addTranslator(0xFF, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0xFF, new TypeHolder[] { set(Type.STRING, data.read(Type.UTF8_STRING, 0)) });
        }
    });
    // named entity spawn
    addTranslator(0x14, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x14, new TypeHolder[] { data.read(0), set(Type.STRING, data.read(Type.UTF8_STRING, 1)), data.read(2), data.read(3), data.read(4), data.read(5), data.read(6), data.read(7) });
        }
    });
    // spawn painting
    addTranslator(0x19, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x19, new TypeHolder[] { data.read(0), set(Type.STRING, data.read(Type.UTF8_STRING, 1)), data.read(2), data.read(3), data.read(4), data.read(5) });
        }
    });
    // update sign
    addTranslator(0x82, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x82, new TypeHolder[] { data.read(0), data.read(1), data.read(2), set(Type.STRING, data.read(Type.UTF8_STRING, 3)), set(Type.STRING, data.read(Type.UTF8_STRING, 4)), set(Type.STRING, data.read(Type.UTF8_STRING, 5)), set(Type.STRING, data.read(Type.UTF8_STRING, 6)) });
        }
    });
    // window click
    addTranslator(0x66, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            boolean shiftClick = (data.read(Type.BYTE, 4) == 1);
            // cancel shift-clicking
            ItemStack itemStack;
            if (!shiftClick) {
                itemStack = data.read(Type.V1_3B_ITEM, 5);
            } else {
                /*
                     * sending invalid/other item than held item is forcing server to send window update packet
                     * It should prevent against all inventory desyncs
                     */
                itemStack = new ItemStack(99, 0, 1, null);
            }
            return PacketUtil.createPacket(0x66, new TypeHolder[] { data.read(0), data.read(1), data.read(2), data.read(3), set(Type.V1_3B_ITEM, itemStack) });
        }
    });
}
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) ItemStack(com.github.dirtpowered.dirtmv.data.protocol.objects.ItemStack) 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