4040import java .util .function .Predicate ;
4141import java .util .regex .Pattern ;
4242
43- /** MidnightConfig v2.1 .0 by TeamMidnightDust & Motschen
43+ /** MidnightConfig v2.2 .0 by TeamMidnightDust & Motschen
4444 * Single class config library - feel free to copy!
4545 *
4646 * Based on https://github.com/Minenash/TinyConfig
@@ -59,6 +59,7 @@ protected static class EntryInfo {
5959 Object widget ;
6060 int width ;
6161 int max ;
62+ boolean centered ;
6263 Map .Entry <TextFieldWidget ,Text > error ;
6364 Object defaultValue ;
6465 Object value ;
@@ -81,8 +82,9 @@ public static void init(String modid, Class<?> config) {
8182
8283 for (Field field : config .getFields ()) {
8384 EntryInfo info = new EntryInfo ();
84- if ((field .isAnnotationPresent (Entry .class ) || field .isAnnotationPresent (Comment .class )) && !field .isAnnotationPresent (Server .class ))
85+ if ((field .isAnnotationPresent (Entry .class ) || field .isAnnotationPresent (Comment .class )) && !field .isAnnotationPresent (Server .class ) && ! field . isAnnotationPresent ( Hidden . class ) )
8586 if (FabricLoader .getInstance ().getEnvironmentType () == EnvType .CLIENT ) initClient (modid , field , info );
87+ if (field .isAnnotationPresent (Comment .class )) info .centered = field .getAnnotation (Comment .class ).centered ();
8688 if (field .isAnnotationPresent (Entry .class ))
8789 try {
8890 info .defaultValue = field .get (null );
@@ -96,8 +98,7 @@ public static void init(String modid, Class<?> config) {
9698 try {
9799 info .value = info .field .get (null );
98100 info .tempValue = info .value .toString ();
99- } catch (IllegalAccessException ignored ) {
100- }
101+ } catch (IllegalAccessException ignored ) {}
101102 }
102103 }
103104 @ Environment (EnvType .CLIENT )
@@ -117,7 +118,7 @@ else if (type == String.class || type == List.class) {
117118 info .max = e .max () == Double .MAX_VALUE ? Integer .MAX_VALUE : (int ) e .max ();
118119 textField (info , String ::length , null , Math .min (e .min (), 0 ), Math .max (e .max (), 1 ), true );
119120 } else if (type == boolean .class ) {
120- Function <Object , Text > func = value -> Text .literal ((Boolean ) value ? "True " : "False " ).formatted ((Boolean ) value ? Formatting .GREEN : Formatting .RED );
121+ Function <Object , Text > func = value -> Text .translatable ((Boolean ) value ? "gui.yes " : "gui.no " ).formatted ((Boolean ) value ? Formatting .GREEN : Formatting .RED );
121122 info .widget = new AbstractMap .SimpleEntry <ButtonWidget .PressAction , Function <Object , Text >>(button -> {
122123 info .value = !(Boolean ) info .value ;
123124 button .setMessage (func .apply (info .value ));
@@ -189,18 +190,18 @@ public static Screen getScreen(Screen parent, String modid) {
189190 return new MidnightConfigScreen (parent , modid );
190191 }
191192 @ Environment (EnvType .CLIENT )
192- private static class MidnightConfigScreen extends Screen {
193+ public static class MidnightConfigScreen extends Screen {
193194 protected MidnightConfigScreen (Screen parent , String modid ) {
194195 super (Text .translatable (modid + ".midnightconfig." + "title" ));
195196 this .parent = parent ;
196197 this .modid = modid ;
197198 this .translationPrefix = modid + ".midnightconfig." ;
198199 }
199- private final String translationPrefix ;
200- private final Screen parent ;
201- private final String modid ;
202- private MidnightConfigListWidget list ;
203- private boolean reload = false ;
200+ public final String translationPrefix ;
201+ public final Screen parent ;
202+ public final String modid ;
203+ public MidnightConfigListWidget list ;
204+ public boolean reload = false ;
204205
205206 // Real Time config update //
206207 @ Override
@@ -209,8 +210,18 @@ public void tick() {
209210 for (EntryInfo info : entries ) {
210211 try {info .field .set (null , info .value );} catch (IllegalAccessException ignored ) {}
211212 }
213+ updateResetButtons ();
214+ }
215+ public void updateResetButtons () {
216+ if (this .list != null ) {
217+ for (ButtonEntry entry : this .list .children ()) {
218+ if (entry .buttons != null && entry .buttons .size () > 1 && entry .buttons .get (1 ) instanceof ButtonWidget button ) {
219+ button .active = !Objects .equals (entry .info .value .toString (), entry .info .defaultValue .toString ());
220+ }
221+ }
222+ }
212223 }
213- private void loadValues () {
224+ public void loadValues () {
214225 try { gson .fromJson (Files .newBufferedReader (path ), configClass .get (modid )); }
215226 catch (Exception e ) { write (modid ); }
216227
@@ -223,7 +234,7 @@ private void loadValues() {
223234 }
224235 }
225236 @ Override
226- protected void init () {
237+ public void init () {
227238 super .init ();
228239 if (!reload ) loadValues ();
229240
@@ -262,7 +273,7 @@ protected void init() {
262273 if (info .widget instanceof Map .Entry ) {
263274 Map .Entry <ButtonWidget .PressAction , Function <Object , Text >> widget = (Map .Entry <ButtonWidget .PressAction , Function <Object , Text >>) info .widget ;
264275 if (info .field .getType ().isEnum ()) widget .setValue (value -> Text .translatable (translationPrefix + "enum." + info .field .getType ().getSimpleName () + "." + info .value .toString ()));
265- this .list .addButton (List .of (new ButtonWidget (width - 160 , 0 ,150 , 20 , widget .getValue ().apply (info .value ), widget .getKey ()),resetButton ), name );
276+ this .list .addButton (List .of (new ButtonWidget (width - 160 , 0 ,150 , 20 , widget .getValue ().apply (info .value ), widget .getKey ()),resetButton ), name , info );
266277 } else if (info .field .getType () == List .class ) {
267278 if (!reload ) info .index = 0 ;
268279 TextFieldWidget widget = new TextFieldWidget (textRenderer , width - 160 , 0 , 150 , 20 , null );
@@ -282,7 +293,7 @@ protected void init() {
282293 Objects .requireNonNull (client ).setScreen (this );
283294 list .setScrollAmount (scrollAmount );
284295 }));
285- this .list .addButton (List .of (widget , resetButton , cycleButton ), name );
296+ this .list .addButton (List .of (widget , resetButton , cycleButton ), name , info );
286297 } else if (info .widget != null ) {
287298 TextFieldWidget widget = new TextFieldWidget (textRenderer , width - 160 , 0 , 150 , 20 , null );
288299 widget .setMaxLength (info .width );
@@ -295,13 +306,14 @@ protected void init() {
295306 ButtonWidget colorButton = new ButtonWidget (width - 185 , 0 , 20 , 20 , Text .literal ("⬛" ), (button -> {}));
296307 try {colorButton .setMessage (Text .literal ("⬛" ).setStyle (Style .EMPTY .withColor (Color .decode (info .tempValue ).getRGB ())));} catch (Exception ignored ) {}
297308 info .colorButton = colorButton ;
298- this .list .addButton (List .of (widget , colorButton , resetButton ), name );
309+ this .list .addButton (List .of (widget , colorButton , resetButton ), name , info );
299310 }
300- else this .list .addButton (List .of (widget , resetButton ), name );
311+ else this .list .addButton (List .of (widget , resetButton ), name , info );
301312 } else {
302- this .list .addButton (List .of (),name );
313+ this .list .addButton (List .of (),name , info );
303314 }
304315 }
316+ updateResetButtons ();
305317 }
306318
307319 }
@@ -344,8 +356,8 @@ public MidnightConfigListWidget(MinecraftClient minecraftClient, int i, int j, i
344356 @ Override
345357 public int getScrollbarPositionX () { return this .width -7 ; }
346358
347- public void addButton (List <ClickableWidget > buttons , Text text ) {
348- this .addEntry (ButtonEntry .create (buttons , text ));
359+ public void addButton (List <ClickableWidget > buttons , Text text , EntryInfo info ) {
360+ this .addEntry (ButtonEntry .create (buttons , text , info ));
349361 }
350362 @ Override
351363 public int getRowWidth () { return 10000 ; }
@@ -362,22 +374,26 @@ public static class ButtonEntry extends ElementListWidget.Entry<ButtonEntry> {
362374 private static final TextRenderer textRenderer = MinecraftClient .getInstance ().textRenderer ;
363375 public final List <ClickableWidget > buttons ;
364376 private final Text text ;
377+ public final EntryInfo info ;
365378 private final List <ClickableWidget > children = new ArrayList <>();
366379 public static final Map <ClickableWidget , Text > buttonsWithText = new HashMap <>();
367380
368- private ButtonEntry (List <ClickableWidget > buttons , Text text ) {
381+ private ButtonEntry (List <ClickableWidget > buttons , Text text , EntryInfo info ) {
369382 if (!buttons .isEmpty ()) buttonsWithText .put (buttons .get (0 ),text );
370383 this .buttons = buttons ;
371384 this .text = text ;
385+ this .info = info ;
372386 children .addAll (buttons );
373387 }
374- public static ButtonEntry create (List <ClickableWidget > buttons , Text text ) {
375- return new ButtonEntry (buttons , text );
388+ public static ButtonEntry create (List <ClickableWidget > buttons , Text text , EntryInfo info ) {
389+ return new ButtonEntry (buttons , text , info );
376390 }
377391 public void render (MatrixStack matrices , int index , int y , int x , int entryWidth , int entryHeight , int mouseX , int mouseY , boolean hovered , float tickDelta ) {
378392 buttons .forEach (b -> { b .y = y ; b .render (matrices , mouseX , mouseY , tickDelta ); });
379- if (text != null && (!text .getString ().contains ("spacer" ) || !buttons .isEmpty ()))
380- DrawableHelper .drawTextWithShadow (matrices ,textRenderer , text ,12 ,y +5 ,0xFFFFFF );
393+ if (text != null && (!text .getString ().contains ("spacer" ) || !buttons .isEmpty ())) {
394+ if (info .centered ) textRenderer .drawWithShadow (matrices , text , MinecraftClient .getInstance ().getWindow ().getScaledWidth () / 2f - (textRenderer .getWidth (text ) / 2f ), y + 5 , 0xFFFFFF );
395+ else DrawableHelper .drawTextWithShadow (matrices , textRenderer , text , 12 , y + 5 , 0xFFFFFF );
396+ }
381397 }
382398 public List <? extends Element > children () {return children ;}
383399 public List <? extends Selectable > selectableChildren () {return children ;}
@@ -391,7 +407,10 @@ public void render(MatrixStack matrices, int index, int y, int x, int entryWidth
391407 }
392408 @ Retention (RetentionPolicy .RUNTIME ) @ Target (ElementType .FIELD ) public @interface Client {}
393409 @ Retention (RetentionPolicy .RUNTIME ) @ Target (ElementType .FIELD ) public @interface Server {}
394- @ Retention (RetentionPolicy .RUNTIME ) @ Target (ElementType .FIELD ) public @interface Comment {}
410+ @ Retention (RetentionPolicy .RUNTIME ) @ Target (ElementType .FIELD ) public @interface Hidden {}
411+ @ Retention (RetentionPolicy .RUNTIME ) @ Target (ElementType .FIELD ) public @interface Comment {
412+ boolean centered () default false ;
413+ }
395414
396415 public static class HiddenAnnotationExclusionStrategy implements ExclusionStrategy {
397416 public boolean shouldSkipClass (Class <?> clazz ) { return false ; }
0 commit comments