Search in sources :

Example 1 with EntityPositionHandler

use of com.viaversion.viabackwards.api.entities.storage.EntityPositionHandler in project ViaBackwards by ViaVersion.

the class EntityPackets1_14 method registerPackets.

@Override
protected void registerPackets() {
    positionHandler = new EntityPositionHandler(this, EntityPositionStorage1_14.class, EntityPositionStorage1_14::new);
    protocol.registerClientbound(ClientboundPackets1_14.ENTITY_STATUS, new PacketRemapper() {

        @Override
        public void registerMap() {
            handler(wrapper -> {
                int entityId = wrapper.passthrough(Type.INT);
                byte status = wrapper.passthrough(Type.BYTE);
                // Check for death status
                if (status != 3)
                    return;
                EntityTracker tracker = tracker(wrapper.user());
                EntityType entityType = tracker.entityType(entityId);
                if (entityType != Entity1_14Types.PLAYER)
                    return;
                // Remove equipment, else the client will see ghost items
                for (int i = 0; i <= 5; i++) {
                    PacketWrapper equipmentPacket = wrapper.create(ClientboundPackets1_13.ENTITY_EQUIPMENT);
                    equipmentPacket.write(Type.VAR_INT, entityId);
                    equipmentPacket.write(Type.VAR_INT, i);
                    equipmentPacket.write(Type.FLAT_VAR_INT_ITEM, null);
                    equipmentPacket.send(Protocol1_13_2To1_14.class);
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_14.ENTITY_TELEPORT, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.VAR_INT);
            map(Type.DOUBLE);
            map(Type.DOUBLE);
            map(Type.DOUBLE);
            handler(wrapper -> positionHandler.cacheEntityPosition(wrapper, false, false));
        }
    });
    PacketRemapper relativeMoveHandler = new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.VAR_INT);
            map(Type.SHORT);
            map(Type.SHORT);
            map(Type.SHORT);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    double x = wrapper.get(Type.SHORT, 0) / EntityPositionHandler.RELATIVE_MOVE_FACTOR;
                    double y = wrapper.get(Type.SHORT, 1) / EntityPositionHandler.RELATIVE_MOVE_FACTOR;
                    double z = wrapper.get(Type.SHORT, 2) / EntityPositionHandler.RELATIVE_MOVE_FACTOR;
                    positionHandler.cacheEntityPosition(wrapper, x, y, z, false, true);
                }
            });
        }
    };
    protocol.registerClientbound(ClientboundPackets1_14.ENTITY_POSITION, relativeMoveHandler);
    protocol.registerClientbound(ClientboundPackets1_14.ENTITY_POSITION_AND_ROTATION, relativeMoveHandler);
    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, 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);
            // 9 - Velocity X
            map(Type.SHORT);
            // 10 - Velocity Y
            map(Type.SHORT);
            // 11 - Velocity Z
            map(Type.SHORT);
            handler(getObjectTrackerHandler());
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int id = wrapper.get(Type.BYTE, 0);
                    int mappedId = newEntityId(id);
                    Entity1_13Types.EntityType entityType = Entity1_13Types.getTypeFromId(mappedId, false);
                    Entity1_13Types.ObjectType objectType;
                    if (entityType.isOrHasParent(Entity1_13Types.EntityType.MINECART_ABSTRACT)) {
                        objectType = Entity1_13Types.ObjectType.MINECART;
                        int data = 0;
                        switch(entityType) {
                            case CHEST_MINECART:
                                data = 1;
                                break;
                            case FURNACE_MINECART:
                                data = 2;
                                break;
                            case TNT_MINECART:
                                data = 3;
                                break;
                            case SPAWNER_MINECART:
                                data = 4;
                                break;
                            case HOPPER_MINECART:
                                data = 5;
                                break;
                            case COMMAND_BLOCK_MINECART:
                                data = 6;
                                break;
                        }
                        if (data != 0)
                            wrapper.set(Type.INT, 0, data);
                    } else {
                        objectType = Entity1_13Types.ObjectType.fromEntityType(entityType).orElse(null);
                    }
                    if (objectType == null)
                        return;
                    wrapper.set(Type.BYTE, 0, (byte) objectType.getId());
                    int data = wrapper.get(Type.INT, 0);
                    if (objectType == Entity1_13Types.ObjectType.FALLING_BLOCK) {
                        int blockState = wrapper.get(Type.INT, 0);
                        int combined = protocol.getMappingData().getNewBlockStateId(blockState);
                        wrapper.set(Type.INT, 0, combined);
                    } else if (entityType.isOrHasParent(Entity1_13Types.EntityType.ABSTRACT_ARROW)) {
                        wrapper.set(Type.INT, 0, data + 1);
                    }
                }
            });
        }
    });
    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, Types1_13_2.METADATA_LIST);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int type = wrapper.get(Type.VAR_INT, 1);
                    EntityType entityType = Entity1_14Types.getTypeFromId(type);
                    addTrackedEntity(wrapper, wrapper.get(Type.VAR_INT, 0), entityType);
                    int oldId = newEntityId(type);
                    if (oldId == -1) {
                        EntityData entityData = entityDataForType(entityType);
                        if (entityData == null) {
                            ViaBackwards.getPlatform().getLogger().warning("Could not find 1.13.2 entity type for 1.14 entity type " + type + "/" + entityType);
                            wrapper.cancel();
                        } else {
                            wrapper.set(Type.VAR_INT, 1, entityData.replacementId());
                        }
                    } else {
                        wrapper.set(Type.VAR_INT, 1, oldId);
                    }
                }
            });
            // Handle entity type & metadata
            handler(getMobSpawnRewriter(Types1_13_2.METADATA_LIST));
        }
    });
    protocol.registerClientbound(ClientboundPackets1_14.SPAWN_EXPERIENCE_ORB, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity id
            map(Type.VAR_INT);
            // Needs to be mapped for the position cache
            map(Type.DOUBLE);
            map(Type.DOUBLE);
            map(Type.DOUBLE);
            handler(wrapper -> addTrackedEntity(wrapper, wrapper.get(Type.VAR_INT, 0), Entity1_14Types.EXPERIENCE_ORB));
        }
    });
    protocol.registerClientbound(ClientboundPackets1_14.SPAWN_GLOBAL_ENTITY, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity id
            map(Type.VAR_INT);
            map(Type.BYTE);
            // Needs to be mapped for the position cache
            map(Type.DOUBLE);
            map(Type.DOUBLE);
            map(Type.DOUBLE);
            handler(wrapper -> addTrackedEntity(wrapper, wrapper.get(Type.VAR_INT, 0), Entity1_14Types.LIGHTNING_BOLT));
        }
    });
    protocol.registerClientbound(ClientboundPackets1_14.SPAWN_PAINTING, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.VAR_INT);
            map(Type.UUID);
            map(Type.VAR_INT);
            map(Type.POSITION1_14, Type.POSITION);
            map(Type.BYTE);
            // Track entity
            handler(wrapper -> addTrackedEntity(wrapper, wrapper.get(Type.VAR_INT, 0), Entity1_14Types.PAINTING));
        }
    });
    protocol.registerClientbound(ClientboundPackets1_14.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
            map(Types1_14.METADATA_LIST, Types1_13_2.METADATA_LIST);
            handler(getTrackerAndMetaHandler(Types1_13_2.METADATA_LIST, Entity1_14Types.PLAYER));
            handler(wrapper -> positionHandler.cacheEntityPosition(wrapper, true, false));
        }
    });
    registerRemoveEntities(ClientboundPackets1_14.DESTROY_ENTITIES);
    registerMetadataRewriter(ClientboundPackets1_14.ENTITY_METADATA, Types1_14.METADATA_LIST, Types1_13_2.METADATA_LIST);
    protocol.registerClientbound(ClientboundPackets1_14.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);
            handler(getTrackerHandler(Entity1_14Types.PLAYER, Type.INT));
            handler(getDimensionHandler(1));
            handler(wrapper -> {
                short difficulty = wrapper.user().get(DifficultyStorage.class).getDifficulty();
                wrapper.write(Type.UNSIGNED_BYTE, difficulty);
                // Max Players
                wrapper.passthrough(Type.UNSIGNED_BYTE);
                // Level Type
                wrapper.passthrough(Type.STRING);
                // Read View Distance
                wrapper.read(Type.VAR_INT);
            // TODO Track client position
            // Manually add position storage
            /*int entitiyId = wrapper.get(Type.INT, 0);
                    StoredEntityData storedEntity = protocol.getEntityRewriter().tracker(wrapper.user()).entityData(entitiyId);
                    storedEntity.put(new EntityPositionStorage1_14());*/
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_14.RESPAWN, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Dimension ID
            map(Type.INT);
            handler(wrapper -> {
                ClientWorld clientWorld = wrapper.user().get(ClientWorld.class);
                int dimensionId = wrapper.get(Type.INT, 0);
                clientWorld.setEnvironment(dimensionId);
                short difficulty = wrapper.user().get(DifficultyStorage.class).getDifficulty();
                wrapper.write(Type.UNSIGNED_BYTE, difficulty);
                wrapper.user().get(ChunkLightStorage.class).clear();
            });
        }
    });
}
Also used : EntityPositionHandler(com.viaversion.viabackwards.api.entities.storage.EntityPositionHandler) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) MetaType(com.viaversion.viaversion.api.minecraft.metadata.MetaType) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) Entity1_13Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_13Types) EntityPositionStorage1_14(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.storage.EntityPositionStorage1_14) Item(com.viaversion.viaversion.api.minecraft.item.Item) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) Types1_14(com.viaversion.viaversion.api.type.types.version.Types1_14) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) ChunkLightStorage(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.storage.ChunkLightStorage) Protocol1_13_2To1_14(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.Protocol1_13_2To1_14) EntityData(com.viaversion.viabackwards.api.entities.storage.EntityData) Particle(com.viaversion.viaversion.api.type.types.Particle) LegacyEntityRewriter(com.viaversion.viabackwards.api.rewriters.LegacyEntityRewriter) Position(com.viaversion.viaversion.api.minecraft.Position) Types1_13_2(com.viaversion.viaversion.api.type.types.version.Types1_13_2) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) EntityTracker(com.viaversion.viaversion.api.data.entity.EntityTracker) EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) VillagerData(com.viaversion.viaversion.api.minecraft.VillagerData) EntityPositionHandler(com.viaversion.viabackwards.api.entities.storage.EntityPositionHandler) ClientboundPackets1_13(com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13) DifficultyStorage(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.storage.DifficultyStorage) MetaHandler(com.viaversion.viaversion.rewriter.meta.MetaHandler) ViaBackwards(com.viaversion.viabackwards.ViaBackwards) ClientboundPackets1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.ClientboundPackets1_14) Entity1_14Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_14Types) EntityPositionStorage1_14(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.storage.EntityPositionStorage1_14) EntityTracker(com.viaversion.viaversion.api.data.entity.EntityTracker) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) EntityData(com.viaversion.viabackwards.api.entities.storage.EntityData) EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) Entity1_13Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_13Types) ClientWorld(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld) Protocol1_13_2To1_14(com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.Protocol1_13_2To1_14)

Aggregations

ViaBackwards (com.viaversion.viabackwards.ViaBackwards)1 EntityData (com.viaversion.viabackwards.api.entities.storage.EntityData)1 EntityPositionHandler (com.viaversion.viabackwards.api.entities.storage.EntityPositionHandler)1 LegacyEntityRewriter (com.viaversion.viabackwards.api.rewriters.LegacyEntityRewriter)1 Protocol1_13_2To1_14 (com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.Protocol1_13_2To1_14)1 ChunkLightStorage (com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.storage.ChunkLightStorage)1 DifficultyStorage (com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.storage.DifficultyStorage)1 EntityPositionStorage1_14 (com.viaversion.viabackwards.protocol.protocol1_13_2to1_14.storage.EntityPositionStorage1_14)1 EntityTracker (com.viaversion.viaversion.api.data.entity.EntityTracker)1 Position (com.viaversion.viaversion.api.minecraft.Position)1 VillagerData (com.viaversion.viaversion.api.minecraft.VillagerData)1 Entity1_13Types (com.viaversion.viaversion.api.minecraft.entities.Entity1_13Types)1 Entity1_14Types (com.viaversion.viaversion.api.minecraft.entities.Entity1_14Types)1 EntityType (com.viaversion.viaversion.api.minecraft.entities.EntityType)1 Item (com.viaversion.viaversion.api.minecraft.item.Item)1 MetaType (com.viaversion.viaversion.api.minecraft.metadata.MetaType)1 Metadata (com.viaversion.viaversion.api.minecraft.metadata.Metadata)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