Search in sources :

Example 21 with TypeHolder

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

the class MetadataDataType method write.

@Override
public void write(TypeHolder typeHolder, PacketOutput packetOutput) throws IOException {
    WatchableObject[] watchableObjects = (WatchableObject[]) typeHolder.getObject();
    if (watchableObjects == null || watchableObjects.length == 0) {
        packetOutput.writeByte(127);
        return;
    }
    for (WatchableObject watchableObject : watchableObjects) {
        int header = (watchableObject.getType().getType() << 5 | watchableObject.getIndex() & 31) & 255;
        packetOutput.writeByte(header);
        switch(watchableObject.getType()) {
            case BYTE:
                BaseProtocol.BYTE.write(new TypeHolder(Type.BYTE, watchableObject.getValue()), packetOutput);
                break;
            case SHORT:
                BaseProtocol.SHORT.write(new TypeHolder(Type.SHORT, watchableObject.getValue()), packetOutput);
                break;
            case INT:
                BaseProtocol.INT.write(new TypeHolder(Type.INT, watchableObject.getValue()), packetOutput);
                break;
            case FLOAT:
                BaseProtocol.FLOAT.write(new TypeHolder(Type.FLOAT, watchableObject.getValue()), packetOutput);
                break;
            case STRING:
                if (getType() == Type.V1_7R_METADATA || getType() == Type.V1_8R_METADATA) {
                    V1_7_2RProtocol.STRING.write(new TypeHolder(Type.V1_7_STRING, watchableObject.getValue()), packetOutput);
                } else {
                    BaseProtocol.STRING.write(new TypeHolder(Type.STRING, watchableObject.getValue()), packetOutput);
                }
                break;
            case ITEM:
                if (getType() == Type.V1_4R_METADATA || getType() == Type.V1_7R_METADATA) {
                    V1_3_1RProtocol.ITEM.write(new TypeHolder(Type.V1_3R_ITEM, watchableObject.getValue()), packetOutput);
                } else if (getType() == Type.V1_8R_METADATA) {
                    V1_8RProtocol.ITEM.write(new TypeHolder(Type.V1_8R_ITEM, watchableObject.getValue()), packetOutput);
                } else {
                    V1_3BProtocol.ITEM.write(new TypeHolder(Type.V1_3B_ITEM, watchableObject.getValue()), packetOutput);
                }
                break;
            case POSITION:
                BlockLocation location = (BlockLocation) watchableObject.getValue();
                packetOutput.writeInt(location.getX());
                packetOutput.writeInt(location.getY());
                packetOutput.writeInt(location.getZ());
                break;
            case ROTATION:
                Rotation rotation = (Rotation) watchableObject.getValue();
                packetOutput.writeFloat(rotation.getX());
                packetOutput.writeFloat(rotation.getY());
                packetOutput.writeFloat(rotation.getZ());
                break;
        }
    }
    packetOutput.writeByte(127);
}
Also used : TypeHolder(com.github.dirtpowered.dirtmv.data.protocol.TypeHolder) BlockLocation(com.github.dirtpowered.dirtmv.data.protocol.objects.BlockLocation) WatchableObject(com.github.dirtpowered.dirtmv.data.protocol.objects.WatchableObject) Rotation(com.github.dirtpowered.dirtmv.data.protocol.objects.Rotation)

Example 22 with TypeHolder

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

the class ItemArrayDataType method write.

@Override
public void write(TypeHolder typeHolder, PacketOutput packetOutput) throws IOException {
    ItemStack[] objArray = (ItemStack[]) typeHolder.getObject();
    packetOutput.writeShort(objArray.length);
    for (ItemStack item : objArray) {
        childInstruction.write(new TypeHolder(childInstruction.getType(), item), packetOutput);
    }
}
Also used : TypeHolder(com.github.dirtpowered.dirtmv.data.protocol.TypeHolder) ItemStack(com.github.dirtpowered.dirtmv.data.protocol.objects.ItemStack)

Example 23 with TypeHolder

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

the class ProtocolPassthroughEncrypted method registerTranslators.

@Override
public void registerTranslators() {
    // server auth data
    addTranslator(0xFD, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @SneakyThrows
        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            PacketData encryptRequest = EncryptionUtils.createEncryptionRequest(session);
            SecretKey secretKey = EncryptionUtils.getSecretKey();
            session.getUserData().setSecretKey(secretKey);
            byte[] publicKeyBytes = (byte[]) data.read(1).getObject();
            X509EncodedKeySpec spec = new X509EncodedKeySpec(publicKeyBytes);
            PublicKey publicKey;
            try {
                KeyFactory keyFactory;
                keyFactory = KeyFactory.getInstance("RSA");
                publicKey = keyFactory.generatePublic(spec);
                // fake proxy client response
                byte[] token = (byte[]) data.read(2).getObject();
                byte[] sharedKey = EncryptionUtils.getSharedKey(secretKey, publicKey);
                byte[] encryptedData = EncryptionUtils.encrypt(publicKey, token);
                PacketData response = PacketUtil.createPacket(0xFC, new TypeHolder[] { set(Type.SHORT_BYTE_ARRAY, sharedKey), set(Type.SHORT_BYTE_ARRAY, encryptedData) });
                session.sendPacket(response, PacketDirection.TO_SERVER, getFrom());
            } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
                e.printStackTrace();
            }
            return encryptRequest;
        }
    });
    // client shared key
    addTranslator(0xFC, PacketDirection.TO_SERVER, new PacketTranslator() {

        @SneakyThrows
        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            SecretKey shared = EncryptionUtils.getSecret(data, session.getUserData().getProxyRequest());
            // server -> client
            EncryptionUtils.sendEmptyEncryptionResponse(session, getFrom());
            // enable encryption
            EncryptionUtils.setEncryption(session.getChannel(), shared);
            // cancel packet
            return cancel();
        }
    });
    // client shared key
    addTranslator(0xFC, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            // enable client connection encryption
            EncryptionUtils.setEncryption(session.getClientSession().getChannel(), session.getUserData().getSecretKey());
            // cancel packet
            return cancel();
        }
    });
}
Also used : TypeHolder(com.github.dirtpowered.dirtmv.data.protocol.TypeHolder) ServerSession(com.github.dirtpowered.dirtmv.network.server.ServerSession) SecretKey(javax.crypto.SecretKey) PacketTranslator(com.github.dirtpowered.dirtmv.data.translator.PacketTranslator) PublicKey(java.security.PublicKey) SneakyThrows(lombok.SneakyThrows) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) KeyFactory(java.security.KeyFactory) PacketData(com.github.dirtpowered.dirtmv.data.protocol.PacketData)

Example 24 with TypeHolder

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

the class ProtocolStateHandler method registerTranslators.

@Override
public void registerTranslators() {
    addTranslator(0x00, ProtocolState.HANDSHAKE, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            UserData userData = session.getUserData();
            userData.setAddress(data.read(Type.V1_7_STRING, 1));
            userData.setPort(data.read(Type.UNSIGNED_SHORT, 2));
            userData.setProtocolState(ProtocolState.fromId(data.read(Type.VAR_INT, 3)));
            return PacketUtil.createPacket(0x00, new TypeHolder[] { // protocol version
            set(Type.VAR_INT, getTo().getProtocolNettyId()), data.read(1), data.read(2), data.read(3) });
        }
    });
    addTranslator(0x02, ProtocolState.LOGIN, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            session.getUserData().setProtocolState(ProtocolState.PLAY);
            return data;
        }
    });
}
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) UserData(com.github.dirtpowered.dirtmv.data.user.UserData) PacketData(com.github.dirtpowered.dirtmv.data.protocol.PacketData)

Example 25 with TypeHolder

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

the class ProtocolRelease23To22 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[] { // protocol version
            set(Type.INT, 22), data.read(1), data.read(2), data.read(4), data.read(5), data.read(6), data.read(7), data.read(8) });
        }
    });
    // 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), // world type
            set(Type.STRING, "default"), data.read(3), data.read(4), data.read(5), data.read(6), data.read(7) });
        }
    });
    // respawn
    addTranslator(0x09, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x09, new TypeHolder[] { data.read(0), data.read(1), data.read(2), data.read(3), data.read(4) });
        }
    });
    // respawn
    addTranslator(0x09, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x09, new TypeHolder[] { data.read(0), data.read(1), data.read(2), data.read(3), data.read(4), // world type
            set(Type.STRING, "default") });
        }
    });
    // custom payload
    addTranslator(0xFA, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            // cancel packet
            return cancel();
        }
    });
    // chat
    addTranslator(0x03, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            String filteredMessage = ChatFilter.filterChat(data.read(Type.STRING, 0));
            return PacketUtil.createPacket(0x03, new TypeHolder[] { set(Type.STRING, filteredMessage) });
        }
    });
    // 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_0R_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_0R_ITEM, item) });
        }
    });
}
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

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