| 1 | package net.minecraft.util;
|
| 2 |
|
| 3 | import com.mojang.datafixers.util.Pair;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.DataResult;
|
| 6 | import com.mojang.serialization.DynamicOps;
|
| 7 | import com.mojang.serialization.Keyable;
|
| 8 | import java.util.Arrays;
|
| 9 | import java.util.Map;
|
| 10 | import java.util.Objects;
|
| 11 | import java.util.function.Function;
|
| 12 | import java.util.function.Supplier;
|
| 13 | import java.util.function.ToIntFunction;
|
| 14 | import java.util.stream.Collectors;
|
| 15 | import java.util.stream.Stream;
|
| 16 | import org.jspecify.annotations.Nullable;
|
| 17 |
|
| 18 | public interface StringRepresentable {
|
| 19 | int PRE_BUILT_MAP_THRESHOLD = 16;
|
| 20 |
|
| 21 | String getSerializedName();
|
| 22 |
|
| 23 | static <E extends Enum<E> & StringRepresentable> StringRepresentable.EnumCodec<E> fromEnum(final Supplier<E[]> values) {
|
| 24 | return fromEnumWithMapping(values, s -> s);
|
| 25 | }
|
| 26 |
|
| 27 | static <E extends Enum<E> & StringRepresentable> StringRepresentable.EnumCodec<E> fromEnumWithMapping(
|
| 28 | final Supplier<E[]> values, final Function<String, String> converter
|
| 29 | ) {
|
| 30 | E[] valueArray = (E[])values.get();
|
| 31 | Function<String, E> lookupFunction = createNameLookup(valueArray, e -> converter.apply(e.getSerializedName()));
|
| 32 | return new StringRepresentable.EnumCodec<>(valueArray, lookupFunction);
|
| 33 | }
|
| 34 |
|
| 35 | static <T extends StringRepresentable> Codec<T> fromValues(final Supplier<T[]> values) {
|
| 36 | T[] valueArray = (T[])values.get();
|
| 37 | Function<String, T> lookupFunction = createNameLookup(valueArray);
|
| 38 | ToIntFunction<T> indexLookup = Util.createIndexLookup(Arrays.asList(valueArray));
|
| 39 | return new StringRepresentable.StringRepresentableCodec<>(valueArray, lookupFunction, indexLookup);
|
| 40 | }
|
| 41 |
|
| 42 | static <T extends StringRepresentable> Function<String, @Nullable T> createNameLookup(final T[] valueArray) {
|
| 43 | return createNameLookup(valueArray, StringRepresentable::getSerializedName);
|
| 44 | }
|
| 45 |
|
| 46 | static <T> Function<String, @Nullable T> createNameLookup(final T[] valueArray, final Function<T, String> converter) {
|
| 47 | if (valueArray.length > 16) {
|
| 48 | Map<String, T> byName = Arrays.<T>stream(valueArray).collect(Collectors.toMap(converter, d -> (T)d));
|
| 49 | return byName::get;
|
| 50 | } else {
|
| 51 | return id -> {
|
| 52 | for (T value : valueArray) {
|
| 53 | if (converter.apply(value).equals(id)) {
|
| 54 | return value;
|
| 55 | }
|
| 56 | }
|
| 57 |
|
| 58 | return null;
|
| 59 | };
|
| 60 | }
|
| 61 | }
|
| 62 |
|
| 63 | static Keyable keys(final StringRepresentable[] values) {
|
| 64 | return new Keyable() {
|
| 65 | @Override
|
| 66 | public <T> Stream<T> keys(final DynamicOps<T> ops) {
|
| 67 | return Arrays.stream(values).map(StringRepresentable::getSerializedName).map(ops::createString);
|
| 68 | }
|
| 69 | };
|
| 70 | }
|
| 71 |
|
| 72 | class EnumCodec<E extends Enum<E> & StringRepresentable> extends StringRepresentable.StringRepresentableCodec<E> {
|
| 73 | private final Function<String, @Nullable E> resolver;
|
| 74 |
|
| 75 | public EnumCodec(final E[] valueArray, final Function<String, E> nameResolver) {
|
| 76 | super(valueArray, nameResolver, rec$ -> rec$.ordinal());
|
| 77 | this.resolver = nameResolver;
|
| 78 | }
|
| 79 |
|
| 80 | public @Nullable E byName(final String name) {
|
| 81 | return this.resolver.apply(name);
|
| 82 | }
|
| 83 |
|
| 84 | public E byName(final String name, final E _default) {
|
| 85 | return Objects.requireNonNullElse(this.byName(name), _default);
|
| 86 | }
|
| 87 |
|
| 88 | public E byName(final String name, final Supplier<? extends E> defaultSupplier) {
|
| 89 | return Objects.requireNonNullElseGet(this.byName(name), defaultSupplier);
|
| 90 | }
|
| 91 | }
|
| 92 |
|
| 93 | class StringRepresentableCodec<S extends StringRepresentable> implements Codec<S> {
|
| 94 | private final Codec<S> codec;
|
| 95 |
|
| 96 | public StringRepresentableCodec(final S[] valueArray, final Function<String, @Nullable S> nameResolver, final ToIntFunction<S> idResolver) {
|
| 97 | this.codec = ExtraCodecs.orCompressed(
|
| 98 | Codec.stringResolver(StringRepresentable::getSerializedName, nameResolver),
|
| 99 | ExtraCodecs.idResolverCodec(idResolver, i -> i >= 0 && i < valueArray.length ? valueArray[i] : null, -1)
|
| 100 | );
|
| 101 | }
|
| 102 |
|
| 103 | @Override
|
| 104 | public <T> DataResult<Pair<S, T>> decode(final DynamicOps<T> ops, final T input) {
|
| 105 | return this.codec.decode(ops, input);
|
| 106 | }
|
| 107 |
|
| 108 | public <T> DataResult<T> encode(final S input, final DynamicOps<T> ops, final T prefix) {
|
| 109 | return this.codec.encode(input, ops, prefix);
|
| 110 | }
|
| 111 | }
|
| 112 | }
|
| 113 |
|