| 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.util.Pair;
|
| 11 | import com.mojang.serialization.Dynamic;
|
| 12 | import java.util.Optional;
|
| 13 | import java.util.function.Function;
|
| 14 | import net.minecraft.util.Util;
|
| 15 | import net.minecraft.util.datafix.ExtraDataFixUtils;
|
| 16 | import net.minecraft.util.datafix.schemas.NamespacedSchema;
|
| 17 |
|
| 18 | public class EntitySpawnerItemVariantComponentFix extends DataFix {
|
| 19 | public EntitySpawnerItemVariantComponentFix(final Schema outputSchema) {
|
| 20 | super(outputSchema, false);
|
| 21 | }
|
| 22 |
|
| 23 | @Override
|
| 24 | public final TypeRewriteRule makeRule() {
|
| 25 | Type<?> itemStackType = this.getInputSchema().getType(References.ITEM_STACK);
|
| 26 | OpticFinder<Pair<String, String>> idFinder = DSL.fieldFinder("id", DSL.named(References.ITEM_NAME.typeName(), NamespacedSchema.namespacedString()));
|
| 27 | OpticFinder<?> componentsFinder = itemStackType.findField("components");
|
| 28 | return this.fixTypeEverywhereTyped(
|
| 29 | "ItemStack bucket_entity_data variants to separate components",
|
| 30 | itemStackType,
|
| 31 | input -> {
|
| 32 | String id = input.getOptional(idFinder).map(Pair::getSecond).orElse("");
|
| 33 |
|
| 34 | return switch (id) {
|
| 35 | case "minecraft:salmon_bucket" -> input.updateTyped(componentsFinder, EntitySpawnerItemVariantComponentFix::fixSalmonBucket);
|
| 36 | case "minecraft:axolotl_bucket" -> input.updateTyped(componentsFinder, EntitySpawnerItemVariantComponentFix::fixAxolotlBucket);
|
| 37 | case "minecraft:tropical_fish_bucket" -> input.updateTyped(componentsFinder, EntitySpawnerItemVariantComponentFix::fixTropicalFishBucket);
|
| 38 | case "minecraft:painting" -> input.updateTyped(
|
| 39 | componentsFinder, components -> Util.writeAndReadTypedOrThrow(components, components.getType(), EntitySpawnerItemVariantComponentFix::fixPainting)
|
| 40 | );
|
| 41 | default -> input;
|
| 42 | };
|
| 43 | }
|
| 44 | );
|
| 45 | }
|
| 46 |
|
| 47 | private static String getBaseColor(final int packedVariant) {
|
| 48 | return ExtraDataFixUtils.dyeColorIdToName(packedVariant >> 16 & 0xFF);
|
| 49 | }
|
| 50 |
|
| 51 | private static String getPatternColor(final int packedVariant) {
|
| 52 | return ExtraDataFixUtils.dyeColorIdToName(packedVariant >> 24 & 0xFF);
|
| 53 | }
|
| 54 |
|
| 55 | private static String getPattern(final int packedVariant) {
|
| 56 | return switch (packedVariant & 65535) {
|
| 57 | case 1 -> "flopper";
|
| 58 | case 256 -> "sunstreak";
|
| 59 | case 257 -> "stripey";
|
| 60 | case 512 -> "snooper";
|
| 61 | case 513 -> "glitter";
|
| 62 | case 768 -> "dasher";
|
| 63 | case 769 -> "blockfish";
|
| 64 | case 1024 -> "brinely";
|
| 65 | case 1025 -> "betty";
|
| 66 | case 1280 -> "spotty";
|
| 67 | case 1281 -> "clayfish";
|
| 68 | default -> "kob";
|
| 69 | };
|
| 70 | }
|
| 71 |
|
| 72 | private static <T> Dynamic<T> fixTropicalFishBucket(final Dynamic<T> remainder, final Dynamic<T> bucketData) {
|
| 73 | Optional<Number> oldVariant = bucketData.get("BucketVariantTag").asNumber().result();
|
| 74 | if (oldVariant.isEmpty()) {
|
| 75 | return remainder;
|
| 76 | }
|
| 77 |
|
| 78 | int packedVariant = oldVariant.get().intValue();
|
| 79 | String pattern = getPattern(packedVariant);
|
| 80 | String baseColor = getBaseColor(packedVariant);
|
| 81 | String patternColor = getPatternColor(packedVariant);
|
| 82 | return remainder.update("minecraft:bucket_entity_data", b -> b.remove("BucketVariantTag"))
|
| 83 | .set("minecraft:tropical_fish/pattern", remainder.createString(pattern))
|
| 84 | .set("minecraft:tropical_fish/base_color", remainder.createString(baseColor))
|
| 85 | .set("minecraft:tropical_fish/pattern_color", remainder.createString(patternColor));
|
| 86 | }
|
| 87 |
|
| 88 | private static <T> Dynamic<T> fixAxolotlBucket(final Dynamic<T> remainder, final Dynamic<T> bucketData) {
|
| 89 | Optional<Number> oldVariant = bucketData.get("Variant").asNumber().result();
|
| 90 | if (oldVariant.isEmpty()) {
|
| 91 | return remainder;
|
| 92 | }
|
| 93 |
|
| 94 | String newVariant = switch (oldVariant.get().intValue()) {
|
| 95 | case 1 -> "wild";
|
| 96 | case 2 -> "gold";
|
| 97 | case 3 -> "cyan";
|
| 98 | case 4 -> "blue";
|
| 99 | default -> "lucy";
|
| 100 | };
|
| 101 | return remainder.update("minecraft:bucket_entity_data", b -> b.remove("Variant")).set("minecraft:axolotl/variant", remainder.createString(newVariant));
|
| 102 | }
|
| 103 |
|
| 104 | private static <T> Dynamic<T> fixSalmonBucket(final Dynamic<T> remainder, final Dynamic<T> bucketData) {
|
| 105 | Optional<Dynamic<T>> type = bucketData.get("type").result();
|
| 106 | return type.isEmpty() ? remainder : remainder.update("minecraft:bucket_entity_data", b -> b.remove("type")).set("minecraft:salmon/size", type.get());
|
| 107 | }
|
| 108 |
|
| 109 | private static <T> Dynamic<T> fixPainting(Dynamic<T> components) {
|
| 110 | Optional<Dynamic<T>> entityData = components.get("minecraft:entity_data").result();
|
| 111 | if (entityData.isEmpty()) {
|
| 112 | return components;
|
| 113 | }
|
| 114 |
|
| 115 | if (entityData.get().get("id").asString().result().filter(id -> id.equals("minecraft:painting")).isEmpty()) {
|
| 116 | return components;
|
| 117 | }
|
| 118 |
|
| 119 | Optional<Dynamic<T>> result = entityData.get().get("variant").result();
|
| 120 | Dynamic<T> entityDataRemainder = entityData.get().remove("variant");
|
| 121 | if (entityDataRemainder.remove("id").equals(entityDataRemainder.emptyMap())) {
|
| 122 | components = components.remove("minecraft:entity_data");
|
| 123 | } else {
|
| 124 | components = components.set("minecraft:entity_data", entityDataRemainder);
|
| 125 | }
|
| 126 |
|
| 127 | if (result.isPresent()) {
|
| 128 | components = components.set("minecraft:painting/variant", result.get());
|
| 129 | }
|
| 130 |
|
| 131 | return components;
|
| 132 | }
|
| 133 |
|
| 134 | @FunctionalInterface
|
| 135 | private interface Fixer extends Function<Typed<?>, Typed<?>> {
|
| 136 | default Typed<?> apply(final Typed<?> components) {
|
| 137 | return components.update(DSL.remainderFinder(), this::fixRemainder);
|
| 138 | }
|
| 139 |
|
| 140 | default <T> Dynamic<T> fixRemainder(final Dynamic<T> remainder) {
|
| 141 | return remainder.get("minecraft:bucket_entity_data").result().map(bucketData -> this.fixRemainder(remainder, (Dynamic<T>)bucketData)).orElse(remainder);
|
| 142 | }
|
| 143 |
|
| 144 | <T> Dynamic<T> fixRemainder(Dynamic<T> remainder, Dynamic<T> bucketData);
|
| 145 | }
|
| 146 | }
|
| 147 |
|