| 1 | package net.minecraft.client.resources.model;
|
| 2 |
|
| 3 | import com.google.gson.JsonElement;
|
| 4 | import com.mojang.logging.LogUtils;
|
| 5 | import com.mojang.serialization.DynamicOps;
|
| 6 | import com.mojang.serialization.JsonOps;
|
| 7 | import java.io.Reader;
|
| 8 | import java.util.ArrayList;
|
| 9 | import java.util.HashMap;
|
| 10 | import java.util.List;
|
| 11 | import java.util.Map;
|
| 12 | import java.util.concurrent.CompletableFuture;
|
| 13 | import java.util.concurrent.Executor;
|
| 14 | import net.fabricmc.api.EnvType;
|
| 15 | import net.fabricmc.api.Environment;
|
| 16 | import net.minecraft.client.multiplayer.ClientRegistryLayer;
|
| 17 | import net.minecraft.client.renderer.item.ClientItem;
|
| 18 | import net.minecraft.core.RegistryAccess.Frozen;
|
| 19 | import net.minecraft.resources.FileToIdConverter;
|
| 20 | import net.minecraft.resources.Identifier;
|
| 21 | import net.minecraft.server.packs.resources.Resource;
|
| 22 | import net.minecraft.server.packs.resources.ResourceManager;
|
| 23 | import net.minecraft.util.PlaceholderLookupProvider;
|
| 24 | import net.minecraft.util.StrictJsonParser;
|
| 25 | import net.minecraft.util.Util;
|
| 26 | import org.jspecify.annotations.Nullable;
|
| 27 | import org.slf4j.Logger;
|
| 28 |
|
| 29 | @Environment(EnvType.CLIENT)
|
| 30 | public class ClientItemInfoLoader {
|
| 31 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 32 | private static final FileToIdConverter LISTER = FileToIdConverter.json("items");
|
| 33 |
|
| 34 | public static CompletableFuture<ClientItemInfoLoader.LoadedClientInfos> scheduleLoad(final ResourceManager manager, final Executor executor) {
|
| 35 | Frozen staticRegistries = ClientRegistryLayer.createRegistryAccess().compositeAccess();
|
| 36 | return CompletableFuture.<Map<Identifier, Resource>>supplyAsync(() -> LISTER.listMatchingResources(manager), executor)
|
| 37 | .thenCompose(
|
| 38 | resources -> {
|
| 39 | List<CompletableFuture<ClientItemInfoLoader.PendingLoad>> pendingLoads = new ArrayList<>(resources.size());
|
| 40 | resources.forEach(
|
| 41 | (resourceId, resource) -> pendingLoads.add(
|
| 42 | CompletableFuture.supplyAsync(
|
| 43 | () -> {
|
| 44 | Identifier modelId = LISTER.fileToId(resourceId);
|
| 45 |
|
| 46 | try (Reader reader = resource.openAsReader()) {
|
| 47 | PlaceholderLookupProvider lookup = new PlaceholderLookupProvider(staticRegistries);
|
| 48 | DynamicOps<JsonElement> ops = lookup.createSerializationContext(JsonOps.INSTANCE);
|
| 49 | ClientItem parsedInfo = ClientItem.CODEC
|
| 50 | .parse(ops, StrictJsonParser.parse(reader))
|
| 51 | .ifError(error -> LOGGER.error("Couldn't parse item model '{}' from pack '{}': {}", modelId, resource.sourcePackId(), error.message()))
|
| 52 | .result()
|
| 53 | .map(clientItem -> lookup.hasRegisteredPlaceholders() ? clientItem.withRegistrySwapper(lookup.createSwapper()) : clientItem)
|
| 54 | .orElse(null);
|
| 55 | return new ClientItemInfoLoader.PendingLoad(modelId, parsedInfo);
|
| 56 | } catch (Exception e) {
|
| 57 | LOGGER.error("Failed to open item model {} from pack '{}'", resourceId, resource.sourcePackId(), e);
|
| 58 | return new ClientItemInfoLoader.PendingLoad(modelId, null);
|
| 59 | }
|
| 60 | },
|
| 61 | executor
|
| 62 | )
|
| 63 | )
|
| 64 | );
|
| 65 | return Util.sequence(pendingLoads).thenApply(loads -> {
|
| 66 | Map<Identifier, ClientItem> resultMap = new HashMap<>();
|
| 67 |
|
| 68 | for (ClientItemInfoLoader.PendingLoad load : loads) {
|
| 69 | if (load.clientItemInfo != null) {
|
| 70 | resultMap.put(load.id, load.clientItemInfo);
|
| 71 | }
|
| 72 | }
|
| 73 |
|
| 74 | return new ClientItemInfoLoader.LoadedClientInfos(resultMap);
|
| 75 | });
|
| 76 | }
|
| 77 | );
|
| 78 | }
|
| 79 |
|
| 80 | @Environment(EnvType.CLIENT)
|
| 81 | public record LoadedClientInfos(Map<Identifier, ClientItem> contents) {
|
| 82 | }
|
| 83 |
|
| 84 | @Environment(EnvType.CLIENT)
|
| 85 | private record PendingLoad(Identifier id, @Nullable ClientItem clientItemInfo) {
|
| 86 | }
|
| 87 | }
|
| 88 |
|