personthecat.catlib.serialization.codec.EasyStateCodec Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of catlib-quilt Show documentation
Show all versions of catlib-quilt Show documentation
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";
}
}