Search in sources :

Example 1 with VehicleTracker

use of com.github.dirtpowered.dirtmv.network.versions.Release73To61.entity.VehicleTracker in project DirtMultiversion by DirtPowered.

the class ProtocolRelease73To61 method registerTranslators.

@Override
public void registerTranslators() {
    // login
    addTranslator(0x01, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            int entityId = data.read(Type.INT, 0);
            session.getUserData().setEntityId(entityId);
            EntityTracker tracker = new EntityTracker();
            tracker.addEntity(entityId, EntityType.HUMAN);
            session.getUserData().getProtocolStorage().set(EntityTracker.class, tracker);
            // send entity attributes (fixes fast movement)
            session.sendPacket(data, PacketDirection.TO_CLIENT, getFrom());
            session.sendPacket(getDefaultAttributes(entityId), PacketDirection.TO_CLIENT, getFrom());
            return cancel();
        }
    });
    // respawn
    addTranslator(0x09, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            int entityId = session.getUserData().getEntityId();
            session.sendPacket(data, PacketDirection.TO_CLIENT, getFrom());
            session.sendPacket(getDefaultAttributes(entityId), PacketDirection.TO_CLIENT, getFrom());
            return cancel();
        }
    });
    // 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) 61), 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.1");
            pingMessage.setProtocol(session.getUserData().getClientVersion().getRegistryId());
            return PacketUtil.createPacket(0xFF, new TypeHolder[] { set(Type.STRING, ServerMotd.serialize(pingMessage)) });
        }
    });
    // chat
    addTranslator(0x03, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            String message = data.read(Type.STRING, 0);
            return PacketUtil.createPacket(0x03, new TypeHolder[] { set(Type.STRING, ChatUtils.legacyToJsonString(message)) });
        }
    });
    // update health
    addTranslator(0x08, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0x08, new TypeHolder[] { set(Type.FLOAT, data.read(Type.SHORT, 0).floatValue()), data.read(1), data.read(2) });
        }
    });
    // mob spawn
    addTranslator(0x18, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            EntityType entityType = EntityType.fromEntityTypeId(data.read(Type.BYTE, 1));
            int entityId = data.read(Type.INT, 0);
            EntityTracker tracker = session.getUserData().getProtocolStorage().get(EntityTracker.class);
            tracker.addEntity(entityId, entityType);
            WatchableObject[] oldMeta = data.read(Type.V1_4R_METADATA, 11);
            WatchableObject[] newMeta = metadataTransformer.transformMetadata(entityType, oldMeta);
            return PacketUtil.createPacket(0x18, new TypeHolder[] { data.read(0), data.read(1), data.read(2), data.read(3), data.read(4), data.read(5), data.read(6), data.read(7), data.read(8), data.read(9), data.read(10), set(Type.V1_4R_METADATA, newMeta) });
        }
    });
    // entity destroy
    addTranslator(0x1D, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            int[] entities = data.read(Type.BYTE_INT_ARRAY, 0);
            EntityTracker tracker = session.getUserData().getProtocolStorage().get(EntityTracker.class);
            for (int entityId : entities) {
                tracker.removeEntity(entityId);
            }
            return data;
        }
    });
    // entity metadata
    addTranslator(0x28, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            int entityId = data.read(Type.INT, 0);
            EntityTracker tracker = session.getUserData().getProtocolStorage().get(EntityTracker.class);
            EntityType entityType = tracker.getEntityById(entityId);
            if (entityType == null) {
                Logger.warn("[{}] skipping translating metadata for {}. Entity is not tracked", session.getLogTag(), entityId);
                return cancel();
            }
            WatchableObject[] oldMeta = data.read(Type.V1_4R_METADATA, 1);
            WatchableObject[] newMeta = metadataTransformer.transformMetadata(entityType, oldMeta);
            return PacketUtil.createPacket(0x28, new TypeHolder[] { data.read(0), set(Type.V1_4R_METADATA, newMeta) });
        }
    });
    // name entity spawn
    addTranslator(0x14, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            int entityId = data.read(Type.INT, 0);
            EntityTracker tracker = session.getUserData().getProtocolStorage().get(EntityTracker.class);
            tracker.addEntity(entityId, EntityType.HUMAN);
            WatchableObject[] oldMeta = data.read(Type.V1_4R_METADATA, 8);
            WatchableObject[] newMeta = metadataTransformer.transformMetadata(EntityType.HUMAN, oldMeta);
            return PacketUtil.createPacket(0x14, new TypeHolder[] { data.read(0), data.read(1), data.read(2), data.read(3), data.read(4), data.read(5), data.read(6), data.read(7), set(Type.V1_4R_METADATA, newMeta) });
        }
    });
    // vehicle spawn
    addTranslator(0x17, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            int entityId = data.read(Type.INT, 0);
            byte vehicleEntity = data.read(Type.BYTE, 1);
            EntityTracker tracker = session.getUserData().getProtocolStorage().get(EntityTracker.class);
            switch(vehicleEntity) {
                case 0x02:
                    /* item */
                    tracker.addEntity(entityId, EntityType.ITEM);
                    break;
                case 0x46:
                    /* sand, gravel */
                    tracker.addEntity(entityId, EntityType.FALLING_OBJECT);
                    break;
            }
            return data;
        }
    });
    // statistics
    addTranslator(0xC8, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0xC8, new TypeHolder[] { data.read(0), set(Type.INT, data.read(Type.BYTE, 1).intValue()) });
        }
    });
    // level sound
    addTranslator(0x3E, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            String soundName = data.read(Type.STRING, 0);
            String newSoundName = soundRemapper.getNewSoundName(soundName);
            if (newSoundName.isEmpty())
                return cancel();
            return PacketUtil.createPacket(0x3E, new TypeHolder[] { set(Type.STRING, newSoundName), data.read(1), data.read(2), data.read(3), data.read(4), data.read(5) });
        }
    });
    // player abilities
    addTranslator(0xCA, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0xCA, new TypeHolder[] { data.read(0), set(Type.BYTE, (byte) (data.read(Type.FLOAT, 1) * 255F)), set(Type.BYTE, (byte) (data.read(Type.FLOAT, 2) * 255F)) });
        }
    });
    // player abilities
    addTranslator(0xCA, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            return PacketUtil.createPacket(0xCA, new TypeHolder[] { data.read(0), set(Type.FLOAT, (data.read(Type.BYTE, 1) / 255F)), set(Type.FLOAT, (data.read(Type.BYTE, 2) / 255F)) });
        }
    });
    // entity action
    addTranslator(0x13, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            // client -> server
            return PacketUtil.createPacket(0x13, new TypeHolder[] { data.read(0), data.read(1) });
        }
    });
    // entity attach
    addTranslator(0x27, PacketDirection.TO_CLIENT, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            VehicleTracker vehicleTracker = session.getUserData().getProtocolStorage().get(VehicleTracker.class);
            int vehicleId = data.read(Type.INT, 1);
            if (vehicleId != -1) {
                vehicleTracker.setVehicleId(vehicleId);
            }
            return PacketUtil.createPacket(0x27, new TypeHolder[] { data.read(0), data.read(1), // leash
            set(Type.BOOLEAN, false) });
        }
    });
    // 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);
            if (item != null && !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) });
        }
    });
    // steer vehicle
    addTranslator(0x1B, PacketDirection.TO_SERVER, new PacketTranslator() {

        @Override
        public PacketData translate(ServerSession session, PacketData data) {
            VehicleTracker vehicleTracker = session.getUserData().getProtocolStorage().get(VehicleTracker.class);
            boolean dismount = data.read(Type.BOOLEAN, 3);
            if (dismount && vehicleTracker.getVehicleId() != -999) {
                int cachedId = vehicleTracker.getVehicleId();
                vehicleTracker.setVehicleId(-999);
                return PacketUtil.createPacket(0x07, new TypeHolder[] { set(Type.INT, session.getUserData().getEntityId()), set(Type.INT, cachedId), set(Type.BYTE, (byte) 0) });
            }
            // packet doesn't exist in 1.5
            return cancel();
        }
    });
}
Also used : TypeHolder(com.github.dirtpowered.dirtmv.data.protocol.TypeHolder) EntityType(com.github.dirtpowered.dirtmv.data.entity.EntityType) ServerSession(com.github.dirtpowered.dirtmv.network.server.ServerSession) PacketTranslator(com.github.dirtpowered.dirtmv.data.translator.PacketTranslator) EntityTracker(com.github.dirtpowered.dirtmv.network.versions.Release73To61.entity.EntityTracker) ServerMotd(com.github.dirtpowered.dirtmv.network.versions.Release73To61.ping.ServerMotd) ItemStack(com.github.dirtpowered.dirtmv.data.protocol.objects.ItemStack) VehicleTracker(com.github.dirtpowered.dirtmv.network.versions.Release73To61.entity.VehicleTracker) PacketData(com.github.dirtpowered.dirtmv.data.protocol.PacketData)

Aggregations

EntityType (com.github.dirtpowered.dirtmv.data.entity.EntityType)1 PacketData (com.github.dirtpowered.dirtmv.data.protocol.PacketData)1 TypeHolder (com.github.dirtpowered.dirtmv.data.protocol.TypeHolder)1 ItemStack (com.github.dirtpowered.dirtmv.data.protocol.objects.ItemStack)1 PacketTranslator (com.github.dirtpowered.dirtmv.data.translator.PacketTranslator)1 ServerSession (com.github.dirtpowered.dirtmv.network.server.ServerSession)1 EntityTracker (com.github.dirtpowered.dirtmv.network.versions.Release73To61.entity.EntityTracker)1 VehicleTracker (com.github.dirtpowered.dirtmv.network.versions.Release73To61.entity.VehicleTracker)1 ServerMotd (com.github.dirtpowered.dirtmv.network.versions.Release73To61.ping.ServerMotd)1