Avoid propagating unbaked model load errors to higher-level code

Related: #625
This commit is contained in:
embeddedt 2026-01-25 21:28:23 -05:00
parent d699187006
commit 8125da7882
No known key found for this signature in database
GPG Key ID: A69433EC199B5613

View File

@ -54,7 +54,8 @@ public class ModelManagerMixin {
@ModifyArg(method = "loadBlockModels", at = @At(value = "INVOKE", target = "Ljava/util/concurrent/CompletableFuture;thenCompose(Ljava/util/function/Function;)Ljava/util/concurrent/CompletableFuture;", ordinal = 0), index = 0) @ModifyArg(method = "loadBlockModels", at = @At(value = "INVOKE", target = "Ljava/util/concurrent/CompletableFuture;thenCompose(Ljava/util/function/Function;)Ljava/util/concurrent/CompletableFuture;", ordinal = 0), index = 0)
private static Function<Map<ResourceLocation, Resource>, ? extends CompletionStage<Map<ResourceLocation, BlockModel>>> deferBlockModelLoad(Function<Map<ResourceLocation, Resource>, ? extends CompletionStage<Map<ResourceLocation, BlockModel>>> fn, @Local(ordinal = 0, argsOnly = true) ResourceManager manager) { private static Function<Map<ResourceLocation, Resource>, ? extends CompletionStage<Map<ResourceLocation, BlockModel>>> deferBlockModelLoad(Function<Map<ResourceLocation, Resource>, ? extends CompletionStage<Map<ResourceLocation, BlockModel>>> fn, @Local(ordinal = 0, argsOnly = true) ResourceManager manager) {
return resourceMap -> { return resourceMap -> {
var cache = CacheUtil.<ResourceLocation, BlockModel>simpleCacheForLambda(location -> loadSingleBlockModel(manager, location), 100L); var fallbackModel = BlockModel.fromString(ModelBakery.MISSING_MODEL_MESH);
var cache = CacheUtil.<ResourceLocation, BlockModel>simpleCacheForLambda(location -> loadSingleBlockModel(manager, location, fallbackModel), 100L);
return CompletableFuture.completedFuture(Maps.asMap(Set.copyOf(resourceMap.keySet()), location -> cache.getUnchecked(location))); return CompletableFuture.completedFuture(Maps.asMap(Set.copyOf(resourceMap.keySet()), location -> cache.getUnchecked(location)));
}; };
} }
@ -70,13 +71,15 @@ public class ModelManagerMixin {
return ImmutableList.of(); return ImmutableList.of();
} }
private static BlockModel loadSingleBlockModel(ResourceManager manager, ResourceLocation location) { private static BlockModel loadSingleBlockModel(ResourceManager manager, ResourceLocation location, BlockModel fallbackModel) {
return manager.getResource(location).map(resource -> { return manager.getResource(location).map(resource -> {
try (BufferedReader reader = resource.openAsReader()) { try (BufferedReader reader = resource.openAsReader()) {
return BlockModel.fromStream(reader); return BlockModel.fromStream(reader);
} catch(IOException e) { } catch (Exception e) {
ModernFix.LOGGER.error("Couldn't load model", e); // We must return some nonnull value to avoid breaking the map convention. The easiest solution
return null; // is to just return a missing model template.
ModernFix.LOGGER.error("Couldn't load model {}, substituting missing", location, e);
return fallbackModel;
} }
}).orElse(null); }).orElse(null);
} }