| 1 | package net.minecraft.client.resources.model.sprite;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.google.gson.JsonElement;
|
| 5 | import com.google.gson.JsonObject;
|
| 6 | import com.google.gson.JsonParseException;
|
| 7 | import com.mojang.logging.LogUtils;
|
| 8 | import com.mojang.serialization.JsonOps;
|
| 9 | import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
|
| 10 | import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
|
| 11 | import it.unimi.dsi.fastutil.objects.Object2ObjectMaps;
|
| 12 | import it.unimi.dsi.fastutil.objects.ObjectIterator;
|
| 13 | import java.util.ArrayList;
|
| 14 | import java.util.List;
|
| 15 | import java.util.Map;
|
| 16 | import java.util.Map.Entry;
|
| 17 | import java.util.stream.Collectors;
|
| 18 | import net.fabricmc.api.EnvType;
|
| 19 | import net.fabricmc.api.Environment;
|
| 20 | import net.minecraft.client.resources.model.ModelDebugName;
|
| 21 | import net.minecraft.client.resources.model.sprite.TextureSlots.Data.Builder;
|
| 22 | import net.minecraft.util.GsonHelper;
|
| 23 | import org.jspecify.annotations.Nullable;
|
| 24 | import org.slf4j.Logger;
|
| 25 |
|
| 26 | @Environment(EnvType.CLIENT)
|
| 27 | public class TextureSlots {
|
| 28 | public static final TextureSlots EMPTY = new TextureSlots(Map.of());
|
| 29 | private static final char REFERENCE_CHAR = '#';
|
| 30 | private final Map<String, Material> resolvedValues;
|
| 31 |
|
| 32 | private TextureSlots(final Map<String, Material> resolvedValues) {
|
| 33 | this.resolvedValues = resolvedValues;
|
| 34 | }
|
| 35 |
|
| 36 | public @Nullable Material getMaterial(String reference) {
|
| 37 | if (isTextureReference(reference)) {
|
| 38 | reference = reference.substring(1);
|
| 39 | }
|
| 40 |
|
| 41 | return this.resolvedValues.get(reference);
|
| 42 | }
|
| 43 |
|
| 44 | private static boolean isTextureReference(final String texture) {
|
| 45 | return texture.charAt(0) == '#';
|
| 46 | }
|
| 47 |
|
| 48 | public static TextureSlots.Data parseTextureMap(final JsonObject texturesObject) {
|
| 49 | Builder builder = new Builder();
|
| 50 |
|
| 51 | for (Entry<String, JsonElement> entry : texturesObject.entrySet()) {
|
| 52 | parseEntry(entry.getKey(), entry.getValue(), builder);
|
| 53 | }
|
| 54 |
|
| 55 | return builder.build();
|
| 56 | }
|
| 57 |
|
| 58 | private static void parseEntry(final String slot, final JsonElement value, final Builder output) {
|
| 59 | if (GsonHelper.isStringValue(value) && isTextureReference(value.getAsString())) {
|
| 60 | output.addReference(slot, value.getAsString().substring(1));
|
| 61 | } else {
|
| 62 | output.addTexture(slot, Material.CODEC.parse(JsonOps.INSTANCE, value).getOrThrow(JsonParseException::new));
|
| 63 | }
|
| 64 | }
|
| 65 |
|
| 66 | @Environment(EnvType.CLIENT)
|
| 67 | public record Data(Map<String, TextureSlots.SlotContents> values) {
|
| 68 | public static final TextureSlots.Data EMPTY = new TextureSlots.Data(Map.of());
|
| 69 | }
|
| 70 |
|
| 71 | @Environment(EnvType.CLIENT)
|
| 72 | private record Reference(String target) implements TextureSlots.SlotContents {
|
| 73 | }
|
| 74 |
|
| 75 | @Environment(EnvType.CLIENT)
|
| 76 | public static class Resolver {
|
| 77 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 78 | private final List<TextureSlots.Data> entries = new ArrayList<>();
|
| 79 |
|
| 80 | public TextureSlots.Resolver addLast(final TextureSlots.Data data) {
|
| 81 | this.entries.addLast(data);
|
| 82 | return this;
|
| 83 | }
|
| 84 |
|
| 85 | public TextureSlots.Resolver addFirst(final TextureSlots.Data data) {
|
| 86 | this.entries.addFirst(data);
|
| 87 | return this;
|
| 88 | }
|
| 89 |
|
| 90 | public TextureSlots resolve(final ModelDebugName debugNameProvider) {
|
| 91 | if (this.entries.isEmpty()) {
|
| 92 | return TextureSlots.EMPTY;
|
| 93 | }
|
| 94 |
|
| 95 | Object2ObjectMap<String, Material> resolved = new Object2ObjectArrayMap<>();
|
| 96 | Object2ObjectMap<String, TextureSlots.Reference> unresolved = new Object2ObjectArrayMap<>();
|
| 97 |
|
| 98 | for (TextureSlots.Data data : Lists.reverse(this.entries)) {
|
| 99 | data.values.forEach((slot, contents) -> {
|
| 100 | switch (contents) {
|
| 101 | case TextureSlots.Value value:
|
| 102 | unresolved.remove(slot);
|
| 103 | resolved.put(slot, value.material());
|
| 104 | break;
|
| 105 | case TextureSlots.Reference reference:
|
| 106 | resolved.remove(slot);
|
| 107 | unresolved.put(slot, reference);
|
| 108 | break;
|
| 109 | default:
|
| 110 | throw new MatchException(null, null);
|
| 111 | }
|
| 112 | });
|
| 113 | }
|
| 114 |
|
| 115 | if (unresolved.isEmpty()) {
|
| 116 | return new TextureSlots(resolved);
|
| 117 | }
|
| 118 |
|
| 119 | boolean hasChanges = true;
|
| 120 |
|
| 121 | while (hasChanges) {
|
| 122 | hasChanges = false;
|
| 123 | ObjectIterator<it.unimi.dsi.fastutil.objects.Object2ObjectMap.Entry<String, TextureSlots.Reference>> iterator = Object2ObjectMaps.fastIterator(unresolved);
|
| 124 |
|
| 125 | while (iterator.hasNext()) {
|
| 126 | it.unimi.dsi.fastutil.objects.Object2ObjectMap.Entry<String, TextureSlots.Reference> entry = iterator.next();
|
| 127 | Material maybeResolved = resolved.get(entry.getValue().target);
|
| 128 | if (maybeResolved != null) {
|
| 129 | resolved.put(entry.getKey(), maybeResolved);
|
| 130 | iterator.remove();
|
| 131 | hasChanges = true;
|
| 132 | }
|
| 133 | }
|
| 134 | }
|
| 135 |
|
| 136 | if (!unresolved.isEmpty()) {
|
| 137 | LOGGER.warn(
|
| 138 | "Unresolved texture references in {}:\n{}",
|
| 139 | debugNameProvider.debugName(),
|
| 140 | unresolved.entrySet().stream().map(e -> "\t#" + e.getKey() + "-> #" + e.getValue().target + "\n").collect(Collectors.joining())
|
| 141 | );
|
| 142 | }
|
| 143 |
|
| 144 | return new TextureSlots(resolved);
|
| 145 | }
|
| 146 | }
|
| 147 |
|
| 148 | @Environment(EnvType.CLIENT)
|
| 149 | public sealed interface SlotContents permits TextureSlots.Value, TextureSlots.Reference {
|
| 150 | }
|
| 151 |
|
| 152 | @Environment(EnvType.CLIENT)
|
| 153 | private record Value(Material material) implements TextureSlots.SlotContents {
|
| 154 | }
|
| 155 | }
|
| 156 |
|