| 1 | package net.minecraft.util.datafix.fixes;
|
| 2 |
|
| 3 | import com.mojang.datafixers.DSL;
|
| 4 | import com.mojang.datafixers.DataFix;
|
| 5 | import com.mojang.datafixers.Typed;
|
| 6 | import com.mojang.datafixers.DSL.TypeReference;
|
| 7 | import com.mojang.datafixers.schemas.Schema;
|
| 8 | import com.mojang.datafixers.types.Type;
|
| 9 | import com.mojang.serialization.Dynamic;
|
| 10 | import java.util.Arrays;
|
| 11 | import java.util.Optional;
|
| 12 | import java.util.UUID;
|
| 13 | import java.util.function.Function;
|
| 14 |
|
| 15 | public abstract class AbstractUUIDFix extends DataFix {
|
| 16 | protected final TypeReference typeReference;
|
| 17 |
|
| 18 | public AbstractUUIDFix(final Schema outputSchema, final TypeReference typeReference) {
|
| 19 | super(outputSchema, false);
|
| 20 | this.typeReference = typeReference;
|
| 21 | }
|
| 22 |
|
| 23 | protected Typed<?> updateNamedChoice(final Typed<?> input, final String name, final Function<Dynamic<?>, Dynamic<?>> function) {
|
| 24 | Type<?> oldType = this.getInputSchema().getChoiceType(this.typeReference, name);
|
| 25 | Type<?> newType = this.getOutputSchema().getChoiceType(this.typeReference, name);
|
| 26 | return input.updateTyped(DSL.namedChoice(name, oldType), newType, typedTag -> typedTag.update(DSL.remainderFinder(), function));
|
| 27 | }
|
| 28 |
|
| 29 | protected static Optional<Dynamic<?>> replaceUUIDString(final Dynamic<?> tag, final String oldKey, final String newKey) {
|
| 30 | return createUUIDFromString(tag, oldKey).map(uuidTag -> tag.remove(oldKey).set(newKey, (Dynamic<?>)uuidTag));
|
| 31 | }
|
| 32 |
|
| 33 | protected static Optional<Dynamic<?>> replaceUUIDMLTag(final Dynamic<?> tag, final String oldKey, final String newKey) {
|
| 34 | return tag.get(oldKey).result().flatMap(AbstractUUIDFix::createUUIDFromML).map(uuidTag -> tag.remove(oldKey).set(newKey, (Dynamic<?>)uuidTag));
|
| 35 | }
|
| 36 |
|
| 37 | protected static Optional<Dynamic<?>> replaceUUIDLeastMost(final Dynamic<?> tag, final String oldKey, final String newKey) {
|
| 38 | String mostKey = oldKey + "Most";
|
| 39 | String leastKey = oldKey + "Least";
|
| 40 | return createUUIDFromLongs(tag, mostKey, leastKey).map(uuidTag -> tag.remove(mostKey).remove(leastKey).set(newKey, (Dynamic<?>)uuidTag));
|
| 41 | }
|
| 42 |
|
| 43 | protected static Optional<Dynamic<?>> createUUIDFromString(final Dynamic<?> tag, final String oldKey) {
|
| 44 | return tag.get(oldKey).result().flatMap(uuidStringTag -> {
|
| 45 | String uuidString = uuidStringTag.asString(null);
|
| 46 | if (uuidString != null) {
|
| 47 | try {
|
| 48 | UUID uuid = UUID.fromString(uuidString);
|
| 49 | return createUUIDTag(tag, uuid.getMostSignificantBits(), uuid.getLeastSignificantBits());
|
| 50 | } catch (IllegalArgumentException var4) {
|
| 51 | }
|
| 52 | }
|
| 53 |
|
| 54 | return Optional.empty();
|
| 55 | });
|
| 56 | }
|
| 57 |
|
| 58 | protected static Optional<Dynamic<?>> createUUIDFromML(final Dynamic<?> tag) {
|
| 59 | return createUUIDFromLongs(tag, "M", "L");
|
| 60 | }
|
| 61 |
|
| 62 | protected static Optional<Dynamic<?>> createUUIDFromLongs(final Dynamic<?> tag, final String mostKey, final String leastKey) {
|
| 63 | long mostSignificantBits = tag.get(mostKey).asLong(0L);
|
| 64 | long leastSignificantBits = tag.get(leastKey).asLong(0L);
|
| 65 | return mostSignificantBits != 0L && leastSignificantBits != 0L ? createUUIDTag(tag, mostSignificantBits, leastSignificantBits) : Optional.empty();
|
| 66 | }
|
| 67 |
|
| 68 | protected static Optional<Dynamic<?>> createUUIDTag(final Dynamic<?> tag, final long mostSignificantBits, final long leastSignificantBits) {
|
| 69 | return Optional.of(
|
| 70 | tag.createIntList(
|
| 71 | Arrays.stream(new int[]{(int)(mostSignificantBits >> 32), (int)mostSignificantBits, (int)(leastSignificantBits >> 32), (int)leastSignificantBits})
|
| 72 | )
|
| 73 | );
|
| 74 | }
|
| 75 | }
|
| 76 |
|