Skip to content

Commit bb65831

Browse files
committed
Try to use a more optimal Set implementation for filtering
1 parent 14f1552 commit bb65831

File tree

1 file changed

+31
-9
lines changed

1 file changed

+31
-9
lines changed

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

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.google.common.graph.GraphBuilder;
77
import com.google.common.graph.MutableGraph;
88
import it.unimi.dsi.fastutil.objects.ObjectLinkedOpenHashSet;
9+
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
910
import net.minecraft.client.resources.model.BakedModel;
1011
import net.minecraft.client.resources.model.ModelBakery;
1112
import net.minecraft.client.resources.model.ModelResourceLocation;
@@ -57,6 +58,7 @@ private enum UniverseVisibility {
5758
.build();
5859
private final Map<ResourceLocation, BakedModel> modelRegistry;
5960
private final Set<ResourceLocation> topLevelModelLocations;
61+
private final Set<String> namespacesWithModels;
6062
private final MutableGraph<String> dependencyGraph;
6163
public ModelBakeEventHelper(Map<ResourceLocation, BakedModel> modelRegistry) {
6264
this.modelRegistry = modelRegistry;
@@ -65,17 +67,25 @@ public ModelBakeEventHelper(Map<ResourceLocation, BakedModel> modelRegistry) {
6567
blockStateCount += b.getStateDefinition().getPossibleStates().size();
6668
}
6769
this.topLevelModelLocations = new ObjectLinkedOpenHashSet<>(blockStateCount + BuiltInRegistries.ITEM.size());
70+
this.namespacesWithModels = new ObjectOpenHashSet<>(ModList.get().size());
6871
var modelLocationBuilder = new ModelLocationBuilder();
6972
BuiltInRegistries.BLOCK.entrySet().forEach(entry -> {
7073
var location = entry.getKey().location();
7174
modelLocationBuilder.generateForBlock(topLevelModelLocations, entry.getValue(), location);
75+
namespacesWithModels.add(location.getNamespace());
76+
});
77+
BuiltInRegistries.ITEM.keySet().forEach(key -> {
78+
topLevelModelLocations.add(new ModelResourceLocation(key, "inventory"));
79+
namespacesWithModels.add(key.getNamespace());
7280
});
73-
BuiltInRegistries.ITEM.keySet().forEach(key -> topLevelModelLocations.add(new ModelResourceLocation(key, "inventory")));
7481
this.topLevelModelLocations.addAll(modelRegistry.keySet());
82+
for (var loc : modelRegistry.keySet()) {
83+
this.namespacesWithModels.add(loc.getNamespace());
84+
}
7585
this.dependencyGraph = buildDependencyGraph();
7686
}
7787

78-
private static MutableGraph<String> buildDependencyGraph() {
88+
private MutableGraph<String> buildDependencyGraph() {
7989
MutableGraph<String> dependencyGraph = GraphBuilder.undirected().build();
8090
ModList.get().forEachModContainer((id, mc) -> {
8191
dependencyGraph.addNode(id);
@@ -88,7 +98,7 @@ private static MutableGraph<String> buildDependencyGraph() {
8898
if(mContainer.isPresent()) {
8999
for(IModInfo.ModVersion version : mContainer.get().getModInfo().getDependencies()) {
90100
// avoid self-loops
91-
if(!Objects.equals(id, version.getModId()))
101+
if(!Objects.equals(id, version.getModId()) && !version.getModId().equals("minecraft") && namespacesWithModels.contains(version.getModId()))
92102
dependencyGraph.putEdge(id, version.getModId());
93103
}
94104
}
@@ -143,17 +153,29 @@ public void replaceAll(BiFunction<? super ResourceLocation, ? super BakedModel,
143153
};
144154
}
145155

156+
private Set<String> computeVisibleModIds(String modId) {
157+
Set<String> deps;
158+
try {
159+
deps = this.dependencyGraph.adjacentNodes(modId);
160+
} catch (IllegalArgumentException e) {
161+
deps = Set.of();
162+
}
163+
if (deps.isEmpty()) {
164+
// avoid extra work below
165+
return Set.of(modId);
166+
}
167+
var set = new ObjectOpenHashSet<String>();
168+
set.add(modId);
169+
set.addAll(deps);
170+
return Set.copyOf(set);
171+
}
172+
146173
public Map<ResourceLocation, BakedModel> wrapRegistry(String modId) {
147174
var config = MOD_VISIBILITY_CONFIGURATION.getOrDefault(modId, UniverseVisibility.EVERYTHING);
148175
if (config == UniverseVisibility.NONE) {
149176
return createWarningRegistry(modId);
150177
}
151-
final Set<String> modIdsToInclude = new HashSet<>();
152-
modIdsToInclude.add(modId);
153-
try {
154-
modIdsToInclude.addAll(this.dependencyGraph.adjacentNodes(modId));
155-
} catch(IllegalArgumentException ignored) { /* sanity check */ }
156-
modIdsToInclude.remove("minecraft");
178+
final Set<String> modIdsToInclude = computeVisibleModIds(modId);
157179
Set<ResourceLocation> ourModelLocations;
158180
if (config == UniverseVisibility.SELF_AND_DEPS) {
159181
ModernFix.LOGGER.debug("Mod {} is restricted to seeing models from mods: [{}]", modId, String.join(", ", modIdsToInclude));

0 commit comments

Comments
 (0)