Skip to content

Commit 53b7d34

Browse files
committed
Merge 1.18 into 1.19.2
2 parents 819cb13 + 2ecd0c6 commit 53b7d34

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

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

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,47 @@ public ModelBakeEventHelper(Map<ResourceLocation, BakedModel> modelRegistry) {
6161
}
6262
}
6363

64+
private static final Set<String> WARNED_MOD_IDS = new HashSet<>();
65+
66+
/**
67+
* Create a model registry that warns if keySet, entrySet, values are accessed.
68+
* @param modId the mod that the event is being fired for
69+
* @return a wrapper around the model registry
70+
*/
71+
private Map<ResourceLocation, BakedModel> createWarningRegistry(String modId) {
72+
return new ForwardingMap<ResourceLocation, BakedModel>() {
73+
@Override
74+
protected Map<ResourceLocation, BakedModel> delegate() {
75+
return modelRegistry;
76+
}
77+
78+
private void logWarning() {
79+
if(!WARNED_MOD_IDS.add(modId))
80+
return;
81+
ModernFix.LOGGER.warn("Mod '{}' is accessing Map#keySet/entrySet/values on the model registry map inside its event handler." +
82+
" This probably won't work as expected with dynamic resources on. Prefer using Map#get/put and constructing ModelResourceLocations another way.", modId);
83+
}
84+
85+
@Override
86+
public Set<ResourceLocation> keySet() {
87+
logWarning();
88+
return super.keySet();
89+
}
90+
91+
@Override
92+
public Set<Entry<ResourceLocation, BakedModel>> entrySet() {
93+
logWarning();
94+
return super.entrySet();
95+
}
96+
97+
@Override
98+
public Collection<BakedModel> values() {
99+
logWarning();
100+
return super.values();
101+
}
102+
};
103+
}
104+
64105
public Map<ResourceLocation, BakedModel> wrapRegistry(String modId) {
65106
final Set<String> modIdsToInclude = new HashSet<>();
66107
modIdsToInclude.add(modId);
@@ -69,7 +110,7 @@ public Map<ResourceLocation, BakedModel> wrapRegistry(String modId) {
69110
} catch(IllegalArgumentException ignored) { /* sanity check */ }
70111
modIdsToInclude.remove("minecraft");
71112
if(modIdsToInclude.stream().noneMatch(INCOMPATIBLE_MODS::contains))
72-
return this.modelRegistry;
113+
return createWarningRegistry(modId);
73114
Set<ResourceLocation> ourModelLocations = Sets.filter(this.topLevelModelLocations, loc -> modIdsToInclude.contains(loc.getNamespace()));
74115
BakedModel missingModel = modelRegistry.get(ModelBakery.MISSING_MODEL_LOCATION);
75116
return new ForwardingMap<ResourceLocation, BakedModel>() {

0 commit comments

Comments
 (0)