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

personthecat.catlib.serialization.codec.ValueMapCodec 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.ImmutableMap;
import com.mojang.datafixers.util.Pair;
import com.mojang.datafixers.util.Unit;
import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.DynamicOps;
import org.apache.commons.lang3.mutable.MutableObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

@SuppressWarnings("unused")
public class ValueMapCodec implements Codec> {
    private final Codec codec;

    public ValueMapCodec(final Codec codec) {
        this.codec = codec;
    }

    public Codec getType() {
        return this.codec;
    }

    @Override
    public  DataResult encode(final Map input, final DynamicOps ops, final T prefix) {
        final Map map = new HashMap<>();
        final List errors = new ArrayList<>();

        for (final Map.Entry entry : input.entrySet()) {
            this.codec.encodeStart(ops, entry.getValue())
                .resultOrPartial(e -> errors.add(ops.createString(e)))
                .ifPresent(t -> map.put(ops.createString(entry.getKey()), t));
        }
        if (!errors.isEmpty()) {
            return DataResult.error("Error encoding map", ops.createList(errors.stream()));
        }
        return ops.mergeToMap(prefix, map);
    }

    @Override
    public  DataResult, T>> decode(final DynamicOps ops, final T input) {
        return ops.getMap(input).flatMap(map -> {
            final ImmutableMap.Builder out = ImmutableMap.builder();
            final Stream.Builder failed = Stream.builder();
            final MutableObject> result = new MutableObject<>(DataResult.success(Unit.INSTANCE));

            map.entries().forEach(pair -> {
                final DataResult> key = Codec.STRING.decode(ops, pair.getFirst());
                final DataResult> element = this.codec.decode(ops, pair.getSecond());

                key.resultOrPartial(e -> failed.add(pair.getFirst())).ifPresent(k -> {
                    element.error().ifPresent(e -> failed.add(pair.getSecond()));
                    result.setValue(result.getValue().apply2stable((r, v) -> {
                        out.put(k.getFirst(), v.getFirst());
                        return r;
                    }, element));
                });
            });

            final Pair, T> pair = Pair.of(out.build(), ops.createList(failed.build()));
            return result.getValue().map(unit -> pair).setPartial(pair);
        });
    }

    @Override
    public String toString() {
        return "ValueMapCodec[String -> " + this.codec + "]";
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy