Search in sources :

Example 1 with MetaType

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

the class ModernMetaType method read.

@Override
public Metadata read(final ByteBuf buffer) throws Exception {
    final short index = buffer.readUnsignedByte();
    // End of metadata
    if (index == END)
        return null;
    final MetaType type = this.getType(Type.VAR_INT.readPrimitive(buffer));
    return new Metadata(index, type, type.type().read(buffer));
}
Also used : Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) MetaType(com.viaversion.viaversion.api.minecraft.metadata.MetaType)

Example 2 with MetaType

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

the class ModernMetaType method write.

@Override
public void write(final ByteBuf buffer, final Metadata object) throws Exception {
    if (object == null) {
        buffer.writeByte(END);
    } else {
        buffer.writeByte(object.id());
        final MetaType type = object.metaType();
        Type.VAR_INT.writePrimitive(buffer, type.typeId());
        type.type().write(buffer, object.getValue());
    }
}
Also used : MetaType(com.viaversion.viaversion.api.minecraft.metadata.MetaType)

Example 3 with MetaType

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

the class AbstractMetaTypes method add.

protected MetaType add(final int typeId, final Type<?> type) {
    final MetaType metaType = MetaType.create(typeId, type);
    values[typeId] = metaType;
    return metaType;
}
Also used : MetaType(com.viaversion.viaversion.api.minecraft.metadata.MetaType)

Example 4 with MetaType

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

the class MetadataRewriter1_9To1_8 method handleMetadata.

@Override
protected void handleMetadata(int entityId, EntityType type, Metadata metadata, List<Metadata> metadatas, UserConnection connection) throws Exception {
    MetaIndex metaIndex = MetaIndex.searchIndex(type, metadata.id());
    if (metaIndex == null) {
        throw new Exception("Could not find valid metadata");
    }
    if (metaIndex.getNewType() == null) {
        metadatas.remove(metadata);
        return;
    }
    metadata.setId(metaIndex.getNewIndex());
    metadata.setMetaTypeUnsafe(metaIndex.getNewType());
    Object value = metadata.getValue();
    switch(metaIndex.getNewType()) {
        case Byte:
            // convert from int, byte
            if (metaIndex.getOldType() == MetaType1_8.Byte) {
                metadata.setValue(value);
            }
            if (metaIndex.getOldType() == MetaType1_8.Int) {
                metadata.setValue(((Integer) value).byteValue());
            }
            // After writing the last one
            if (metaIndex == MetaIndex.ENTITY_STATUS && type == Entity1_10Types.EntityType.PLAYER) {
                Byte val = 0;
                if ((((Byte) value) & 0x10) == 0x10) {
                    // Player eating/aiming/drinking
                    val = 1;
                }
                int newIndex = MetaIndex.PLAYER_HAND.getNewIndex();
                MetaType metaType = MetaIndex.PLAYER_HAND.getNewType();
                metadatas.add(new Metadata(newIndex, metaType, val));
            }
            break;
        case OptUUID:
            String owner = (String) value;
            UUID toWrite = null;
            if (!owner.isEmpty()) {
                try {
                    toWrite = UUID.fromString(owner);
                } catch (Exception ignored) {
                }
            }
            metadata.setValue(toWrite);
            break;
        case VarInt:
            // convert from int, short, byte
            if (metaIndex.getOldType() == MetaType1_8.Byte) {
                metadata.setValue(((Byte) value).intValue());
            }
            if (metaIndex.getOldType() == MetaType1_8.Short) {
                metadata.setValue(((Short) value).intValue());
            }
            if (metaIndex.getOldType() == MetaType1_8.Int) {
                metadata.setValue(value);
            }
            break;
        case Float:
            metadata.setValue(value);
            break;
        case String:
            metadata.setValue(value);
            break;
        case Boolean:
            if (metaIndex == MetaIndex.AGEABLE_AGE)
                metadata.setValue((Byte) value < 0);
            else
                metadata.setValue((Byte) value != 0);
            break;
        case Slot:
            metadata.setValue(value);
            ItemRewriter.toClient((Item) metadata.getValue());
            break;
        case Position:
            Vector vector = (Vector) value;
            metadata.setValue(vector);
            break;
        case Vector3F:
            EulerAngle angle = (EulerAngle) value;
            metadata.setValue(angle);
            break;
        case Chat:
            value = Protocol1_9To1_8.fixJson(value.toString());
            metadata.setValue(value);
            break;
        case BlockID:
            // Convert from int, short, byte
            metadata.setValue(((Number) value).intValue());
            break;
        default:
            metadatas.remove(metadata);
            throw new Exception("Unhandled MetaDataType: " + metaIndex.getNewType());
    }
}
Also used : Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) UUID(java.util.UUID) EulerAngle(com.viaversion.viaversion.api.minecraft.EulerAngle) Vector(com.viaversion.viaversion.api.minecraft.Vector) MetaType(com.viaversion.viaversion.api.minecraft.metadata.MetaType)

Example 5 with MetaType

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

the class EntityPackets1_14 method registerRewrites.

@Override
protected void registerRewrites() {
    mapTypes(Entity1_14Types.values(), Entity1_13Types.EntityType.class);
    mapEntityTypeWithData(Entity1_14Types.CAT, Entity1_14Types.OCELOT).jsonName();
    mapEntityTypeWithData(Entity1_14Types.TRADER_LLAMA, Entity1_14Types.LLAMA).jsonName();
    mapEntityTypeWithData(Entity1_14Types.FOX, Entity1_14Types.WOLF).jsonName();
    mapEntityTypeWithData(Entity1_14Types.PANDA, Entity1_14Types.POLAR_BEAR).jsonName();
    mapEntityTypeWithData(Entity1_14Types.PILLAGER, Entity1_14Types.VILLAGER).jsonName();
    mapEntityTypeWithData(Entity1_14Types.WANDERING_TRADER, Entity1_14Types.VILLAGER).jsonName();
    mapEntityTypeWithData(Entity1_14Types.RAVAGER, Entity1_14Types.COW).jsonName();
    filter().handler((event, meta) -> {
        int typeId = meta.metaType().typeId();
        if (typeId <= 15) {
            meta.setMetaType(Types1_13_2.META_TYPES.byId(typeId));
        }
        MetaType type = meta.metaType();
        if (type == Types1_13_2.META_TYPES.itemType) {
            Item item = (Item) meta.getValue();
            meta.setValue(protocol.getItemRewriter().handleItemToClient(item));
        } else if (type == Types1_13_2.META_TYPES.blockStateType) {
            int blockstate = meta.value();
            meta.setValue(protocol.getMappingData().getNewBlockStateId(blockstate));
        }
    });
    filter().type(Entity1_14Types.PILLAGER).cancel(15);
    filter().type(Entity1_14Types.FOX).cancel(15);
    filter().type(Entity1_14Types.FOX).cancel(16);
    filter().type(Entity1_14Types.FOX).cancel(17);
    filter().type(Entity1_14Types.FOX).cancel(18);
    filter().type(Entity1_14Types.PANDA).cancel(15);
    filter().type(Entity1_14Types.PANDA).cancel(16);
    filter().type(Entity1_14Types.PANDA).cancel(17);
    filter().type(Entity1_14Types.PANDA).cancel(18);
    filter().type(Entity1_14Types.PANDA).cancel(19);
    filter().type(Entity1_14Types.PANDA).cancel(20);
    filter().type(Entity1_14Types.CAT).cancel(18);
    filter().type(Entity1_14Types.CAT).cancel(19);
    filter().type(Entity1_14Types.CAT).cancel(20);
    filter().handler((event, meta) -> {
        EntityType type = event.entityType();
        if (type == null)
            return;
        if (type.isOrHasParent(Entity1_14Types.ABSTRACT_ILLAGER_BASE) || type == Entity1_14Types.RAVAGER || type == Entity1_14Types.WITCH) {
            int index = event.index();
            if (index == 14) {
                event.cancel();
            } else if (index > 14) {
                event.setIndex(index - 1);
            }
        }
    });
    filter().type(Entity1_14Types.AREA_EFFECT_CLOUD).index(10).handler((event, meta) -> {
        rewriteParticle((Particle) meta.getValue());
    });
    filter().type(Entity1_14Types.FIREWORK_ROCKET).index(8).handler((event, meta) -> {
        meta.setMetaType(Types1_13_2.META_TYPES.varIntType);
        Integer value = (Integer) meta.getValue();
        if (value == null) {
            meta.setValue(0);
        }
    });
    filter().filterFamily(Entity1_14Types.ABSTRACT_ARROW).removeIndex(9);
    // Head shake timer
    filter().type(Entity1_14Types.VILLAGER).cancel(15);
    MetaHandler villagerDataHandler = (event, meta) -> {
        VillagerData villagerData = (VillagerData) meta.getValue();
        meta.setTypeAndValue(Types1_13_2.META_TYPES.varIntType, villagerDataToProfession(villagerData));
        if (meta.id() == 16) {
            // decreased by 2 again in one of the following handlers
            event.setIndex(15);
        }
    };
    filter().type(Entity1_14Types.ZOMBIE_VILLAGER).index(18).handler(villagerDataHandler);
    filter().type(Entity1_14Types.VILLAGER).index(16).handler(villagerDataHandler);
    // Holding arms up - from bitfield into own boolean
    filter().filterFamily(Entity1_14Types.ABSTRACT_SKELETON).index(13).handler((event, meta) -> {
        byte value = (byte) meta.getValue();
        if ((value & 4) != 0) {
            event.createExtraMeta(new Metadata(14, Types1_13_2.META_TYPES.booleanType, true));
        }
    });
    filter().filterFamily(Entity1_14Types.ZOMBIE).index(13).handler((event, meta) -> {
        byte value = (byte) meta.getValue();
        if ((value & 4) != 0) {
            event.createExtraMeta(new Metadata(16, Types1_13_2.META_TYPES.booleanType, true));
        }
    });
    filter().filterFamily(Entity1_14Types.ZOMBIE).addIndex(16);
    // Remove bed location
    filter().filterFamily(Entity1_14Types.LIVINGENTITY).handler((event, meta) -> {
        int index = event.index();
        if (index == 12) {
            Position position = (Position) meta.getValue();
            if (position != null) {
                // Use bed
                PacketWrapper wrapper = PacketWrapper.create(ClientboundPackets1_13.USE_BED, null, event.user());
                wrapper.write(Type.VAR_INT, event.entityId());
                wrapper.write(Type.POSITION, position);
                try {
                    wrapper.scheduleSend(Protocol1_13_2To1_14.class);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            event.cancel();
        } else if (index > 12) {
            event.setIndex(index - 1);
        }
    });
    // Pose
    filter().removeIndex(6);
    filter().type(Entity1_14Types.OCELOT).index(13).handler((event, meta) -> {
        event.setIndex(15);
        meta.setTypeAndValue(Types1_13_2.META_TYPES.varIntType, 0);
    });
    filter().type(Entity1_14Types.CAT).handler((event, meta) -> {
        if (event.index() == 15) {
            meta.setValue(1);
        } else if (event.index() == 13) {
            meta.setValue((byte) ((byte) meta.getValue() & 0x4));
        }
    });
    filter().handler((event, meta) -> {
        if (meta.metaType().typeId() > 15) {
            throw new IllegalArgumentException("Unhandled metadata: " + meta);
        }
    });
}
Also used : 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) Position(com.viaversion.viaversion.api.minecraft.Position) Metadata(com.viaversion.viaversion.api.minecraft.metadata.Metadata) EntityType(com.viaversion.viaversion.api.minecraft.entities.EntityType) Item(com.viaversion.viaversion.api.minecraft.item.Item) PacketWrapper(com.viaversion.viaversion.api.protocol.packet.PacketWrapper) VillagerData(com.viaversion.viaversion.api.minecraft.VillagerData) Entity1_13Types(com.viaversion.viaversion.api.minecraft.entities.Entity1_13Types) MetaHandler(com.viaversion.viaversion.rewriter.meta.MetaHandler) MetaType(com.viaversion.viaversion.api.minecraft.metadata.MetaType)

Aggregations

MetaType (com.viaversion.viaversion.api.minecraft.metadata.MetaType)9 Metadata (com.viaversion.viaversion.api.minecraft.metadata.Metadata)5 Particle (com.viaversion.viaversion.api.type.types.Particle)3 Item (com.viaversion.viaversion.api.minecraft.item.Item)2 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 WolfDataMaskStorage (com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.storage.WolfDataMaskStorage)1 EntityTracker (com.viaversion.viaversion.api.data.entity.EntityTracker)1 StoredEntityData (com.viaversion.viaversion.api.data.entity.StoredEntityData)1 EulerAngle (com.viaversion.viaversion.api.minecraft.EulerAngle)1 Position (com.viaversion.viaversion.api.minecraft.Position)1 Vector (com.viaversion.viaversion.api.minecraft.Vector)1 VillagerData (com.viaversion.viaversion.api.minecraft.VillagerData)1 Entity1_13Types (com.viaversion.viaversion.api.minecraft.entities.Entity1_13Types)1