Search in sources :

Example 6 with EntityType

use of com.viaversion.viaversion.api.minecraft.entities.EntityType in project ViaBackwards by ViaVersion.

the class EntityPackets1_11 method registerPackets.

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

        @Override
        public void registerMap() {
            map(Type.INT);
            map(Type.POSITION);
            map(Type.INT);
            handler(wrapper -> {
                int type = wrapper.get(Type.INT, 0);
                if (type == 2002 || type == 2007) {
                    // 2007 potion id doesn't exist in 1.10
                    if (type == 2007) {
                        wrapper.set(Type.INT, 0, 2002);
                    }
                    int mappedData = PotionSplashHandler.getOldData(wrapper.get(Type.INT, 1));
                    if (mappedData != -1) {
                        wrapper.set(Type.INT, 1, mappedData);
                    }
                }
            });
        }
    });
    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_11Types.EntityType.EXPERIENCE_ORB);
    registerTracker(ClientboundPackets1_9_3.SPAWN_GLOBAL_ENTITY, Entity1_11Types.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.VAR_INT, 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(wrapper -> {
                int entityId = wrapper.get(Type.VAR_INT, 0);
                EntityType type = tracker(wrapper.user()).entityType(entityId);
                List<Metadata> list = wrapper.get(Types1_9.METADATA_LIST, 0);
                handleMetadata(wrapper.get(Type.VAR_INT, 0), list, wrapper.user());
                EntityData entityData = entityDataForType(type);
                if (entityData != null) {
                    wrapper.set(Type.UNSIGNED_BYTE, 0, (short) entityData.replacementId());
                    if (entityData.hasBaseMeta()) {
                        entityData.defaultMeta().createMeta(new WrappedMetadata(list));
                    }
                }
                // Sub 1.11 clients will error if the list is empty
                if (list.isEmpty()) {
                    list.add(new Metadata(0, MetaType1_9.Byte, (byte) 0));
                }
            });
        }
    });
    registerTracker(ClientboundPackets1_9_3.SPAWN_PAINTING, Entity1_11Types.EntityType.PAINTING);
    registerJoinGame(ClientboundPackets1_9_3.JOIN_GAME, Entity1_11Types.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));
            handler(wrapper -> {
                // Sub 1.11 clients will cry if the list is empty
                List<Metadata> metadata = wrapper.get(Types1_9.METADATA_LIST, 0);
                if (metadata.isEmpty()) {
                    metadata.add(new Metadata(0, MetaType1_9.Byte, (byte) 0));
                }
            });
        }
    });
    registerRemoveEntities(ClientboundPackets1_9_3.DESTROY_ENTITIES);
    registerMetadataRewriter(ClientboundPackets1_9_3.ENTITY_METADATA, Types1_9.METADATA_LIST);
    protocol.registerClientbound(ClientboundPackets1_9_3.ENTITY_STATUS, new PacketRemapper() {

        @Override
        public void registerMap() {
            // 0 - Entity ID
            map(Type.INT);
            // 1 - Entity Status
            map(Type.BYTE);
            handler(new PacketHandler() {

                @Override
                public void handle(PacketWrapper wrapper) throws Exception {
                    byte b = wrapper.get(Type.BYTE, 0);
                    if (b == 35) {
                        wrapper.clearPacket();
                        // Change Game State
                        wrapper.setId(0x1E);
                        // Play Elder Guardian animation
                        wrapper.write(Type.UNSIGNED_BYTE, (short) 10);
                        wrapper.write(Type.FLOAT, 0F);
                    }
                }
            });
        }
    });
}
Also used : EntityData(com.viaversion.viabackwards.api.entities.storage.EntityData) Entity1_12Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_12Types) PacketHandler(com.viaversion.viaversion.api.protocol.remapper.PacketHandler) LegacyEntityRewriter(com.viaversion.viabackwards.api.rewriters.LegacyEntityRewriter) WrappedMetadata(com.viaversion.viabackwards.api.entities.storage.WrappedMetadata) ChestedHorseStorage(com.viaversion.viabackwards.protocol.protocol1_10to1_11.storage.ChestedHorseStorage) PotionSplashHandler(com.viaversion.viabackwards.protocol.protocol1_10to1_11.PotionSplashHandler) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) MetaType1_9(com.viaversion.viaversion.api.minecraft.metadata.types.MetaType1_9) Block(com.viaversion.viabackwards.utils.Block) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) Type(com.viaversion.viaversion.api.type.Type) List(java.util.List) StoredEntityData(com.viaversion.viaversion.api.data.entity.StoredEntityData) Types1_9(com.viaversion.viaversion.api.type.types.version.Types1_9) Optional(java.util.Optional) Entity1_11Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_11Types) ClientboundPackets1_9_3(com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ClientboundPackets1_9_3) Protocol1_10To1_11(com.viaversion.viabackwards.protocol.protocol1_10to1_11.Protocol1_10To1_11) PacketRemapper(com.viaversion.viaversion.api.protocol.remapper.PacketRemapper) EntityData(com.viaversion.viabackwards.api.entities.storage.EntityData) StoredEntityData(com.viaversion.viaversion.api.data.entity.StoredEntityData) WrappedMetadata(com.viaversion.viabackwards.api.entities.storage.WrappedMetadata) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) 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) List(java.util.List)

Example 7 with EntityType

use of com.viaversion.viaversion.api.minecraft.entities.EntityType in project ViaBackwards by ViaVersion.

the class EntityRewriterBase method handleMetadata.

@Override
public void handleMetadata(int entityId, List<Metadata> metadataList, UserConnection connection) {
    super.handleMetadata(entityId, metadataList, connection);
    EntityType type = tracker(connection).entityType(entityId);
    if (type == null) {
        // Don't handle untracked entities - basically always the fault of a plugin sending virtual entities through concurrency-unsafe handling
        return;
    }
    EntityData entityData = entityDataForType(type);
    // Set the mapped entity name if there is no custom name set already
    Metadata meta = getMeta(displayNameIndex, metadataList);
    if (meta != null && entityData != null && entityData.mobName() != null && (meta.getValue() == null || meta.getValue().toString().isEmpty()) && meta.metaType().typeId() == displayNameMetaType.typeId()) {
        meta.setValue(entityData.mobName());
        if (ViaBackwards.getConfig().alwaysShowOriginalMobName()) {
            removeMeta(displayVisibilityIndex, metadataList);
            metadataList.add(new Metadata(displayVisibilityIndex, displayVisibilityMetaType, true));
        }
    }
    // TODO only do this once for a first meta packet?
    if (entityData != null && entityData.hasBaseMeta()) {
        entityData.defaultMeta().createMeta(new WrappedMetadata(metadataList));
    }
}
Also used : EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) EntityData(com.viaversion.viabackwards.api.entities.storage.EntityData) StoredEntityData(com.viaversion.viaversion.api.data.entity.StoredEntityData) WrappedMetadata(com.viaversion.viabackwards.api.entities.storage.WrappedMetadata) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) WrappedMetadata(com.viaversion.viabackwards.api.entities.storage.WrappedMetadata)

Example 8 with EntityType

use of com.viaversion.viaversion.api.minecraft.entities.EntityType in project ViaBackwards by ViaVersion.

the class EntityRewriterBase method mapTypes.

/**
 * Maps entity ids based on the enum constant's names.
 *
 * @param oldTypes     entity types of the higher version
 * @param newTypeClass entity types enum class of the lower version
 * @param <E>          new enum type
 */
@Override
public <E extends Enum<E> & EntityType> void mapTypes(EntityType[] oldTypes, Class<E> newTypeClass) {
    if (typeMappings == null) {
        typeMappings = new Int2IntOpenHashMap(oldTypes.length, 0.99F);
        typeMappings.defaultReturnValue(-1);
    }
    for (EntityType oldType : oldTypes) {
        try {
            E newType = Enum.valueOf(newTypeClass, oldType.name());
            typeMappings.put(oldType.getId(), newType.getId());
        } catch (IllegalArgumentException ignored) {
        // Don't warn
        }
    }
}
Also used : EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) Int2IntOpenHashMap(com.viaversion.viaversion.libs.fastutil.ints.Int2IntOpenHashMap)

Example 9 with EntityType

use of com.viaversion.viaversion.api.minecraft.entities.EntityType in project ViaVersion by ViaVersion.

the class EntityTypeUtil method toOrderedArray.

/**
 * Returns an ordered array with each index representing the actual entity id.
 *
 * @param values entity types
 * @return ordered array with each index representing the actual entity id
 */
public static EntityType[] toOrderedArray(EntityType[] values) {
    List<EntityType> types = new ArrayList<>();
    for (EntityType type : values) {
        if (type.getId() != -1) {
            types.add(type);
        }
    }
    types.sort(Comparator.comparingInt(EntityType::getId));
    return types.toArray(new EntityType[0]);
}
Also used : EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) ArrayList(java.util.ArrayList)

Example 10 with EntityType

use of com.viaversion.viaversion.api.minecraft.entities.EntityType in project ViaVersion by ViaVersion.

the class EntityRewriter method handleMetadata.

@Override
public void handleMetadata(int entityId, List<Metadata> metadataList, UserConnection connection) {
    EntityType type = tracker(connection).entityType(entityId);
    // Count index for fast removal
    int i = 0;
    for (Metadata metadata : metadataList.toArray(EMPTY_ARRAY)) {
        // Call handlers implementing the old handleMetadata
        if (!callOldMetaHandler(entityId, type, metadata, metadataList, connection)) {
            metadataList.remove(i--);
            continue;
        }
        MetaHandlerEvent event = null;
        for (MetaFilter filter : metadataFilters) {
            if (!filter.isFiltered(type, metadata)) {
                continue;
            }
            if (event == null) {
                // Only initialize when needed and share event instance
                event = new MetaHandlerEventImpl(connection, type, entityId, metadata, metadataList);
            }
            try {
                filter.handler().handle(event, metadata);
            } catch (Exception e) {
                logException(e, type, metadataList, metadata);
                metadataList.remove(i--);
                break;
            }
            if (event.cancelled()) {
                // Remove meta, decrease list index counter, and break current filter loop
                metadataList.remove(i--);
                break;
            }
        }
        if (event != null && event.extraMeta() != null) {
            // Finally, add newly created meta
            metadataList.addAll(event.extraMeta());
        }
        i++;
    }
}
Also used : EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) MetaFilter(com.viaversion.viaversion.rewriter.meta.MetaFilter) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) MetaHandlerEvent(com.viaversion.viaversion.rewriter.meta.MetaHandlerEvent) MetaHandlerEventImpl(com.viaversion.viaversion.rewriter.meta.MetaHandlerEventImpl)

Aggregations

EntityType (com.viaversion.viaversion.api.minecraft.entities.EntityType)16 Metadata (com.viaversion.viaversion.api.minecraft.metadata.Metadata)11 PacketWrapper (com.viaversion.viaversion.api.protocol.packet.PacketWrapper)9 PacketRemapper (com.viaversion.viaversion.api.protocol.remapper.PacketRemapper)9 PacketHandler (com.viaversion.viaversion.api.protocol.remapper.PacketHandler)7 Type (com.viaversion.viaversion.api.type.Type)7 EntityData (com.viaversion.viabackwards.api.entities.storage.EntityData)5 Item (com.viaversion.viaversion.api.minecraft.item.Item)5 ViaBackwards (com.viaversion.viabackwards.ViaBackwards)4 LegacyEntityRewriter (com.viaversion.viabackwards.api.rewriters.LegacyEntityRewriter)4 StoredEntityData (com.viaversion.viaversion.api.data.entity.StoredEntityData)4 Particle (com.viaversion.viaversion.api.type.types.Particle)4 Types1_14 (com.viaversion.viaversion.api.type.types.version.Types1_14)4 ClientboundPackets1_13 (com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13)4 ClientWorld (com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld)4 EntityPositionHandler (com.viaversion.viabackwards.api.entities.storage.EntityPositionHandler)3 WrappedMetadata (com.viaversion.viabackwards.api.entities.storage.WrappedMetadata)3 Position (com.viaversion.viaversion.api.minecraft.Position)3 Entity1_13Types (com.viaversion.viaversion.api.minecraft.entities.Entity1_13Types)3 ArrayList (java.util.ArrayList)3