personthecat.catlib.serialization.codec.CodecUtils 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.datafixers.util.*;
import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.DynamicOps;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import org.jetbrains.annotations.NotNull;
import personthecat.catlib.exception.JsonFormatException;
import personthecat.catlib.util.Shorthand;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;
import net.minecraft.class_1959;
import net.minecraft.class_2960;
@SuppressWarnings("unused")
public class CodecUtils {
public static final ValueMapCodec STRING_MAP = mapOf(Codec.STRING);
public static final ValueMapCodec BOOLEAN_MAP = mapOf(Codec.BOOL);
public static final ValueMapCodec INT_MAP = mapOf(Codec.INT);
public static final ValueMapCodec FLOAT_MAP = mapOf(Codec.FLOAT);
public static final Codec> STRING_LIST = easyList(Codec.STRING);
public static final Codec> INT_LIST = easyList(Codec.INT);
public static final Codec> FLOAT_LIST = easyList(Codec.FLOAT);
public static final Codec> ID_LIST = easyList(class_2960.field_25139);
public static final Codec> CATEGORY_LIST = easyList(ofEnum(class_1959.class_1961.class));
public static ValueMapCodec mapOf(final Codec codec) {
return new ValueMapCodec<>(codec);
}
public static Codec> easyList(final @NotNull Codec codec) {
return Codec.either(codec, codec.listOf()).xmap(
either -> either.map(Collections::singletonList, Function.identity()),
list -> {
if (list == null) return Either.right(Collections.emptyList());
return list.size() == 1 ? Either.left(list.get(0)) : Either.right(list);
}
);
}
public static SetCodec setOf(final @NotNull Codec codec) {
return new SetCodec<>(codec);
}
public static Codec> easySet(final @NotNull Codec codec) {
return Codec.either(codec, setOf(codec)).xmap(
either -> either.map(Collections::singleton, Function.identity()),
set -> {
if (set == null) return Either.right(Collections.emptySet());
return set.size() == 1 ? Either.left(set.iterator().next()) : Either.right(set);
}
);
}
public static Codec> autoFlatten(final @NotNull Codec codec) {
return new AutoFlatListCodec<>(codec);
}
public static > Codec ofEnum(final Class e) {
final StringBuilder sb = new StringBuilder("[");
for (final E constant : e.getEnumConstants()) {
sb.append(constant.name()).append(',');
}
final String names = sb.append(']').toString();
return Codec.STRING.flatXmap(
name -> {
final E c = Shorthand.getEnumConstant(name, e).orElse(null);
return c == null ? DataResult.error("Unknown key: " + name + ". Expected one of " + names) : DataResult.success(c);
},
constant -> DataResult.success(constant.name())
);
}
public static SimpleEitherCodec simpleEither(final Codec first, final Codec second) {
return new SimpleEitherCodec<>(first, second);
}
@SuppressWarnings("unchecked")
public static Codec asParent(final Codec extends T> codec) {
return (Codec) codec;
}
public static Optional readOptional(final Codec codec, final DynamicOps ops, final T prefix) {
return codec.parse(ops, prefix).result();
}
public static A readThrowing(final Codec codec, final DynamicOps ops, final T prefix) {
return codec.parse(ops, prefix).get().map(Function.identity(), partial -> {
throw new JsonFormatException(partial.message());
});
}
public static EasyMapReader easyReader(final DynamicOps ops, final T prefix) {
return new EasyMapReader<>(ops, prefix);
}
public static DynamicCodec.Builder dynamic(final Supplier builder) {
return new DynamicCodec.Builder<>(builder, Function.identity(), Function.identity());
}
public static DynamicCodec.Builder dynamic(final Supplier builder, final Function out) {
return new DynamicCodec.Builder<>(builder, Function.identity(), out);
}
public static DynamicCodec.Builder dynamic(final Supplier builder, final Function in, final Function out) {
return new DynamicCodec.Builder<>(builder, in, out);
}
public static Codec codecOf(FieldDescriptor f1, Function c) {
return RecordCodecBuilder.create(i -> i.group(f1.f).apply(i, t1 -> c.apply(f1.r(t1))));
}
public static Codec codecOf(
FieldDescriptor f1, FieldDescriptor f2, BiFunction c) {
return RecordCodecBuilder.create(i -> i.group(f1.f, f2.f).apply(i, (t1, t2) -> c.apply(f1.r(t1), f2.r(t2))));
}
public static Codec codecOf(
FieldDescriptor f1, FieldDescriptor f2, FieldDescriptor f3,
Function3 c) {
return RecordCodecBuilder.create(i -> i.group(f1.f, f2.f, f3.f)
.apply(i, (t1, t2, t3) -> c.apply(f1.r(t1), f2.r(t2), f3.r(t3))));
}
public static Codec codecOf(
FieldDescriptor f1, FieldDescriptor f2, FieldDescriptor f3,
FieldDescriptor f4, Function4 c) {
return RecordCodecBuilder.create(i -> i.group(f1.f, f2.f, f3.f, f4.f)
.apply(i, (t1, t2, t3, t4) -> c.apply(f1.r(t1), f2.r(t2), f3.r(t3), f4.r(t4))));
}
public static Codec codecOf(
FieldDescriptor f1, FieldDescriptor f2, FieldDescriptor f3,
FieldDescriptor f4, FieldDescriptor f5, Function5 c) {
return RecordCodecBuilder.create(i -> i.group(f1.f, f2.f, f3.f, f4.f, f5.f)
.apply(i, (t1, t2, t3, t4, t5) -> c.apply(f1.r(t1), f2.r(t2), f3.r(t3), f4.r(t4), f5.r(t5))));
}
public static Codec codecOf(
FieldDescriptor f1, FieldDescriptor f2, FieldDescriptor f3,
FieldDescriptor f4, FieldDescriptor f5, FieldDescriptor f6,
Function6 c) {
return RecordCodecBuilder.create(i -> i.group(f1.f, f2.f, f3.f, f4.f, f5.f, f6.f)
.apply(i, (t1, t2, t3, t4, t5, t6) -> c.apply(f1.r(t1), f2.r(t2), f3.r(t3), f4.r(t4), f5.r(t5), f6.r(t6))));
}
public static Codec codecOf(
FieldDescriptor f1, FieldDescriptor f2, FieldDescriptor f3,
FieldDescriptor f4, FieldDescriptor f5, FieldDescriptor f6,
FieldDescriptor f7, Function7 c) {
return RecordCodecBuilder.create(i -> i.group(f1.f, f2.f, f3.f, f4.f, f5.f, f6.f, f7.f)
.apply(i, (t1, t2, t3, t4, t5, t6, t7) ->
c.apply(f1.r(t1), f2.r(t2), f3.r(t3), f4.r(t4), f5.r(t5), f6.r(t6), f7.r(t7))));
}
public static Codec codecOf(
FieldDescriptor f1, FieldDescriptor f2, FieldDescriptor f3,
FieldDescriptor f4, FieldDescriptor f5, FieldDescriptor f6,
FieldDescriptor f7, FieldDescriptor f8,
Function8 c) {
return RecordCodecBuilder.create(i -> i.group(f1.f, f2.f, f3.f, f4.f, f5.f, f6.f, f7.f, f8.f)
.apply(i, (t1, t2, t3, t4, t5, t6, t7, t8) ->
c.apply(f1.r(t1), f2.r(t2), f3.r(t3), f4.r(t4), f5.r(t5), f6.r(t6), f7.r(t7), f8.r(t8))));
}
public static Codec codecOf(
FieldDescriptor f1, FieldDescriptor f2, FieldDescriptor f3,
FieldDescriptor f4, FieldDescriptor f5, FieldDescriptor f6,
FieldDescriptor f7, FieldDescriptor f8, FieldDescriptor f9,
Function9 c) {
return RecordCodecBuilder.create(i -> i.group(f1.f, f2.f, f3.f, f4.f, f5.f, f6.f, f7.f, f8.f, f9.f)
.apply(i, (t1, t2, t3, t4, t5, t6, t7, t8, t9) ->
c.apply(f1.r(t1), f2.r(t2), f3.r(t3), f4.r(t4), f5.r(t5), f6.r(t6), f7.r(t7), f8.r(t8), f9.r(t9))));
}
public static Codec codecOf(
FieldDescriptor f1, FieldDescriptor f2, FieldDescriptor f3,
FieldDescriptor f4, FieldDescriptor f5, FieldDescriptor f6,
FieldDescriptor f7, FieldDescriptor f8, FieldDescriptor f9,
FieldDescriptor f10, Function10 c) {
return RecordCodecBuilder.create(i -> i.group(f1.f, f2.f, f3.f, f4.f, f5.f, f6.f, f7.f, f8.f, f9.f, f10.f)
.apply(i, (t1, t2, t3, t4, t5, t6, t7, t8, t9, t10) ->
c.apply(f1.r(t1), f2.r(t2), f3.r(t3), f4.r(t4), f5.r(t5), f6.r(t6), f7.r(t7), f8.r(t8), f9.r(t9), f10.r(t10))));
}
public static Codec codecOf(
FieldDescriptor f1, FieldDescriptor f2, FieldDescriptor f3,
FieldDescriptor f4, FieldDescriptor f5, FieldDescriptor f6,
FieldDescriptor f7, FieldDescriptor f8, FieldDescriptor f9,
FieldDescriptor f10, FieldDescriptor f11,
Function11 c) {
return RecordCodecBuilder.create(i -> i.group(f1.f, f2.f, f3.f, f4.f, f5.f, f6.f, f7.f, f8.f, f9.f, f10.f, f11.f)
.apply(i, (t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11) ->
c.apply(f1.r(t1), f2.r(t2), f3.r(t3), f4.r(t4), f5.r(t5), f6.r(t6), f7.r(t7), f8.r(t8), f9.r(t9), f10.r(t10), f11.r(t11))));
}
public static Codec codecOf(
FieldDescriptor f1, FieldDescriptor f2, FieldDescriptor f3,
FieldDescriptor f4, FieldDescriptor f5, FieldDescriptor f6,
FieldDescriptor f7, FieldDescriptor f8, FieldDescriptor f9,
FieldDescriptor f10, FieldDescriptor f11, FieldDescriptor f12,
Function12 c) {
return RecordCodecBuilder.create(i -> i.group(f1.f, f2.f, f3.f, f4.f, f5.f, f6.f, f7.f, f8.f, f9.f, f10.f, f11.f, f12.f)
.apply(i, (t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12) ->
c.apply(f1.r(t1), f2.r(t2), f3.r(t3), f4.r(t4), f5.r(t5), f6.r(t6), f7.r(t7), f8.r(t8), f9.r(t9), f10.r(t10), f11.r(t11), f12.r(t12))));
}
public static Codec codecOf(
FieldDescriptor f1, FieldDescriptor f2, FieldDescriptor f3,
FieldDescriptor f4, FieldDescriptor f5, FieldDescriptor f6,
FieldDescriptor f7, FieldDescriptor f8, FieldDescriptor f9,
FieldDescriptor f10, FieldDescriptor f11, FieldDescriptor f12,
FieldDescriptor f13, Function13 c) {
return RecordCodecBuilder.create(i -> i.group(f1.f, f2.f, f3.f, f4.f, f5.f, f6.f, f7.f, f8.f, f9.f, f10.f, f11.f, f12.f, f13.f)
.apply(i, (t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13) ->
c.apply(f1.r(t1), f2.r(t2), f3.r(t3), f4.r(t4), f5.r(t5), f6.r(t6), f7.r(t7), f8.r(t8), f9.r(t9), f10.r(t10), f11.r(t11), f12.r(t12), f13.r(t13))));
}
public static Codec codecOf(
FieldDescriptor f1, FieldDescriptor f2, FieldDescriptor f3,
FieldDescriptor f4, FieldDescriptor f5, FieldDescriptor f6,
FieldDescriptor f7, FieldDescriptor f8, FieldDescriptor f9,
FieldDescriptor f10, FieldDescriptor f11, FieldDescriptor f12,
FieldDescriptor f13, FieldDescriptor f14,
Function14 c) {
return RecordCodecBuilder.create(i -> i.group(f1.f, f2.f, f3.f, f4.f, f5.f, f6.f, f7.f, f8.f, f9.f, f10.f, f11.f, f12.f, f13.f, f14.f)
.apply(i, (t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14) ->
c.apply(f1.r(t1), f2.r(t2), f3.r(t3), f4.r(t4), f5.r(t5), f6.r(t6), f7.r(t7), f8.r(t8), f9.r(t9), f10.r(t10), f11.r(t11), f12.r(t12), f13.r(t13), f14.r(t14))));
}
public static Codec codecOf(
FieldDescriptor f1, FieldDescriptor f2, FieldDescriptor f3,
FieldDescriptor f4, FieldDescriptor f5, FieldDescriptor f6,
FieldDescriptor f7, FieldDescriptor f8, FieldDescriptor f9,
FieldDescriptor f10, FieldDescriptor f11, FieldDescriptor f12,
FieldDescriptor f13, FieldDescriptor f14, FieldDescriptor f15,
Function15 c) {
return RecordCodecBuilder.create(i -> i.group(f1.f, f2.f, f3.f, f4.f, f5.f, f6.f, f7.f, f8.f, f9.f, f10.f, f11.f, f12.f, f13.f, f14.f, f15.f)
.apply(i, (t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15) ->
c.apply(f1.r(t1), f2.r(t2), f3.r(t3), f4.r(t4), f5.r(t5), f6.r(t6), f7.r(t7), f8.r(t8), f9.r(t9), f10.r(t10), f11.r(t11), f12.r(t12), f13.r(t13), f14.r(t14), f15.r(t15))));
}
public static Codec codecOf(
FieldDescriptor f1, FieldDescriptor f2, FieldDescriptor f3,
FieldDescriptor f4, FieldDescriptor f5, FieldDescriptor f6,
FieldDescriptor f7, FieldDescriptor f8, FieldDescriptor f9,
FieldDescriptor f10, FieldDescriptor f11, FieldDescriptor f12,
FieldDescriptor f13, FieldDescriptor f14, FieldDescriptor f15,
FieldDescriptor f16, Function16 c) {
return RecordCodecBuilder.create(i -> i.group(f1.f, f2.f, f3.f, f4.f, f5.f, f6.f, f7.f, f8.f, f9.f, f10.f, f11.f, f12.f, f13.f, f14.f, f15.f, f16.f)
.apply(i, (t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16) ->
c.apply(f1.r(t1), f2.r(t2), f3.r(t3), f4.r(t4), f5.r(t5), f6.r(t6), f7.r(t7), f8.r(t8), f9.r(t9), f10.r(t10), f11.r(t11), f12.r(t12), f13.r(t13), f14.r(t14), f15.r(t15), f16.r(t16))));
}
public static Codec codecOf(RecordCodecBuilder t1, Function f) {
return RecordCodecBuilder.create(i -> i.group(t1).apply(i, f));
}
public static Codec codecOf(
RecordCodecBuilder t1, RecordCodecBuilder t2, BiFunction f) {
return RecordCodecBuilder.create(i -> i.group(t1, t2).apply(i, f));
}
public static Codec codecOf(
RecordCodecBuilder t1, RecordCodecBuilder t2, RecordCodecBuilder t3,
Function3 f) {
return RecordCodecBuilder.create(i -> i.group(t1, t2, t3).apply(i, f));
}
public static Codec codecOf(
RecordCodecBuilder t1, RecordCodecBuilder t2, RecordCodecBuilder t3,
RecordCodecBuilder t4, Function4 f) {
return RecordCodecBuilder.create(i -> i.group(t1, t2, t3, t4).apply(i, f));
}
public static