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

personthecat.catlib.serialization.codec.EasyStateCodec 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.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.datafixers.util.Pair;
import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.DynamicOps;
import net.minecraft.class_2259;
import net.minecraft.class_2680;

public class EasyStateCodec implements Codec {

    public static final EasyStateCodec INSTANCE = new EasyStateCodec();

    EasyStateCodec() {}

    @Override
    public  DataResult encode(final class_2680 input, final DynamicOps ops, final T prefix) {
        return DataResult.success(ops.createString(class_2259.method_9685(input)));
    }

    @Override
    public  DataResult> decode(final DynamicOps ops, final T input) {
        return ops.getStringValue(input).flatMap(id -> {
            try {
                final class_2259 parser = new class_2259(new StringReader(id), false).method_9678(false);
                return DataResult.success(Pair.of(parser.method_9669(), input));
            } catch (final CommandSyntaxException e) {
                return DataResult.error(e.getMessage());
            }
        });
    }

    @Override
    public String toString() {
        return "EasyStateCodec";
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy