1
1
package vg .civcraft .mc .civmodcore .playersettings .impl .collection ;
2
2
3
- import java .util .Collection ;
4
- import java .util .Iterator ;
5
- import java .util .LinkedList ;
6
- import java .util .List ;
7
- import java .util .UUID ;
8
- import java .util .function .Function ;
9
-
10
3
import org .bukkit .ChatColor ;
11
4
import org .bukkit .Material ;
12
5
import org .bukkit .entity .Player ;
13
6
import org .bukkit .inventory .ItemStack ;
14
7
import org .bukkit .plugin .java .JavaPlugin ;
15
-
16
- import vg .civcraft .mc .civmodcore .CivModCorePlugin ;
17
8
import vg .civcraft .mc .civmodcore .api .ItemAPI ;
18
9
import vg .civcraft .mc .civmodcore .chatDialog .Dialog ;
19
10
import vg .civcraft .mc .civmodcore .inventorygui .Clickable ;
23
14
import vg .civcraft .mc .civmodcore .playersettings .SettingTypeManager ;
24
15
import vg .civcraft .mc .civmodcore .playersettings .gui .MenuSection ;
25
16
26
- public class AbstractCollectionSetting <C extends Collection <T >, T > extends PlayerSetting <C > {
17
+ import java .util .ArrayList ;
18
+ import java .util .Collection ;
19
+ import java .util .Iterator ;
20
+ import java .util .List ;
21
+ import java .util .UUID ;
22
+ import java .util .function .Function ;
23
+
24
+ public abstract class AbstractCollectionSetting <C extends Collection <T >, T > extends PlayerSetting <C > {
27
25
28
26
private static final char SEPARATOR = ',' ;
29
27
private static final String SEPARATOR_STRING = String .valueOf (SEPARATOR );
@@ -39,9 +37,6 @@ public AbstractCollectionSetting(JavaPlugin owningPlugin, C defaultValue, String
39
37
ItemStack gui , String description , Class <T > elementClass , Function <C , C > newFunction ) {
40
38
super (owningPlugin , defaultValue , name , identifier , gui , description );
41
39
this .newFunction = newFunction ;
42
- if (defaultValue == null ) {
43
- defaultValue = newFunction .apply (null );
44
- }
45
40
elementSetting = SettingTypeManager .getSetting (elementClass );
46
41
if (elementSetting == null ) {
47
42
throw new IllegalArgumentException ("Can not keep " + elementClass .getName ()
@@ -98,7 +93,7 @@ public boolean isValidValue(String input) {
98
93
}
99
94
}
100
95
// final element not checked by the loop, because there's no comma after it
101
- return elementSetting .isValidValue (input .substring (startingIndex , input . length () ));
96
+ return elementSetting .isValidValue (input .substring (startingIndex ));
102
97
}
103
98
104
99
@ Override
@@ -123,12 +118,12 @@ public C deserialize(String serial) {
123
118
}
124
119
}
125
120
// final element not checked by the loop, because there's no comma after it
126
- result .add (elementSetting .deserialize (removeEscapes (serial .substring (startingIndex , serial . length () ))));
121
+ result .add (elementSetting .deserialize (removeEscapes (serial .substring (startingIndex ))));
127
122
return result ;
128
123
}
129
124
130
125
private static String removeEscapes (String val ) {
131
- return val .replaceAll (SEPARATOR_REPLACE , SEPARATOR_REPLACE ).replaceAll (ESCAPE_REPLACE , ESCAPE_STRING );
126
+ return val .replaceAll (SEPARATOR_REPLACE , SEPARATOR_STRING ).replaceAll (ESCAPE_REPLACE , ESCAPE_STRING );
132
127
}
133
128
134
129
private static String escape (String val ) {
@@ -166,8 +161,9 @@ public String toText(C value) {
166
161
167
162
@ Override
168
163
public void handleMenuClick (Player player , MenuSection menu ) {
169
- List <IClickable > clickables = new LinkedList <>();
170
- for (T element : getValue (player )) {
164
+ C value = getValue (player );
165
+ List <IClickable > clickables = new ArrayList <>(value .size ());
166
+ for (T element : value ) {
171
167
elementSetting .setValue (player , element );
172
168
ItemStack is = elementSetting .getGuiRepresentation (player .getUniqueId ());
173
169
ItemAPI .setDisplayName (is , ChatColor .GOLD + elementSetting .toText (element ));
@@ -198,7 +194,7 @@ public void clicked(Player p) {
198
194
199
195
@ Override
200
196
public void clicked (Player p ) {
201
- new Dialog (p , CivModCorePlugin . getInstance (), ChatColor .GOLD + "Enter the name of the entry to add" ) {
197
+ new Dialog (p , getOwningPlugin (), ChatColor .GOLD + "Enter the name of the entry to add" ) {
202
198
203
199
@ Override
204
200
public List <String > onTabComplete (String wordCompleted , String [] fullMessage ) {
@@ -207,13 +203,7 @@ public List<String> onTabComplete(String wordCompleted, String[] fullMessage) {
207
203
208
204
@ Override
209
205
public void onReply (String [] message ) {
210
- StringBuilder sb = new StringBuilder ();
211
- for (int i = 0 ; i < message .length - 1 ; i ++) {
212
- sb .append (message [i ]);
213
- sb .append (' ' );
214
- }
215
- sb .append (message [message .length - 1 ]);
216
- String full = sb .toString ();
206
+ String full = String .join (" " , message );
217
207
if (!elementSetting .isValidValue (full )) {
218
208
p .sendMessage (ChatColor .RED + "You entered an invalid value" );
219
209
} else {
0 commit comments