| 1 | package net.minecraft.util.datafix.fixes;
|
| 2 |
|
| 3 | import com.google.common.collect.Streams;
|
| 4 | import com.mojang.datafixers.schemas.Schema;
|
| 5 | import com.mojang.serialization.Dynamic;
|
| 6 | import java.util.List;
|
| 7 | import java.util.Optional;
|
| 8 | import java.util.stream.Stream;
|
| 9 | import net.minecraft.util.datafix.LegacyComponentDataFixUtils;
|
| 10 |
|
| 11 | public class BlockEntitySignDoubleSidedEditableTextFix extends NamedEntityWriteReadFix {
|
| 12 | public static final List<String> FIELDS_TO_DROP = List.of(
|
| 13 | "Text1", "Text2", "Text3", "Text4", "FilteredText1", "FilteredText2", "FilteredText3", "FilteredText4", "Color", "GlowingText"
|
| 14 | );
|
| 15 | public static final String FILTERED_CORRECT = "_filtered_correct";
|
| 16 | private static final String DEFAULT_COLOR = "black";
|
| 17 |
|
| 18 | public BlockEntitySignDoubleSidedEditableTextFix(final Schema outputSchema, final String name, final String entityName) {
|
| 19 | super(outputSchema, true, name, References.BLOCK_ENTITY, entityName);
|
| 20 | }
|
| 21 |
|
| 22 | @Override
|
| 23 | protected <T> Dynamic<T> fix(Dynamic<T> input) {
|
| 24 | input = input.set("front_text", fixFrontTextTag(input))
|
| 25 | .set("back_text", createDefaultText(input))
|
| 26 | .set("is_waxed", input.createBoolean(false))
|
| 27 | .set("_filtered_correct", input.createBoolean(true));
|
| 28 |
|
| 29 | for (String field : FIELDS_TO_DROP) {
|
| 30 | input = input.remove(field);
|
| 31 | }
|
| 32 |
|
| 33 | return input;
|
| 34 | }
|
| 35 |
|
| 36 | private static <T> Dynamic<T> fixFrontTextTag(final Dynamic<T> tag) {
|
| 37 | Dynamic<T> emptyLine = LegacyComponentDataFixUtils.createEmptyComponent(tag.getOps());
|
| 38 | List<Dynamic<T>> lines = getLines(tag, "Text").map(line -> line.orElse(emptyLine)).toList();
|
| 39 | Dynamic<T> text = tag.emptyMap()
|
| 40 | .set("messages", tag.createList(lines.stream()))
|
| 41 | .set("color", tag.get("Color").result().orElse(tag.createString("black")))
|
| 42 | .set("has_glowing_text", tag.get("GlowingText").result().orElse(tag.createBoolean(false)));
|
| 43 | List<Optional<Dynamic<T>>> filteredLines = getLines(tag, "FilteredText").toList();
|
| 44 | if (filteredLines.stream().anyMatch(Optional::isPresent)) {
|
| 45 | text = text.set("filtered_messages", tag.createList(Streams.mapWithIndex(filteredLines.stream(), (line, index) -> {
|
| 46 | Dynamic<T> fallbackLine = lines.get((int)index);
|
| 47 | return line.orElse(fallbackLine);
|
| 48 | })));
|
| 49 | }
|
| 50 |
|
| 51 | return text;
|
| 52 | }
|
| 53 |
|
| 54 | private static <T> Stream<Optional<Dynamic<T>>> getLines(final Dynamic<T> tag, final String linePrefix) {
|
| 55 | return Stream.of(
|
| 56 | tag.get(linePrefix + "1").result(), tag.get(linePrefix + "2").result(), tag.get(linePrefix + "3").result(), tag.get(linePrefix + "4").result()
|
| 57 | );
|
| 58 | }
|
| 59 |
|
| 60 | private static <T> Dynamic<T> createDefaultText(final Dynamic<T> tag) {
|
| 61 | return tag.emptyMap().set("messages", createEmptyLines(tag)).set("color", tag.createString("black")).set("has_glowing_text", tag.createBoolean(false));
|
| 62 | }
|
| 63 |
|
| 64 | private static <T> Dynamic<T> createEmptyLines(final Dynamic<T> tag) {
|
| 65 | Dynamic<T> emptyComponent = LegacyComponentDataFixUtils.createEmptyComponent(tag.getOps());
|
| 66 | return tag.createList(Stream.of(emptyComponent, emptyComponent, emptyComponent, emptyComponent));
|
| 67 | }
|
| 68 | }
|
| 69 |
|