Skip to content

Commit 7174ae1

Browse files
committed
Do not actually load all models in replaceAll, use clever trick instead
Otherwise, it takes 20 seconds to run this with just Mekanism+Additions+Pneumaticcraft
1 parent 3214311 commit 7174ae1

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

forge/src/main/java/org/embeddedt/modernfix/forge/dynresources/ModelBakeEventHelper.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,26 @@ public boolean containsKey(@Nullable Object key) {
153153

154154
@Override
155155
public void replaceAll(BiFunction<? super ResourceLocation, ? super BakedModel, ? extends BakedModel> function) {
156-
ModernFix.LOGGER.warn("Mod '{}' is calling replaceAll on the model registry. This requires temporarily loading every model for that mod, which is slow.", modId);
156+
ModernFix.LOGGER.warn("Mod '{}' is calling replaceAll on the model registry. Some hacks will be used to keep this fast, but they may not be 100% compatible.", modId);
157157
List<ResourceLocation> locations = new ArrayList<>(keySet());
158158
for(ResourceLocation location : locations) {
159-
BakedModel existing = get(location);
160-
BakedModel replacement = function.apply(location, existing);
161-
if(replacement != existing) {
162-
put(location, replacement);
159+
/*
160+
* Fetching every model is insanely slow. So we call the function with a null object first, since it
161+
* probably isn't expecting that. If we get an exception thrown, or it returns nonnull, then we know
162+
* it actually cares about the given model.
163+
*/
164+
boolean needsReplacement;
165+
try {
166+
needsReplacement = function.apply(location, null) != null;
167+
} catch(Throwable e) {
168+
needsReplacement = true;
169+
}
170+
if(needsReplacement) {
171+
BakedModel existing = get(location);
172+
BakedModel replacement = function.apply(location, existing);
173+
if(replacement != existing) {
174+
put(location, replacement);
175+
}
163176
}
164177
}
165178
}

0 commit comments

Comments
 (0)