Search in sources :

Example 1 with PlayerPositionStorage1_13

use of com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.storage.PlayerPositionStorage1_13 in project ViaBackwards by ViaVersion.

the class EntityPackets1_13 method registerPackets.

@Override
protected void registerPackets() {
    protocol.registerClientbound(ClientboundPackets1_13.PLAYER_POSITION, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.DOUBLE);
            map(Type.DOUBLE);
            map(Type.DOUBLE);
            map(Type.FLOAT);
            map(Type.FLOAT);
            map(Type.BYTE);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    if (!ViaBackwards.getConfig().isFix1_13FacePlayer())
                        return;
                    PlayerPositionStorage1_13 playerStorage = wrapper.user().get(PlayerPositionStorage1_13.class);
                    byte bitField = wrapper.get(Type.BYTE, 0);
                    playerStorage.setX(toSet(bitField, 0, playerStorage.getX(), wrapper.get(Type.DOUBLE, 0)));
                    playerStorage.setY(toSet(bitField, 1, playerStorage.getY(), wrapper.get(Type.DOUBLE, 1)));
                    playerStorage.setZ(toSet(bitField, 2, playerStorage.getZ(), wrapper.get(Type.DOUBLE, 2)));
                }

                private double toSet(int field, int bitIndex, double origin, double packetValue) {
                    // If bit is set, coordinate is relative
                    return (field & (1 << bitIndex)) != 0 ? origin + packetValue : packetValue;
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_13.SPAWN_ENTITY, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.VAR_INT);
            map(Type.UUID);
            map(Type.BYTE);
            map(Type.DOUBLE);
            map(Type.DOUBLE);
            map(Type.DOUBLE);
            map(Type.BYTE);
            map(Type.BYTE);
            map(Type.INT);
            handler(getObjectTrackerHandler());
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    Optional<Entity1_13Types.ObjectType> optionalType = Entity1_13Types.ObjectType.findById(wrapper.get(Type.BYTE, 0));
                    if (!optionalType.isPresent())
                        return;
                    Entity1_13Types.ObjectType type = optionalType.get();
                    if (type == Entity1_13Types.ObjectType.FALLING_BLOCK) {
                        int blockState = wrapper.get(Type.INT, 0);
                        int combined = Protocol1_12_2To1_13.MAPPINGS.getNewBlockStateId(blockState);
                        combined = ((combined >> 4) & 0xFFF) | ((combined & 0xF) << 12);
                        wrapper.set(Type.INT, 0, combined);
                    } else if (type == Entity1_13Types.ObjectType.ITEM_FRAME) {
                        int data = wrapper.get(Type.INT, 0);
                        switch(data) {
                            case 3:
                                data = 0;
                                break;
                            case 4:
                                data = 1;
                                break;
                            case 5:
                                data = 3;
                                break;
                        }
                        wrapper.set(Type.INT, 0, data);
                    } else if (type == Entity1_13Types.ObjectType.TRIDENT) {
                        wrapper.set(Type.BYTE, 0, (byte) Entity1_13Types.ObjectType.TIPPED_ARROW.getId());
                    }
                }
            });
        }
    });
    registerTracker(ClientboundPackets1_13.SPAWN_EXPERIENCE_ORB, Entity1_13Types.EntityType.EXPERIENCE_ORB);
    registerTracker(ClientboundPackets1_13.SPAWN_GLOBAL_ENTITY, Entity1_13Types.EntityType.LIGHTNING_BOLT);
    protocol.registerClientbound(ClientboundPackets1_13.SPAWN_MOB, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.VAR_INT);
            map(Type.UUID);
            map(Type.VAR_INT);
            map(Type.DOUBLE);
            map(Type.DOUBLE);
            map(Type.DOUBLE);
            map(Type.BYTE);
            map(Type.BYTE);
            map(Type.BYTE);
            map(Type.SHORT);
            map(Type.SHORT);
            map(Type.SHORT);
            map(Types1_13.METADATA_LIST, Types1_12.METADATA_LIST);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int type = wrapper.get(Type.VAR_INT, 1);
                    EntityType entityType = Entity1_13Types.getTypeFromId(type, false);
                    tracker(wrapper.user()).addEntity(wrapper.get(Type.VAR_INT, 0), entityType);
                    int oldId = EntityTypeMapping.getOldId(type);
                    if (oldId == -1) {
                        if (!hasData(entityType)) {
                            ViaBackwards.getPlatform().getLogger().warning("Could not find 1.12 entity type for 1.13 entity type " + type + "/" + entityType);
                        }
                    } else {
                        wrapper.set(Type.VAR_INT, 1, oldId);
                    }
                }
            });
            // Rewrite entity type / metadata
            handler(getMobSpawnRewriter(Types1_12.METADATA_LIST));
        }
    });
    protocol.registerClientbound(ClientboundPackets1_13.SPAWN_PLAYER, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.VAR_INT);
            map(Type.UUID);
            map(Type.DOUBLE);
            map(Type.DOUBLE);
            map(Type.DOUBLE);
            map(Type.BYTE);
            map(Type.BYTE);
            map(Types1_13.METADATA_LIST, Types1_12.METADATA_LIST);
            handler(getTrackerAndMetaHandler(Types1_12.METADATA_LIST, Entity1_13Types.EntityType.PLAYER));
        }
    });
    protocol.registerClientbound(ClientboundPackets1_13.SPAWN_PAINTING, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.VAR_INT);
            map(Type.UUID);
            handler(getTrackerHandler(Entity1_13Types.EntityType.PAINTING, Type.VAR_INT));
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int motive = wrapper.read(Type.VAR_INT);
                    String title = PaintingMapping.getStringId(motive);
                    wrapper.write(Type.STRING, title);
                }
            });
        }
    });
    registerJoinGame(ClientboundPackets1_13.JOIN_GAME, Entity1_13Types.EntityType.PLAYER);
    protocol.registerClientbound(ClientboundPackets1_13.RESPAWN, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Dimension ID
            map(Type.INT);
            handler(getDimensionHandler(0));
            handler(wrapper -> wrapper.user().get(BackwardsBlockStorage.class).clear());
        }
    });
    registerRemoveEntities(ClientboundPackets1_13.DESTROY_ENTITIES);
    registerMetadataRewriter(ClientboundPackets1_13.ENTITY_METADATA, Types1_13.METADATA_LIST, Types1_12.METADATA_LIST);
    // Face Player (new packet)
    protocol.registerClientbound(ClientboundPackets1_13.FACE_PLAYER, null, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    wrapper.cancel();
                    if (!ViaBackwards.getConfig().isFix1_13FacePlayer())
                        return;
                    // We will just accept a possible, very minor mismatch between server and client position,
                    // and will take the server's one in both cases, else we would have to cache all entities' positions.
                    // feet/eyes enum
                    final int anchor = wrapper.read(Type.VAR_INT);
                    final double x = wrapper.read(Type.DOUBLE);
                    final double y = wrapper.read(Type.DOUBLE);
                    final double z = wrapper.read(Type.DOUBLE);
                    PlayerPositionStorage1_13 positionStorage = wrapper.user().get(PlayerPositionStorage1_13.class);
                    // Send teleport packet to client
                    PacketWrapper positionAndLook = wrapper.create(ClientboundPackets1_12_1.PLAYER_POSITION);
                    positionAndLook.write(Type.DOUBLE, 0D);
                    positionAndLook.write(Type.DOUBLE, 0D);
                    positionAndLook.write(Type.DOUBLE, 0D);
                    // TODO properly cache and calculate head position?
                    EntityPositionHandler.writeFacingDegrees(positionAndLook, positionStorage.getX(), anchor == 1 ? positionStorage.getY() + 1.62 : positionStorage.getY(), positionStorage.getZ(), x, y, z);
                    // bitfield, 0=absolute, 1=relative - x,y,z relative, yaw,pitch absolute
                    positionAndLook.write(Type.BYTE, (byte) 7);
                    positionAndLook.write(Type.VAR_INT, -1);
                    positionAndLook.send(Protocol1_12_2To1_13.class);
                }
            });
        }
    });
    if (ViaBackwards.getConfig().isFix1_13FacePlayer()) {
        PacketRemapper movementRemapper = new PacketRemapper() {

            @Override
            public void registerMap() {
                map(Type.DOUBLE);
                map(Type.DOUBLE);
                map(Type.DOUBLE);
                handler(wrapper -> wrapper.user().get(PlayerPositionStorage1_13.class).setCoordinates(wrapper, false));
            }
        };
        // Player Position
        protocol.registerServerbound(ServerboundPackets1_12_1.PLAYER_POSITION, movementRemapper);
        // Player Position And Look (serverbound)
        protocol.registerServerbound(ServerboundPackets1_12_1.PLAYER_POSITION_AND_ROTATION, movementRemapper);
        // Vehicle Move (serverbound)
        protocol.registerServerbound(ServerboundPackets1_12_1.VEHICLE_MOVE, movementRemapper);
    }
}
Also used : PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) PlayerPositionStorage1_13(com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.storage.PlayerPositionStorage1_13) Entity1_13Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_13Types) Types1_13(com.viaversion.viaversion.api.type.types.version.Types1_13) Types1_12(com.viaversion.viaversion.api.type.types.version.Types1_12) Item(com.viaversion.viaversion.api.minecraft.item.Item) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) ClientboundPackets1_12_1(com.viaversion.viaversion.protocols.protocol1_12_1to1_12.ClientboundPackets1_12_1) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) Protocol1_12_2To1_13(com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.Protocol1_12_2To1_13) MetaType1_12(com.viaversion.viaversion.api.minecraft.metadata.types.MetaType1_12) Particle(com.viaversion.viaversion.api.type.types.Particle) LegacyEntityRewriter(com.viaversion.viabackwards.api.rewriters.LegacyEntityRewriter) ServerboundPackets1_12_1(com.viaversion.viaversion.protocols.protocol1_12_1to1_12.ServerboundPackets1_12_1) BackwardsBlockStorage(com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.storage.BackwardsBlockStorage) EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) ParticleMapping(com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.data.ParticleMapping) EntityPositionHandler(com.viaversion.viabackwards.api.entities.storage.EntityPositionHandler) EntityTypeMapping(com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.data.EntityTypeMapping) ClientboundPackets1_13(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13) ChatRewriter(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ChatRewriter) ViaBackwards(com.viaversion.viabackwards.ViaBackwards) Optional(java.util.Optional) PaintingMapping(com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.data.PaintingMapping) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) BackwardsBlockStorage(com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.storage.BackwardsBlockStorage) PlayerPositionStorage1_13(com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.storage.PlayerPositionStorage1_13) Entity1_13Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_13Types)

Example 2 with PlayerPositionStorage1_13

use of com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.storage.PlayerPositionStorage1_13 in project ViaBackwards by ViaVersion.

the class Protocol1_12_2To1_13 method init.

@Override
public void init(UserConnection user) {
    // Register ClientWorld
    if (!user.has(ClientWorld.class)) {
        user.put(new ClientWorld(user));
    }
    user.addEntityTracker(this.getClass(), new EntityTrackerBase(user, Entity1_13Types.EntityType.PLAYER));
    user.put(new BackwardsBlockStorage());
    user.put(new TabCompleteStorage());
    if (ViaBackwards.getConfig().isFix1_13FacePlayer() && !user.has(PlayerPositionStorage1_13.class)) {
        user.put(new PlayerPositionStorage1_13());
    }
}
Also used : BackwardsBlockStorage(com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.storage.BackwardsBlockStorage) PlayerPositionStorage1_13(com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.storage.PlayerPositionStorage1_13) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) EntityTrackerBase(com.viaversion.viaversion.data.entity.EntityTrackerBase) TabCompleteStorage(com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.storage.TabCompleteStorage)

Aggregations

BackwardsBlockStorage (com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.storage.BackwardsBlockStorage)2 PlayerPositionStorage1_13 (com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.storage.PlayerPositionStorage1_13)2 ViaBackwards (com.viaversion.viabackwards.ViaBackwards)1 EntityPositionHandler (com.viaversion.viabackwards.api.entities.storage.EntityPositionHandler)1 LegacyEntityRewriter (com.viaversion.viabackwards.api.rewriters.LegacyEntityRewriter)1 Protocol1_12_2To1_13 (com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.Protocol1_12_2To1_13)1 EntityTypeMapping (com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.data.EntityTypeMapping)1 PaintingMapping (com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.data.PaintingMapping)1 ParticleMapping (com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.data.ParticleMapping)1 TabCompleteStorage (com.viaversion.viabackwards.protocol.protocol1_12_2to1_13.storage.TabCompleteStorage)1 Entity1_13Types (com.viaversion.viaversion.api.minecraft.entities.Entity1_13Types)1 EntityType (com.viaversion.viaversion.api.minecraft.entities.EntityType)1 Item (com.viaversion.viaversion.api.minecraft.item.Item)1 Metadata (com.viaversion.viaversion.api.minecraft.metadata.Metadata)1 MetaType1_12 (com.viaversion.viaversion.api.minecraft.metadata.types.MetaType1_12)1 PacketWrapper (com.viaversion.viaversion.api.protocol.packet.PacketWrapper)1 PacketHandler (com.viaversion.viaversion.api.protocol.remapper.PacketHandler)1 PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)1 Type (com.viaversion.viaversion.api.type.Type)1 Particle (com.viaversion.viaversion.api.type.types.Particle)1