11package org .embeddedt .modernfix .forge .dynresources ;
22
33import com .google .common .collect .ForwardingMap ;
4+ import com .google .common .collect .Sets ;
5+ import com .google .common .graph .GraphBuilder ;
6+ import com .google .common .graph .MutableGraph ;
47import net .minecraft .client .resources .model .BakedModel ;
58import net .minecraft .resources .ResourceLocation ;
69import net .minecraft .world .item .Item ;
710import net .minecraft .world .level .block .Block ;
811import net .minecraft .world .level .block .state .BlockState ;
12+ import net .minecraftforge .fml .ModContainer ;
13+ import net .minecraftforge .fml .ModList ;
14+ import net .minecraftforge .forgespi .language .IModInfo ;
915import net .minecraftforge .registries .ForgeRegistries ;
1016import org .embeddedt .modernfix .dynamicresources .ModelLocationCache ;
1117import org .jetbrains .annotations .Nullable ;
1218
1319import java .util .HashSet ;
1420import java .util .Map ;
21+ import java .util .Optional ;
1522import java .util .Set ;
1623
24+ /**
25+ * Stores a list of all known default block/item models in the game, and provides a namespaced version
26+ * of the model registry that emulates vanilla keySet behavior.
27+ */
1728public class ModelBakeEventHelper {
18- public static Map <ResourceLocation , BakedModel > wrapRegistry (Map <ResourceLocation , BakedModel > modelRegistry ) {
19- Set <ResourceLocation > topLevelModelLocations = new HashSet <>(modelRegistry .keySet ());
29+ private final Map <ResourceLocation , BakedModel > modelRegistry ;
30+ private final Set <ResourceLocation > topLevelModelLocations ;
31+ private final MutableGraph <String > dependencyGraph ;
32+ public ModelBakeEventHelper (Map <ResourceLocation , BakedModel > modelRegistry ) {
33+ this .modelRegistry = modelRegistry ;
34+ this .topLevelModelLocations = new HashSet <>(modelRegistry .keySet ());
2035 for (Block block : ForgeRegistries .BLOCKS ) {
2136 for (BlockState state : block .getStateDefinition ().getPossibleStates ()) {
2237 topLevelModelLocations .add (ModelLocationCache .get (state ));
@@ -25,6 +40,28 @@ public static Map<ResourceLocation, BakedModel> wrapRegistry(Map<ResourceLocatio
2540 for (Item item : ForgeRegistries .ITEMS ) {
2641 topLevelModelLocations .add (ModelLocationCache .get (item ));
2742 }
43+ this .dependencyGraph = GraphBuilder .undirected ().build ();
44+ ModList .get ().forEachModContainer ((id , mc ) -> {
45+ this .dependencyGraph .addNode (id );
46+ });
47+ for (String id : this .dependencyGraph .nodes ()) {
48+ Optional <? extends ModContainer > mContainer = ModList .get ().getModContainerById (id );
49+ if (mContainer .isPresent ()) {
50+ for (IModInfo .ModVersion version : mContainer .get ().getModInfo ().getDependencies ()) {
51+ this .dependencyGraph .putEdge (id , version .getModId ());
52+ }
53+ }
54+ }
55+ }
56+
57+ public Map <ResourceLocation , BakedModel > wrapRegistry (String modId ) {
58+ final Set <String > modIdsToInclude = new HashSet <>();
59+ modIdsToInclude .add (modId );
60+ try {
61+ modIdsToInclude .addAll (this .dependencyGraph .adjacentNodes (modId ));
62+ } catch (IllegalArgumentException ignored ) { /* sanity check */ }
63+ modIdsToInclude .remove ("minecraft" );
64+ Set <ResourceLocation > ourModelLocations = Sets .filter (this .topLevelModelLocations , loc -> modIdsToInclude .contains (loc .getNamespace ()));
2865 return new ForwardingMap <ResourceLocation , BakedModel >() {
2966 @ Override
3067 protected Map <ResourceLocation , BakedModel > delegate () {
@@ -33,18 +70,12 @@ protected Map<ResourceLocation, BakedModel> delegate() {
3370
3471 @ Override
3572 public Set <ResourceLocation > keySet () {
36- return topLevelModelLocations ;
73+ return ourModelLocations ;
3774 }
3875
3976 @ Override
4077 public boolean containsKey (@ Nullable Object key ) {
41- return topLevelModelLocations .contains (key ) || super .containsKey (key );
42- }
43-
44- @ Override
45- public BakedModel put (ResourceLocation key , BakedModel value ) {
46- topLevelModelLocations .add (key );
47- return super .put (key , value );
78+ return ourModelLocations .contains (key ) || super .containsKey (key );
4879 }
4980 };
5081 }
0 commit comments