| 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.OpticFinder;
|
| 6 | import com.mojang.datafixers.TypeRewriteRule;
|
| 7 | import com.mojang.datafixers.Typed;
|
| 8 | import com.mojang.datafixers.schemas.Schema;
|
| 9 | import com.mojang.datafixers.types.Type;
|
| 10 | import com.mojang.datafixers.types.templates.TaggedChoice.TaggedChoiceType;
|
| 11 | import com.mojang.datafixers.util.Pair;
|
| 12 | import com.mojang.serialization.Dynamic;
|
| 13 | import java.util.Map;
|
| 14 | import java.util.Optional;
|
| 15 | import net.minecraft.util.Util;
|
| 16 | import net.minecraft.util.datafix.LegacyComponentDataFixUtils;
|
| 17 |
|
| 18 | public class BannerEntityCustomNameToOverrideComponentFix extends DataFix {
|
| 19 | public BannerEntityCustomNameToOverrideComponentFix(final Schema outputSchema) {
|
| 20 | super(outputSchema, false);
|
| 21 | }
|
| 22 |
|
| 23 | @Override
|
| 24 | public TypeRewriteRule makeRule() {
|
| 25 | Type<?> blockEntityType = this.getInputSchema().getType(References.BLOCK_ENTITY);
|
| 26 | TaggedChoiceType<?> blockEntityIdFinder = this.getInputSchema().findChoiceType(References.BLOCK_ENTITY);
|
| 27 | OpticFinder<?> customNameFinder = blockEntityType.findField("CustomName");
|
| 28 | OpticFinder<Pair<String, String>> textComponentFinder = DSL.typeFinder((Type<Pair<String, String>>)this.getInputSchema().getType(References.TEXT_COMPONENT));
|
| 29 | return this.fixTypeEverywhereTyped("Banner entity custom_name to item_name component fix", blockEntityType, input -> {
|
| 30 | Object blockEntityId = input.get(blockEntityIdFinder.finder()).getFirst();
|
| 31 | return blockEntityId.equals("minecraft:banner") ? this.fix(input, textComponentFinder, customNameFinder) : input;
|
| 32 | });
|
| 33 | }
|
| 34 |
|
| 35 | private Typed<?> fix(final Typed<?> input, final OpticFinder<Pair<String, String>> textComponentFinder, final OpticFinder<?> customNameFinder) {
|
| 36 | Optional<String> customName = input.getOptionalTyped(customNameFinder).flatMap(name -> name.getOptional(textComponentFinder).map(Pair::getSecond));
|
| 37 | boolean isOminousBanner = customName.flatMap(LegacyComponentDataFixUtils::extractTranslationString)
|
| 38 | .filter(e -> e.equals("block.minecraft.ominous_banner"))
|
| 39 | .isPresent();
|
| 40 | return isOminousBanner
|
| 41 | ? Util.writeAndReadTypedOrThrow(
|
| 42 | input,
|
| 43 | input.getType(),
|
| 44 | dynamic -> {
|
| 45 | Dynamic<?> components = dynamic.createMap(
|
| 46 | Map.of(
|
| 47 | dynamic.createString("minecraft:item_name"),
|
| 48 | dynamic.createString(customName.get()),
|
| 49 | dynamic.createString("minecraft:hide_additional_tooltip"),
|
| 50 | dynamic.emptyMap()
|
| 51 | )
|
| 52 | );
|
| 53 | return dynamic.set("components", components).remove("CustomName");
|
| 54 | }
|
| 55 | )
|
| 56 | : input;
|
| 57 | }
|
| 58 | }
|
| 59 |
|