11package net .errorcraft .codecium .mixin .mojang .serialization .codecs ;
22
3- import com .llamalad7 .mixinextras .injector .ModifyReturnValue ;
3+ import com .llamalad7 .mixinextras .injector .ModifyExpressionValue ;
44import com .llamalad7 .mixinextras .sugar .Local ;
55import com .mojang .datafixers .util .Pair ;
66import com .mojang .serialization .DataResult ;
7+ import com .mojang .serialization .DynamicOps ;
78import com .mojang .serialization .codecs .ListCodec ;
9+ import net .errorcraft .codecium .access .mojang .serialization .codecs .ListCodecAccess ;
810import net .errorcraft .codecium .util .EnglishUtil ;
911import org .spongepowered .asm .mixin .Final ;
1012import org .spongepowered .asm .mixin .Mixin ;
1113import org .spongepowered .asm .mixin .Shadow ;
14+ import org .spongepowered .asm .mixin .Unique ;
1215import org .spongepowered .asm .mixin .injection .At ;
16+ import org .spongepowered .asm .mixin .injection .Inject ;
1317import org .spongepowered .asm .mixin .injection .ModifyArg ;
18+ import org .spongepowered .asm .mixin .injection .callback .CallbackInfoReturnable ;
1419
1520import java .util .List ;
1621import java .util .function .Supplier ;
1722
1823@ Mixin (value = ListCodec .class , remap = false )
19- public class ListCodecExtender <E > {
24+ public class ListCodecExtender <E > implements ListCodecAccess {
2025 @ Shadow
2126 @ Final
2227 private int minSize ;
@@ -25,6 +30,9 @@ public class ListCodecExtender<E> {
2530 @ Final
2631 private int maxSize ;
2732
33+ @ Unique
34+ private Object temporaryInput ;
35+
2836 @ ModifyArg (
2937 method = "createTooShortError" ,
3038 at = @ At (
@@ -47,11 +55,47 @@ private Supplier<String> tooLongUseBetterErrorMessage(Supplier<String> message,
4755 return () -> "List must have at most " + this .maxSize + " " + EnglishUtil .pluralize (this .maxSize , "value" ) + ", but got " + size ;
4856 }
4957
50- @ ModifyReturnValue (
58+ @ Inject (
5159 method = "decode" ,
52- at = @ At ("TAIL " )
60+ at = @ At ("HEAD " )
5361 )
54- private <T > DataResult <Pair <List <E >, T >> listErrorAddListToErrorMessage (DataResult <Pair <List <E >, T >> original , @ Local (argsOnly = true ) final T input ) {
55- return original .mapError (message -> message + ": " + input );
62+ private <T > void storeTemporaryInput (DynamicOps <T > ops , T input , CallbackInfoReturnable <DataResult <Pair <List <E >, T >>> info ) {
63+ this .temporaryInput = input ;
64+ }
65+
66+ @ Override
67+ public Object codecium$getAndRemoveTemporaryInput () {
68+ Object input = this .temporaryInput ;
69+ this .temporaryInput = null ;
70+ return input ;
71+ }
72+
73+ @ Mixin (targets = "com/mojang/serialization/codecs/ListCodec$DecoderState" , remap = false )
74+ public static class DecoderStateExtender <T > {
75+ @ Shadow
76+ @ Final
77+ ListCodec <T > this$0 ;
78+
79+ @ ModifyExpressionValue (
80+ method = "build" ,
81+ at = @ At (
82+ value = "INVOKE" ,
83+ target = "Lcom/mojang/serialization/codecs/ListCodec;createTooShortError(I)Lcom/mojang/serialization/DataResult;"
84+ )
85+ )
86+ private <R > DataResult <R > tooShortAddListToErrorMessage (DataResult <R > original ) {
87+ return original .mapError (message -> message + ": " + ((ListCodecAccess )(Object ) this .this$0 ).codecium$getAndRemoveTemporaryInput ());
88+ }
89+
90+ @ ModifyExpressionValue (
91+ method = "build" ,
92+ at = @ At (
93+ value = "INVOKE" ,
94+ target = "Lcom/mojang/serialization/codecs/ListCodec;createTooLongError(I)Lcom/mojang/serialization/DataResult;"
95+ )
96+ )
97+ private <R > DataResult <R > tooLongAddListToErrorMessage (DataResult <R > original ) {
98+ return original .mapError (message -> message + ": " + ((ListCodecAccess )(Object ) this .this$0 ).codecium$getAndRemoveTemporaryInput ());
99+ }
56100 }
57101}
0 commit comments