| 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 net.minecraft.util.datafix.LegacyComponentDataFixUtils;
|
| 13 | import net.minecraft.util.datafix.schemas.NamespacedSchema;
|
| 14 |
|
| 15 | public class OminousBannerRarityFix extends DataFix {
|
| 16 | public OminousBannerRarityFix(final Schema outputSchema) {
|
| 17 | super(outputSchema, false);
|
| 18 | }
|
| 19 |
|
| 20 | @Override
|
| 21 | public TypeRewriteRule makeRule() {
|
| 22 | Type<?> blockEntityType = this.getInputSchema().getType(References.BLOCK_ENTITY);
|
| 23 | Type<?> itemStackType = this.getInputSchema().getType(References.ITEM_STACK);
|
| 24 | TaggedChoiceType<?> blockEntityIdFinder = this.getInputSchema().findChoiceType(References.BLOCK_ENTITY);
|
| 25 | OpticFinder<Pair<String, String>> itemStackIdFinder = DSL.fieldFinder("id", DSL.named(References.ITEM_NAME.typeName(), NamespacedSchema.namespacedString()));
|
| 26 | OpticFinder<?> blockEntityComponentsFieldFinder = blockEntityType.findField("components");
|
| 27 | OpticFinder<?> itemStackComponentsFieldFinder = itemStackType.findField("components");
|
| 28 | OpticFinder<?> itemNameFinder = blockEntityComponentsFieldFinder.type().findField("minecraft:item_name");
|
| 29 | OpticFinder<Pair<String, String>> textComponentFinder = DSL.typeFinder((Type<Pair<String, String>>)this.getInputSchema().getType(References.TEXT_COMPONENT));
|
| 30 | return TypeRewriteRule.seq(this.fixTypeEverywhereTyped("Ominous Banner block entity common rarity to uncommon rarity fix", blockEntityType, input -> {
|
| 31 | Object blockEntityId = input.get(blockEntityIdFinder.finder()).getFirst();
|
| 32 | return blockEntityId.equals("minecraft:banner") ? this.fix(input, blockEntityComponentsFieldFinder, itemNameFinder, textComponentFinder) : input;
|
| 33 | }), this.fixTypeEverywhereTyped("Ominous Banner item stack common rarity to uncommon rarity fix", itemStackType, input -> {
|
| 34 | String itemStackId = input.getOptional(itemStackIdFinder).map(Pair::getSecond).orElse("");
|
| 35 | return itemStackId.equals("minecraft:white_banner") ? this.fix(input, itemStackComponentsFieldFinder, itemNameFinder, textComponentFinder) : input;
|
| 36 | }));
|
| 37 | }
|
| 38 |
|
| 39 | private Typed<?> fix(
|
| 40 | final Typed<?> input,
|
| 41 | final OpticFinder<?> componentsFieldFinder,
|
| 42 | final OpticFinder<?> itemNameFinder,
|
| 43 | final OpticFinder<Pair<String, String>> textComponentFinder
|
| 44 | ) {
|
| 45 | return input.updateTyped(
|
| 46 | componentsFieldFinder,
|
| 47 | components -> {
|
| 48 | boolean isOminousBanner = components.getOptionalTyped(itemNameFinder)
|
| 49 | .flatMap(itemName -> itemName.getOptional(textComponentFinder))
|
| 50 | .map(Pair::getSecond)
|
| 51 | .flatMap(LegacyComponentDataFixUtils::extractTranslationString)
|
| 52 | .filter(e -> e.equals("block.minecraft.ominous_banner"))
|
| 53 | .isPresent();
|
| 54 | return isOminousBanner
|
| 55 | ? components.updateTyped(
|
| 56 | itemNameFinder,
|
| 57 | itemName -> itemName.set(
|
| 58 | textComponentFinder,
|
| 59 | Pair.of(References.TEXT_COMPONENT.typeName(), LegacyComponentDataFixUtils.createTranslatableComponentJson("block.minecraft.ominous_banner"))
|
| 60 | )
|
| 61 | )
|
| 62 | .update(DSL.remainderFinder(), remainder -> remainder.set("minecraft:rarity", remainder.createString("uncommon")))
|
| 63 | : components;
|
| 64 | }
|
| 65 | );
|
| 66 | }
|
| 67 | }
|
| 68 |
|