Search in sources :

Example 26 with Metadata

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

Example 27 with Metadata

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

the class EntityPackets1_13_1 method registerPackets.

@Override
protected void registerPackets() {
    protocol.registerClientbound(ClientboundPackets1_13.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);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int entityId = wrapper.get(Type.VAR_INT, 0);
                    byte type = wrapper.get(Type.BYTE, 0);
                    Entity1_13Types.EntityType entType = Entity1_13Types.getTypeFromId(type, true);
                    if (entType == null) {
                        ViaBackwards.getPlatform().getLogger().warning("Could not find 1.13 entity type " + type);
                        return;
                    }
                    // Rewrite falling block
                    if (entType.is(Entity1_13Types.EntityType.FALLING_BLOCK)) {
                        int data = wrapper.get(Type.INT, 0);
                        wrapper.set(Type.INT, 0, protocol.getMappingData().getNewBlockStateId(data));
                    }
                    // Track Entity
                    tracker(wrapper.user()).addEntity(entityId, entType);
                }
            });
        }
    });
    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() {
            // 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_13.METADATA_LIST);
            // Track Entity
            handler(getTrackerHandler());
            // Rewrite Metadata
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    List<Metadata> metadata = wrapper.get(Types1_13.METADATA_LIST, 0);
                    handleMetadata(wrapper.get(Type.VAR_INT, 0), metadata, wrapper.user());
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_13.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_13.METADATA_LIST);
            handler(getTrackerAndMetaHandler(Types1_13.METADATA_LIST, Entity1_13Types.EntityType.PLAYER));
        }
    });
    registerTracker(ClientboundPackets1_13.SPAWN_PAINTING, Entity1_13Types.EntityType.PAINTING);
    registerJoinGame(ClientboundPackets1_13.JOIN_GAME, Entity1_13Types.EntityType.PLAYER);
    registerRespawn(ClientboundPackets1_13.RESPAWN);
    registerRemoveEntities(ClientboundPackets1_13.DESTROY_ENTITIES);
    registerMetadataRewriter(ClientboundPackets1_13.ENTITY_METADATA, Types1_13.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) Entity1_13Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_13Types)

Example 28 with Metadata

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

the class EntityPackets1_16 method registerRewrites.

@Override
protected void registerRewrites() {
    filter().handler((event, meta) -> {
        meta.setMetaType(Types1_14.META_TYPES.byId(meta.metaType().typeId()));
        MetaType type = meta.metaType();
        if (type == Types1_14.META_TYPES.itemType) {
            meta.setValue(protocol.getItemRewriter().handleItemToClient((Item) meta.getValue()));
        } else if (type == Types1_14.META_TYPES.blockStateType) {
            meta.setValue(protocol.getMappingData().getNewBlockStateId((int) meta.getValue()));
        } else if (type == Types1_14.META_TYPES.particleType) {
            rewriteParticle((Particle) meta.getValue());
        } else if (type == Types1_14.META_TYPES.optionalComponentType) {
            JsonElement text = meta.value();
            if (text != null) {
                protocol.getTranslatableRewriter().processText(text);
            }
        }
    });
    mapEntityType(Entity1_16Types.ZOMBIFIED_PIGLIN, Entity1_15Types.ZOMBIE_PIGMAN);
    mapTypes(Entity1_16Types.values(), Entity1_15Types.class);
    mapEntityTypeWithData(Entity1_16Types.HOGLIN, Entity1_16Types.COW).jsonName();
    mapEntityTypeWithData(Entity1_16Types.ZOGLIN, Entity1_16Types.COW).jsonName();
    mapEntityTypeWithData(Entity1_16Types.PIGLIN, Entity1_16Types.ZOMBIFIED_PIGLIN).jsonName();
    mapEntityTypeWithData(Entity1_16Types.STRIDER, Entity1_16Types.MAGMA_CUBE).jsonName();
    filter().type(Entity1_16Types.ZOGLIN).cancel(16);
    filter().type(Entity1_16Types.HOGLIN).cancel(15);
    filter().type(Entity1_16Types.PIGLIN).cancel(16);
    filter().type(Entity1_16Types.PIGLIN).cancel(17);
    filter().type(Entity1_16Types.PIGLIN).cancel(18);
    filter().type(Entity1_16Types.STRIDER).index(15).handler((event, meta) -> {
        boolean baby = meta.value();
        meta.setTypeAndValue(Types1_14.META_TYPES.varIntType, baby ? 1 : 3);
    });
    filter().type(Entity1_16Types.STRIDER).cancel(16);
    filter().type(Entity1_16Types.STRIDER).cancel(17);
    filter().type(Entity1_16Types.STRIDER).cancel(18);
    filter().type(Entity1_16Types.FISHING_BOBBER).cancel(8);
    filter().filterFamily(Entity1_16Types.ABSTRACT_ARROW).cancel(8);
    filter().filterFamily(Entity1_16Types.ABSTRACT_ARROW).handler((event, meta) -> {
        if (event.index() >= 8) {
            event.setIndex(event.index() + 1);
        }
    });
    filter().type(Entity1_16Types.WOLF).index(16).handler((event, meta) -> {
        byte mask = meta.value();
        StoredEntityData data = tracker(event.user()).entityData(event.entityId());
        data.put(new WolfDataMaskStorage(mask));
    });
    filter().type(Entity1_16Types.WOLF).index(20).handler((event, meta) -> {
        StoredEntityData data = tracker(event.user()).entityDataIfPresent(event.entityId());
        byte previousMask = 0;
        if (data != null) {
            WolfDataMaskStorage wolfData = data.get(WolfDataMaskStorage.class);
            if (wolfData != null) {
                previousMask = wolfData.tameableMask();
            }
        }
        int angerTime = meta.value();
        byte tameableMask = (byte) (angerTime > 0 ? previousMask | 2 : previousMask & -3);
        event.createExtraMeta(new Metadata(16, Types1_14.META_TYPES.byteType, tameableMask));
        event.cancel();
    });
}
Also used : Item(com.viaversion.viaversion.api.minecraft.item.Item) WolfDataMaskStorage(com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.storage.WolfDataMaskStorage) JsonElement(com.viaversion.viaversion.libs.gson.JsonElement) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) StoredEntityData(com.viaversion.viaversion.api.data.entity.StoredEntityData) MetaType(com.viaversion.viaversion.api.minecraft.metadata.MetaType)

Example 29 with Metadata

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

the class EntityPackets method register.

public static void register(Protocol1_14To1_13_2 protocol) {
    MetadataRewriter1_14To1_13_2 metadataRewriter = protocol.get(MetadataRewriter1_14To1_13_2.class);
    protocol.registerClientbound(ClientboundPackets1_13.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, Type.VAR_INT);
            // 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);
            // Track Entity
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    int entityId = wrapper.get(Type.VAR_INT, 0);
                    int typeId = wrapper.get(Type.VAR_INT, 1);
                    Entity1_13Types.EntityType type1_13 = Entity1_13Types.getTypeFromId(typeId, true);
                    typeId = metadataRewriter.newEntityId(type1_13.getId());
                    EntityType type1_14 = Entity1_14Types.getTypeFromId(typeId);
                    if (type1_14 != null) {
                        int data = wrapper.get(Type.INT, 0);
                        if (type1_14.is(Entity1_14Types.FALLING_BLOCK)) {
                            wrapper.set(Type.INT, 0, protocol.getMappingData().getNewBlockStateId(data));
                        } else if (type1_14.is(Entity1_14Types.MINECART)) {
                            // default is 0 = rideable minecart
                            switch(data) {
                                case 1:
                                    typeId = Entity1_14Types.CHEST_MINECART.getId();
                                    break;
                                case 2:
                                    typeId = Entity1_14Types.FURNACE_MINECART.getId();
                                    break;
                                case 3:
                                    typeId = Entity1_14Types.TNT_MINECART.getId();
                                    break;
                                case 4:
                                    typeId = Entity1_14Types.SPAWNER_MINECART.getId();
                                    break;
                                case 5:
                                    typeId = Entity1_14Types.HOPPER_MINECART.getId();
                                    break;
                                case 6:
                                    typeId = Entity1_14Types.COMMAND_BLOCK_MINECART.getId();
                                    break;
                            }
                        } else if ((type1_14.is(Entity1_14Types.ITEM) && data > 0) || type1_14.isOrHasParent(Entity1_14Types.ABSTRACT_ARROW)) {
                            if (type1_14.isOrHasParent(Entity1_14Types.ABSTRACT_ARROW)) {
                                wrapper.set(Type.INT, 0, data - 1);
                            }
                            // send velocity in separate packet, 1.14 is now ignoring the velocity
                            PacketWrapper velocity = wrapper.create(0x45);
                            velocity.write(Type.VAR_INT, entityId);
                            velocity.write(Type.SHORT, wrapper.get(Type.SHORT, 0));
                            velocity.write(Type.SHORT, wrapper.get(Type.SHORT, 1));
                            velocity.write(Type.SHORT, wrapper.get(Type.SHORT, 2));
                            velocity.scheduleSend(Protocol1_14To1_13_2.class);
                        }
                        // Register Type ID
                        wrapper.user().getEntityTracker(Protocol1_14To1_13_2.class).addEntity(entityId, type1_14);
                    }
                    wrapper.set(Type.VAR_INT, 1, typeId);
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_13.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_13_2.METADATA_LIST, Types1_14.METADATA_LIST);
            handler(metadataRewriter.trackerAndRewriterHandler(Types1_14.METADATA_LIST));
        }
    });
    protocol.registerClientbound(ClientboundPackets1_13.SPAWN_PAINTING, new PacketRemapper() {

        @Override
        public void registerMap() {
            map(Type.VAR_INT);
            map(Type.UUID);
            map(Type.VAR_INT);
            map(Type.POSITION, Type.POSITION1_14);
            map(Type.BYTE);
        }
    });
    protocol.registerClientbound(ClientboundPackets1_13.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_13_2.METADATA_LIST, Types1_14.METADATA_LIST);
            handler(metadataRewriter.trackerAndRewriterHandler(Types1_14.METADATA_LIST, Entity1_14Types.PLAYER));
        }
    });
    protocol.registerClientbound(ClientboundPackets1_13.ENTITY_ANIMATION, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    short animation = wrapper.passthrough(Type.UNSIGNED_BYTE);
                    if (animation == 2) {
                        // Leave bed
                        EntityTracker1_14 tracker = wrapper.user().getEntityTracker(Protocol1_14To1_13_2.class);
                        int entityId = wrapper.get(Type.VAR_INT, 0);
                        tracker.setSleeping(entityId, false);
                        PacketWrapper metadataPacket = wrapper.create(ClientboundPackets1_14.ENTITY_METADATA);
                        metadataPacket.write(Type.VAR_INT, entityId);
                        List<Metadata> metadataList = new LinkedList<>();
                        if (tracker.clientEntityId() != entityId) {
                            metadataList.add(new Metadata(6, Types1_14.META_TYPES.poseType, MetadataRewriter1_14To1_13_2.recalculatePlayerPose(entityId, tracker)));
                        }
                        metadataList.add(new Metadata(12, Types1_14.META_TYPES.optionalPositionType, null));
                        metadataPacket.write(Types1_14.METADATA_LIST, metadataList);
                        metadataPacket.scheduleSend(Protocol1_14To1_13_2.class);
                    }
                }
            });
        }
    });
    protocol.registerClientbound(ClientboundPackets1_13.USE_BED, ClientboundPackets1_14.ENTITY_METADATA, new PacketRemapper() {

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

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    EntityTracker1_14 tracker = wrapper.user().getEntityTracker(Protocol1_14To1_13_2.class);
                    int entityId = wrapper.get(Type.VAR_INT, 0);
                    tracker.setSleeping(entityId, true);
                    Position position = wrapper.read(Type.POSITION);
                    List<Metadata> metadataList = new LinkedList<>();
                    metadataList.add(new Metadata(12, Types1_14.META_TYPES.optionalPositionType, position));
                    if (tracker.clientEntityId() != entityId) {
                        metadataList.add(new Metadata(6, Types1_14.META_TYPES.poseType, MetadataRewriter1_14To1_13_2.recalculatePlayerPose(entityId, tracker)));
                    }
                    wrapper.write(Types1_14.METADATA_LIST, metadataList);
                }
            });
        }
    });
    metadataRewriter.registerRemoveEntities(ClientboundPackets1_13.DESTROY_ENTITIES);
    metadataRewriter.registerMetadataRewriter(ClientboundPackets1_13.ENTITY_METADATA, Types1_13_2.METADATA_LIST, Types1_14.METADATA_LIST);
}
Also used : EntityTracker1_14(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.storage.EntityTracker1_14) Position(com.viaversion.viaversion.api.minecraft.Position) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) MetadataRewriter1_14To1_13_2(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.metadata.MetadataRewriter1_14To1_13_2) LinkedList(java.util.LinkedList) EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) Protocol1_14To1_13_2(com.viaversion.viaversion.protocols.protocol1_14to1_13_2.Protocol1_14To1_13_2) Entity1_13Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_13Types)

Example 30 with Metadata

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

the class EntityPackets method sendMetadataPacket.

private static void sendMetadataPacket(PacketWrapper wrapper, int entityId, EntityRewriter rewriter) throws Exception {
    // Meta is no longer included in the spawn packets, but sent separately
    List<Metadata> metadata = wrapper.read(Types1_14.METADATA_LIST);
    if (metadata.isEmpty()) {
        return;
    }
    // Send the spawn packet manually
    wrapper.send(Protocol1_15To1_14_4.class);
    wrapper.cancel();
    // Handle meta
    rewriter.handleMetadata(entityId, metadata, wrapper.user());
    PacketWrapper metadataPacket = PacketWrapper.create(ClientboundPackets1_15.ENTITY_METADATA, wrapper.user());
    metadataPacket.write(Type.VAR_INT, entityId);
    metadataPacket.write(Types1_14.METADATA_LIST, metadata);
    metadataPacket.send(Protocol1_15To1_14_4.class);
}
Also used : PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata)

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