Skip to content

Commit

Permalink
MidnightLib 0.6.0 - Centered Comments, Inactive Reset Buttons, Hidden…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
Motschen committed Aug 22, 2022
1 parent 71c16ff commit 383de29
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 32 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G
loader_version=0.14.6

# Mod Properties
mod_version = 0.5.2
mod_version = 0.6.0
maven_group = eu.midnightdust
archives_base_name = midnightlib

Expand Down
3 changes: 1 addition & 2 deletions src/main/java/eu/midnightdust/core/MidnightLibServer.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package eu.midnightdust.core;

import eu.midnightdust.core.config.MidnightLibConfig;
import eu.midnightdust.lib.config.AutoCommand;
import eu.midnightdust.lib.config.MidnightConfig;
import net.fabricmc.api.DedicatedServerModInitializer;
Expand All @@ -13,7 +12,7 @@ public class MidnightLibServer implements DedicatedServerModInitializer {
public void onInitializeServer() {
MidnightConfig.configClass.forEach((modid, config) -> {
for (Field field : config.getFields()) {
if (field.isAnnotationPresent(MidnightConfig.Entry.class) && !field.isAnnotationPresent(MidnightConfig.Client.class))
if (field.isAnnotationPresent(MidnightConfig.Entry.class) && !field.isAnnotationPresent(MidnightConfig.Client.class) && !field.isAnnotationPresent(MidnightConfig.Hidden.class))
new AutoCommand(field, modid).register();
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import net.fabricmc.loader.api.FabricLoader;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ protected void init() {
this.list = new MidnightOverviewListWidget(this.client, this.width, this.height, 32, this.height - 32, 25);
if (this.client != null && this.client.world != null) this.list.setRenderBackground(false);
this.addSelectableChild(this.list);
MidnightConfig.configClass.forEach((modid, configClass) -> {
List<String> sortedMods = new ArrayList<>(MidnightConfig.configClass.keySet());
Collections.sort(sortedMods);
sortedMods.forEach((modid) -> {
if (!MidnightLibClient.hiddenMods.contains(modid)) {
list.addButton(new ButtonWidget(this.width / 2 - 100, this.height - 28, 200, 20, Text.translatable(modid +".midnightconfig.title"), (button) ->
Objects.requireNonNull(client).setScreen(MidnightConfig.getScreen(this,modid))));
Expand Down
71 changes: 45 additions & 26 deletions src/main/java/eu/midnightdust/lib/config/MidnightConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
import java.util.function.Predicate;
import java.util.regex.Pattern;

/** MidnightConfig v2.1.0 by TeamMidnightDust & Motschen
/** MidnightConfig v2.2.0 by TeamMidnightDust & Motschen
* Single class config library - feel free to copy!
*
* Based on https://github.com/Minenash/TinyConfig
Expand All @@ -59,6 +59,7 @@ protected static class EntryInfo {
Object widget;
int width;
int max;
boolean centered;
Map.Entry<TextFieldWidget,Text> error;
Object defaultValue;
Object value;
Expand All @@ -81,8 +82,9 @@ public static void init(String modid, Class<?> config) {

for (Field field : config.getFields()) {
EntryInfo info = new EntryInfo();
if ((field.isAnnotationPresent(Entry.class) || field.isAnnotationPresent(Comment.class)) && !field.isAnnotationPresent(Server.class))
if ((field.isAnnotationPresent(Entry.class) || field.isAnnotationPresent(Comment.class)) && !field.isAnnotationPresent(Server.class) && !field.isAnnotationPresent(Hidden.class))
if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) initClient(modid, field, info);
if (field.isAnnotationPresent(Comment.class)) info.centered = field.getAnnotation(Comment.class).centered();
if (field.isAnnotationPresent(Entry.class))
try {
info.defaultValue = field.get(null);
Expand All @@ -96,8 +98,7 @@ public static void init(String modid, Class<?> config) {
try {
info.value = info.field.get(null);
info.tempValue = info.value.toString();
} catch (IllegalAccessException ignored) {
}
} catch (IllegalAccessException ignored) {}
}
}
@Environment(EnvType.CLIENT)
Expand All @@ -117,7 +118,7 @@ else if (type == String.class || type == List.class) {
info.max = e.max() == Double.MAX_VALUE ? Integer.MAX_VALUE : (int) e.max();
textField(info, String::length, null, Math.min(e.min(), 0), Math.max(e.max(), 1), true);
} else if (type == boolean.class) {
Function<Object, Text> func = value -> Text.literal((Boolean) value ? "True" : "False").formatted((Boolean) value ? Formatting.GREEN : Formatting.RED);
Function<Object, Text> func = value -> Text.translatable((Boolean) value ? "gui.yes" : "gui.no").formatted((Boolean) value ? Formatting.GREEN : Formatting.RED);
info.widget = new AbstractMap.SimpleEntry<ButtonWidget.PressAction, Function<Object, Text>>(button -> {
info.value = !(Boolean) info.value;
button.setMessage(func.apply(info.value));
Expand Down Expand Up @@ -189,18 +190,18 @@ public static Screen getScreen(Screen parent, String modid) {
return new MidnightConfigScreen(parent, modid);
}
@Environment(EnvType.CLIENT)
private static class MidnightConfigScreen extends Screen {
public static class MidnightConfigScreen extends Screen {
protected MidnightConfigScreen(Screen parent, String modid) {
super(Text.translatable(modid + ".midnightconfig." + "title"));
this.parent = parent;
this.modid = modid;
this.translationPrefix = modid + ".midnightconfig.";
}
private final String translationPrefix;
private final Screen parent;
private final String modid;
private MidnightConfigListWidget list;
private boolean reload = false;
public final String translationPrefix;
public final Screen parent;
public final String modid;
public MidnightConfigListWidget list;
public boolean reload = false;

// Real Time config update //
@Override
Expand All @@ -209,8 +210,18 @@ public void tick() {
for (EntryInfo info : entries) {
try {info.field.set(null, info.value);} catch (IllegalAccessException ignored) {}
}
updateResetButtons();
}
public void updateResetButtons() {
if (this.list != null) {
for (ButtonEntry entry : this.list.children()) {
if (entry.buttons != null && entry.buttons.size() > 1 && entry.buttons.get(1) instanceof ButtonWidget button) {
button.active = !Objects.equals(entry.info.value.toString(), entry.info.defaultValue.toString());
}
}
}
}
private void loadValues() {
public void loadValues() {
try { gson.fromJson(Files.newBufferedReader(path), configClass.get(modid)); }
catch (Exception e) { write(modid); }

Expand All @@ -223,7 +234,7 @@ private void loadValues() {
}
}
@Override
protected void init() {
public void init() {
super.init();
if (!reload) loadValues();

Expand Down Expand Up @@ -262,7 +273,7 @@ protected void init() {
if (info.widget instanceof Map.Entry) {
Map.Entry<ButtonWidget.PressAction, Function<Object, Text>> widget = (Map.Entry<ButtonWidget.PressAction, Function<Object, Text>>) info.widget;
if (info.field.getType().isEnum()) widget.setValue(value -> Text.translatable(translationPrefix + "enum." + info.field.getType().getSimpleName() + "." + info.value.toString()));
this.list.addButton(List.of(new ButtonWidget(width - 160, 0,150, 20, widget.getValue().apply(info.value), widget.getKey()),resetButton), name);
this.list.addButton(List.of(new ButtonWidget(width - 160, 0,150, 20, widget.getValue().apply(info.value), widget.getKey()),resetButton), name, info);
} else if (info.field.getType() == List.class) {
if (!reload) info.index = 0;
TextFieldWidget widget = new TextFieldWidget(textRenderer, width - 160, 0, 150, 20, null);
Expand All @@ -282,7 +293,7 @@ protected void init() {
Objects.requireNonNull(client).setScreen(this);
list.setScrollAmount(scrollAmount);
}));
this.list.addButton(List.of(widget, resetButton, cycleButton), name);
this.list.addButton(List.of(widget, resetButton, cycleButton), name, info);
} else if (info.widget != null) {
TextFieldWidget widget = new TextFieldWidget(textRenderer, width - 160, 0, 150, 20, null);
widget.setMaxLength(info.width);
Expand All @@ -295,13 +306,14 @@ protected void init() {
ButtonWidget colorButton = new ButtonWidget(width - 185, 0, 20, 20, Text.literal("⬛"), (button -> {}));
try {colorButton.setMessage(Text.literal("⬛").setStyle(Style.EMPTY.withColor(Color.decode(info.tempValue).getRGB())));} catch (Exception ignored) {}
info.colorButton = colorButton;
this.list.addButton(List.of(widget, colorButton, resetButton), name);
this.list.addButton(List.of(widget, colorButton, resetButton), name, info);
}
else this.list.addButton(List.of(widget, resetButton), name);
else this.list.addButton(List.of(widget, resetButton), name, info);
} else {
this.list.addButton(List.of(),name);
this.list.addButton(List.of(),name, info);
}
}
updateResetButtons();
}

}
Expand Down Expand Up @@ -344,8 +356,8 @@ public MidnightConfigListWidget(MinecraftClient minecraftClient, int i, int j, i
@Override
public int getScrollbarPositionX() { return this.width -7; }

public void addButton(List<ClickableWidget> buttons, Text text) {
this.addEntry(ButtonEntry.create(buttons, text));
public void addButton(List<ClickableWidget> buttons, Text text, EntryInfo info) {
this.addEntry(ButtonEntry.create(buttons, text, info));
}
@Override
public int getRowWidth() { return 10000; }
Expand All @@ -362,22 +374,26 @@ public static class ButtonEntry extends ElementListWidget.Entry<ButtonEntry> {
private static final TextRenderer textRenderer = MinecraftClient.getInstance().textRenderer;
public final List<ClickableWidget> buttons;
private final Text text;
public final EntryInfo info;
private final List<ClickableWidget> children = new ArrayList<>();
public static final Map<ClickableWidget, Text> buttonsWithText = new HashMap<>();

private ButtonEntry(List<ClickableWidget> buttons, Text text) {
private ButtonEntry(List<ClickableWidget> buttons, Text text, EntryInfo info) {
if (!buttons.isEmpty()) buttonsWithText.put(buttons.get(0),text);
this.buttons = buttons;
this.text = text;
this.info = info;
children.addAll(buttons);
}
public static ButtonEntry create(List<ClickableWidget> buttons, Text text) {
return new ButtonEntry(buttons, text);
public static ButtonEntry create(List<ClickableWidget> buttons, Text text, EntryInfo info) {
return new ButtonEntry(buttons, text, info);
}
public void render(MatrixStack matrices, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) {
buttons.forEach(b -> { b.y = y; b.render(matrices, mouseX, mouseY, tickDelta); });
if (text != null && (!text.getString().contains("spacer") || !buttons.isEmpty()))
DrawableHelper.drawTextWithShadow(matrices,textRenderer, text,12,y+5,0xFFFFFF);
if (text != null && (!text.getString().contains("spacer") || !buttons.isEmpty())) {
if (info.centered) textRenderer.drawWithShadow(matrices, text, MinecraftClient.getInstance().getWindow().getScaledWidth() / 2f - (textRenderer.getWidth(text) / 2f), y + 5, 0xFFFFFF);
else DrawableHelper.drawTextWithShadow(matrices, textRenderer, text, 12, y + 5, 0xFFFFFF);
}
}
public List<? extends Element> children() {return children;}
public List<? extends Selectable> selectableChildren() {return children;}
Expand All @@ -391,7 +407,10 @@ public void render(MatrixStack matrices, int index, int y, int x, int entryWidth
}
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Client {}
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Server {}
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Comment {}
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Hidden {}
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Comment {
boolean centered() default false;
}

public static class HiddenAnnotationExclusionStrategy implements ExclusionStrategy {
public boolean shouldSkipClass(Class<?> clazz) { return false; }
Expand Down
Binary file modified src/main/resources/assets/midnightlib/icon.png
100755 → 100644
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 383de29

Please sign in to comment.