31
31
import java .util .function .Predicate ;
32
32
import java .util .regex .Pattern ;
33
33
34
- // MidnightConfig v1.0.2
34
+ // MidnightConfig v1.0.3
35
35
// Single class config library - feel free to copy!
36
36
// Changelog:
37
+ // - 1.0.3:
38
+ // - Text field length is now configurable
39
+ // - Better separation of client and server
37
40
// - 1.0.2:
38
41
// - Update to 21w20a
39
42
// - 1.0.1:
@@ -61,6 +64,7 @@ protected static class EntryInfo {
61
64
Field field ;
62
65
Object widget ;
63
66
int width ;
67
+ int max ;
64
68
Map .Entry <TextFieldWidget ,Text > error ;
65
69
Object defaultValue ;
66
70
Object value ;
@@ -81,9 +85,7 @@ public static void init(String modid, Class<?> config) {
81
85
for (Field field : config .getFields ()) {
82
86
EntryInfo info = new EntryInfo ();
83
87
if (field .isAnnotationPresent (Entry .class ) || field .isAnnotationPresent (Comment .class ))
84
- try {
85
- initClient (modid , field , info );
86
- } catch (Exception e ) {continue ;}
88
+ if (FabricLoader .getInstance ().getEnvironmentType () == EnvType .CLIENT ) initClient (modid , field , info );
87
89
if (field .isAnnotationPresent (Entry .class ))
88
90
try {
89
91
info .defaultValue = field .get (null );
@@ -102,29 +104,32 @@ public static void init(String modid, Class<?> config) {
102
104
}
103
105
}
104
106
@ Environment (EnvType .CLIENT )
105
- public static void initClient (String modid , Field field , EntryInfo info ) {
107
+ private static void initClient (String modid , Field field , EntryInfo info ) {
106
108
Class <?> type = field .getType ();
107
109
Entry e = field .getAnnotation (Entry .class );
108
110
info .width = e != null ? e .width () : 0 ;
109
111
info .field = field ;
110
112
info .id = modid ;
111
113
112
114
if (e != null )
113
- if (type == int .class ) textField (info , Integer ::parseInt , INTEGER_ONLY , e .min (), e .max (), true );
114
- else if (type == double .class ) textField (info , Double ::parseDouble , DECIMAL_ONLY , e .min (), e .max (),false );
115
- else if (type == String .class ) textField (info , String ::length , null , Math .min (e .min (),0 ), Math .max (e .max (),1 ),true );
115
+ if (type == int .class ) textField (info , Integer ::parseInt , INTEGER_ONLY , e .min (), e .max (), true );
116
+ else if (type == double .class ) textField (info , Double ::parseDouble , DECIMAL_ONLY , e .min (), e .max (), false );
117
+ else if (type == String .class ) {
118
+ info .max = e .max () == Double .MAX_VALUE ? Integer .MAX_VALUE : (int ) e .max ();
119
+ textField (info , String ::length , null , Math .min (e .min (), 0 ), Math .max (e .max (), 1 ), true );
120
+ }
116
121
else if (type == boolean .class ) {
117
- Function <Object ,Text > func = value -> new LiteralText ((Boolean ) value ? "True" : "False" ).formatted ((Boolean ) value ? Formatting .GREEN : Formatting .RED );
122
+ Function <Object , Text > func = value -> new LiteralText ((Boolean ) value ? "True" : "False" ).formatted ((Boolean ) value ? Formatting .GREEN : Formatting .RED );
118
123
info .widget = new AbstractMap .SimpleEntry <ButtonWidget .PressAction , Function <Object , Text >>(button -> {
119
124
info .value = !(Boolean ) info .value ;
120
125
button .setMessage (func .apply (info .value ));
121
126
}, func );
122
127
} else if (type .isEnum ()) {
123
128
List <?> values = Arrays .asList (field .getType ().getEnumConstants ());
124
- Function <Object ,Text > func = value -> new TranslatableText (modid + ".midnightconfig." + "enum." + type .getSimpleName () + "." + info .value .toString ());
125
- info .widget = new AbstractMap .SimpleEntry <ButtonWidget .PressAction , Function <Object ,Text >>( button -> {
129
+ Function <Object , Text > func = value -> new TranslatableText (modid + ".midnightconfig." + "enum." + type .getSimpleName () + "." + info .value .toString ());
130
+ info .widget = new AbstractMap .SimpleEntry <ButtonWidget .PressAction , Function <Object , Text >>(button -> {
126
131
int index = values .indexOf (info .value ) + 1 ;
127
- info .value = values .get (index >= values .size ()? 0 : index );
132
+ info .value = values .get (index >= values .size () ? 0 : index );
128
133
button .setMessage (func .apply (info .value ));
129
134
}, func );
130
135
}
@@ -171,16 +176,14 @@ public static void write(String modid) {
171
176
e .printStackTrace ();
172
177
}
173
178
}
174
-
175
179
@ Environment (EnvType .CLIENT )
176
180
public static Screen getScreen (Screen parent , String modid ) {
177
- return new TinyConfigScreen (parent , modid );
181
+ return new MidnightConfigScreen (parent , modid );
178
182
}
179
-
180
183
@ Environment (EnvType .CLIENT )
181
- private static class TinyConfigScreen extends Screen {
184
+ private static class MidnightConfigScreen extends Screen {
182
185
183
- protected TinyConfigScreen (Screen parent , String modid ) {
186
+ protected MidnightConfigScreen (Screen parent , String modid ) {
184
187
super (new TranslatableText (modid + ".midnightconfig." + "title" ));
185
188
this .parent = parent ;
186
189
this .modid = modid ;
@@ -250,6 +253,7 @@ protected void init() {
250
253
} else if (info .widget != null ) {
251
254
TextFieldWidget widget = new TextFieldWidget (textRenderer , width - 110 , 0 , info .width , 20 , null );
252
255
256
+ widget .setMaxLength (info .max );
253
257
widget .setText (info .tempValue );
254
258
Predicate <String > processor = ((BiFunction <TextFieldWidget , ButtonWidget , Predicate <String >>) info .widget ).apply (widget , done );
255
259
widget .setTextPredicate (processor );
0 commit comments