Search in sources :

Example 6 with TypeHolder

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

the class PacketUtil method readModernPacket.

public static PacketData readModernPacket(MinecraftVersion ver, ProtocolState state, PacketInput buf, PacketDirection dir, int id) throws IOException {
    BaseProtocol protocol = ProtocolRegistry.getProtocolFromVersion(ver);
    DataType[] parts = protocol.getStateDependedProtocol().getInstruction(id, state, dir);
    if (parts == null) {
        Logger.warn("Unknown packet id {} ({}), state: {}, direction: {}", StringUtils.intToHexStr(id), id, state, dir);
        return new PacketData(0);
    }
    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(buf));
        i++;
    }
    return new PacketData(id, 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 TypeHolder

use of com.github.dirtpowered.dirtmv.data.protocol.TypeHolder 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 8 with TypeHolder

use of com.github.dirtpowered.dirtmv.data.protocol.TypeHolder 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 9 with TypeHolder

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

Example 10 with TypeHolder

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

the class ProtocolBeta14To13 method registerTranslators.

@Override
public void registerTranslators() {
    // 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, 13), data.read(1), 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), data.read(1), data.read(2), data.read(3) });
        }
    });
}
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) PacketData(com.github.dirtpowered.dirtmv.data.protocol.PacketData)

Aggregations

TypeHolder (com.github.dirtpowered.dirtmv.data.protocol.TypeHolder)35 PacketData (com.github.dirtpowered.dirtmv.data.protocol.PacketData)28 PacketTranslator (com.github.dirtpowered.dirtmv.data.translator.PacketTranslator)23 ServerSession (com.github.dirtpowered.dirtmv.network.server.ServerSession)23 ItemStack (com.github.dirtpowered.dirtmv.data.protocol.objects.ItemStack)15 ProtocolStorage (com.github.dirtpowered.dirtmv.data.user.ProtocolStorage)9 List (java.util.List)8 Motion (com.github.dirtpowered.dirtmv.data.protocol.objects.Motion)5 WatchableObject (com.github.dirtpowered.dirtmv.data.protocol.objects.WatchableObject)5 SneakyThrows (lombok.SneakyThrows)5 MinecraftVersion (com.github.dirtpowered.dirtmv.data.MinecraftVersion)4 EntityType (com.github.dirtpowered.dirtmv.data.entity.EntityType)4 BlockLocation (com.github.dirtpowered.dirtmv.data.protocol.objects.BlockLocation)4 V1_2MultiBlockArray (com.github.dirtpowered.dirtmv.data.protocol.objects.V1_2MultiBlockArray)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 DirtMultiVersion (com.github.dirtpowered.dirtmv.DirtMultiVersion)3 Location (com.github.dirtpowered.dirtmv.data.protocol.objects.Location)3 V1_2Chunk (com.github.dirtpowered.dirtmv.data.protocol.objects.V1_2Chunk)3 BlockStorage (com.github.dirtpowered.dirtmv.network.versions.Beta17To14.storage.BlockStorage)3