All Downloads are FREE. Search and download functionalities are using the official Maven repository.

personthecat.catlib.serialization.codec.SimpleAnyCodec Maven / Gradle / Ivy

Go to download

Utilities for serialization, commands, noise generation, IO, and some new data types.

The newest version!
package personthecat.catlib.serialization.codec;

import com.google.common.collect.ImmutableList;
import com.mojang.datafixers.util.Pair;
import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.DynamicOps;
import com.mojang.serialization.Encoder;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

@SuppressWarnings("unused")
public class SimpleAnyCodec implements Codec {
    private final Function> encoder;
    private final List> codecs;

    @SafeVarargs
    public SimpleAnyCodec(final Codec first, final Codec... others) {
        this(a -> first, first, others);
    }

    @SafeVarargs
    public SimpleAnyCodec(
            final Function> encoder, final Codec first, final Codec... others) {
        this(encoder, ImmutableList.>builder().add(first).add(others).build());
    }

    private SimpleAnyCodec(final Function> encoder, final List> codecs) {
        this.encoder = wrapDowncast(encoder);
        this.codecs = codecs;
    }

    public SimpleAnyCodec withEncoder(final Encoder encoder) {
        return this.withEncoder(a -> encoder);
    }

    public SimpleAnyCodec withEncoder(final Function> encoder) {
        return new SimpleAnyCodec<>(encoder, this.codecs);
    }

    @SuppressWarnings("unchecked")
    private static  Function> wrapDowncast(final Function> encoder) {
        return a -> (Encoder) encoder.apply(a);
    }

    @Override
    public  DataResult> decode(final DynamicOps ops, final T input) {
        final List errors = new ArrayList<>();
        for (final Codec codec : this.codecs) {
            final DataResult> result = downcast(codec.decode(ops, input));
            if (result.result().isPresent()) {
                return result;
            }
            result.error().ifPresent(e -> errors.add(e.message()));
        }
        final StringBuilder message =
            new StringBuilder("Fix any: [\"").append('"').append(errors.get(0)).append(",\"");
        for (int i = 1; i < errors.size(); i++) {
            message.append('"').append(errors.get(i)).append('"');
        }
        return DataResult.error(message.append(']').toString());
    }

    @SuppressWarnings("unchecked")
    private static  DataResult> downcast(final DataResult> pair) {
        return (DataResult>) (Object) pair;
    }

    @Override
    public  DataResult encode(final A input, final DynamicOps ops, final T prefix) {
        return this.encoder.apply(input).encode(input, ops, prefix);
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("SimpleAny[").append(this.codecs.get(0));
        for (int i = 1; i < this.codecs.size(); i++) {
            sb.append('|').append(this.codecs.get(i));
        }
        return sb.append(']').toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy