mirror of
https://github.com/TeamMidnightDust/MidnightLib.git
synced 2025-12-15 17:05:09 +01:00
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
This commit is contained in:
@@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G
|
|||||||
loader_version=0.14.6
|
loader_version=0.14.6
|
||||||
|
|
||||||
# Mod Properties
|
# Mod Properties
|
||||||
mod_version = 0.5.2
|
mod_version = 0.6.0
|
||||||
maven_group = eu.midnightdust
|
maven_group = eu.midnightdust
|
||||||
archives_base_name = midnightlib
|
archives_base_name = midnightlib
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package eu.midnightdust.core;
|
package eu.midnightdust.core;
|
||||||
|
|
||||||
import eu.midnightdust.core.config.MidnightLibConfig;
|
|
||||||
import eu.midnightdust.lib.config.AutoCommand;
|
import eu.midnightdust.lib.config.AutoCommand;
|
||||||
import eu.midnightdust.lib.config.MidnightConfig;
|
import eu.midnightdust.lib.config.MidnightConfig;
|
||||||
import net.fabricmc.api.DedicatedServerModInitializer;
|
import net.fabricmc.api.DedicatedServerModInitializer;
|
||||||
@@ -13,7 +12,7 @@ public class MidnightLibServer implements DedicatedServerModInitializer {
|
|||||||
public void onInitializeServer() {
|
public void onInitializeServer() {
|
||||||
MidnightConfig.configClass.forEach((modid, config) -> {
|
MidnightConfig.configClass.forEach((modid, config) -> {
|
||||||
for (Field field : config.getFields()) {
|
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();
|
new AutoCommand(field, modid).register();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -4,10 +4,10 @@ import eu.midnightdust.lib.config.MidnightConfig;
|
|||||||
import net.fabricmc.loader.api.FabricLoader;
|
import net.fabricmc.loader.api.FabricLoader;
|
||||||
|
|
||||||
public class MidnightLibConfig extends MidnightConfig {
|
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
|
@Entry // Enable or disable the MidnightConfig overview screen button
|
||||||
public static ConfigButton config_screen_list = FabricLoader.getInstance().isModLoaded("modmenu") ? ConfigButton.MODMENU : ConfigButton.TRUE;
|
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.
|
@Entry // Enable or disable hats for contributors, friends and donors.
|
||||||
public static boolean special_hats = true;
|
public static boolean special_hats = true;
|
||||||
|
|
||||||
|
|||||||
@@ -33,7 +33,9 @@ public class MidnightConfigOverviewScreen extends Screen {
|
|||||||
this.list = new MidnightOverviewListWidget(this.client, this.width, this.height, 32, this.height - 32, 25);
|
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);
|
if (this.client != null && this.client.world != null) this.list.setRenderBackground(false);
|
||||||
this.addSelectableChild(this.list);
|
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)) {
|
if (!MidnightLibClient.hiddenMods.contains(modid)) {
|
||||||
list.addButton(new ButtonWidget(this.width / 2 - 100, this.height - 28, 200, 20, Text.translatable(modid +".midnightconfig.title"), (button) ->
|
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))));
|
Objects.requireNonNull(client).setScreen(MidnightConfig.getScreen(this,modid))));
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ import java.util.function.Function;
|
|||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
import java.util.regex.Pattern;
|
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!
|
* Single class config library - feel free to copy!
|
||||||
*
|
*
|
||||||
* Based on https://github.com/Minenash/TinyConfig
|
* Based on https://github.com/Minenash/TinyConfig
|
||||||
@@ -59,6 +59,7 @@ public abstract class MidnightConfig {
|
|||||||
Object widget;
|
Object widget;
|
||||||
int width;
|
int width;
|
||||||
int max;
|
int max;
|
||||||
|
boolean centered;
|
||||||
Map.Entry<TextFieldWidget,Text> error;
|
Map.Entry<TextFieldWidget,Text> error;
|
||||||
Object defaultValue;
|
Object defaultValue;
|
||||||
Object value;
|
Object value;
|
||||||
@@ -81,8 +82,9 @@ public abstract class MidnightConfig {
|
|||||||
|
|
||||||
for (Field field : config.getFields()) {
|
for (Field field : config.getFields()) {
|
||||||
EntryInfo info = new EntryInfo();
|
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 (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))
|
if (field.isAnnotationPresent(Entry.class))
|
||||||
try {
|
try {
|
||||||
info.defaultValue = field.get(null);
|
info.defaultValue = field.get(null);
|
||||||
@@ -96,8 +98,7 @@ public abstract class MidnightConfig {
|
|||||||
try {
|
try {
|
||||||
info.value = info.field.get(null);
|
info.value = info.field.get(null);
|
||||||
info.tempValue = info.value.toString();
|
info.tempValue = info.value.toString();
|
||||||
} catch (IllegalAccessException ignored) {
|
} catch (IllegalAccessException ignored) {}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@Environment(EnvType.CLIENT)
|
@Environment(EnvType.CLIENT)
|
||||||
@@ -117,7 +118,7 @@ public abstract class MidnightConfig {
|
|||||||
info.max = e.max() == Double.MAX_VALUE ? Integer.MAX_VALUE : (int) e.max();
|
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);
|
textField(info, String::length, null, Math.min(e.min(), 0), Math.max(e.max(), 1), true);
|
||||||
} else if (type == boolean.class) {
|
} 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.widget = new AbstractMap.SimpleEntry<ButtonWidget.PressAction, Function<Object, Text>>(button -> {
|
||||||
info.value = !(Boolean) info.value;
|
info.value = !(Boolean) info.value;
|
||||||
button.setMessage(func.apply(info.value));
|
button.setMessage(func.apply(info.value));
|
||||||
@@ -189,18 +190,18 @@ public abstract class MidnightConfig {
|
|||||||
return new MidnightConfigScreen(parent, modid);
|
return new MidnightConfigScreen(parent, modid);
|
||||||
}
|
}
|
||||||
@Environment(EnvType.CLIENT)
|
@Environment(EnvType.CLIENT)
|
||||||
private static class MidnightConfigScreen extends Screen {
|
public static class MidnightConfigScreen extends Screen {
|
||||||
protected MidnightConfigScreen(Screen parent, String modid) {
|
protected MidnightConfigScreen(Screen parent, String modid) {
|
||||||
super(Text.translatable(modid + ".midnightconfig." + "title"));
|
super(Text.translatable(modid + ".midnightconfig." + "title"));
|
||||||
this.parent = parent;
|
this.parent = parent;
|
||||||
this.modid = modid;
|
this.modid = modid;
|
||||||
this.translationPrefix = modid + ".midnightconfig.";
|
this.translationPrefix = modid + ".midnightconfig.";
|
||||||
}
|
}
|
||||||
private final String translationPrefix;
|
public final String translationPrefix;
|
||||||
private final Screen parent;
|
public final Screen parent;
|
||||||
private final String modid;
|
public final String modid;
|
||||||
private MidnightConfigListWidget list;
|
public MidnightConfigListWidget list;
|
||||||
private boolean reload = false;
|
public boolean reload = false;
|
||||||
|
|
||||||
// Real Time config update //
|
// Real Time config update //
|
||||||
@Override
|
@Override
|
||||||
@@ -209,8 +210,18 @@ public abstract class MidnightConfig {
|
|||||||
for (EntryInfo info : entries) {
|
for (EntryInfo info : entries) {
|
||||||
try {info.field.set(null, info.value);} catch (IllegalAccessException ignored) {}
|
try {info.field.set(null, info.value);} catch (IllegalAccessException ignored) {}
|
||||||
}
|
}
|
||||||
|
updateResetButtons();
|
||||||
}
|
}
|
||||||
private void loadValues() {
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void loadValues() {
|
||||||
try { gson.fromJson(Files.newBufferedReader(path), configClass.get(modid)); }
|
try { gson.fromJson(Files.newBufferedReader(path), configClass.get(modid)); }
|
||||||
catch (Exception e) { write(modid); }
|
catch (Exception e) { write(modid); }
|
||||||
|
|
||||||
@@ -223,7 +234,7 @@ public abstract class MidnightConfig {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
protected void init() {
|
public void init() {
|
||||||
super.init();
|
super.init();
|
||||||
if (!reload) loadValues();
|
if (!reload) loadValues();
|
||||||
|
|
||||||
@@ -262,7 +273,7 @@ public abstract class MidnightConfig {
|
|||||||
if (info.widget instanceof Map.Entry) {
|
if (info.widget instanceof Map.Entry) {
|
||||||
Map.Entry<ButtonWidget.PressAction, Function<Object, Text>> widget = (Map.Entry<ButtonWidget.PressAction, Function<Object, Text>>) info.widget;
|
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()));
|
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) {
|
} else if (info.field.getType() == List.class) {
|
||||||
if (!reload) info.index = 0;
|
if (!reload) info.index = 0;
|
||||||
TextFieldWidget widget = new TextFieldWidget(textRenderer, width - 160, 0, 150, 20, null);
|
TextFieldWidget widget = new TextFieldWidget(textRenderer, width - 160, 0, 150, 20, null);
|
||||||
@@ -282,7 +293,7 @@ public abstract class MidnightConfig {
|
|||||||
Objects.requireNonNull(client).setScreen(this);
|
Objects.requireNonNull(client).setScreen(this);
|
||||||
list.setScrollAmount(scrollAmount);
|
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) {
|
} else if (info.widget != null) {
|
||||||
TextFieldWidget widget = new TextFieldWidget(textRenderer, width - 160, 0, 150, 20, null);
|
TextFieldWidget widget = new TextFieldWidget(textRenderer, width - 160, 0, 150, 20, null);
|
||||||
widget.setMaxLength(info.width);
|
widget.setMaxLength(info.width);
|
||||||
@@ -295,13 +306,14 @@ public abstract class MidnightConfig {
|
|||||||
ButtonWidget colorButton = new ButtonWidget(width - 185, 0, 20, 20, Text.literal("⬛"), (button -> {}));
|
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) {}
|
try {colorButton.setMessage(Text.literal("⬛").setStyle(Style.EMPTY.withColor(Color.decode(info.tempValue).getRGB())));} catch (Exception ignored) {}
|
||||||
info.colorButton = colorButton;
|
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 {
|
} else {
|
||||||
this.list.addButton(List.of(),name);
|
this.list.addButton(List.of(),name, info);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
updateResetButtons();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -344,8 +356,8 @@ public abstract class MidnightConfig {
|
|||||||
@Override
|
@Override
|
||||||
public int getScrollbarPositionX() { return this.width -7; }
|
public int getScrollbarPositionX() { return this.width -7; }
|
||||||
|
|
||||||
public void addButton(List<ClickableWidget> buttons, Text text) {
|
public void addButton(List<ClickableWidget> buttons, Text text, EntryInfo info) {
|
||||||
this.addEntry(ButtonEntry.create(buttons, text));
|
this.addEntry(ButtonEntry.create(buttons, text, info));
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public int getRowWidth() { return 10000; }
|
public int getRowWidth() { return 10000; }
|
||||||
@@ -362,22 +374,26 @@ public abstract class MidnightConfig {
|
|||||||
private static final TextRenderer textRenderer = MinecraftClient.getInstance().textRenderer;
|
private static final TextRenderer textRenderer = MinecraftClient.getInstance().textRenderer;
|
||||||
public final List<ClickableWidget> buttons;
|
public final List<ClickableWidget> buttons;
|
||||||
private final Text text;
|
private final Text text;
|
||||||
|
public final EntryInfo info;
|
||||||
private final List<ClickableWidget> children = new ArrayList<>();
|
private final List<ClickableWidget> children = new ArrayList<>();
|
||||||
public static final Map<ClickableWidget, Text> buttonsWithText = new HashMap<>();
|
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);
|
if (!buttons.isEmpty()) buttonsWithText.put(buttons.get(0),text);
|
||||||
this.buttons = buttons;
|
this.buttons = buttons;
|
||||||
this.text = text;
|
this.text = text;
|
||||||
|
this.info = info;
|
||||||
children.addAll(buttons);
|
children.addAll(buttons);
|
||||||
}
|
}
|
||||||
public static ButtonEntry create(List<ClickableWidget> buttons, Text text) {
|
public static ButtonEntry create(List<ClickableWidget> buttons, Text text, EntryInfo info) {
|
||||||
return new ButtonEntry(buttons, text);
|
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) {
|
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); });
|
buttons.forEach(b -> { b.y = y; b.render(matrices, mouseX, mouseY, tickDelta); });
|
||||||
if (text != null && (!text.getString().contains("spacer") || !buttons.isEmpty()))
|
if (text != null && (!text.getString().contains("spacer") || !buttons.isEmpty())) {
|
||||||
DrawableHelper.drawTextWithShadow(matrices,textRenderer, text,12,y+5,0xFFFFFF);
|
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 Element> children() {return children;}
|
||||||
public List<? extends Selectable> selectableChildren() {return children;}
|
public List<? extends Selectable> selectableChildren() {return children;}
|
||||||
@@ -391,7 +407,10 @@ public abstract class MidnightConfig {
|
|||||||
}
|
}
|
||||||
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Client {}
|
@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 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 static class HiddenAnnotationExclusionStrategy implements ExclusionStrategy {
|
||||||
public boolean shouldSkipClass(Class<?> clazz) { return false; }
|
public boolean shouldSkipClass(Class<?> clazz) { return false; }
|
||||||
|
|||||||
BIN
src/main/resources/assets/midnightlib/icon.png
Executable file → Normal file
BIN
src/main/resources/assets/midnightlib/icon.png
Executable file → Normal file
Binary file not shown.
|
Before Width: | Height: | Size: 4.6 KiB After Width: | Height: | Size: 2.7 KiB |
Reference in New Issue
Block a user