11package com .robotgryphon .compactcrafting .recipes .json ;
22
33import com .google .gson .*;
4+ import com .mojang .datafixers .util .Pair ;
5+ import com .mojang .serialization .JsonOps ;
46import com .robotgryphon .compactcrafting .CompactCrafting ;
57import com .robotgryphon .compactcrafting .recipes .MiniaturizationRecipe ;
68import com .robotgryphon .compactcrafting .recipes .MiniaturizationRecipeManager ;
79import com .robotgryphon .compactcrafting .recipes .layers .IRecipeLayer ;
10+ import net .minecraft .block .BlockState ;
811import net .minecraft .client .resources .JsonReloadListener ;
12+ import net .minecraft .item .ItemStack ;
913import net .minecraft .profiler .IProfiler ;
1014import net .minecraft .resources .IResourceManager ;
1115import net .minecraft .util .ResourceLocation ;
1216
1317import java .util .Arrays ;
1418import java .util .Collections ;
1519import java .util .Map ;
20+ import java .util .Optional ;
1621
1722public class MiniaturizationPatternLoader extends JsonReloadListener {
1823 public MiniaturizationPatternLoader () {
@@ -33,32 +38,100 @@ protected void apply(Map<ResourceLocation, JsonElement> objectIn, IResourceManag
3338 continue ;
3439 }
3540
41+ // TODO: Eventually we want to have a version spec here, but for now we're just going to require it for future updates
3642 int version = root .get ("version" ).getAsInt ();
3743 if (version <= 0 ) {
3844 CompactCrafting .LOGGER .debug ("Skipping pattern loading for recipe " + rl .toString () + "; version must be at least 1." );
3945 continue ;
4046 }
4147
42- // TODO: Eventually we want to have a version spec here, but for now we're just going to require it for future updates
48+ MiniaturizationRecipe recipe = new MiniaturizationRecipe ( rl );
4349
44- if (! root . has ( "layers" )) {
45- CompactCrafting . LOGGER . debug ( "Skipping pattern loading for recipe " + rl . toString () + "; no layers defined." );
50+ boolean layersLoaded = loadLayers ( recipe , root );
51+ if (! layersLoaded )
4652 continue ;
47- }
48-
49- JsonArray layers = root .get ("layers" ).getAsJsonArray ();
50- LayerDeserializer layerJsonSerializer = new LayerDeserializer ();
51- Gson g = new GsonBuilder ()
52- .registerTypeAdapter (IRecipeLayer .class , layerJsonSerializer )
53- .create ();
5453
55- IRecipeLayer [] iRecipeLayers = g .fromJson (layers , IRecipeLayer [].class );
56- Collections .reverse (Arrays .asList (iRecipeLayers ));
54+ loadComponents (root , recipe );
5755
58- MiniaturizationRecipe recipe = new MiniaturizationRecipe ();
59- recipe .setLayers (iRecipeLayers );
56+ loadCatalyst (recipe , root );
6057
6158 MiniaturizationRecipeManager .add (rl , recipe );
6259 }
6360 }
61+
62+ private boolean loadCatalyst (MiniaturizationRecipe recipe , JsonObject root ) {
63+ if (!root .has ("catalyst" )) {
64+ CompactCrafting .LOGGER .warn ("Catalyst entry not found for recipe {}; skipping rest of recipe loading." , recipe .getRegistryName ());
65+ return false ;
66+ }
67+
68+ JsonObject catalyst = root .getAsJsonObject ("catalyst" );
69+
70+ Optional <ItemStack > stack = ItemStack .CODEC .decode (JsonOps .INSTANCE , catalyst )
71+ .get ()
72+ .ifRight (err -> CompactCrafting .LOGGER .warn ("Failed to load itemstack for catalyst: {}" , err .message ()))
73+ .mapLeft (Pair ::getFirst )
74+ .left ();
75+
76+ if (!stack .isPresent ())
77+ return false ;
78+
79+ ItemStack c = stack .get ();
80+
81+ if (c .getCount () != 1 ) {
82+ CompactCrafting .LOGGER .warn ("Catalyst definition called for a non-1 count; this is not yet supported." );
83+ c .setCount (1 );
84+ }
85+
86+ recipe .catalyst = c ;
87+
88+ return true ;
89+ }
90+
91+ private boolean loadLayers (MiniaturizationRecipe recipe , JsonObject root ) {
92+ if (!root .has ("layers" )) {
93+ CompactCrafting .LOGGER .debug ("Skipping pattern loading for recipe " + recipe .getRegistryName ().toString () + "; no layers defined." );
94+ return false ;
95+ }
96+
97+ JsonArray layers = root .get ("layers" ).getAsJsonArray ();
98+ LayerDeserializer layerJsonSerializer = new LayerDeserializer ();
99+ Gson g = new GsonBuilder ()
100+ .registerTypeAdapter (IRecipeLayer .class , layerJsonSerializer )
101+ .create ();
102+
103+ IRecipeLayer [] iRecipeLayers = g .fromJson (layers , IRecipeLayer [].class );
104+ Collections .reverse (Arrays .asList (iRecipeLayers ));
105+
106+
107+ recipe .setLayers (iRecipeLayers );
108+ return true ;
109+ }
110+
111+ private void loadComponents (JsonObject root , MiniaturizationRecipe recipe ) {
112+ JsonObject components = root .get ("components" ).getAsJsonObject ();
113+ if (components .size () == 0 ) {
114+ throw new JsonParseException ("Error: No components defined." );
115+ }
116+
117+ components .entrySet ().forEach (component -> {
118+ String key = component .getKey ();
119+ Optional <BlockState > state = extractComponentDefinition (key , component .getValue ());
120+
121+ if (key .isEmpty () || !state .isPresent ()) {
122+ CompactCrafting .LOGGER .warn ("Failed to process blockstate for component {}; definition not found." , key );
123+ return ;
124+ }
125+
126+ recipe .addComponent (key , state .get ());
127+ });
128+ }
129+
130+ private Optional <BlockState > extractComponentDefinition (String key , JsonElement definition ) {
131+ JsonObject comp = definition .getAsJsonObject ();
132+ return BlockState .CODEC .decode (JsonOps .INSTANCE , comp )
133+ .get ().ifRight (error -> {
134+ CompactCrafting .LOGGER .warn ("Failed to process blockstate for component {}: {}" , key , error .message ());
135+ }).mapLeft (Pair ::getFirst ).left ();
136+ }
64137}
0 commit comments