| 1 | package net.minecraft.util.datafix;
|
| 2 |
|
| 3 | import com.mojang.datafixers.DataFixUtils;
|
| 4 | import com.mojang.datafixers.OpticFinder;
|
| 5 | import com.mojang.datafixers.RewriteResult;
|
| 6 | import com.mojang.datafixers.TypeRewriteRule;
|
| 7 | import com.mojang.datafixers.Typed;
|
| 8 | import com.mojang.datafixers.View;
|
| 9 | import com.mojang.datafixers.functions.PointFreeRule;
|
| 10 | import com.mojang.datafixers.types.Type;
|
| 11 | import com.mojang.serialization.Dynamic;
|
| 12 | import com.mojang.serialization.DynamicOps;
|
| 13 | import java.util.BitSet;
|
| 14 | import java.util.Optional;
|
| 15 | import java.util.function.Function;
|
| 16 | import java.util.function.UnaryOperator;
|
| 17 | import java.util.stream.IntStream;
|
| 18 | import net.minecraft.util.Util;
|
| 19 |
|
| 20 | public class ExtraDataFixUtils {
|
| 21 | public static Dynamic<?> fixBlockPos(final Dynamic<?> pos) {
|
| 22 | Optional<Number> x = pos.get("X").asNumber().result();
|
| 23 | Optional<Number> y = pos.get("Y").asNumber().result();
|
| 24 | Optional<Number> z = pos.get("Z").asNumber().result();
|
| 25 | return !x.isEmpty() && !y.isEmpty() && !z.isEmpty() ? createBlockPos(pos, x.get().intValue(), y.get().intValue(), z.get().intValue()) : pos;
|
| 26 | }
|
| 27 |
|
| 28 | public static Dynamic<?> fixInlineBlockPos(final Dynamic<?> input, final String fieldX, final String fieldY, final String fieldZ, final String newField) {
|
| 29 | Optional<Number> x = input.get(fieldX).asNumber().result();
|
| 30 | Optional<Number> y = input.get(fieldY).asNumber().result();
|
| 31 | Optional<Number> z = input.get(fieldZ).asNumber().result();
|
| 32 | return !x.isEmpty() && !y.isEmpty() && !z.isEmpty()
|
| 33 | ? input.remove(fieldX).remove(fieldY).remove(fieldZ).set(newField, createBlockPos(input, x.get().intValue(), y.get().intValue(), z.get().intValue()))
|
| 34 | : input;
|
| 35 | }
|
| 36 |
|
| 37 | public static Dynamic<?> createBlockPos(final Dynamic<?> dynamic, final int x, final int y, final int z) {
|
| 38 | return dynamic.createIntList(IntStream.of(x, y, z));
|
| 39 | }
|
| 40 |
|
| 41 | public static <T, R> Typed<R> cast(final Type<R> type, final Typed<T> typed) {
|
| 42 | return new Typed<>(type, typed.getOps(), (R)typed.getValue());
|
| 43 | }
|
| 44 |
|
| 45 | public static <T> Typed<T> cast(final Type<T> type, final Object value, final DynamicOps<?> ops) {
|
| 46 | return new Typed<>(type, ops, (T)value);
|
| 47 | }
|
| 48 |
|
| 49 | public static Type<?> patchSubType(final Type<?> type, final Type<?> find, final Type<?> replace) {
|
| 50 | return type.all(typePatcher(find, replace), true, false).view().newType();
|
| 51 | }
|
| 52 |
|
| 53 | private static <A, B> TypeRewriteRule typePatcher(final Type<A> inputEntityType, final Type<B> outputEntityType) {
|
| 54 | RewriteResult<A, B> view = RewriteResult.create(View.create("Patcher", inputEntityType, outputEntityType, ops -> a -> {
|
| 55 | throw new UnsupportedOperationException();
|
| 56 | }), new BitSet());
|
| 57 | return TypeRewriteRule.everywhere(TypeRewriteRule.ifSame(inputEntityType, view), PointFreeRule.nop(), true, true);
|
| 58 | }
|
| 59 |
|
| 60 | @SafeVarargs
|
| 61 | public static <T> Function<Typed<?>, Typed<?>> chainAllFilters(final Function<Typed<?>, Typed<?>>... fixers) {
|
| 62 | return typed -> {
|
| 63 | for (Function<Typed<?>, Typed<?>> fixer : fixers) {
|
| 64 | typed = fixer.apply(typed);
|
| 65 | }
|
| 66 |
|
| 67 | return typed;
|
| 68 | };
|
| 69 | }
|
| 70 |
|
| 71 | public static Dynamic<?> fixStringField(final Dynamic<?> dynamic, final String fieldName, final UnaryOperator<String> fix) {
|
| 72 | return dynamic.update(fieldName, field -> DataFixUtils.orElse(field.asString().map(fix).map(dynamic::createString).result(), field));
|
| 73 | }
|
| 74 |
|
| 75 | public static Optional<String> getPlainTranslationKey(final Dynamic<?> textComponent) {
|
| 76 | return textComponent.getMapValues().result().filter(fields -> fields.size() == 1).flatMap(fields -> textComponent.get("translate").asString().result());
|
| 77 | }
|
| 78 |
|
| 79 | public static String dyeColorIdToName(final int id) {
|
| 80 | return switch (id) {
|
| 81 | case 1 -> "orange";
|
| 82 | case 2 -> "magenta";
|
| 83 | case 3 -> "light_blue";
|
| 84 | case 4 -> "yellow";
|
| 85 | case 5 -> "lime";
|
| 86 | case 6 -> "pink";
|
| 87 | case 7 -> "gray";
|
| 88 | case 8 -> "light_gray";
|
| 89 | case 9 -> "cyan";
|
| 90 | case 10 -> "purple";
|
| 91 | case 11 -> "blue";
|
| 92 | case 12 -> "brown";
|
| 93 | case 13 -> "green";
|
| 94 | case 14 -> "red";
|
| 95 | case 15 -> "black";
|
| 96 | default -> "white";
|
| 97 | };
|
| 98 | }
|
| 99 |
|
| 100 | public static <T> Typed<?> readAndSet(final Typed<?> target, final OpticFinder<T> optic, final Dynamic<?> value) {
|
| 101 | return target.set(optic, Util.readTypedOrThrow(optic.type(), value, true));
|
| 102 | }
|
| 103 | }
|
| 104 |
|