11package com .rae .creatingspace .mixin .recipe ;
22
3- import com .google .gson .JsonElement ;
4- import com .google .gson .JsonObject ;
3+ import com .mojang .serialization .*;
54import com .rae .creatingspace .content .recipes .IMoreNbtConditions ;
65import com .simibubi .create .content .processing .sequenced .SequencedAssemblyRecipe ;
76import com .simibubi .create .content .processing .sequenced .SequencedAssemblyRecipeSerializer ;
8- import net .minecraft .resources .ResourceLocation ;
9- import net .minecraft .util .GsonHelper ;
7+ import io .netty .buffer .ByteBuf ;
8+ import net .minecraft .network .RegistryFriendlyByteBuf ;
9+ import net .minecraft .network .codec .ByteBufCodecs ;
10+ import net .minecraft .network .codec .StreamCodec ;
11+ import org .spongepowered .asm .mixin .Final ;
1012import org .spongepowered .asm .mixin .Mixin ;
13+ import org .spongepowered .asm .mixin .Shadow ;
14+ import org .spongepowered .asm .mixin .Unique ;
1115import org .spongepowered .asm .mixin .injection .At ;
1216import org .spongepowered .asm .mixin .injection .Inject ;
17+ import org .spongepowered .asm .mixin .injection .callback .CallbackInfo ;
1318import org .spongepowered .asm .mixin .injection .callback .CallbackInfoReturnable ;
1419
1520import java .util .ArrayList ;
21+ import java .util .List ;
22+ import java .util .stream .Stream ;
1623
1724@ Mixin (value = SequencedAssemblyRecipeSerializer .class )
1825public abstract class SequencedAssemblyRecipeSerializerMixin {
19- @ Inject (method = "readFromJson" , at = @ At ("RETURN" ), remap = false , cancellable = true )
20- public void readKeepNbtJson (ResourceLocation recipeId , JsonObject json , CallbackInfoReturnable <SequencedAssemblyRecipe > cir ) {
21- SequencedAssemblyRecipe recipe = cir .getReturnValue ();
22- if (GsonHelper .isValidNode (json , "keepNbt" )) {
23- ArrayList <String > collector = new ArrayList <>();
24- for (JsonElement ob : GsonHelper .getAsJsonArray (json , "keepNbt" )) {
25- collector .add (ob .getAsString ());
26- }
27- ((IMoreNbtConditions ) recipe ).setKeepNbt (collector );
2826
27+ @ Shadow @ Final private MapCodec <SequencedAssemblyRecipe > CODEC ;
28+ final MapCodec <SequencedAssemblyRecipe > NEW_CODEC = new MapCodec <>() {
29+ @ Override
30+ public <T > RecordBuilder <T > encode (SequencedAssemblyRecipe input , DynamicOps <T > ops , RecordBuilder <T > prefix ) {
31+ return cS_1_21_1$encode (input , ops , prefix );
2932 }
30- if (GsonHelper .isValidNode (json , "machNbt" )) {
31- ArrayList <String > collector = new ArrayList <>();
32- for (JsonElement ob : GsonHelper .getAsJsonArray (json , "machNbt" )) {
33- collector .add (ob .getAsString ());
34- }
35- ((IMoreNbtConditions ) recipe ).setMachNbt (collector );
33+
34+ @ Override
35+ public <T > DataResult <SequencedAssemblyRecipe > decode (DynamicOps <T > ops , MapLike <T > input ) {
36+ return cS_1_21_1$decode (ops , input );
3637 }
38+
39+ @ Override
40+ public <T > Stream <T > keys (DynamicOps <T > ops ) {
41+ return Stream .concat (
42+ CODEC .keys (ops ),
43+ Stream .of (ops .createString ("keepNbt" ), ops .createString ("matchNbt" ))
44+ );
45+ }
46+ };
47+
48+
49+ //Black Magic
50+ @ Unique
51+ private <T > DataResult <SequencedAssemblyRecipe > cS_1_21_1$decode (DynamicOps <T > ops , MapLike <T > input ) {
52+ return CODEC .decode (ops , input ).flatMap (assemblyRecipe -> {
53+
54+ DataResult <List <String >> keepNbtResult = Codec .STRING .listOf ().parse (ops ,input .get (ops .createString ("keepNbt" )));
55+
56+ DataResult <List <String >> matchNbtResult = Codec .STRING .listOf ().parse (ops ,input .get (ops .createString ("matchNbt" )));
57+
58+ return keepNbtResult .flatMap (keepNbt ->
59+ matchNbtResult .map (matchNbt -> {
60+ ((IMoreNbtConditions )assemblyRecipe ).setKeepNbt (new ArrayList <>(keepNbt ));
61+ ((IMoreNbtConditions )assemblyRecipe ).setMachNbt (new ArrayList <>(matchNbt ));
62+ return assemblyRecipe ;
63+ })
64+ );
65+ });
66+ }
67+
68+ @ Unique
69+ private <T > RecordBuilder <T > cS_1_21_1$encode (SequencedAssemblyRecipe recipe , DynamicOps <T > ops , RecordBuilder <T > prefix ) {
70+ return CODEC .encode (recipe , ops , prefix )
71+ .add ("keepNbt" , Codec .STRING .listOf ().encodeStart (ops , ((IMoreNbtConditions ) recipe ).getKeepNbt ()))
72+ .add ("matchNbt" , Codec .STRING .listOf ().encodeStart (ops , ((IMoreNbtConditions ) recipe ).getMachNbt ()));
73+ }
74+
75+ private static final
76+ @ Unique
77+ StreamCodec <ByteBuf , List <String >> STRING_LIST_STREAM_CODEC =
78+ ByteBufCodecs .fromCodec (Codec .list (Codec .STRING ));
79+
80+ @ Inject (method = "fromNetwork" , at = @ At ("RETURN" ), cancellable = true )
81+ public void readKeepNbt (RegistryFriendlyByteBuf buffer , CallbackInfoReturnable <SequencedAssemblyRecipe > cir ) {
82+ SequencedAssemblyRecipe recipe = cir .getReturnValue ();
83+ ((IMoreNbtConditions ) recipe ).setKeepNbt (new ArrayList <>(STRING_LIST_STREAM_CODEC .decode (buffer )));
84+ ((IMoreNbtConditions ) recipe ).setMachNbt (new ArrayList <>(STRING_LIST_STREAM_CODEC .decode (buffer )));
3785 cir .setReturnValue (recipe );
3886 }
87+
88+ @ Inject (method = "toNetwork" , at = @ At ("RETURN" ))
89+ public void writeKeepNbt (RegistryFriendlyByteBuf buffer , SequencedAssemblyRecipe recipe , CallbackInfo ci ) {
90+ STRING_LIST_STREAM_CODEC .encode (buffer , ((IMoreNbtConditions ) recipe ).getKeepNbt ());
91+ STRING_LIST_STREAM_CODEC .encode (buffer , ((IMoreNbtConditions ) recipe ).getMachNbt ());
92+ }
93+
94+ @ Inject (method = "codec" , at = @ At ("RETURN" ))
95+ public void addToCodec (CallbackInfoReturnable <MapCodec <SequencedAssemblyRecipe >> cir ) {
96+ cir .setReturnValue (NEW_CODEC );
97+ }
3998}
0 commit comments