Skip to content

Commit 383de29

Browse files
committed
MidnightLib 0.6.0 - Centered Comments, Inactive Reset Buttons, Hidden Entries
- Comments can now be centered via a property in the Annotation - Entries can now be completely hidden using the respective annotation (allows for things like config versions being saved) - Reset buttons now get deactivated when the value matches the default - The MidnightConfigOverview list is now sorted alphabetically - Make more fields publicly accessible - Ukrainian translation by @Altegar
1 parent 71c16ff commit 383de29

File tree

6 files changed

+52
-32
lines changed

6 files changed

+52
-32
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G
88
loader_version=0.14.6
99

1010
# Mod Properties
11-
mod_version = 0.5.2
11+
mod_version = 0.6.0
1212
maven_group = eu.midnightdust
1313
archives_base_name = midnightlib
1414

src/main/java/eu/midnightdust/core/MidnightLibServer.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package eu.midnightdust.core;
22

3-
import eu.midnightdust.core.config.MidnightLibConfig;
43
import eu.midnightdust.lib.config.AutoCommand;
54
import eu.midnightdust.lib.config.MidnightConfig;
65
import net.fabricmc.api.DedicatedServerModInitializer;
@@ -13,7 +12,7 @@ public class MidnightLibServer implements DedicatedServerModInitializer {
1312
public void onInitializeServer() {
1413
MidnightConfig.configClass.forEach((modid, config) -> {
1514
for (Field field : config.getFields()) {
16-
if (field.isAnnotationPresent(MidnightConfig.Entry.class) && !field.isAnnotationPresent(MidnightConfig.Client.class))
15+
if (field.isAnnotationPresent(MidnightConfig.Entry.class) && !field.isAnnotationPresent(MidnightConfig.Client.class) && !field.isAnnotationPresent(MidnightConfig.Hidden.class))
1716
new AutoCommand(field, modid).register();
1817
}
1918
});

src/main/java/eu/midnightdust/core/config/MidnightLibConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
import net.fabricmc.loader.api.FabricLoader;
55

66
public class MidnightLibConfig extends MidnightConfig {
7-
@Comment public static Comment midnightlib_description;
7+
@Comment(centered = true) public static Comment midnightlib_description;
88
@Entry // Enable or disable the MidnightConfig overview screen button
99
public static ConfigButton config_screen_list = FabricLoader.getInstance().isModLoaded("modmenu") ? ConfigButton.MODMENU : ConfigButton.TRUE;
10-
@Comment public static Comment midnighthats_description;
10+
@Comment(centered = true) public static Comment midnighthats_description;
1111
@Entry // Enable or disable hats for contributors, friends and donors.
1212
public static boolean special_hats = true;
1313

src/main/java/eu/midnightdust/core/screen/MidnightConfigOverviewScreen.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ protected void init() {
3333
this.list = new MidnightOverviewListWidget(this.client, this.width, this.height, 32, this.height - 32, 25);
3434
if (this.client != null && this.client.world != null) this.list.setRenderBackground(false);
3535
this.addSelectableChild(this.list);
36-
MidnightConfig.configClass.forEach((modid, configClass) -> {
36+
List<String> sortedMods = new ArrayList<>(MidnightConfig.configClass.keySet());
37+
Collections.sort(sortedMods);
38+
sortedMods.forEach((modid) -> {
3739
if (!MidnightLibClient.hiddenMods.contains(modid)) {
3840
list.addButton(new ButtonWidget(this.width / 2 - 100, this.height - 28, 200, 20, Text.translatable(modid +".midnightconfig.title"), (button) ->
3941
Objects.requireNonNull(client).setScreen(MidnightConfig.getScreen(this,modid))));

src/main/java/eu/midnightdust/lib/config/MidnightConfig.java

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
import java.util.function.Predicate;
4141
import 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; }

src/main/resources/assets/midnightlib/icon.png

100755100644
-1.86 KB
Loading

0 commit comments

Comments
 (0)