8
8
import io .netty .buffer .ByteBuf ;
9
9
import net .minecraft .network .codec .ByteBufCodecs ;
10
10
import net .minecraft .network .codec .StreamCodec ;
11
- import org .jetbrains .annotations .NotNull ;
12
11
13
12
import java .util .ArrayList ;
14
13
import java .util .Arrays ;
15
14
import java .util .List ;
16
15
import java .util .function .Function ;
17
16
18
- public record CompoundGradient (List <PositionedColor > colors , PositionedColor [] sorted ) implements Gradient {
19
- public record PositionedColor (float position , Color color , Easing easing ) implements Comparable <PositionedColor > {
20
- public static final Codec <PositionedColor > CODEC = RecordCodecBuilder .create (instance -> instance .group (
21
- Codec .FLOAT .fieldOf ("position" ).forGetter (PositionedColor ::position ),
22
- Color .CODEC .fieldOf ("colors" ).forGetter (PositionedColor ::color ),
23
- Easing .CODEC .optionalFieldOf ("easing" , Easing .LINEAR ).forGetter (PositionedColor ::easing )
24
- ).apply (instance , PositionedColor ::new ));
25
-
26
- public static final StreamCodec <ByteBuf , PositionedColor > STREAM_CODEC = StreamCodec .composite (
27
- ByteBufCodecs .FLOAT , PositionedColor ::position ,
28
- Color .STREAM_CODEC , PositionedColor ::color ,
29
- Easing .STREAM_CODEC , PositionedColor ::easing ,
30
- PositionedColor ::new
31
- );
32
-
33
- @ Override
34
- public int compareTo (@ NotNull PositionedColor other ) {
35
- return Float .compare (position , other .position );
36
- }
37
- }
38
-
17
+ public record CompoundGradient (PositionedColor [] sorted ) implements Gradient {
39
18
public static final Codec <CompoundGradient > DIRECT_CODEC = RecordCodecBuilder .create (instance -> instance .group (
40
- PositionedColor .CODEC .listOf ().fieldOf ("colors" ).forGetter (CompoundGradient ::colors )
19
+ PositionedColor .CODEC .listOf ().fieldOf ("colors" ).forGetter (CompoundGradient ::getColorList )
41
20
).apply (instance , CompoundGradient ::new ));
42
21
43
22
public static CompoundGradient ofColors (List <Color > colors ) {
@@ -51,10 +30,10 @@ public static CompoundGradient ofColors(List<Color> colors) {
51
30
}
52
31
53
32
public static final Codec <CompoundGradient > CODEC = Codec .either (DIRECT_CODEC , Color .CODEC .listOf ()).xmap (e -> e .map (Function .identity (), CompoundGradient ::ofColors ), g -> g .isSimple () ? Either .right (g .rawColors ()) : Either .left (g ));
54
- public static final StreamCodec <ByteBuf , CompoundGradient > STREAM_CODEC = PositionedColor .STREAM_CODEC .apply (ByteBufCodecs .list ()).map (CompoundGradient ::new , CompoundGradient ::colors );
33
+ public static final StreamCodec <ByteBuf , CompoundGradient > STREAM_CODEC = PositionedColor .STREAM_CODEC .apply (ByteBufCodecs .list ()).map (CompoundGradient ::new , CompoundGradient ::getColorList );
55
34
56
35
public CompoundGradient (List <PositionedColor > colors ) {
57
- this (colors , colors .toArray (new PositionedColor [0 ]));
36
+ this (colors .toArray (new PositionedColor [0 ]));
58
37
Arrays .sort (sorted );
59
38
}
60
39
@@ -63,9 +42,9 @@ public Color get(float delta) {
63
42
if (sorted .length == 0 ) {
64
43
return Color .TRANSPARENT ;
65
44
} else if (delta <= 0F || sorted .length == 1 ) {
66
- return sorted [0 ].color ;
45
+ return sorted [0 ].color () ;
67
46
} else if (delta >= 1F ) {
68
- return sorted [sorted .length - 1 ].color .get (1F );
47
+ return sorted [sorted .length - 1 ].color () .get (1F );
69
48
}
70
49
71
50
var left = sorted [0 ];
@@ -74,24 +53,24 @@ public Color get(float delta) {
74
53
for (int i = sorted .length - 1 ; i >= 0 ; i --) {
75
54
var c = sorted [i ];
76
55
77
- if (c .position <= delta ) {
56
+ if (c .position () <= delta ) {
78
57
left = c ;
79
58
right = i < sorted .length - 1 ? sorted [i + 1 ] : c ;
80
59
break ;
81
60
}
82
61
}
83
62
84
- return left .color .lerp (left .easing .ease (KMath .map (delta , left .position , right .position , 0F , 1F )), right .color );
63
+ return left .color () .lerp (left .easing () .ease (KMath .map (delta , left .position () , right .position () , 0F , 1F )), right .color () );
85
64
}
86
65
87
66
@ Override
88
67
public Gradient resolve () {
89
68
if (sorted .length == 0 ) {
90
69
return Color .TRANSPARENT ;
91
- } else if (sorted .length == 2 && sorted [0 ].easing == Easing .LINEAR ) {
92
- return new LinearPairGradient (sorted [0 ].color , sorted [sorted .length - 1 ].color ).resolve ();
70
+ } else if (sorted .length == 2 && sorted [0 ].easing () == Easing .LINEAR ) {
71
+ return new LinearPairGradient (sorted [0 ].color () , sorted [sorted .length - 1 ].color () ).resolve ();
93
72
} else if (sorted .length == 1 ) {
94
- return sorted [0 ].color .resolve ();
73
+ return sorted [0 ].color () .resolve ();
95
74
} else {
96
75
return this ;
97
76
}
@@ -101,19 +80,23 @@ public boolean isSimple() {
101
80
for (int i = 0 ; i < sorted .length ; i ++) {
102
81
var c = sorted [i ];
103
82
104
- if (c .easing != Easing .LINEAR || Math .abs (c .position - (i / (float ) (sorted .length - 1 ))) > 0.001F ) {
83
+ if (c .easing () != Easing .LINEAR || Math .abs (c .position () - (i / (float ) (sorted .length - 1 ))) > 0.001F ) {
105
84
return false ;
106
85
}
107
86
}
108
87
109
88
return true ;
110
89
}
111
90
91
+ public List <PositionedColor > getColorList () {
92
+ return Arrays .asList (sorted );
93
+ }
94
+
112
95
public List <Color > rawColors () {
113
96
var list = new ArrayList <Color >(sorted .length );
114
97
115
98
for (var c : sorted ) {
116
- list .add (c .color );
99
+ list .add (c .color () );
117
100
}
118
101
119
102
return list ;
0 commit comments