1- package games .moegirl .sinocraft .sinocore .fabric .client .resource ;
1+ package games .moegirl .sinocraft .sinocore .fabric .api . client .resource ;
22
33import com .google .gson .Gson ;
44import com .google .gson .JsonElement ;
1616import net .minecraft .world .level .block .Block ;
1717import org .jetbrains .annotations .Nullable ;
1818
19- import java .util .HashMap ;
20- import java .util .Map ;
19+ import java .util .*;
2120
2221/**
2322 * Load render type from model JSON like neoforge.
3231 */
3332public class RenderTypeLoader extends SimpleJsonResourceReloadListener implements IdentifiableResourceReloadListener {
3433
34+ private static final Set <Block > EXCLUDED_BLOCKS = new HashSet <>();
35+
36+ /**
37+ * Exclude blocks from being modified by this loader.
38+ * @param blocks Blocks to exclude.
39+ */
40+ public void exclude (Block ... blocks ) {
41+ EXCLUDED_BLOCKS .addAll (Arrays .asList (blocks ));
42+ }
43+
3544 private static final Map <Block , RenderType > MODIFIED_BLOCK_RENDER_TYPES = new HashMap <>();
3645
3746 private RenderTypeLoader () {
@@ -42,52 +51,77 @@ private RenderTypeLoader() {
4251 protected void apply (Map <ResourceLocation , JsonElement > map , ResourceManager resourceManager , ProfilerFiller profiler ) {
4352 profiler .push ("sinocore_render_type_loader" );
4453
54+ Map <Block , RenderType > toModify = new HashMap <>();
55+
56+ // Collect modifications.
4557 for (var entry : map .entrySet ()) {
4658 var key = entry .getKey ();
4759 var value = entry .getValue ();
4860
4961 if (key .getPath ().startsWith ("block/" )) {
5062 var id = ResourceLocation .fromNamespaceAndPath (key .getNamespace (), key .getPath ().substring ("block/" .length ()));
51- applyForBlock (id , value );
63+ var blockOptional = BuiltInRegistries .BLOCK .getOptional (id );
64+ if (blockOptional .isEmpty ()) {
65+ // Has no block with id, pass.
66+ continue ;
67+ }
68+ var block = blockOptional .get ();
69+ if (EXCLUDED_BLOCKS .contains (block )) {
70+ continue ;
71+ }
72+
73+ if (value instanceof JsonObject model ) {
74+ if (model .has ("render_type" )) {
75+ var typeId = ResourceLocation .parse (model .get ("render_type" ).getAsString ());
76+ RenderType renderType = getVanillaBlockRenderType (typeId );
77+ if (renderType != null ) {
78+ toModify .put (block , renderType );
79+ }
80+ }
81+ }
5282 }
5383 }
5484
55- profiler .pop ();
56- }
57-
58- private void applyForBlock (ResourceLocation id , JsonElement value ) {
59- var blockOptional = BuiltInRegistries .BLOCK .getOptional (id );
60- if (blockOptional .isEmpty ()) {
61- // Has no block with id, pass.
62- return ;
63- }
85+ // Restore changed previous modifications.
86+ var it = MODIFIED_BLOCK_RENDER_TYPES .entrySet ().iterator ();
87+ while (it .hasNext ()) {
88+ var entry = it .next ();
89+ var block = entry .getKey ();
90+ var renderType = entry .getValue ();
91+
92+ if (!toModify .containsKey (block )) {
93+ // Not modified now.
94+ var existing = ItemBlockRenderTypes .TYPE_BY_BLOCK .get (block );
95+ if (existing == renderType ) {
96+ // We modified before, but no need now, remove it.
97+ ItemBlockRenderTypes .TYPE_BY_BLOCK .remove (block );
98+ } // Else, somebody else modified it, leave it alone.
99+ it .remove ();
100+ }
64101
65- var block = blockOptional .get ();
66- var existing = ItemBlockRenderTypes .TYPE_BY_BLOCK .get (block );
67- var modified = MODIFIED_BLOCK_RENDER_TYPES .get (block );
68- if (value instanceof JsonObject model ) {
69- if (model .has ("render_type" )) {
70- var typeId = ResourceLocation .parse (model .get ("render_type" ).getAsString ());
71- RenderType renderType = getVanillaBlockRenderType (typeId );
72- if (renderType != null ) {
73- // It has a valid render type in model.
74- if (renderType != existing ) {
75- // Apply our modify.
76- BlockRenderLayerMap .INSTANCE .putBlock (block , renderType );
77- MODIFIED_BLOCK_RENDER_TYPES .put (block , renderType );
78- return ;
79- }
80- }
102+ var newRenderType = toModify .get (block );
103+ if (renderType == newRenderType ) {
104+ // Same as before, skip.
105+ toModify .remove (block );
81106 }
82107 }
83108
84- if (modified != null ) {
85- // It has a render type previously, but now not, remove it.
86- if (modified == existing ) {
87- ItemBlockRenderTypes .TYPE_BY_BLOCK .remove (block );
109+ // Apply new modifications.
110+ for (var entry : toModify .entrySet ()) {
111+ var block = entry .getKey ();
112+ var renderType = entry .getValue ();
113+
114+ var existing = ItemBlockRenderTypes .TYPE_BY_BLOCK .get (block );
115+ if (existing != null ) {
116+ // It has a render type already, we back-off.
117+ continue ;
88118 }
89- MODIFIED_BLOCK_RENDER_TYPES .remove (block );
119+
120+ BlockRenderLayerMap .INSTANCE .putBlock (block , renderType );
121+ MODIFIED_BLOCK_RENDER_TYPES .put (block , renderType );
90122 }
123+
124+ profiler .pop ();
91125 }
92126
93127 private static final ResourceLocation SOLID = ResourceLocation .withDefaultNamespace ("solid" );
0 commit comments