| 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.schemas.Schema;
|
| 8 | import com.mojang.datafixers.types.Type;
|
| 9 | import com.mojang.datafixers.util.Pair;
|
| 10 | import net.minecraft.util.datafix.LegacyComponentDataFixUtils;
|
| 11 |
|
| 12 | public class ItemLoreFix extends DataFix {
|
| 13 | public ItemLoreFix(final Schema outputSchema) {
|
| 14 | super(outputSchema, false);
|
| 15 | }
|
| 16 |
|
| 17 | @Override
|
| 18 | protected TypeRewriteRule makeRule() {
|
| 19 | Type<?> itemStackType = this.getInputSchema().getType(References.ITEM_STACK);
|
| 20 | Type<Pair<String, String>> textComponentType = (Type<Pair<String, String>>)this.getInputSchema().getType(References.TEXT_COMPONENT);
|
| 21 | OpticFinder<?> tagFinder = itemStackType.findField("tag");
|
| 22 | OpticFinder<?> displayFinder = tagFinder.type().findField("display");
|
| 23 | OpticFinder<?> loreFinder = displayFinder.type().findField("Lore");
|
| 24 | OpticFinder<Pair<String, String>> textComponentFinder = DSL.typeFinder(textComponentType);
|
| 25 | return this.fixTypeEverywhereTyped(
|
| 26 | "Item Lore componentize",
|
| 27 | itemStackType,
|
| 28 | itemStack -> itemStack.updateTyped(
|
| 29 | tagFinder,
|
| 30 | tag -> tag.updateTyped(
|
| 31 | displayFinder,
|
| 32 | display -> display.updateTyped(
|
| 33 | loreFinder, lore -> lore.update(textComponentFinder, textComponent -> textComponent.mapSecond(LegacyComponentDataFixUtils::createTextComponentJson))
|
| 34 | )
|
| 35 | )
|
| 36 | )
|
| 37 | );
|
| 38 | }
|
| 39 | }
|
| 40 |
|