11package org .embeddedt .modernfix .forge .mixin .perf .dynamic_resources .ctm ;
22
33import com .google .common .collect .ImmutableList ;
4+ import it .unimi .dsi .fastutil .objects .Reference2ReferenceOpenHashMap ;
45import net .minecraft .client .renderer .ItemBlockRenderTypes ;
56import net .minecraft .client .renderer .RenderType ;
67import net .minecraft .client .resources .model .*;
1617import org .embeddedt .modernfix .annotation .RequiresMod ;
1718import org .embeddedt .modernfix .api .entrypoint .ModernFixClientIntegration ;
1819import org .embeddedt .modernfix .api .helpers .ModelHelpers ;
20+ import org .embeddedt .modernfix .forge .dynresources .ModernFixCTMPredicate ;
1921import org .spongepowered .asm .mixin .Final ;
2022import org .spongepowered .asm .mixin .Mixin ;
2123import org .spongepowered .asm .mixin .Overwrite ;
2729import team .chisel .ctm .client .util .CTMPackReloadListener ;
2830
2931import java .util .Map ;
30- import java .util .concurrent .ConcurrentHashMap ;
3132import java .util .function .Predicate ;
3233
3334@ Mixin (CTMPackReloadListener .class )
@@ -37,7 +38,7 @@ public abstract class CTMPackReloadListenerMixin implements ModernFixClientInteg
3738 /* caches the original render checks */
3839 @ Shadow (remap = false ) @ Final private static Map <IRegistryDelegate <Block >, Predicate <RenderType >> blockRenderChecks ;
3940
40- private static Map <IRegistryDelegate <Block >, Predicate < RenderType >> renderCheckOverrides = new ConcurrentHashMap <>() ;
41+ private static volatile Map <IRegistryDelegate <Block >, ModernFixCTMPredicate > mfixBouncerPredicates = null ;
4142
4243 private static Predicate <RenderType > DEFAULT_PREDICATE = type -> type == RenderType .solid ();
4344
@@ -50,33 +51,55 @@ private void onInit(CallbackInfo ci) {
5051 ModernFixClient .CLIENT_INTEGRATIONS .add (this );
5152 }
5253
54+ private static void initMap () {
55+ if (mfixBouncerPredicates == null ) {
56+ synchronized (CTMPackReloadListener .class ) {
57+ if (mfixBouncerPredicates == null ) {
58+ Map <IRegistryDelegate <Block >, ModernFixCTMPredicate > map = new Reference2ReferenceOpenHashMap <>();
59+ for (Block registeredBlock : ForgeRegistries .BLOCKS .getValues ()) {
60+ map .put (registeredBlock .delegate , new ModernFixCTMPredicate ());
61+ }
62+ mfixBouncerPredicates = map ;
63+ }
64+ }
65+ }
66+ }
67+
5368 /**
5469 * @author embeddedt
5570 * @reason handle layer changes dynamically
5671 */
5772 @ Overwrite (remap = false )
5873 private void refreshLayerHacks () {
59- renderCheckOverrides .clear ();
60- if (blockRenderChecks .isEmpty ()) {
61- for (Block block : ForgeRegistries .BLOCKS .getValues ()) {
62- Predicate <RenderType > original = this .getExistingRenderCheck (block );
63- if (original == null )
64- original = DEFAULT_PREDICATE ;
65- blockRenderChecks .put (block .delegate , original );
66- updateBlockPredicate (block );
67- }
68- }
74+ // Make sure predicate map exists
75+ initMap ();
76+ mfixBouncerPredicates .values ().forEach (bouncer -> bouncer .ctmOverride = null );
6977 }
7078
71- private void updateBlockPredicate (Block block ) {
72- ItemBlockRenderTypes .setRenderLayer (block , type -> this .useOverrideIfPresent (block .delegate , type ));
79+ private static ModernFixCTMPredicate getPredicateForBlock (Block block ) {
80+ initMap ();
81+ ModernFixCTMPredicate predicate = mfixBouncerPredicates .get (block .delegate );
82+ if (predicate == null ) {
83+ throw new NullPointerException ("ModernFix CTM predicate missing for block: " + block .getRegistryName ());
84+ }
85+ return predicate ;
7386 }
7487
75- private boolean useOverrideIfPresent (IRegistryDelegate <Block > delegate , RenderType type ) {
76- Predicate <RenderType > override = renderCheckOverrides .get (delegate );
77- if (override == null )
78- override = blockRenderChecks .get (delegate );
79- return override .test (type );
88+ private void updateBlockPredicate (Block block , Predicate <RenderType > override ) {
89+ Predicate <RenderType > original = this .getExistingRenderCheck (block );
90+ if (original == null ) {
91+ original = DEFAULT_PREDICATE ;
92+ }
93+ ModernFixCTMPredicate bouncer = getPredicateForBlock (block );
94+ if (original != bouncer ) {
95+ // Give the bouncer the original predicate for correct behavior
96+ bouncer .defaultPredicate = original ;
97+ synchronized (ItemBlockRenderTypes .class ) {
98+ blockRenderChecks .put (block .delegate , original );
99+ }
100+ }
101+ bouncer .ctmOverride = override ;
102+ ItemBlockRenderTypes .setRenderLayer (block , bouncer );
80103 }
81104
82105 @ Override
@@ -87,7 +110,7 @@ public BakedModel onBakedModelLoad(ResourceLocation location, UnbakedModel baseM
87110 return originalModel ;
88111 /* we construct a new ResourceLocation because an MRL is coming in */
89112 Block block = ForgeRegistries .BLOCKS .getValue (new ResourceLocation (location .getNamespace (), location .getPath ()));
90- if (block == null || block == Blocks .AIR || renderCheckOverrides . containsKey (block . delegate ) )
113+ if (block == null || block == Blocks .AIR || getPredicateForBlock (block ). ctmOverride != null )
91114 return originalModel ;
92115 /* find all states that match this MRL */
93116 ImmutableList <BlockState > allStates ;
@@ -100,8 +123,7 @@ public BakedModel onBakedModelLoad(ResourceLocation location, UnbakedModel baseM
100123 for (BlockState state : allStates ) {
101124 Predicate <RenderType > newPredicate = this .getLayerCheck (state , originalModel );
102125 if (newPredicate != null ) {
103- renderCheckOverrides .put (block .delegate , newPredicate );
104- updateBlockPredicate (block );
126+ updateBlockPredicate (block , newPredicate );
105127 return originalModel ;
106128 }
107129 }
0 commit comments