minecraft-src/net/minecraft/client/resources/model/ClientItemInfoLoader.java
2025-07-04 03:15:13 +03:00

98 lines
3.3 KiB
Java

package net.minecraft.client.resources.model;
import com.google.gson.JsonParser;
import com.mojang.logging.LogUtils;
import com.mojang.serialization.JsonOps;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.Util;
import net.minecraft.client.renderer.item.ClientItem;
import net.minecraft.resources.FileToIdConverter;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.ResourceManager;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
@Environment(EnvType.CLIENT)
public class ClientItemInfoLoader {
private static final Logger LOGGER = LogUtils.getLogger();
private static final FileToIdConverter LISTER = FileToIdConverter.json("items");
public static CompletableFuture<ClientItemInfoLoader.LoadedClientInfos> scheduleLoad(ResourceManager resourceManager, Executor executor) {
return CompletableFuture.supplyAsync(() -> LISTER.listMatchingResources(resourceManager), executor)
.thenCompose(
map -> {
List<CompletableFuture<ClientItemInfoLoader.PendingLoad>> list = new ArrayList(map.size());
map.forEach(
(resourceLocation, resource) -> list.add(
CompletableFuture.supplyAsync(
() -> {
ResourceLocation resourceLocation2 = LISTER.fileToId(resourceLocation);
try {
Reader reader = resource.openAsReader();
ClientItemInfoLoader.PendingLoad var5;
try {
ClientItem clientItem = (ClientItem)ClientItem.CODEC
.parse(JsonOps.INSTANCE, JsonParser.parseReader(reader))
.ifError(error -> LOGGER.error("Couldn't parse item model '{}' from pack '{}': {}", resourceLocation2, resource.sourcePackId(), error.message()))
.result()
.orElse(null);
var5 = new ClientItemInfoLoader.PendingLoad(resourceLocation2, clientItem);
} catch (Throwable var7) {
if (reader != null) {
try {
reader.close();
} catch (Throwable var6) {
var7.addSuppressed(var6);
}
}
throw var7;
}
if (reader != null) {
reader.close();
}
return var5;
} catch (Exception var8) {
LOGGER.error("Failed to open item model {} from pack '{}'", resourceLocation, resource.sourcePackId(), var8);
return new ClientItemInfoLoader.PendingLoad(resourceLocation2, null);
}
},
executor
)
)
);
return Util.sequence(list).thenApply(listx -> {
Map<ResourceLocation, ClientItem> mapx = new HashMap();
for (ClientItemInfoLoader.PendingLoad pendingLoad : listx) {
if (pendingLoad.clientItemInfo != null) {
mapx.put(pendingLoad.id, pendingLoad.clientItemInfo);
}
}
return new ClientItemInfoLoader.LoadedClientInfos(mapx);
});
}
);
}
@Environment(EnvType.CLIENT)
public record LoadedClientInfos(Map<ResourceLocation, ClientItem> contents) {
}
@Environment(EnvType.CLIENT)
record PendingLoad(ResourceLocation id, @Nullable ClientItem clientItemInfo) {
}
}