11package dev .compactmods .crafting .recipes ;
22
3+ import java .util .Collections ;
34import java .util .List ;
45import java .util .Map ;
6+ import java .util .Optional ;
57import com .google .common .collect .ImmutableList ;
68import com .mojang .datafixers .util .Pair ;
7- import com .mojang .serialization .Codec ;
8- import com .mojang .serialization .DataResult ;
9- import com .mojang .serialization .DynamicOps ;
10- import com .mojang .serialization .RecordBuilder ;
9+ import com .mojang .serialization .*;
1110import dev .compactmods .crafting .CompactCrafting ;
12- import dev .compactmods .crafting .recipes .components .RecipeComponentTypeCodec ;
13- import dev .compactmods .crafting .recipes .layers .RecipeLayerTypeCodec ;
14- import dev .compactmods .crafting .server .ServerConfig ;
1511import dev .compactmods .crafting .api .components .IRecipeComponent ;
1612import dev .compactmods .crafting .api .components .RecipeComponentType ;
1713import dev .compactmods .crafting .api .field .FieldProjectionSize ;
1814import dev .compactmods .crafting .api .recipe .layers .IRecipeLayer ;
1915import dev .compactmods .crafting .api .recipe .layers .RecipeLayerType ;
2016import dev .compactmods .crafting .api .recipe .layers .dim .IFixedSizedRecipeLayer ;
17+ import dev .compactmods .crafting .recipes .components .RecipeComponentTypeCodec ;
18+ import dev .compactmods .crafting .recipes .layers .RecipeLayerTypeCodec ;
19+ import dev .compactmods .crafting .server .ServerConfig ;
2120import net .minecraft .item .ItemStack ;
2221
2322public class MiniaturizationRecipeCodec implements Codec <MiniaturizationRecipe > {
@@ -38,53 +37,85 @@ public <T> DataResult<Pair<MiniaturizationRecipe, T>> decode(DynamicOps<T> ops,
3837 CompactCrafting .RECIPE_LOGGER .debug ("Starting recipe decode: {}" , input .toString ());
3938 }
4039
40+ MiniaturizationRecipe recipe = new MiniaturizationRecipe ();
41+ StringBuilder errorBuilder = new StringBuilder ();
42+
4143 int recipeSize = Codec .INT .optionalFieldOf ("recipeSize" , -1 )
4244 .codec ()
4345 .parse (ops , input )
44- .result ().get ();
46+ .result ()
47+ .get ();
4548
46- ItemStack catalyst = ItemStack .CODEC .fieldOf ("catalyst" ).codec ()
47- .parse (ops , input )
48- .resultOrPartial (CompactCrafting .RECIPE_LOGGER ::error )
49- .orElse (ItemStack .EMPTY );
49+ recipe .setRecipeSize (recipeSize );
5050
51- List <IRecipeLayer > layers = LAYER_CODEC .listOf ().fieldOf ("layers" ).codec ()
52- .parse (ops , input )
53- .resultOrPartial (CompactCrafting .RECIPE_LOGGER ::error )
54- .get ();
51+ final DataResult <List <IRecipeLayer >> layers = LAYER_CODEC .listOf ()
52+ .fieldOf ("layers" ).codec ()
53+ .parse (ops , input );
5554
56- List <ItemStack > outputs = ItemStack .CODEC .listOf ().fieldOf ("outputs" ).codec ()
57- .parse (ops , input )
58- .resultOrPartial (CompactCrafting .RECIPE_LOGGER ::error )
59- .get ();
55+ if (layers .error ().isPresent ()) {
56+ final Optional <List <IRecipeLayer >> partialLayers = layers .resultOrPartial (errorBuilder ::append );
57+ partialLayers .ifPresent (recipe ::applyLayers );
58+ return DataResult .error (errorBuilder .toString (), Pair .of (recipe , input ), Lifecycle .stable ());
59+ }
6060
61- Map < String , IRecipeComponent > components = Codec . unboundedMap ( Codec . STRING , COMPONENT_CODEC ). fieldOf ( "components" )
62- .codec ( )
63- .parse ( ops , input )
64- . resultOrPartial ( CompactCrafting . RECIPE_LOGGER :: error )
65- . get ( );
61+ final List < IRecipeLayer > layerList = layers
62+ .resultOrPartial ( errorBuilder :: append )
63+ .orElse ( Collections . emptyList ());
64+
65+ recipe . applyLayers ( layerList );
6666
67- boolean hasFixedLayers = layers .stream ().anyMatch (l -> l instanceof IFixedSizedRecipeLayer );
67+ boolean hasFixedLayers = layerList .stream ().anyMatch (l -> l instanceof IFixedSizedRecipeLayer );
6868 if (debugOutput ) {
69- CompactCrafting .RECIPE_LOGGER .debug ("Number of layers defined: {}" , layers .size ());
69+ CompactCrafting .RECIPE_LOGGER .debug ("Number of layers defined: {}" , layerList .size ());
7070 CompactCrafting .RECIPE_LOGGER .debug ("Is fixed size: {}" , hasFixedLayers );
7171 }
7272
7373 // if we don't have a fixed size layer to base dimensions off of, and the recipe size won't fit in a field
7474 if (!hasFixedLayers && !FieldProjectionSize .canFitDimensions (recipeSize )) {
75- MiniaturizationRecipe partial = new MiniaturizationRecipe (layers , catalyst , outputs , components );
76- return DataResult .error (
77- "Specified recipe size will not fit in a crafting field: " + recipeSize ,
78- Pair .of (partial , input ));
75+ errorBuilder .append ("Specified recipe size will not fit in a crafting field: " ).append (recipeSize );
76+ return DataResult .error (errorBuilder .toString (), Pair .of (recipe , input ), Lifecycle .stable ());
7977 }
8078
81- MiniaturizationRecipe recipe = new MiniaturizationRecipe (recipeSize , layers , catalyst , outputs , components );
8279 recipe .recalculateDimensions ();
8380
81+ ItemStack catalyst = ItemStack .CODEC .fieldOf ("catalyst" ).codec ()
82+ .parse (ops , input )
83+ .resultOrPartial (errorBuilder ::append )
84+ .orElse (ItemStack .EMPTY );
85+
86+ if (catalyst .isEmpty ()) {
87+ CompactCrafting .LOGGER .warn ("Warning: recipe has no catalyst; this may be unintentional." );
88+ }
89+
90+ Optional <List <ItemStack >> outputs = ItemStack .CODEC .listOf ().fieldOf ("outputs" ).codec ()
91+ .parse (ops , input )
92+ .resultOrPartial (errorBuilder ::append );
93+
94+ outputs .ifPresent (recipe ::setOutputs );
95+ if (!outputs .isPresent ()) {
96+ return DataResult .error (errorBuilder .toString (), Pair .of (recipe , input ), Lifecycle .stable ());
97+ }
98+
99+ if (recipe .getOutputs ().length == 0 ) {
100+ errorBuilder .append ("No outputs were defined." );
101+ return DataResult .error (errorBuilder .toString (), Pair .of (recipe , input ), Lifecycle .stable ());
102+ }
103+
104+ Optional <Map <String , IRecipeComponent >> components = Codec .unboundedMap (Codec .STRING , COMPONENT_CODEC )
105+ .optionalFieldOf ("components" , Collections .emptyMap ())
106+ .codec ()
107+ .parse (ops , input )
108+ .resultOrPartial (errorBuilder ::append );
109+
110+ components .ifPresent (compNode -> {
111+ CompactCrafting .RECIPE_LOGGER .trace ("Got components map; checking any exist and applying to recipe." );
112+ recipe .applyComponents (compNode );
113+ });
114+
84115 if (debugOutput )
85116 CompactCrafting .RECIPE_LOGGER .debug ("Finishing recipe decode." );
86117
87- return DataResult .success (Pair .of (recipe , input ));
118+ return DataResult .success (Pair .of (recipe , input ), Lifecycle . stable () );
88119 }
89120
90121 @ Override
@@ -110,7 +141,7 @@ public <T> DataResult<T> encode(MiniaturizationRecipe recipe, DynamicOps<T> ops,
110141 builder .add ("type" , Codec .STRING .encodeStart (ops , "compactcrafting:miniaturization" ));
111142
112143 if (recipe .hasSpecifiedSize ())
113- builder .add ("recipeSize" , Codec .INT .encodeStart (ops , recipe .getSize ()));
144+ builder .add ("recipeSize" , Codec .INT .encodeStart (ops , recipe .getRecipeSize ()));
114145
115146 return builder .add ("layers" , layers )
116147 .add ("components" , components )
0 commit comments