11package com .robotgryphon .compactcrafting .recipes .data ;
22
33import com .google .gson .JsonObject ;
4+ import com .mojang .serialization .DataResult ;
5+ import com .robotgryphon .compactcrafting .CompactCrafting ;
46import com .robotgryphon .compactcrafting .recipes .MiniaturizationRecipe ;
57import com .robotgryphon .compactcrafting .recipes .data .json .MiniaturizationRecipeJsonSerializer ;
8+ import net .minecraft .block .BlockState ;
9+ import net .minecraft .item .ItemStack ;
610import net .minecraft .item .crafting .IRecipeSerializer ;
11+ import net .minecraft .nbt .CompoundNBT ;
12+ import net .minecraft .nbt .INBT ;
13+ import net .minecraft .nbt .ListNBT ;
14+ import net .minecraft .nbt .NBTDynamicOps ;
715import net .minecraft .network .PacketBuffer ;
816import net .minecraft .util .ResourceLocation ;
17+ import net .minecraftforge .common .util .Constants ;
918import net .minecraftforge .registries .ForgeRegistryEntry ;
1019
1120import javax .annotation .Nullable ;
@@ -25,11 +34,74 @@ public MiniaturizationRecipe read(ResourceLocation recipeId, JsonObject json) {
2534 public MiniaturizationRecipe read (ResourceLocation recipeId , PacketBuffer buffer ) {
2635 MiniaturizationRecipe recipe = new MiniaturizationRecipe (recipeId );
2736
37+ CompoundNBT outputMeta = buffer .readCompoundTag ();
38+ if (outputMeta .getInt ("outputs" ) > 0 ) {
39+ int numOutputs = outputMeta .getInt ("outputs" );
40+ for (int out = 0 ; out < numOutputs ; out ++) {
41+ ItemStack output = buffer .readItemStack ();
42+ recipe .addOutput (output );
43+ }
44+ }
45+
46+ CompoundNBT components = buffer .readCompoundTag ();
47+ int numComponents = components .getInt ("count" );
48+ if (numComponents > 0 ) {
49+ ListNBT compList = components .getList ("components" , Constants .NBT .TAG_COMPOUND );
50+ compList .forEach (comp -> {
51+ CompoundNBT compC = (CompoundNBT ) comp ;
52+ String key = compC .getString ("key" );
53+ CompoundNBT stateTag = compC .getCompound ("state" );
54+
55+ BlockState .CODEC .decode (NBTDynamicOps .INSTANCE , stateTag )
56+ .resultOrPartial (CompactCrafting .LOGGER ::error )
57+ .ifPresent (state -> {
58+ BlockState compState = state .getFirst ();
59+ recipe .addComponent (key , compState );
60+
61+ CompactCrafting .LOGGER .debug ("Got component: {} ({})" , key , compState .toString ());
62+ });
63+ });
64+ }
2865 return recipe ;
2966 }
3067
3168 @ Override
3269 public void write (PacketBuffer buffer , MiniaturizationRecipe recipe ) {
70+ ItemStack [] outputs = recipe .getOutputs ();
71+
72+ CompoundNBT outputMeta = new CompoundNBT ();
73+ outputMeta .putInt ("outputs" , outputs .length );
74+ buffer .writeCompoundTag (outputMeta );
75+
76+ if (outputs .length > 0 ) {
77+ for (ItemStack out : outputs ) buffer .writeItemStack (out );
78+ }
79+
80+ try {
81+ CompoundNBT componentMeta = new CompoundNBT ();
82+ int numComponents = recipe .getComponentKeys ().size ();
83+ componentMeta .putInt ("count" , numComponents );
84+
85+ if (numComponents > 0 ) {
86+ ListNBT compList = new ListNBT ();
87+ recipe .getComponents ().forEach ((key , state ) -> {
88+ DataResult <INBT > encode = BlockState .CODEC .encode (state , NBTDynamicOps .INSTANCE , null );
89+ encode
90+ .resultOrPartial (CompactCrafting .LOGGER ::error )
91+ .ifPresent (stateNbt -> {
92+ CompoundNBT componentTag = new CompoundNBT ();
93+ componentTag .put ("state" , stateNbt );
94+ componentTag .putString ("key" , key );
95+
96+ compList .add (componentTag );
97+ });
98+ });
99+
100+ componentMeta .put ("components" , compList );
101+ }
33102
103+ buffer .writeCompoundTag (componentMeta );
104+ } catch (Exception ex ) {
105+ }
34106 }
35107}
0 commit comments