| 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 |
|