2
2
3
3
import com .google .common .base .Preconditions ;
4
4
5
+ import me .flame .menus .adventure .Lore ;
6
+ import me .flame .menus .adventure .TextHolder ;
5
7
import me .flame .menus .items .MenuItem ;
6
8
7
- import org .bukkit .Bukkit ;
8
9
import org .bukkit .inventory .ItemStack ;
9
10
11
+ import org .bukkit .inventory .meta .ItemMeta ;
12
+ import org .jetbrains .annotations .ApiStatus ;
10
13
import org .jetbrains .annotations .Contract ;
11
14
import org .jetbrains .annotations .NotNull ;
12
15
13
- import java .util .List ;
14
16
import java .util .function .DoubleSupplier ;
15
17
import java .util .function .IntSupplier ;
16
18
import java .util .function .LongSupplier ;
29
31
* @author FlameyosFlow
30
32
* @since 2.0.0
31
33
*/
34
+ @ ApiStatus .Experimental
32
35
public class State {
33
- private final String key ;
36
+ private final TextHolder key ;
34
37
private final MenuItem item ;
35
- private final Supplier <String > value ;
38
+ private final Supplier <TextHolder > value ;
36
39
37
- private State (String key , Supplier <String > value , @ NotNull MenuItem item ) {
40
+ private State (TextHolder key , Supplier <TextHolder > value , @ NotNull MenuItem item ) {
38
41
this .item = item ;
39
42
40
43
ItemStack stack = item .getItemStack ();
@@ -51,38 +54,63 @@ private State(String key, Supplier<String> value, @NotNull MenuItem item) {
51
54
52
55
@ Contract (value = "_, _, _ -> new" , pure = true )
53
56
public static @ NotNull State of (String key , String value , MenuItem item ) {
54
- return new State (key , () -> value , item );
57
+ return new State (TextHolder . of ( key ) , () -> TextHolder . of ( value ) , item );
55
58
}
56
59
57
60
@ Contract (value = "_, _, _ -> new" , pure = true )
58
61
public static @ NotNull State of (String key , IntSupplier value , MenuItem item ) {
59
- return new State (key , () -> String .valueOf (value .getAsInt ()), item );
62
+ return new State (TextHolder . of ( key ) , () -> TextHolder . of ( String .valueOf (value .getAsInt () )), item );
60
63
}
61
64
62
65
@ Contract (value = "_, _, _ -> new" , pure = true )
63
66
public static @ NotNull State of (String key , DoubleSupplier value , MenuItem item ) {
64
- return new State (key , () -> String .valueOf (value .getAsDouble ()), item );
67
+ return new State (TextHolder . of ( key ) , () -> TextHolder . of ( String .valueOf (value .getAsDouble () )), item );
65
68
}
66
69
67
70
@ Contract (value = "_, _, _ -> new" , pure = true )
68
71
public static @ NotNull State of (String key , LongSupplier value , MenuItem item ) {
69
- return new State (key , () -> String .valueOf (value .getAsLong ()), item );
72
+ return new State (TextHolder .of (key ), () -> TextHolder .of (String .valueOf (value .getAsLong ())), item );
73
+ }
74
+
75
+ @ Contract (value = "_, _, _ -> new" , pure = true )
76
+ public static @ NotNull State of (TextHolder key , String value , MenuItem item ) {
77
+ return new State (key , () -> TextHolder .of (value ), item );
78
+ }
79
+
80
+ @ Contract (value = "_, _, _ -> new" , pure = true )
81
+ public static @ NotNull State of (TextHolder key , IntSupplier value , MenuItem item ) {
82
+ return new State (key , () -> TextHolder .of (String .valueOf (value .getAsInt ())), item );
83
+ }
84
+
85
+ @ Contract (value = "_, _, _ -> new" , pure = true )
86
+ public static @ NotNull State of (TextHolder key , DoubleSupplier value , MenuItem item ) {
87
+ return new State (key , () -> TextHolder .of (String .valueOf (value .getAsDouble ())), item );
88
+ }
89
+
90
+ @ Contract (value = "_, _, _ -> new" , pure = true )
91
+ public static @ NotNull State of (TextHolder key , LongSupplier value , MenuItem item ) {
92
+ return new State (key , () -> TextHolder .of (String .valueOf (value .getAsLong ())), item );
93
+ }
94
+
95
+ @ Contract (value = "_, _, _ -> new" , pure = true )
96
+ public static @ NotNull State of (TextHolder key , TextHolder value , MenuItem item ) {
97
+ return new State (key , () -> value , item );
70
98
}
71
99
72
100
@ Contract (pure = true )
73
101
public void update () {
74
102
ItemStack otherItem = item .getItemStack ();
75
- List < String > list = Preconditions .checkNotNull (otherItem .getItemMeta ()). getLore ( );
76
- if ( list == null ) return ;
103
+ ItemMeta meta = Preconditions .checkNotNull (otherItem .getItemMeta ());
104
+ Lore lore = new Lore ( meta ) ;
77
105
78
- int size = list .size ();
79
- for (int stringIndex = 0 ; stringIndex < size ; stringIndex ++) replaceWithKey (stringIndex , list );
80
- item . editor (). setLore ( list ). done ( );
106
+ int size = lore .size ();
107
+ for (int stringIndex = 0 ; stringIndex < size ; stringIndex ++) replaceWithKey (stringIndex , lore , key , value );
108
+ lore . toItemLore ( item . getItemStack () );
81
109
}
82
110
83
- private void replaceWithKey (int stringIndex , @ NotNull List < String > list ) {
84
- String currentString = list .get (stringIndex );
111
+ private static void replaceWithKey (int stringIndex , @ NotNull Lore list , TextHolder key , Supplier < TextHolder > value ) {
112
+ TextHolder currentString = list .get (stringIndex );
85
113
if (!currentString .contains (key )) return ;
86
- list .set (stringIndex , currentString . replace ( key , value .get () ));
114
+ list .set (stringIndex , value .get ());
87
115
}
88
116
}
0 commit comments