TextComponentHoverAndClickEventFix .java

net.minecraft.util.datafix.fixes.TextComponentHoverAndClickEventFix (class, 169 lines): members: newTextComponentType, C2, makeRule. strings: "clickEvent".

net.minecraft.util.datafix.fixesclass169 linescommonextends DataFix6.0 KB

API Surface (1)

TypeRewriteRule makeRule()

Fields & Constants (2)

C2
newTextComponentType

String literals (26)

"hoverEvent"
"extra"
"separator"
"Text component type did not match, expected "
" but got "
"clickEvent"
"hover_event"
"click_event"
"action"
"show_text"
"contents"
"show_item"
"count"
"components"
"show_entity"
"uuid"
"open_url"
"open_file"
"path"
"run_command"
"suggest_command"
"command"
"change_page"
"page"

Code Graph: Dependencies (2)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

TextComponentHoverAndClickEventFix.java169 lines · Minecraft 26.3 · verbatim
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.TypeRewriteRule;
6
import com.mojang.datafixers.schemas.Schema;
7
import com.mojang.datafixers.types.Type;
8
import com.mojang.datafixers.util.Either;
9
import com.mojang.datafixers.util.Pair;
10
import com.mojang.datafixers.util.Unit;
11
import com.mojang.serialization.Dynamic;
12
import java.net.URI;
13
import java.net.URISyntaxException;
14
import java.util.List;
15
import java.util.Locale;
16
import java.util.Optional;
17
import net.minecraft.util.Util;
18
import net.minecraft.util.datafix.ExtraDataFixUtils;
19
import org.jspecify.annotations.Nullable;
20
21
public class TextComponentHoverAndClickEventFix extends DataFix {
22
	public TextComponentHoverAndClickEventFix(final Schema outputSchema) {
23
		super(outputSchema, true);
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(
32
			this.getInputSchema().getTypeRaw(References.TEXT_COMPONENT), this.getOutputSchema().getType(References.TEXT_COMPONENT), hoverEventType
33
		);
34
	}
35
36
	private <C1, C2, H extends Pair<String, ?>> TypeRewriteRule createFixer(
37
		final Type<C1> oldRawTextComponentType, final Type<C2> newTextComponentType, final Type<H> hoverEventType
38
	) {
39
		Type<Pair<String, Either<Either<String, List<C1>>, Pair<Either<List<C1>, Unit>, Pair<Either<C1, Unit>, Pair<Either<H, Unit>, Dynamic<?>>>>>>> oldTextComponentType = DSL.named(
40
			References.TEXT_COMPONENT.typeName(),
41
			DSL.or(
42
				DSL.or(DSL.string(), DSL.list(oldRawTextComponentType)),
43
				DSL.and(
44
					DSL.optional(DSL.field("extra", DSL.list(oldRawTextComponentType))),
45
					DSL.optional(DSL.field("separator", oldRawTextComponentType)),
46
					DSL.optional(DSL.field("hoverEvent", hoverEventType)),
47
					DSL.remainderType()
48
				)
49
			)
50
		);
51
		if (!oldTextComponentType.equals(this.getInputSchema().getType(References.TEXT_COMPONENT))) {
52
			throw new IllegalStateException(
53
				"Text component type did not match, expected " + oldTextComponentType + " but got " + this.getInputSchema().getType(References.TEXT_COMPONENT)
54
			);
55
		}
56
57
		Type<?> patchedInputType = ExtraDataFixUtils.patchSubType(oldTextComponentType, oldTextComponentType, newTextComponentType);
58
		return this.fixTypeEverywhere(
59
			"TextComponentHoverAndClickEventFix",
60
			oldTextComponentType,
61
			newTextComponentType,
62
			ops -> textComponent -> {
63
				boolean hasHoverOrClick = textComponent.getSecond().map(simple -> false, full -> {
64
					Pair<Either<H, Unit>, Dynamic<?>> hoverAndRemainder = full.getSecond().getSecond();
65
					boolean hasHover = hoverAndRemainder.getFirst().left().isPresent();
66
					boolean hasClick = hoverAndRemainder.getSecond().get("clickEvent").result().isPresent();
67
					return hasHover || hasClick;
68
				});
69
				return (C2)(!hasHoverOrClick
70
					? textComponent
71
					: Util.writeAndReadTypedOrThrow(
72
							ExtraDataFixUtils.cast(patchedInputType, textComponent, ops), newTextComponentType, TextComponentHoverAndClickEventFix::fixTextComponent
73
						)
74
						.getValue());
75
			}
76
		);
77
	}
78
79
	private static Dynamic<?> fixTextComponent(final Dynamic<?> dynamic) {
80
		return dynamic.renameAndFixField("hoverEvent", "hover_event", TextComponentHoverAndClickEventFix::fixHoverEvent)
81
			.renameAndFixField("clickEvent", "click_event", TextComponentHoverAndClickEventFix::fixClickEvent);
82
	}
83
84
	private static Dynamic<?> copyFields(Dynamic<?> target, final Dynamic<?> source, final String... fields) {
85
		for (String field : fields) {
86
			target = Dynamic.copyField(source, field, target, field);
87
		}
88
89
		return target;
90
	}
91
92
	private static Dynamic<?> fixHoverEvent(final Dynamic<?> dynamic) {
93
		String action = dynamic.get("action").asString("");
94
95
		return switch (action) {
96
			case "show_text" -> dynamic.renameField("contents", "value");
97
			case "show_item" -> {
98
				Dynamic<?> contents = dynamic.get("contents").orElseEmptyMap();
99
				yield copyFields(dynamic.remove("contents"), contents, "id", "count", "components");
100
			}
101
			case "show_entity" -> {
102
				Dynamic<?> contents = dynamic.get("contents").orElseEmptyMap();
103
				yield copyFields(dynamic.remove("contents"), contents, "id", "type", "name").renameField("id", "uuid").renameField("type", "id");
104
			}
105
			default -> dynamic;
106
		};
107
	}
108
109
	private static <T> @Nullable Dynamic<T> fixClickEvent(final Dynamic<T> dynamic) {
110
		String action = dynamic.get("action").asString("");
111
		String value = dynamic.get("value").asString("");
112
113
		return switch (action) {
114
			case "open_url" -> !validateUri(value) ? null : dynamic.renameField("value", "url");
115
			case "open_file" -> dynamic.renameField("value", "path");
116
			case "run_command", "suggest_command" -> !validateChat(value) ? null : dynamic.renameField("value", "command");
117
			case "change_page" -> {
118
				Integer oldPage = dynamic.get("value").result().map(TextComponentHoverAndClickEventFix::parseOldPage).orElse(null);
119
				if (oldPage == null) {
120
					yield null;
121
				} else {
122
					int page = Math.max(oldPage, 1);
123
					yield dynamic.remove("value").set("page", dynamic.createInt(page));
124
				}
125
			}
126
			default -> dynamic;
127
		};
128
	}
129
130
	private static @Nullable Integer parseOldPage(final Dynamic<?> value) {
131
		Optional<Number> numberValue = value.asNumber().result();
132
		if (numberValue.isPresent()) {
133
			return numberValue.get().intValue();
134
		}
135
136
		try {
137
			return Integer.parseInt(value.asString(""));
138
		} catch (Exception ignored) {
139
			return null;
140
		}
141
	}
142
143
	private static boolean validateUri(final String uri) {
144
		try {
145
			URI parsedUri = new URI(uri);
146
			String scheme = parsedUri.getScheme();
147
			if (scheme == null) {
148
				return false;
149
			}
150
151
			String protocol = scheme.toLowerCase(Locale.ROOT);
152
			return "http".equals(protocol) || "https".equals(protocol);
153
		} catch (URISyntaxException e) {
154
			return false;
155
		}
156
	}
157
158
	private static boolean validateChat(final String string) {
159
		for (int i = 0; i < string.length(); i++) {
160
			char c = string.charAt(i);
161
			if (c == 167 || c < ' ' || c == 127) {
162
				return false;
163
			}
164
		}
165
166
		return true;
167
	}
168
}
169

Decompiled Minecraft 26.3 source of TextComponentHoverAndClickEventFix (net/minecraft/util/datafix/fixes/TextComponentHoverAndClickEventFix.java) with Mojang official mappings, stored verbatim on the site.