| 1 | package net.minecraft.util.datafix.fixes;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableMap;
|
| 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.Typed;
|
| 9 | import com.mojang.datafixers.schemas.Schema;
|
| 10 | import com.mojang.datafixers.types.Type;
|
| 11 | import com.mojang.datafixers.util.Pair;
|
| 12 | import com.mojang.serialization.Dynamic;
|
| 13 | import java.util.Objects;
|
| 14 | import java.util.Optional;
|
| 15 | import java.util.stream.Stream;
|
| 16 | import net.minecraft.util.datafix.schemas.NamespacedSchema;
|
| 17 |
|
| 18 | public class ItemBannerColorFix extends DataFix {
|
| 19 | public ItemBannerColorFix(final Schema outputSchema, final boolean changesType) {
|
| 20 | super(outputSchema, changesType);
|
| 21 | }
|
| 22 |
|
| 23 | @Override
|
| 24 | public TypeRewriteRule makeRule() {
|
| 25 | Type<?> itemStackType = this.getInputSchema().getType(References.ITEM_STACK);
|
| 26 | OpticFinder<Pair<String, String>> idF = DSL.fieldFinder("id", DSL.named(References.ITEM_NAME.typeName(), NamespacedSchema.namespacedString()));
|
| 27 | OpticFinder<?> tagF = itemStackType.findField("tag");
|
| 28 | OpticFinder<?> blockEntityF = tagF.type().findField("BlockEntityTag");
|
| 29 | return this.fixTypeEverywhereTyped("ItemBannerColorFix", itemStackType, input -> {
|
| 30 | Optional<Pair<String, String>> id = input.getOptional(idF);
|
| 31 | if (id.isPresent() && Objects.equals(id.get().getSecond(), "minecraft:banner")) {
|
| 32 | Dynamic<?> rest = input.get(DSL.remainderFinder());
|
| 33 | Optional<? extends Typed<?>> tagOpt = input.getOptionalTyped(tagF);
|
| 34 | if (tagOpt.isPresent()) {
|
| 35 | Typed<?> tag = (Typed<?>)tagOpt.get();
|
| 36 | Optional<? extends Typed<?>> blockEntityOpt = tag.getOptionalTyped(blockEntityF);
|
| 37 | if (blockEntityOpt.isPresent()) {
|
| 38 | Typed<?> blockEntity = (Typed<?>)blockEntityOpt.get();
|
| 39 | Dynamic<?> tagRest = tag.get(DSL.remainderFinder());
|
| 40 | Dynamic<?> blockEntityRest = blockEntity.getOrCreate(DSL.remainderFinder());
|
| 41 | if (blockEntityRest.get("Base").asNumber().result().isPresent()) {
|
| 42 | rest = rest.set("Damage", rest.createShort((short)(blockEntityRest.get("Base").asInt(0) & 15)));
|
| 43 | Optional<? extends Dynamic<?>> displayOptional = tagRest.get("display").result();
|
| 44 | if (displayOptional.isPresent()) {
|
| 45 | Dynamic<?> display = (Dynamic<?>)displayOptional.get();
|
| 46 | Dynamic<?> pickMarker = display.createMap(ImmutableMap.of(display.createString("Lore"), display.createList(Stream.of(display.createString("(+NBT")))));
|
| 47 | if (Objects.equals(display, pickMarker)) {
|
| 48 | return input.set(DSL.remainderFinder(), rest);
|
| 49 | }
|
| 50 | }
|
| 51 |
|
| 52 | blockEntityRest.remove("Base");
|
| 53 | return input.set(DSL.remainderFinder(), rest).set(tagF, tag.set(blockEntityF, blockEntity.set(DSL.remainderFinder(), blockEntityRest)));
|
| 54 | }
|
| 55 | }
|
| 56 | }
|
| 57 |
|
| 58 | return input.set(DSL.remainderFinder(), rest);
|
| 59 | } else {
|
| 60 | return input;
|
| 61 | }
|
| 62 | });
|
| 63 | }
|
| 64 | }
|
| 65 |
|