| 1 | package net.minecraft.nbt;
|
| 2 |
|
| 3 | import com.mojang.datafixers.util.Pair;
|
| 4 | import com.mojang.serialization.DataResult;
|
| 5 | import com.mojang.serialization.DynamicOps;
|
| 6 | import com.mojang.serialization.MapLike;
|
| 7 | import com.mojang.serialization.RecordBuilder;
|
| 8 | import com.mojang.serialization.RecordBuilder.AbstractStringBuilder;
|
| 9 | import it.unimi.dsi.fastutil.bytes.ByteArrayList;
|
| 10 | import it.unimi.dsi.fastutil.ints.IntArrayList;
|
| 11 | import it.unimi.dsi.fastutil.longs.LongArrayList;
|
| 12 | import java.nio.ByteBuffer;
|
| 13 | import java.util.ArrayList;
|
| 14 | import java.util.Arrays;
|
| 15 | import java.util.Iterator;
|
| 16 | import java.util.List;
|
| 17 | import java.util.Map;
|
| 18 | import java.util.Optional;
|
| 19 | import java.util.Map.Entry;
|
| 20 | import java.util.function.BiConsumer;
|
| 21 | import java.util.function.Consumer;
|
| 22 | import java.util.stream.IntStream;
|
| 23 | import java.util.stream.LongStream;
|
| 24 | import java.util.stream.Stream;
|
| 25 | import net.minecraft.util.Util;
|
| 26 | import org.jspecify.annotations.Nullable;
|
| 27 |
|
| 28 | public class NbtOps implements DynamicOps<Tag> {
|
| 29 | public static final NbtOps INSTANCE = new NbtOps();
|
| 30 |
|
| 31 | private NbtOps() {
|
| 32 | }
|
| 33 |
|
| 34 | public Tag empty() {
|
| 35 | return EndTag.INSTANCE;
|
| 36 | }
|
| 37 |
|
| 38 | public Tag emptyList() {
|
| 39 | return new ListTag();
|
| 40 | }
|
| 41 |
|
| 42 | public Tag emptyMap() {
|
| 43 | return new CompoundTag();
|
| 44 | }
|
| 45 |
|
| 46 | public <U> U convertTo(final DynamicOps<U> outOps, final Tag input) {
|
| 47 | return (U)(switch (input) {
|
| 48 | case EndTag ignored -> outOps.empty();
|
| 49 | case ByteTag(byte value) -> outOps.createByte(value);
|
| 50 | case ShortTag(short value) -> outOps.createShort(value);
|
| 51 | case IntTag(int value) -> outOps.createInt(value);
|
| 52 | case LongTag(long value) -> outOps.createLong(value);
|
| 53 | case FloatTag(float value) -> outOps.createFloat(value);
|
| 54 | case DoubleTag(double value) -> outOps.createDouble(value);
|
| 55 | case ByteArrayTag byteArrayTag -> outOps.createByteList(ByteBuffer.wrap(byteArrayTag.getAsByteArray()));
|
| 56 | case StringTag(String value) -> outOps.createString(value);
|
| 57 | case ListTag listTag -> this.convertList(outOps, listTag);
|
| 58 | case CompoundTag compoundTag -> this.convertMap(outOps, compoundTag);
|
| 59 | case IntArrayTag intArrayTag -> outOps.createIntList(Arrays.stream(intArrayTag.getAsIntArray()));
|
| 60 | case LongArrayTag longArrayTag -> outOps.createLongList(Arrays.stream(longArrayTag.getAsLongArray()));
|
| 61 | default -> throw new MatchException(null, null);
|
| 62 | });
|
| 63 | }
|
| 64 |
|
| 65 | public DataResult<Number> getNumberValue(final Tag input) {
|
| 66 | return input.asNumber().map(DataResult::success).orElseGet(() -> DataResult.error(() -> "Not a number"));
|
| 67 | }
|
| 68 |
|
| 69 | public Tag createNumeric(final Number i) {
|
| 70 | return DoubleTag.valueOf(i.doubleValue());
|
| 71 | }
|
| 72 |
|
| 73 | public Tag createByte(final byte value) {
|
| 74 | return ByteTag.valueOf(value);
|
| 75 | }
|
| 76 |
|
| 77 | public Tag createShort(final short value) {
|
| 78 | return ShortTag.valueOf(value);
|
| 79 | }
|
| 80 |
|
| 81 | public Tag createInt(final int value) {
|
| 82 | return IntTag.valueOf(value);
|
| 83 | }
|
| 84 |
|
| 85 | public Tag createLong(final long value) {
|
| 86 | return LongTag.valueOf(value);
|
| 87 | }
|
| 88 |
|
| 89 | public Tag createFloat(final float value) {
|
| 90 | return FloatTag.valueOf(value);
|
| 91 | }
|
| 92 |
|
| 93 | public Tag createDouble(final double value) {
|
| 94 | return DoubleTag.valueOf(value);
|
| 95 | }
|
| 96 |
|
| 97 | public DataResult<Boolean> getBooleanValue(final Tag input) {
|
| 98 | return this.getNumberValue(input).map(value -> value.doubleValue() != 0.0);
|
| 99 | }
|
| 100 |
|
| 101 | public Tag createBoolean(final boolean value) {
|
| 102 | return ByteTag.valueOf(value);
|
| 103 | }
|
| 104 |
|
| 105 | public DataResult<String> getStringValue(final Tag input) {
|
| 106 | return input instanceof StringTag(String value) ? DataResult.success(value) : DataResult.error(() -> "Not a string");
|
| 107 | }
|
| 108 |
|
| 109 | public Tag createString(final String value) {
|
| 110 | return StringTag.valueOf(value);
|
| 111 | }
|
| 112 |
|
| 113 | public DataResult<Tag> mergeToList(final Tag list, final Tag value) {
|
| 114 | return createCollector(list)
|
| 115 | .map(collector -> DataResult.success(collector.accept(value).result()))
|
| 116 | .orElseGet(() -> DataResult.error(() -> "mergeToList called with not a list: " + list, list));
|
| 117 | }
|
| 118 |
|
| 119 | public DataResult<Tag> mergeToList(final Tag list, final List<Tag> values) {
|
| 120 | return createCollector(list)
|
| 121 | .map(collector -> DataResult.success(collector.acceptAll(values).result()))
|
| 122 | .orElseGet(() -> DataResult.error(() -> "mergeToList called with not a list: " + list, list));
|
| 123 | }
|
| 124 |
|
| 125 | public DataResult<Tag> mergeToMap(final Tag map, final Tag key, final Tag value) {
|
| 126 | if (!(map instanceof CompoundTag) && !(map instanceof EndTag)) {
|
| 127 | return DataResult.error(() -> "mergeToMap called with not a map: " + map, map);
|
| 128 | } else if (key instanceof StringTag(String stringKey)) {
|
| 129 | CompoundTag output = map instanceof CompoundTag tag ? tag.shallowCopy() : new CompoundTag();
|
| 130 | output.put(stringKey, value);
|
| 131 | return DataResult.success(output);
|
| 132 | } else {
|
| 133 | return DataResult.error(() -> "key is not a string: " + key, map);
|
| 134 | }
|
| 135 | }
|
| 136 |
|
| 137 | public DataResult<Tag> mergeToMap(final Tag map, final MapLike<Tag> values) {
|
| 138 | if (!(map instanceof CompoundTag) && !(map instanceof EndTag)) {
|
| 139 | return DataResult.error(() -> "mergeToMap called with not a map: " + map, map);
|
| 140 | }
|
| 141 |
|
| 142 | Iterator<Pair<Tag, Tag>> valuesIterator = values.entries().iterator();
|
| 143 | if (!valuesIterator.hasNext()) {
|
| 144 | return map == this.empty() ? DataResult.success(this.emptyMap()) : DataResult.success(map);
|
| 145 | }
|
| 146 |
|
| 147 | CompoundTag output = map instanceof CompoundTag tag ? tag.shallowCopy() : new CompoundTag();
|
| 148 | List<Tag> missed = new ArrayList<>();
|
| 149 | valuesIterator.forEachRemaining(entry -> {
|
| 150 | Tag key = entry.getFirst();
|
| 151 | if (key instanceof StringTag(String stringKey)) {
|
| 152 | output.put(stringKey, entry.getSecond());
|
| 153 | } else {
|
| 154 | missed.add(key);
|
| 155 | }
|
| 156 | });
|
| 157 | return !missed.isEmpty() ? DataResult.error(() -> "some keys are not strings: " + missed, output) : DataResult.success(output);
|
| 158 | }
|
| 159 |
|
| 160 | public DataResult<Tag> mergeToMap(final Tag map, final Map<Tag, Tag> values) {
|
| 161 | if (!(map instanceof CompoundTag) && !(map instanceof EndTag)) {
|
| 162 | return DataResult.error(() -> "mergeToMap called with not a map: " + map, map);
|
| 163 | }
|
| 164 |
|
| 165 | if (values.isEmpty()) {
|
| 166 | return map == this.empty() ? DataResult.success(this.emptyMap()) : DataResult.success(map);
|
| 167 | }
|
| 168 |
|
| 169 | CompoundTag output = map instanceof CompoundTag tag ? tag.shallowCopy() : new CompoundTag();
|
| 170 | List<Tag> missed = new ArrayList<>();
|
| 171 |
|
| 172 | for (Entry<Tag, Tag> entry : values.entrySet()) {
|
| 173 | Tag key = entry.getKey();
|
| 174 | if (key instanceof StringTag(String stringKey)) {
|
| 175 | output.put(stringKey, entry.getValue());
|
| 176 | } else {
|
| 177 | missed.add(key);
|
| 178 | }
|
| 179 | }
|
| 180 |
|
| 181 | return !missed.isEmpty() ? DataResult.error(() -> "some keys are not strings: " + missed, output) : DataResult.success(output);
|
| 182 | }
|
| 183 |
|
| 184 | public DataResult<Stream<Pair<Tag, Tag>>> getMapValues(final Tag input) {
|
| 185 | return input instanceof CompoundTag tag
|
| 186 | ? DataResult.success(tag.entrySet().stream().map(entry -> Pair.of(this.createString(entry.getKey()), entry.getValue())))
|
| 187 | : DataResult.error(() -> "Not a map: " + input);
|
| 188 | }
|
| 189 |
|
| 190 | public DataResult<Consumer<BiConsumer<Tag, Tag>>> getMapEntries(final Tag input) {
|
| 191 | return input instanceof CompoundTag tag ? DataResult.success(c -> {
|
| 192 | for (Entry<String, Tag> entry : tag.entrySet()) {
|
| 193 | c.accept(this.createString(entry.getKey()), entry.getValue());
|
| 194 | }
|
| 195 | }) : DataResult.error(() -> "Not a map: " + input);
|
| 196 | }
|
| 197 |
|
| 198 | public DataResult<MapLike<Tag>> getMap(final Tag input) {
|
| 199 | return input instanceof CompoundTag tag ? DataResult.success(new MapLike<Tag>() {
|
| 200 | public @Nullable Tag get(final Tag key) {
|
| 201 | if (key instanceof StringTag(String stringKey)) {
|
| 202 | return tag.get(stringKey);
|
| 203 | } else {
|
| 204 | throw new UnsupportedOperationException("Cannot get map entry with non-string key: " + key);
|
| 205 | }
|
| 206 | }
|
| 207 |
|
| 208 | public @Nullable Tag get(final String key) {
|
| 209 | return tag.get(key);
|
| 210 | }
|
| 211 |
|
| 212 | @Override
|
| 213 | public Stream<Pair<Tag, Tag>> entries() {
|
| 214 | return tag.entrySet().stream().map(entry -> Pair.of(NbtOps.this.createString(entry.getKey()), entry.getValue()));
|
| 215 | }
|
| 216 |
|
| 217 | @Override
|
| 218 | public String toString() {
|
| 219 | return "MapLike[" + tag + "]";
|
| 220 | }
|
| 221 | }) : DataResult.error(() -> "Not a map: " + input);
|
| 222 | }
|
| 223 |
|
| 224 | public Tag createMap(final Stream<Pair<Tag, Tag>> map) {
|
| 225 | CompoundTag tag = new CompoundTag();
|
| 226 | map.forEach(entry -> {
|
| 227 | Tag key = entry.getFirst();
|
| 228 | Tag value = entry.getSecond();
|
| 229 | if (key instanceof StringTag(String stringKey)) {
|
| 230 | tag.put(stringKey, value);
|
| 231 | } else {
|
| 232 | throw new UnsupportedOperationException("Cannot create map with non-string key: " + key);
|
| 233 | }
|
| 234 | });
|
| 235 | return tag;
|
| 236 | }
|
| 237 |
|
| 238 | public DataResult<Stream<Tag>> getStream(final Tag input) {
|
| 239 | return input instanceof CollectionTag collection ? DataResult.success(collection.stream()) : DataResult.error(() -> "Not a list");
|
| 240 | }
|
| 241 |
|
| 242 | public DataResult<Consumer<Consumer<Tag>>> getList(final Tag input) {
|
| 243 | return input instanceof CollectionTag collection ? DataResult.success(collection::forEach) : DataResult.error(() -> "Not a list: " + input);
|
| 244 | }
|
| 245 |
|
| 246 | public DataResult<ByteBuffer> getByteBuffer(final Tag input) {
|
| 247 | return input instanceof ByteArrayTag array ? DataResult.success(ByteBuffer.wrap(array.getAsByteArray())) : DynamicOps.super.getByteBuffer(input);
|
| 248 | }
|
| 249 |
|
| 250 | public Tag createByteList(final ByteBuffer input) {
|
| 251 | ByteBuffer wholeBuffer = input.duplicate().clear();
|
| 252 | byte[] bytes = new byte[input.capacity()];
|
| 253 | wholeBuffer.get(0, bytes, 0, bytes.length);
|
| 254 | return new ByteArrayTag(bytes);
|
| 255 | }
|
| 256 |
|
| 257 | public DataResult<IntStream> getIntStream(final Tag input) {
|
| 258 | return input instanceof IntArrayTag array ? DataResult.success(Arrays.stream(array.getAsIntArray())) : DynamicOps.super.getIntStream(input);
|
| 259 | }
|
| 260 |
|
| 261 | public Tag createIntList(final IntStream input) {
|
| 262 | return new IntArrayTag(input.toArray());
|
| 263 | }
|
| 264 |
|
| 265 | public DataResult<LongStream> getLongStream(final Tag input) {
|
| 266 | return input instanceof LongArrayTag array ? DataResult.success(Arrays.stream(array.getAsLongArray())) : DynamicOps.super.getLongStream(input);
|
| 267 | }
|
| 268 |
|
| 269 | public Tag createLongList(final LongStream input) {
|
| 270 | return new LongArrayTag(input.toArray());
|
| 271 | }
|
| 272 |
|
| 273 | public Tag createList(final Stream<Tag> input) {
|
| 274 | return new ListTag(input.collect(Util.toMutableList()));
|
| 275 | }
|
| 276 |
|
| 277 | public Tag remove(final Tag input, final String key) {
|
| 278 | if (input instanceof CompoundTag tag) {
|
| 279 | CompoundTag result = tag.shallowCopy();
|
| 280 | result.remove(key);
|
| 281 | return result;
|
| 282 | } else {
|
| 283 | return input;
|
| 284 | }
|
| 285 | }
|
| 286 |
|
| 287 | @Override
|
| 288 | public String toString() {
|
| 289 | return "NBT";
|
| 290 | }
|
| 291 |
|
| 292 | @Override
|
| 293 | public RecordBuilder<Tag> mapBuilder() {
|
| 294 | return new NbtOps.NbtRecordBuilder();
|
| 295 | }
|
| 296 |
|
| 297 | private static Optional<NbtOps.ListCollector> createCollector(final Tag tag) {
|
| 298 | if (tag instanceof EndTag) {
|
| 299 | return Optional.of(new NbtOps.GenericListCollector());
|
| 300 | }
|
| 301 |
|
| 302 | if (tag instanceof CollectionTag collection) {
|
| 303 | if (collection.isEmpty()) {
|
| 304 | return Optional.of(new NbtOps.GenericListCollector());
|
| 305 | }
|
| 306 |
|
| 307 | return switch (collection) {
|
| 308 | case ListTag list -> Optional.of(new NbtOps.GenericListCollector(list));
|
| 309 | case ByteArrayTag array -> Optional.of(new NbtOps.ByteListCollector(array.getAsByteArray()));
|
| 310 | case IntArrayTag array -> Optional.of(new NbtOps.IntListCollector(array.getAsIntArray()));
|
| 311 | case LongArrayTag array -> Optional.of(new NbtOps.LongListCollector(array.getAsLongArray()));
|
| 312 | default -> throw new MatchException(null, null);
|
| 313 | };
|
| 314 | } else {
|
| 315 | return Optional.empty();
|
| 316 | }
|
| 317 | }
|
| 318 |
|
| 319 | private static class ByteListCollector implements NbtOps.ListCollector {
|
| 320 | private final ByteArrayList values = new ByteArrayList();
|
| 321 |
|
| 322 | public ByteListCollector(final byte[] initialValues) {
|
| 323 | this.values.addElements(0, initialValues);
|
| 324 | }
|
| 325 |
|
| 326 | @Override
|
| 327 | public NbtOps.ListCollector accept(final Tag tag) {
|
| 328 | if (tag instanceof ByteTag byteTag) {
|
| 329 | this.values.add(byteTag.byteValue());
|
| 330 | return this;
|
| 331 | } else {
|
| 332 | return new NbtOps.GenericListCollector(this.values).accept(tag);
|
| 333 | }
|
| 334 | }
|
| 335 |
|
| 336 | @Override
|
| 337 | public Tag result() {
|
| 338 | return new ByteArrayTag(this.values.toByteArray());
|
| 339 | }
|
| 340 | }
|
| 341 |
|
| 342 | private static class GenericListCollector implements NbtOps.ListCollector {
|
| 343 | private final ListTag result = new ListTag();
|
| 344 |
|
| 345 | private GenericListCollector() {
|
| 346 | }
|
| 347 |
|
| 348 | private GenericListCollector(final ListTag initial) {
|
| 349 | this.result.addAll(initial);
|
| 350 | }
|
| 351 |
|
| 352 | public GenericListCollector(final IntArrayList initials) {
|
| 353 | initials.forEach(v -> this.result.add(IntTag.valueOf(v)));
|
| 354 | }
|
| 355 |
|
| 356 | public GenericListCollector(final ByteArrayList initials) {
|
| 357 | initials.forEach(v -> this.result.add(ByteTag.valueOf(v)));
|
| 358 | }
|
| 359 |
|
| 360 | public GenericListCollector(final LongArrayList initials) {
|
| 361 | initials.forEach(v -> this.result.add(LongTag.valueOf(v)));
|
| 362 | }
|
| 363 |
|
| 364 | @Override
|
| 365 | public NbtOps.ListCollector accept(final Tag tag) {
|
| 366 | this.result.add(tag);
|
| 367 | return this;
|
| 368 | }
|
| 369 |
|
| 370 | @Override
|
| 371 | public Tag result() {
|
| 372 | return this.result;
|
| 373 | }
|
| 374 | }
|
| 375 |
|
| 376 | private static class IntListCollector implements NbtOps.ListCollector {
|
| 377 | private final IntArrayList values = new IntArrayList();
|
| 378 |
|
| 379 | public IntListCollector(final int[] initialValues) {
|
| 380 | this.values.addElements(0, initialValues);
|
| 381 | }
|
| 382 |
|
| 383 | @Override
|
| 384 | public NbtOps.ListCollector accept(final Tag tag) {
|
| 385 | if (tag instanceof IntTag intTag) {
|
| 386 | this.values.add(intTag.intValue());
|
| 387 | return this;
|
| 388 | } else {
|
| 389 | return new NbtOps.GenericListCollector(this.values).accept(tag);
|
| 390 | }
|
| 391 | }
|
| 392 |
|
| 393 | @Override
|
| 394 | public Tag result() {
|
| 395 | return new IntArrayTag(this.values.toIntArray());
|
| 396 | }
|
| 397 | }
|
| 398 |
|
| 399 | private interface ListCollector {
|
| 400 | NbtOps.ListCollector accept(Tag t);
|
| 401 |
|
| 402 | default NbtOps.ListCollector acceptAll(final Iterable<Tag> tags) {
|
| 403 | NbtOps.ListCollector collector = this;
|
| 404 |
|
| 405 | for (Tag tag : tags) {
|
| 406 | collector = collector.accept(tag);
|
| 407 | }
|
| 408 |
|
| 409 | return collector;
|
| 410 | }
|
| 411 |
|
| 412 | Tag result();
|
| 413 | }
|
| 414 |
|
| 415 | private static class LongListCollector implements NbtOps.ListCollector {
|
| 416 | private final LongArrayList values = new LongArrayList();
|
| 417 |
|
| 418 | public LongListCollector(final long[] initialValues) {
|
| 419 | this.values.addElements(0, initialValues);
|
| 420 | }
|
| 421 |
|
| 422 | @Override
|
| 423 | public NbtOps.ListCollector accept(final Tag tag) {
|
| 424 | if (tag instanceof LongTag longTag) {
|
| 425 | this.values.add(longTag.longValue());
|
| 426 | return this;
|
| 427 | } else {
|
| 428 | return new NbtOps.GenericListCollector(this.values).accept(tag);
|
| 429 | }
|
| 430 | }
|
| 431 |
|
| 432 | @Override
|
| 433 | public Tag result() {
|
| 434 | return new LongArrayTag(this.values.toLongArray());
|
| 435 | }
|
| 436 | }
|
| 437 |
|
| 438 | private class NbtRecordBuilder extends AbstractStringBuilder<Tag, CompoundTag> {
|
| 439 | protected NbtRecordBuilder() {
|
| 440 | super(NbtOps.this);
|
| 441 | }
|
| 442 |
|
| 443 | protected CompoundTag initBuilder() {
|
| 444 | return new CompoundTag();
|
| 445 | }
|
| 446 |
|
| 447 | protected CompoundTag append(final String key, final Tag value, final CompoundTag builder) {
|
| 448 | builder.put(key, value);
|
| 449 | return builder;
|
| 450 | }
|
| 451 |
|
| 452 | protected DataResult<Tag> build(final CompoundTag builder, final Tag prefix) {
|
| 453 | if (prefix == null || prefix == EndTag.INSTANCE) {
|
| 454 | return DataResult.success(builder);
|
| 455 | } else if (!(prefix instanceof CompoundTag compound)) {
|
| 456 | return DataResult.error(() -> "mergeToMap called with not a map: " + prefix, prefix);
|
| 457 | } else {
|
| 458 | CompoundTag result = compound.shallowCopy();
|
| 459 |
|
| 460 | for (Entry<String, Tag> entry : builder.entrySet()) {
|
| 461 | result.put(entry.getKey(), entry.getValue());
|
| 462 | }
|
| 463 |
|
| 464 | return DataResult.success(result);
|
| 465 | }
|
| 466 | }
|
| 467 | }
|
| 468 | }
|
| 469 |
|