| 1 | package net.minecraft.util.datafix.fixes;
|
| 2 |
|
| 3 | import com.mojang.brigadier.StringReader;
|
| 4 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 5 | import com.mojang.datafixers.DataFix;
|
| 6 | import com.mojang.datafixers.TypeRewriteRule;
|
| 7 | import com.mojang.datafixers.schemas.Schema;
|
| 8 | import com.mojang.datafixers.types.Type;
|
| 9 | import com.mojang.logging.LogUtils;
|
| 10 | import com.mojang.serialization.Dynamic;
|
| 11 | import com.mojang.serialization.DynamicOps;
|
| 12 | import java.util.HashMap;
|
| 13 | import java.util.Map;
|
| 14 | import java.util.Optional;
|
| 15 | import java.util.stream.IntStream;
|
| 16 | import java.util.stream.Stream;
|
| 17 | import net.minecraft.nbt.TagParser;
|
| 18 | import net.minecraft.util.Mth;
|
| 19 | import net.minecraft.util.datafix.schemas.NamespacedSchema;
|
| 20 | import org.jspecify.annotations.Nullable;
|
| 21 | import org.slf4j.Logger;
|
| 22 |
|
| 23 | public class ParticleUnflatteningFix extends DataFix {
|
| 24 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 25 |
|
| 26 | public ParticleUnflatteningFix(final Schema outputSchema) {
|
| 27 | super(outputSchema, true);
|
| 28 | }
|
| 29 |
|
| 30 | @Override
|
| 31 | protected TypeRewriteRule makeRule() {
|
| 32 | Type<?> oldType = this.getInputSchema().getType(References.PARTICLE);
|
| 33 | Type<?> newType = this.getOutputSchema().getType(References.PARTICLE);
|
| 34 | return this.writeFixAndRead("ParticleUnflatteningFix", oldType, newType, this::fix);
|
| 35 | }
|
| 36 |
|
| 37 | private <T> Dynamic<T> fix(final Dynamic<T> input) {
|
| 38 | Optional<String> maybeString = input.asString().result();
|
| 39 | if (maybeString.isEmpty()) {
|
| 40 | return input;
|
| 41 | }
|
| 42 |
|
| 43 | String particleDescription = maybeString.get();
|
| 44 | String[] parts = particleDescription.split(" ", 2);
|
| 45 | String id = NamespacedSchema.ensureNamespaced(parts[0]);
|
| 46 | Dynamic<T> result = input.createMap(Map.of(input.createString("type"), input.createString(id)));
|
| 47 |
|
| 48 | return switch (id) {
|
| 49 | case "minecraft:item" -> parts.length > 1 ? this.updateItem(result, parts[1]) : result;
|
| 50 | case "minecraft:block", "minecraft:block_marker", "minecraft:falling_dust", "minecraft:dust_pillar" -> parts.length > 1
|
| 51 | ? this.updateBlock(result, parts[1])
|
| 52 | : result;
|
| 53 | case "minecraft:dust" -> parts.length > 1 ? this.updateDust(result, parts[1]) : result;
|
| 54 | case "minecraft:dust_color_transition" -> parts.length > 1 ? this.updateDustTransition(result, parts[1]) : result;
|
| 55 | case "minecraft:sculk_charge" -> parts.length > 1 ? this.updateSculkCharge(result, parts[1]) : result;
|
| 56 | case "minecraft:vibration" -> parts.length > 1 ? this.updateVibration(result, parts[1]) : result;
|
| 57 | case "minecraft:shriek" -> parts.length > 1 ? this.updateShriek(result, parts[1]) : result;
|
| 58 | default -> result;
|
| 59 | };
|
| 60 | }
|
| 61 |
|
| 62 | private <T> Dynamic<T> updateItem(final Dynamic<T> result, final String contents) {
|
| 63 | int tagPartStart = contents.indexOf("{");
|
| 64 | Dynamic<T> itemStack = result.createMap(Map.of(result.createString("Count"), result.createInt(1)));
|
| 65 | if (tagPartStart == -1) {
|
| 66 | itemStack = itemStack.set("id", result.createString(contents));
|
| 67 | } else {
|
| 68 | itemStack = itemStack.set("id", result.createString(contents.substring(0, tagPartStart)));
|
| 69 | Dynamic<T> itemTag = parseTag(result.getOps(), contents.substring(tagPartStart));
|
| 70 | if (itemTag != null) {
|
| 71 | itemStack = itemStack.set("tag", itemTag);
|
| 72 | }
|
| 73 | }
|
| 74 |
|
| 75 | return result.set("item", itemStack);
|
| 76 | }
|
| 77 |
|
| 78 | private static <T> @Nullable Dynamic<T> parseTag(final DynamicOps<T> ops, final String contents) {
|
| 79 | try {
|
| 80 | return new Dynamic<>(ops, TagParser.create(ops).parseFully(contents));
|
| 81 | } catch (Exception e) {
|
| 82 | LOGGER.warn("Failed to parse tag: {}", contents, e);
|
| 83 | return null;
|
| 84 | }
|
| 85 | }
|
| 86 |
|
| 87 | private <T> Dynamic<T> updateBlock(final Dynamic<T> result, final String contents) {
|
| 88 | int statePartStart = contents.indexOf("[");
|
| 89 | Dynamic<T> blockState = result.emptyMap();
|
| 90 | if (statePartStart == -1) {
|
| 91 | blockState = blockState.set("Name", result.createString(NamespacedSchema.ensureNamespaced(contents)));
|
| 92 | } else {
|
| 93 | blockState = blockState.set("Name", result.createString(NamespacedSchema.ensureNamespaced(contents.substring(0, statePartStart))));
|
| 94 | Map<Dynamic<T>, Dynamic<T>> properties = parseBlockProperties(result, contents.substring(statePartStart));
|
| 95 | if (!properties.isEmpty()) {
|
| 96 | blockState = blockState.set("Properties", result.createMap(properties));
|
| 97 | }
|
| 98 | }
|
| 99 |
|
| 100 | return result.set("block_state", blockState);
|
| 101 | }
|
| 102 |
|
| 103 | private static <T> Map<Dynamic<T>, Dynamic<T>> parseBlockProperties(final Dynamic<T> dynamic, final String contents) {
|
| 104 | try {
|
| 105 | Map<Dynamic<T>, Dynamic<T>> result = new HashMap<>();
|
| 106 | StringReader reader = new StringReader(contents);
|
| 107 | reader.expect('[');
|
| 108 | reader.skipWhitespace();
|
| 109 |
|
| 110 | while (reader.canRead() && reader.peek() != ']') {
|
| 111 | reader.skipWhitespace();
|
| 112 | String key = reader.readString();
|
| 113 | reader.skipWhitespace();
|
| 114 | reader.expect('=');
|
| 115 | reader.skipWhitespace();
|
| 116 | String value = reader.readString();
|
| 117 | reader.skipWhitespace();
|
| 118 | result.put(dynamic.createString(key), dynamic.createString(value));
|
| 119 | if (reader.canRead()) {
|
| 120 | if (reader.peek() != ',') {
|
| 121 | break;
|
| 122 | }
|
| 123 |
|
| 124 | reader.skip();
|
| 125 | }
|
| 126 | }
|
| 127 |
|
| 128 | reader.expect(']');
|
| 129 | return result;
|
| 130 | } catch (Exception e) {
|
| 131 | LOGGER.warn("Failed to parse block properties: {}", contents, e);
|
| 132 | return Map.of();
|
| 133 | }
|
| 134 | }
|
| 135 |
|
| 136 | private static <T> Dynamic<T> readVector(final Dynamic<T> result, final StringReader reader) throws CommandSyntaxException {
|
| 137 | float x = reader.readFloat();
|
| 138 | reader.expect(' ');
|
| 139 | float y = reader.readFloat();
|
| 140 | reader.expect(' ');
|
| 141 | float z = reader.readFloat();
|
| 142 | return result.createList(Stream.of(x, y, z).map(result::createFloat));
|
| 143 | }
|
| 144 |
|
| 145 | private <T> Dynamic<T> updateDust(final Dynamic<T> result, final String contents) {
|
| 146 | try {
|
| 147 | StringReader reader = new StringReader(contents);
|
| 148 | Dynamic<T> vector = readVector(result, reader);
|
| 149 | reader.expect(' ');
|
| 150 | float scale = reader.readFloat();
|
| 151 | return result.set("color", vector).set("scale", result.createFloat(scale));
|
| 152 | } catch (Exception e) {
|
| 153 | LOGGER.warn("Failed to parse particle options: {}", contents, e);
|
| 154 | return result;
|
| 155 | }
|
| 156 | }
|
| 157 |
|
| 158 | private <T> Dynamic<T> updateDustTransition(final Dynamic<T> result, final String contents) {
|
| 159 | try {
|
| 160 | StringReader reader = new StringReader(contents);
|
| 161 | Dynamic<T> from = readVector(result, reader);
|
| 162 | reader.expect(' ');
|
| 163 | float scale = reader.readFloat();
|
| 164 | reader.expect(' ');
|
| 165 | Dynamic<T> to = readVector(result, reader);
|
| 166 | return result.set("from_color", from).set("to_color", to).set("scale", result.createFloat(scale));
|
| 167 | } catch (Exception e) {
|
| 168 | LOGGER.warn("Failed to parse particle options: {}", contents, e);
|
| 169 | return result;
|
| 170 | }
|
| 171 | }
|
| 172 |
|
| 173 | private <T> Dynamic<T> updateSculkCharge(final Dynamic<T> result, final String contents) {
|
| 174 | try {
|
| 175 | StringReader reader = new StringReader(contents);
|
| 176 | float roll = reader.readFloat();
|
| 177 | return result.set("roll", result.createFloat(roll));
|
| 178 | } catch (Exception e) {
|
| 179 | LOGGER.warn("Failed to parse particle options: {}", contents, e);
|
| 180 | return result;
|
| 181 | }
|
| 182 | }
|
| 183 |
|
| 184 | private <T> Dynamic<T> updateVibration(final Dynamic<T> result, final String contents) {
|
| 185 | try {
|
| 186 | StringReader reader = new StringReader(contents);
|
| 187 | float destX = (float)reader.readDouble();
|
| 188 | reader.expect(' ');
|
| 189 | float destY = (float)reader.readDouble();
|
| 190 | reader.expect(' ');
|
| 191 | float destZ = (float)reader.readDouble();
|
| 192 | reader.expect(' ');
|
| 193 | int arrivalInTicks = reader.readInt();
|
| 194 | Dynamic<T> blockPos = (Dynamic<T>)result.createIntList(IntStream.of(Mth.floor(destX), Mth.floor(destY), Mth.floor(destZ)));
|
| 195 | Dynamic<T> positionSource = result.createMap(
|
| 196 | Map.of(result.createString("type"), result.createString("minecraft:block"), result.createString("pos"), blockPos)
|
| 197 | );
|
| 198 | return result.set("destination", positionSource).set("arrival_in_ticks", result.createInt(arrivalInTicks));
|
| 199 | } catch (Exception e) {
|
| 200 | LOGGER.warn("Failed to parse particle options: {}", contents, e);
|
| 201 | return result;
|
| 202 | }
|
| 203 | }
|
| 204 |
|
| 205 | private <T> Dynamic<T> updateShriek(final Dynamic<T> result, final String contents) {
|
| 206 | try {
|
| 207 | StringReader reader = new StringReader(contents);
|
| 208 | int delay = reader.readInt();
|
| 209 | return result.set("delay", result.createInt(delay));
|
| 210 | } catch (Exception e) {
|
| 211 | LOGGER.warn("Failed to parse particle options: {}", contents, e);
|
| 212 | return result;
|
| 213 | }
|
| 214 | }
|
| 215 | }
|
| 216 |
|