| 1 | package net.minecraft.util.datafix.fixes;
|
| 2 |
|
| 3 | import com.google.gson.JsonElement;
|
| 4 | import com.mojang.datafixers.DSL;
|
| 5 | import com.mojang.datafixers.DataFix;
|
| 6 | import com.mojang.datafixers.TypeRewriteRule;
|
| 7 | import com.mojang.datafixers.schemas.Schema;
|
| 8 | import com.mojang.datafixers.types.Type;
|
| 9 | import com.mojang.datafixers.util.Either;
|
| 10 | import com.mojang.datafixers.util.Pair;
|
| 11 | import com.mojang.datafixers.util.Unit;
|
| 12 | import com.mojang.serialization.Dynamic;
|
| 13 | import com.mojang.serialization.JavaOps;
|
| 14 | import com.mojang.serialization.JsonOps;
|
| 15 | import java.util.List;
|
| 16 | import java.util.Map;
|
| 17 | import java.util.Optional;
|
| 18 | import net.minecraft.util.GsonHelper;
|
| 19 | import net.minecraft.util.Util;
|
| 20 |
|
| 21 | public class LegacyHoverEventFix extends DataFix {
|
| 22 | public LegacyHoverEventFix(final Schema outputSchema) {
|
| 23 | super(outputSchema, false);
|
| 24 | }
|
| 25 |
|
| 26 | @Override
|
| 27 | protected TypeRewriteRule makeRule() {
|
| 28 | Type<? extends Pair<String, ?>> hoverEventType = (Type<? extends Pair<String, ?>>)this.getInputSchema()
|
| 29 | .getType(References.TEXT_COMPONENT)
|
| 30 | .findFieldType("hoverEvent");
|
| 31 | return this.createFixer(this.getInputSchema().getTypeRaw(References.TEXT_COMPONENT), hoverEventType);
|
| 32 | }
|
| 33 |
|
| 34 | private <C, H extends Pair<String, ?>> TypeRewriteRule createFixer(final Type<C> rawTextComponentType, final Type<H> hoverEventType) {
|
| 35 | Type<Pair<String, Either<Either<String, List<C>>, Pair<Either<List<C>, Unit>, Pair<Either<C, Unit>, Pair<Either<H, Unit>, Dynamic<?>>>>>>> textComponentType = DSL.named(
|
| 36 | References.TEXT_COMPONENT.typeName(),
|
| 37 | DSL.or(
|
| 38 | DSL.or(DSL.string(), DSL.list(rawTextComponentType)),
|
| 39 | DSL.and(
|
| 40 | DSL.optional(DSL.field("extra", DSL.list(rawTextComponentType))),
|
| 41 | DSL.optional(DSL.field("separator", rawTextComponentType)),
|
| 42 | DSL.optional(DSL.field("hoverEvent", hoverEventType)),
|
| 43 | DSL.remainderType()
|
| 44 | )
|
| 45 | )
|
| 46 | );
|
| 47 | if (!textComponentType.equals(this.getInputSchema().getType(References.TEXT_COMPONENT))) {
|
| 48 | throw new IllegalStateException(
|
| 49 | "Text component type did not match, expected " + textComponentType + " but got " + this.getInputSchema().getType(References.TEXT_COMPONENT)
|
| 50 | );
|
| 51 | } else {
|
| 52 | return this.fixTypeEverywhere(
|
| 53 | "LegacyHoverEventFix",
|
| 54 | textComponentType,
|
| 55 | ops -> named -> named.mapSecond(
|
| 56 | simpleOrFull -> simpleOrFull.mapRight(full -> full.mapSecond(separatorHoverRemainder -> separatorHoverRemainder.mapSecond(hoverAndRemainder -> {
|
| 57 | Dynamic<?> remainder = hoverAndRemainder.getSecond();
|
| 58 | Optional<? extends Dynamic<?>> hoverEvent = remainder.get("hoverEvent").result();
|
| 59 | if (hoverEvent.isEmpty()) {
|
| 60 | return hoverAndRemainder;
|
| 61 | }
|
| 62 |
|
| 63 | Optional<? extends Dynamic<?>> legacyHoverValue = hoverEvent.get().get("value").result();
|
| 64 | if (legacyHoverValue.isEmpty()) {
|
| 65 | return hoverAndRemainder;
|
| 66 | }
|
| 67 |
|
| 68 | String hoverAction = hoverAndRemainder.getFirst().left().map(Pair::getFirst).orElse("");
|
| 69 | H newHoverEvent = this.fixHoverEvent(hoverEventType, hoverAction, (Dynamic<?>)hoverEvent.get());
|
| 70 | return hoverAndRemainder.mapFirst(ignored -> Either.left(newHoverEvent));
|
| 71 | })))
|
| 72 | )
|
| 73 | );
|
| 74 | }
|
| 75 | }
|
| 76 |
|
| 77 | private <H> H fixHoverEvent(final Type<H> hoverEventType, final String action, final Dynamic<?> oldHoverEvent) {
|
| 78 | return "show_text".equals(action) ? fixShowTextHover(hoverEventType, oldHoverEvent) : createPlaceholderHover(hoverEventType, oldHoverEvent);
|
| 79 | }
|
| 80 |
|
| 81 | private static <H> H fixShowTextHover(final Type<H> hoverEventType, final Dynamic<?> oldHoverEvent) {
|
| 82 | Dynamic<?> newHoverEvent = oldHoverEvent.renameField("value", "contents");
|
| 83 | return Util.readTypedOrThrow(hoverEventType, newHoverEvent).getValue();
|
| 84 | }
|
| 85 |
|
| 86 | private static <H> H createPlaceholderHover(final Type<H> hoverEventType, final Dynamic<?> oldHoverEvent) {
|
| 87 | JsonElement oldJson = oldHoverEvent.convert(JsonOps.INSTANCE).getValue();
|
| 88 | Dynamic<?> placeholderHoverEvent = new Dynamic<>(
|
| 89 | JavaOps.INSTANCE, Map.of("action", "show_text", "contents", Map.<String, String>of("text", "Legacy hoverEvent: " + GsonHelper.toStableString(oldJson)))
|
| 90 | );
|
| 91 | return Util.readTypedOrThrow(hoverEventType, placeholderHoverEvent).getValue();
|
| 92 | }
|
| 93 | }
|
| 94 |
|