| 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.DataFixUtils;
|
| 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 net.minecraft.util.datafix.ExtraDataFixUtils;
|
| 16 | import net.minecraft.util.datafix.schemas.NamespacedSchema;
|
| 17 | import org.jspecify.annotations.Nullable;
|
| 18 |
|
| 19 | public class ItemSpawnEggFix extends DataFix {
|
| 20 | private static final @Nullable String[] ID_TO_ENTITY = DataFixUtils.make(new String[256], map -> {
|
| 21 | map[1] = "Item";
|
| 22 | map[2] = "XPOrb";
|
| 23 | map[7] = "ThrownEgg";
|
| 24 | map[8] = "LeashKnot";
|
| 25 | map[9] = "Painting";
|
| 26 | map[10] = "Arrow";
|
| 27 | map[11] = "Snowball";
|
| 28 | map[12] = "Fireball";
|
| 29 | map[13] = "SmallFireball";
|
| 30 | map[14] = "ThrownEnderpearl";
|
| 31 | map[15] = "EyeOfEnderSignal";
|
| 32 | map[16] = "ThrownPotion";
|
| 33 | map[17] = "ThrownExpBottle";
|
| 34 | map[18] = "ItemFrame";
|
| 35 | map[19] = "WitherSkull";
|
| 36 | map[20] = "PrimedTnt";
|
| 37 | map[21] = "FallingSand";
|
| 38 | map[22] = "FireworksRocketEntity";
|
| 39 | map[23] = "TippedArrow";
|
| 40 | map[24] = "SpectralArrow";
|
| 41 | map[25] = "ShulkerBullet";
|
| 42 | map[26] = "DragonFireball";
|
| 43 | map[30] = "ArmorStand";
|
| 44 | map[41] = "Boat";
|
| 45 | map[42] = "MinecartRideable";
|
| 46 | map[43] = "MinecartChest";
|
| 47 | map[44] = "MinecartFurnace";
|
| 48 | map[45] = "MinecartTNT";
|
| 49 | map[46] = "MinecartHopper";
|
| 50 | map[47] = "MinecartSpawner";
|
| 51 | map[40] = "MinecartCommandBlock";
|
| 52 | map[50] = "Creeper";
|
| 53 | map[51] = "Skeleton";
|
| 54 | map[52] = "Spider";
|
| 55 | map[53] = "Giant";
|
| 56 | map[54] = "Zombie";
|
| 57 | map[55] = "Slime";
|
| 58 | map[56] = "Ghast";
|
| 59 | map[57] = "PigZombie";
|
| 60 | map[58] = "Enderman";
|
| 61 | map[59] = "CaveSpider";
|
| 62 | map[60] = "Silverfish";
|
| 63 | map[61] = "Blaze";
|
| 64 | map[62] = "LavaSlime";
|
| 65 | map[63] = "EnderDragon";
|
| 66 | map[64] = "WitherBoss";
|
| 67 | map[65] = "Bat";
|
| 68 | map[66] = "Witch";
|
| 69 | map[67] = "Endermite";
|
| 70 | map[68] = "Guardian";
|
| 71 | map[69] = "Shulker";
|
| 72 | map[90] = "Pig";
|
| 73 | map[91] = "Sheep";
|
| 74 | map[92] = "Cow";
|
| 75 | map[93] = "Chicken";
|
| 76 | map[94] = "Squid";
|
| 77 | map[95] = "Wolf";
|
| 78 | map[96] = "MushroomCow";
|
| 79 | map[97] = "SnowMan";
|
| 80 | map[98] = "Ozelot";
|
| 81 | map[99] = "VillagerGolem";
|
| 82 | map[100] = "EntityHorse";
|
| 83 | map[101] = "Rabbit";
|
| 84 | map[120] = "Villager";
|
| 85 | map[200] = "EnderCrystal";
|
| 86 | });
|
| 87 |
|
| 88 | public ItemSpawnEggFix(final Schema outputSchema, final boolean changesType) {
|
| 89 | super(outputSchema, changesType);
|
| 90 | }
|
| 91 |
|
| 92 | @Override
|
| 93 | public TypeRewriteRule makeRule() {
|
| 94 | Schema inputSchema = this.getInputSchema();
|
| 95 | Type<?> itemStackType = inputSchema.getType(References.ITEM_STACK);
|
| 96 | OpticFinder<Pair<String, String>> idFinder = DSL.fieldFinder("id", DSL.named(References.ITEM_NAME.typeName(), NamespacedSchema.namespacedString()));
|
| 97 | OpticFinder<String> entityIdFinder = DSL.fieldFinder("id", DSL.string());
|
| 98 | OpticFinder<?> tagFinder = itemStackType.findField("tag");
|
| 99 | OpticFinder<?> entityTagFinder = tagFinder.type().findField("EntityTag");
|
| 100 | OpticFinder<?> entityFinder = DSL.typeFinder(inputSchema.getTypeRaw(References.ENTITY));
|
| 101 | return this.fixTypeEverywhereTyped("ItemSpawnEggFix", itemStackType, input -> {
|
| 102 | Optional<Pair<String, String>> id = input.getOptional(idFinder);
|
| 103 | if (id.isPresent() && Objects.equals(id.get().getSecond(), "minecraft:spawn_egg")) {
|
| 104 | Dynamic<?> rest = input.get(DSL.remainderFinder());
|
| 105 | short damage = rest.get("Damage").asShort((short)0);
|
| 106 | Optional<? extends Typed<?>> tagOptional = input.getOptionalTyped(tagFinder);
|
| 107 | Optional<? extends Typed<?>> entityTreeOptional = tagOptional.flatMap(value -> value.getOptionalTyped(entityTagFinder));
|
| 108 | Optional<? extends Typed<?>> entityOptional = entityTreeOptional.flatMap(value -> value.getOptionalTyped(entityFinder));
|
| 109 | Optional<String> oldId = entityOptional.flatMap(value -> value.getOptional(entityIdFinder));
|
| 110 | Typed<?> output = input;
|
| 111 | String entityName = ID_TO_ENTITY[damage & 0xFF];
|
| 112 | if (entityName != null && (oldId.isEmpty() || !Objects.equals(oldId.get(), entityName))) {
|
| 113 | Typed<?> tag = input.getOrCreateTyped(tagFinder);
|
| 114 | Dynamic<?> entityTag = DataFixUtils.orElse(tag.getOptionalTyped(entityTagFinder).map(entityTree -> entityTree.write().getOrThrow()), rest.emptyMap());
|
| 115 | entityTag = entityTag.set("id", entityTag.createString(entityName));
|
| 116 | output = output.set(tagFinder, ExtraDataFixUtils.readAndSet(tag, entityTagFinder, entityTag));
|
| 117 | }
|
| 118 |
|
| 119 | if (damage != 0) {
|
| 120 | rest = rest.set("Damage", rest.createShort((short)0));
|
| 121 | output = output.set(DSL.remainderFinder(), rest);
|
| 122 | }
|
| 123 |
|
| 124 | return output;
|
| 125 | } else {
|
| 126 | return input;
|
| 127 | }
|
| 128 | });
|
| 129 | }
|
| 130 | }
|
| 131 |
|