| 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.TypeRewriteRule;
|
| 6 | import com.mojang.datafixers.Typed;
|
| 7 | import com.mojang.datafixers.schemas.Schema;
|
| 8 | import com.mojang.datafixers.types.Type;
|
| 9 | import com.mojang.serialization.Dynamic;
|
| 10 | import com.mojang.serialization.OptionalDynamic;
|
| 11 | import java.util.Arrays;
|
| 12 | import java.util.function.Function;
|
| 13 |
|
| 14 | public class EntityProjectileOwnerFix extends DataFix {
|
| 15 | public EntityProjectileOwnerFix(final Schema outputSchema) {
|
| 16 | super(outputSchema, false);
|
| 17 | }
|
| 18 |
|
| 19 | @Override
|
| 20 | protected TypeRewriteRule makeRule() {
|
| 21 | Schema inputSchema = this.getInputSchema();
|
| 22 | return this.fixTypeEverywhereTyped("EntityProjectileOwner", inputSchema.getType(References.ENTITY), this::updateProjectiles);
|
| 23 | }
|
| 24 |
|
| 25 | private Typed<?> updateProjectiles(Typed<?> input) {
|
| 26 | input = this.updateEntity(input, "minecraft:egg", this::updateOwnerThrowable);
|
| 27 | input = this.updateEntity(input, "minecraft:ender_pearl", this::updateOwnerThrowable);
|
| 28 | input = this.updateEntity(input, "minecraft:experience_bottle", this::updateOwnerThrowable);
|
| 29 | input = this.updateEntity(input, "minecraft:snowball", this::updateOwnerThrowable);
|
| 30 | input = this.updateEntity(input, "minecraft:potion", this::updateOwnerThrowable);
|
| 31 | input = this.updateEntity(input, "minecraft:llama_spit", this::updateOwnerLlamaSpit);
|
| 32 | input = this.updateEntity(input, "minecraft:arrow", this::updateOwnerArrow);
|
| 33 | input = this.updateEntity(input, "minecraft:spectral_arrow", this::updateOwnerArrow);
|
| 34 | return this.updateEntity(input, "minecraft:trident", this::updateOwnerArrow);
|
| 35 | }
|
| 36 |
|
| 37 | private Dynamic<?> updateOwnerArrow(final Dynamic<?> tag) {
|
| 38 | long mostSignificantBits = tag.get("OwnerUUIDMost").asLong(0L);
|
| 39 | long leastSignificantBits = tag.get("OwnerUUIDLeast").asLong(0L);
|
| 40 | return this.setUUID(tag, mostSignificantBits, leastSignificantBits).remove("OwnerUUIDMost").remove("OwnerUUIDLeast");
|
| 41 | }
|
| 42 |
|
| 43 | private Dynamic<?> updateOwnerLlamaSpit(final Dynamic<?> tag) {
|
| 44 | OptionalDynamic<?> owner = tag.get("Owner");
|
| 45 | long mostSignificantBits = owner.get("OwnerUUIDMost").asLong(0L);
|
| 46 | long leastSignificantBits = owner.get("OwnerUUIDLeast").asLong(0L);
|
| 47 | return this.setUUID(tag, mostSignificantBits, leastSignificantBits).remove("Owner");
|
| 48 | }
|
| 49 |
|
| 50 | private Dynamic<?> updateOwnerThrowable(final Dynamic<?> tag) {
|
| 51 | String ownerKey = "owner";
|
| 52 | OptionalDynamic<?> owner = tag.get("owner");
|
| 53 | long mostSignificantBits = owner.get("M").asLong(0L);
|
| 54 | long leastSignificantBits = owner.get("L").asLong(0L);
|
| 55 | return this.setUUID(tag, mostSignificantBits, leastSignificantBits).remove("owner");
|
| 56 | }
|
| 57 |
|
| 58 | private Dynamic<?> setUUID(final Dynamic<?> tag, final long mostSignificantBits, final long leastSignificantBits) {
|
| 59 | String name = "OwnerUUID";
|
| 60 | return mostSignificantBits != 0L && leastSignificantBits != 0L
|
| 61 | ? tag.set("OwnerUUID", tag.createIntList(Arrays.stream(createUUIDArray(mostSignificantBits, leastSignificantBits))))
|
| 62 | : tag;
|
| 63 | }
|
| 64 |
|
| 65 | private static int[] createUUIDArray(final long mostSignificantBits, final long leastSignificantBits) {
|
| 66 | return new int[]{(int)(mostSignificantBits >> 32), (int)mostSignificantBits, (int)(leastSignificantBits >> 32), (int)leastSignificantBits};
|
| 67 | }
|
| 68 |
|
| 69 | private Typed<?> updateEntity(final Typed<?> input, final String name, final Function<Dynamic<?>, Dynamic<?>> function) {
|
| 70 | Type<?> oldType = this.getInputSchema().getChoiceType(References.ENTITY, name);
|
| 71 | Type<?> newType = this.getOutputSchema().getChoiceType(References.ENTITY, name);
|
| 72 | return input.updateTyped(DSL.namedChoice(name, oldType), newType, entity -> entity.update(DSL.remainderFinder(), function));
|
| 73 | }
|
| 74 | }
|
| 75 |
|