Search in sources :

Example 1 with Metadata

use of com.viaversion.viaversion.api.minecraft.metadata.Metadata in project ViaBackwards by ViaVersion.

the class EntityPackets1_10 method registerPackets.

@Override
protected void registerPackets() {
    protocol.registerClientbound(ClientboundPackets1_9_3.SPAWN_ENTITY, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity id
            map(Type.VAR_INT);
            // 1 - UUID
            map(Type.UUID);
            // 2 - Type
            map(Type.BYTE);
            // 3 - x
            map(Type.DOUBLE);
            // 4 - y
            map(Type.DOUBLE);
            // 5 - z
            map(Type.DOUBLE);
            // 6 - Pitch
            map(Type.BYTE);
            // 7 - Yaw
            map(Type.BYTE);
            // 8 - data
            map(Type.INT);
            // Track Entity
            handler(getObjectTrackerHandler());
            handler(getObjectRewriter(id -> Entity1_11Types.ObjectType.findById(id).orElse(null)));
            // Handle FallingBlock blocks
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    Optional<Entity1_12Types.ObjectType> type = Entity1_12Types.ObjectType.findById(wrapper.get(Type.BYTE, 0));
                    if (type.isPresent() && type.get() == Entity1_12Types.ObjectType.FALLING_BLOCK) {
                        int objectData = wrapper.get(Type.INT, 0);
                        int objType = objectData & 4095;
                        int data = objectData >> 12 & 15;
                        Block block = protocol.getItemRewriter().handleBlock(objType, data);
                        if (block == null)
                            return;
                        wrapper.set(Type.INT, 0, block.getId() | block.getData() << 12);
                    }
                }
            });
        }
    });
    registerTracker(ClientboundPackets1_9_3.SPAWN_EXPERIENCE_ORB, Entity1_10Types.EntityType.EXPERIENCE_ORB);
    registerTracker(ClientboundPackets1_9_3.SPAWN_GLOBAL_ENTITY, Entity1_10Types.EntityType.WEATHER);
    protocol.registerClientbound(ClientboundPackets1_9_3.SPAWN_MOB, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity id
            map(Type.VAR_INT);
            // 1 - UUID
            map(Type.UUID);
            // 2 - Entity Type
            map(Type.UNSIGNED_BYTE);
            // 3 - X
            map(Type.DOUBLE);
            // 4 - Y
            map(Type.DOUBLE);
            // 5 - Z
            map(Type.DOUBLE);
            // 6 - Yaw
            map(Type.BYTE);
            // 7 - Pitch
            map(Type.BYTE);
            // 8 - Head Pitch
            map(Type.BYTE);
            // 9 - Velocity X
            map(Type.SHORT);
            // 10 - Velocity Y
            map(Type.SHORT);
            // 11 - Velocity Z
            map(Type.SHORT);
            // 12 - Metadata
            map(Types1_9.METADATA_LIST);
            // Track entity
            handler(getTrackerHandler(Type.UNSIGNED_BYTE, 0));
            // Rewrite entity type / metadata
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int entityId = wrapper.get(Type.VAR_INT, 0);
                    EntityType type = tracker(wrapper.user()).entityType(entityId);
                    List<Metadata> metadata = wrapper.get(Types1_9.METADATA_LIST, 0);
                    handleMetadata(wrapper.get(Type.VAR_INT, 0), metadata, wrapper.user());
                    EntityData entityData = entityDataForType(type);
                    if (entityData != null) {
                        WrappedMetadata storage = new WrappedMetadata(metadata);
                        wrapper.set(Type.UNSIGNED_BYTE, 0, (short) entityData.replacementId());
                        if (entityData.hasBaseMeta())
                            entityData.defaultMeta().createMeta(storage);
                    }
                }
            });
        }
    });
    registerTracker(ClientboundPackets1_9_3.SPAWN_PAINTING, Entity1_10Types.EntityType.PAINTING);
    registerJoinGame(ClientboundPackets1_9_3.JOIN_GAME, Entity1_10Types.EntityType.PLAYER);
    registerRespawn(ClientboundPackets1_9_3.RESPAWN);
    protocol.registerClientbound(ClientboundPackets1_9_3.SPAWN_PLAYER, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // 1 - Player UUID
            map(Type.UUID);
            // 2 - X
            map(Type.DOUBLE);
            // 3 - Y
            map(Type.DOUBLE);
            // 4 - Z
            map(Type.DOUBLE);
            // 5 - Yaw
            map(Type.BYTE);
            // 6 - Pitch
            map(Type.BYTE);
            // 7 - Metadata list
            map(Types1_9.METADATA_LIST);
            handler(getTrackerAndMetaHandler(Types1_9.METADATA_LIST, Entity1_11Types.EntityType.PLAYER));
        }
    });
    registerRemoveEntities(ClientboundPackets1_9_3.DESTROY_ENTITIES);
    registerMetadataRewriter(ClientboundPackets1_9_3.ENTITY_METADATA, Types1_9.METADATA_LIST);
}
Also used : PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) WrappedMetadata(com.viaversion.viabackwards.api.entities.storage.WrappedMetadata) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) EntityData(com.viaversion.viabackwards.api.entities.storage.EntityData) WrappedMetadata(com.viaversion.viabackwards.api.entities.storage.WrappedMetadata) EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) Block(com.viaversion.viabackwards.utils.Block)

Example 2 with Metadata

use of com.viaversion.viaversion.api.minecraft.metadata.Metadata in project ViaBackwards by ViaVersion.

the class EntityPackets1_15 method registerRewrites.

@Override
protected void registerRewrites() {
    registerMetaTypeHandler(Types1_14.META_TYPES.itemType, Types1_14.META_TYPES.blockStateType, Types1_14.META_TYPES.particleType, null);
    filter().filterFamily(Entity1_15Types.LIVINGENTITY).removeIndex(12);
    filter().type(Entity1_15Types.BEE).cancel(15);
    filter().type(Entity1_15Types.BEE).cancel(16);
    mapEntityTypeWithData(Entity1_15Types.BEE, Entity1_15Types.PUFFERFISH).jsonName().spawnMetadata(storage -> {
        storage.add(new Metadata(14, Types1_14.META_TYPES.booleanType, false));
        storage.add(new Metadata(15, Types1_14.META_TYPES.varIntType, 2));
    });
    filter().type(Entity1_15Types.ENDERMAN).cancel(16);
    filter().type(Entity1_15Types.TRIDENT).cancel(10);
    // Redundant health removed in 1.15
    filter().type(Entity1_15Types.WOLF).addIndex(17);
    filter().type(Entity1_15Types.WOLF).index(8).handler((event, meta) -> {
        event.createExtraMeta(new Metadata(17, /*WOLF_HEALTH*/
        Types1_14.META_TYPES.floatType, event.meta().value()));
    });
}
Also used : Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata)

Example 3 with Metadata

use of com.viaversion.viaversion.api.minecraft.metadata.Metadata in project ViaBackwards by ViaVersion.

the class EntityPackets1_15 method registerPackets.

@Override
protected void registerPackets() {
    protocol.registerClientbound(ClientboundPackets1_15.UPDATE_HEALTH, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                float health = wrapper.passthrough(Type.FLOAT);
                if (health > 0)
                    return;
                if (!wrapper.user().get(ImmediateRespawn.class).isImmediateRespawn())
                    return;
                // Instantly request respawn when 1.15 gamerule is set
                PacketWrapper statusPacket = wrapper.create(ServerboundPackets1_14.CLIENT_STATUS);
                statusPacket.write(Type.VAR_INT, 0);
                statusPacket.sendToServer(Protocol1_14_4To1_15.class);
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_15.GAME_EVENT, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.UNSIGNED_BYTE);
            map(Type.FLOAT);
            handler(wrapper -> {
                if (wrapper.get(Type.UNSIGNED_BYTE, 0) == 11) {
                    wrapper.user().get(ImmediateRespawn.class).setImmediateRespawn(wrapper.get(Type.FLOAT, 0) == 1);
                }
            });
        }
    });
    registerTrackerWithData(ClientboundPackets1_15.SPAWN_ENTITY, Entity1_15Types.FALLING_BLOCK);
    protocol.registerClientbound(ClientboundPackets1_15.SPAWN_MOB, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // 1 - Entity UUID
            map(Type.UUID);
            // 2 - Entity Type
            map(Type.VAR_INT);
            // 3 - X
            map(Type.DOUBLE);
            // 4 - Y
            map(Type.DOUBLE);
            // 5 - Z
            map(Type.DOUBLE);
            // 6 - Yaw
            map(Type.BYTE);
            // 7 - Pitch
            map(Type.BYTE);
            // 8 - Head Pitch
            map(Type.BYTE);
            // 9 - Velocity X
            map(Type.SHORT);
            // 10 - Velocity Y
            map(Type.SHORT);
            // 11 - Velocity Z
            map(Type.SHORT);
            // Metadata is no longer sent in 1.15, so we have to send an empty one
            handler(wrapper -> wrapper.write(Types1_14.METADATA_LIST, new ArrayList<>()));
            handler(wrapper -> {
                int type = wrapper.get(Type.VAR_INT, 1);
                EntityType entityType = Entity1_15Types.getTypeFromId(type);
                tracker(wrapper.user()).addEntity(wrapper.get(Type.VAR_INT, 0), entityType);
                wrapper.set(Type.VAR_INT, 1, EntityTypeMapping.getOldEntityId(type));
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_15.RESPAWN, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.INT);
            // Seed
            map(Type.LONG, Type.NOTHING);
        }
    });
    protocol.registerClientbound(ClientboundPackets1_15.JOIN_GAME, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.INT);
            // 1 - Gamemode
            map(Type.UNSIGNED_BYTE);
            // 2 - Dimension
            map(Type.INT);
            // Seed
            map(Type.LONG, Type.NOTHING);
            // 3 - Max Players
            map(Type.UNSIGNED_BYTE);
            // 4 - Level Type
            map(Type.STRING);
            // 5 - View Distance
            map(Type.VAR_INT);
            // 6 - Reduce Debug Info
            map(Type.BOOLEAN);
            handler(getTrackerHandler(Entity1_15Types.PLAYER, Type.INT));
            handler(wrapper -> {
                // Inverted
                boolean immediateRespawn = !wrapper.read(Type.BOOLEAN);
                wrapper.user().get(ImmediateRespawn.class).setImmediateRespawn(immediateRespawn);
            });
        }
    });
    registerTracker(ClientboundPackets1_15.SPAWN_EXPERIENCE_ORB, Entity1_15Types.EXPERIENCE_ORB);
    registerTracker(ClientboundPackets1_15.SPAWN_GLOBAL_ENTITY, Entity1_15Types.LIGHTNING_BOLT);
    registerTracker(ClientboundPackets1_15.SPAWN_PAINTING, Entity1_15Types.PAINTING);
    protocol.registerClientbound(ClientboundPackets1_15.SPAWN_PLAYER, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // 1 - Player UUID
            map(Type.UUID);
            // 2 - X
            map(Type.DOUBLE);
            // 3 - Y
            map(Type.DOUBLE);
            // 4 - Z
            map(Type.DOUBLE);
            // 5 - Yaw
            map(Type.BYTE);
            // 6 - Pitch
            map(Type.BYTE);
            // Metadata is no longer sent in 1.15, so we have to send an empty one
            handler(wrapper -> wrapper.write(Types1_14.METADATA_LIST, new ArrayList<>()));
            handler(getTrackerHandler(Entity1_15Types.PLAYER, Type.VAR_INT));
        }
    });
    registerRemoveEntities(ClientboundPackets1_15.DESTROY_ENTITIES);
    registerMetadataRewriter(ClientboundPackets1_15.ENTITY_METADATA, Types1_14.METADATA_LIST);
    // Attributes (get rid of generic.flyingSpeed for the Bee remap)
    protocol.registerClientbound(ClientboundPackets1_15.ENTITY_PROPERTIES, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.VAR_INT);
            map(Type.INT);
            handler(wrapper -> {
                int entityId = wrapper.get(Type.VAR_INT, 0);
                EntityType entityType = tracker(wrapper.user()).entityType(entityId);
                if (entityType != Entity1_15Types.BEE)
                    return;
                int size = wrapper.get(Type.INT, 0);
                int newSize = size;
                for (int i = 0; i < size; i++) {
                    String key = wrapper.read(Type.STRING);
                    if (key.equals("generic.flyingSpeed")) {
                        newSize--;
                        wrapper.read(Type.DOUBLE);
                        int modSize = wrapper.read(Type.VAR_INT);
                        for (int j = 0; j < modSize; j++) {
                            wrapper.read(Type.UUID);
                            wrapper.read(Type.DOUBLE);
                            wrapper.read(Type.BYTE);
                        }
                    } else {
                        wrapper.write(Type.STRING, key);
                        wrapper.passthrough(Type.DOUBLE);
                        int modSize = wrapper.passthrough(Type.VAR_INT);
                        for (int j = 0; j < modSize; j++) {
                            wrapper.passthrough(Type.UUID);
                            wrapper.passthrough(Type.DOUBLE);
                            wrapper.passthrough(Type.BYTE);
                        }
                    }
                }
                if (newSize != size) {
                    wrapper.set(Type.INT, 0, newSize);
                }
            });
        }
    });
}
Also used : Entity1_15Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_15Types) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) ServerboundPackets1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ServerboundPackets1_14) EntityTypeMapping(com.viaversion.viabackwards.protocol.protocol1_14_4to1_15.data.EntityTypeMapping) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) Types1_14(com.viaversion.viaversion.api.type.types.version.Types1_14) ArrayList(java.util.ArrayList) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) ImmediateRespawn(com.viaversion.viabackwards.protocol.protocol1_14_4to1_15.data.ImmediateRespawn) Protocol1_14_4To1_15(com.viaversion.viabackwards.protocol.protocol1_14_4to1_15.Protocol1_14_4To1_15) ClientboundPackets1_15(com.viaversion.viaversion.protocols.protocol1_15to1_14_4.ClientboundPackets1_15) EntityRewriter(com.viaversion.viabackwards.api.rewriters.EntityRewriter) EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) ImmediateRespawn(com.viaversion.viabackwards.protocol.protocol1_14_4to1_15.data.ImmediateRespawn) Protocol1_14_4To1_15(com.viaversion.viabackwards.protocol.protocol1_14_4to1_15.Protocol1_14_4To1_15) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)

Example 4 with Metadata

use of com.viaversion.viaversion.api.minecraft.metadata.Metadata in project ViaBackwards by ViaVersion.

the class EntityPackets1_14_1 method registerPackets.

@Override
protected void registerPackets() {
    registerTracker(ClientboundPackets1_14.SPAWN_EXPERIENCE_ORB, Entity1_14Types.EXPERIENCE_ORB);
    registerTracker(ClientboundPackets1_14.SPAWN_GLOBAL_ENTITY, Entity1_14Types.LIGHTNING_BOLT);
    registerTracker(ClientboundPackets1_14.SPAWN_PAINTING, Entity1_14Types.PAINTING);
    registerTracker(ClientboundPackets1_14.SPAWN_PLAYER, Entity1_14Types.PLAYER);
    registerTracker(ClientboundPackets1_14.JOIN_GAME, Entity1_14Types.PLAYER, Type.INT);
    registerRemoveEntities(ClientboundPackets1_14.DESTROY_ENTITIES);
    protocol.registerClientbound(ClientboundPackets1_14.SPAWN_ENTITY, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity id
            map(Type.VAR_INT);
            // 1 - UUID
            map(Type.UUID);
            // 2 - Type
            map(Type.VAR_INT);
            handler(getTrackerHandler());
        }
    });
    protocol.registerClientbound(ClientboundPackets1_14.SPAWN_MOB, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.VAR_INT);
            // 1 - Entity UUID
            map(Type.UUID);
            // 2 - Entity Type
            map(Type.VAR_INT);
            // 3 - X
            map(Type.DOUBLE);
            // 4 - Y
            map(Type.DOUBLE);
            // 5 - Z
            map(Type.DOUBLE);
            // 6 - Yaw
            map(Type.BYTE);
            // 7 - Pitch
            map(Type.BYTE);
            // 8 - Head Pitch
            map(Type.BYTE);
            // 9 - Velocity X
            map(Type.SHORT);
            // 10 - Velocity Y
            map(Type.SHORT);
            // 11 - Velocity Z
            map(Type.SHORT);
            // 12 - Metadata
            map(Types1_14.METADATA_LIST);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int entityId = wrapper.get(Type.VAR_INT, 0);
                    int type = wrapper.get(Type.VAR_INT, 1);
                    // Register Type ID
                    tracker(wrapper.user()).addEntity(entityId, Entity1_14Types.getTypeFromId(type));
                    List<Metadata> metadata = wrapper.get(Types1_14.METADATA_LIST, 0);
                    handleMetadata(entityId, metadata, wrapper.user());
                }
            });
        }
    });
    // Entity Metadata
    registerMetadataRewriter(ClientboundPackets1_14.ENTITY_METADATA, Types1_14.METADATA_LIST);
}
Also used : PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata)

Example 5 with Metadata

use of com.viaversion.viaversion.api.minecraft.metadata.Metadata 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)

Aggregations

Metadata (com.viaversion.viaversion.api.minecraft.metadata.Metadata)37 PacketWrapper (com.viaversion.viaversion.api.protocol.packet.PacketWrapper)21 PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)15 PacketHandler (com.viaversion.viaversion.api.protocol.remapper.PacketHandler)14 EntityType (com.viaversion.viaversion.api.minecraft.entities.EntityType)11 Item (com.viaversion.viaversion.api.minecraft.item.Item)10 Type (com.viaversion.viaversion.api.type.Type)8 StoredEntityData (com.viaversion.viaversion.api.data.entity.StoredEntityData)6 MetaType (com.viaversion.viaversion.api.minecraft.metadata.MetaType)6 EntityData (com.viaversion.viabackwards.api.entities.storage.EntityData)5 LegacyEntityRewriter (com.viaversion.viabackwards.api.rewriters.LegacyEntityRewriter)5 Particle (com.viaversion.viaversion.api.type.types.Particle)5 ClientboundPackets1_13 (com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13)5 ViaBackwards (com.viaversion.viabackwards.ViaBackwards)4 WrappedMetadata (com.viaversion.viabackwards.api.entities.storage.WrappedMetadata)4 Entity1_13Types (com.viaversion.viaversion.api.minecraft.entities.Entity1_13Types)4 DataItem (com.viaversion.viaversion.api.minecraft.item.DataItem)4 ArrayList (java.util.ArrayList)4 EntityPositionHandler (com.viaversion.viabackwards.api.entities.storage.EntityPositionHandler)3 Block (com.viaversion.viabackwards.utils.Block)3