DropInvalidSignDataFix .java

net.minecraft.util.datafix.fixes.DropInvalidSignDataFix (class): members: entityName, makeRule. strings: "DropInvalidSignDataFix for ", "front_text".

net.minecraft.util.datafix.fixesclass72 linescommonextends DataFix2.7 KB

API Surface (1)

TypeRewriteRule makeRule()

Fields & Constants (1)

entityName

String literals (6)

"front_text"
"back_text"
"filtered_messages"
"messages"
"DropInvalidSignDataFix for "
"_filtered_correct"

Code Graph: Dependencies (2)

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

Full Source

DropInvalidSignDataFix.java72 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.util.datafix.fixes;
2
3
import com.google.common.collect.Streams;
4
import com.mojang.datafixers.DSL;
5
import com.mojang.datafixers.DataFix;
6
import com.mojang.datafixers.OpticFinder;
7
import com.mojang.datafixers.TypeRewriteRule;
8
import com.mojang.datafixers.schemas.Schema;
9
import com.mojang.datafixers.types.Type;
10
import com.mojang.serialization.Dynamic;
11
import java.util.List;
12
import java.util.Optional;
13
import java.util.stream.Stream;
14
import net.minecraft.util.Util;
15
import net.minecraft.util.datafix.LegacyComponentDataFixUtils;
16
17
public class DropInvalidSignDataFix extends DataFix {
18
	private final String entityName;
19
20
	public DropInvalidSignDataFix(final Schema outputSchema, final String entityName) {
21
		super(outputSchema, false);
22
		this.entityName = entityName;
23
	}
24
25
	private <T> Dynamic<T> fix(Dynamic<T> tag) {
26
		tag = tag.update("front_text", DropInvalidSignDataFix::fixText);
27
		tag = tag.update("back_text", DropInvalidSignDataFix::fixText);
28
29
		for (String field : BlockEntitySignDoubleSidedEditableTextFix.FIELDS_TO_DROP) {
30
			tag = tag.remove(field);
31
		}
32
33
		return tag;
34
	}
35
36
	private static <T> Dynamic<T> fixText(final Dynamic<T> tag) {
37
		Optional<Stream<Dynamic<T>>> filteredLines = tag.get("filtered_messages").asStreamOpt().result();
38
		if (filteredLines.isEmpty()) {
39
			return tag;
40
		}
41
42
		Dynamic<T> emptyComponent = LegacyComponentDataFixUtils.createEmptyComponent(tag.getOps());
43
		List<Dynamic<T>> lines = tag.get("messages").asStreamOpt().result().orElse(Stream.of()).toList();
44
		List<Dynamic<T>> newFilteredLines = Streams.mapWithIndex(filteredLines.get(), (line, index) -> {
45
			Dynamic<T> fallbackLine = index < lines.size() ? lines.get((int)index) : emptyComponent;
46
			return line.equals(emptyComponent) ? fallbackLine : line;
47
		}).toList();
48
		return newFilteredLines.equals(lines) ? tag.remove("filtered_messages") : tag.set("filtered_messages", tag.createList(newFilteredLines.stream()));
49
	}
50
51
	@Override
52
	public TypeRewriteRule makeRule() {
53
		Type<?> entityType = this.getInputSchema().getType(References.BLOCK_ENTITY);
54
		Type<?> entityChoiceType = this.getInputSchema().getChoiceType(References.BLOCK_ENTITY, this.entityName);
55
		OpticFinder<?> entityF = DSL.namedChoice(this.entityName, entityChoiceType);
56
		return this.fixTypeEverywhereTyped(
57
			"DropInvalidSignDataFix for " + this.entityName,
58
			entityType,
59
			input -> input.updateTyped(
60
				entityF,
61
				entityChoiceType,
62
				entity -> {
63
					boolean filteredCorrect = entity.get(DSL.remainderFinder()).get("_filtered_correct").asBoolean(false);
64
					return filteredCorrect
65
						? entity.update(DSL.remainderFinder(), remainder -> remainder.remove("_filtered_correct"))
66
						: Util.writeAndReadTypedOrThrow(entity, entityChoiceType, this::fix);
67
				}
68
			)
69
		);
70
	}
71
}
72

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