3232import java .io .Reader ;
3333import java .util .List ;
3434import java .util .Map ;
35+ import java .util .Optional ;
3536import java .util .Set ;
37+ import java .util .function .BiFunction ;
3638import java .util .stream .Collectors ;
3739
3840public class DynamicModelSystem {
@@ -65,6 +67,10 @@ public interface SingleBlockStateEntryLoader {
6567 BlockStateModelLoader .LoadedModels loadEntry (Identifier identifier , List <Resource > blockstateResources );
6668 }
6769
70+ public static Set <BlockState > getAllBlockStates () {
71+ return ((IdMapperAccessor <BlockState >)Block .BLOCK_STATE_REGISTRY ).getReferenceMap ().keySet ();
72+ }
73+
6874 public static BlockStateModelLoader .LoadedModels createDynamicBlockStateLoadedModels (Map <Identifier , List <Resource >> resourceMap , SingleBlockStateEntryLoader entryLoader ) {
6975 LoadingCache <Identifier , BlockStateModelLoader .LoadedModels > definitionCache = CacheBuilder .newBuilder ().softValues ().maximumSize (1000 ).build (new CacheLoader <>() {
7076 @ Override
@@ -77,8 +83,7 @@ public BlockStateModelLoader.LoadedModels load(Identifier key) throws Exception
7783 return entryLoader .loadEntry (file , resources );
7884 }
7985 });
80- Set <BlockState > allStates = ((IdMapperAccessor <BlockState >)Block .BLOCK_STATE_REGISTRY ).getReferenceMap ().keySet ();
81- return new BlockStateModelLoader .LoadedModels (Maps .asMap (allStates , state -> {
86+ return new BlockStateModelLoader .LoadedModels (Maps .asMap (getAllBlockStates (), state -> {
8287 var identifier = state .getBlock ().builtInRegistryHolder ().getKey ().identifier ();
8388 var loadedModels = definitionCache .getUnchecked (identifier );
8489 return loadedModels .models ().get (state );
@@ -148,4 +153,23 @@ public int getInt(Object key) {
148153 return -1 ;
149154 }
150155 }
156+
157+ public static <K , U , V > Map <K , V > createDynamicBakedRegistry (Map <K , U > input , BiFunction <K , U , V > baker ) {
158+ // TODO: support persistence of overrides
159+ LoadingCache <K , Optional <V >> bakedCache = CacheBuilder .newBuilder ().softValues ().maximumSize (1000 ).build (new CacheLoader <>() {
160+ @ Override
161+ public Optional <V > load (K key ) throws Exception {
162+ var unbaked = input .get (key );
163+ if (unbaked != null ) {
164+ if (DEBUG_DYNAMIC_MODEL_LOADING ) {
165+ ModernFix .LOGGER .info ("Baking {}" , key );
166+ }
167+ return Optional .ofNullable (baker .apply (key , unbaked ));
168+ } else {
169+ return Optional .empty ();
170+ }
171+ }
172+ });
173+ return Maps .asMap (input .keySet (), k -> k != null ? bakedCache .getUnchecked (k ).orElse (null ) : null );
174+ }
151175}
0 commit comments