Search in sources :

Example 1 with Vector

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

Aggregations

EulerAngle (com.viaversion.viaversion.api.minecraft.EulerAngle)1 Vector (com.viaversion.viaversion.api.minecraft.Vector)1 MetaType (com.viaversion.viaversion.api.minecraft.metadata.MetaType)1 Metadata (com.viaversion.viaversion.api.minecraft.metadata.Metadata)1 UUID (java.util.UUID)1