More compact code & finishing touches

This commit is contained in:
Martin Prokoph
2024-08-25 12:17:04 +02:00
parent 5fa635911f
commit 8aab796530
8 changed files with 115 additions and 304 deletions

View File

@@ -20,11 +20,8 @@ public class MidnightLib {
@Environment(EnvType.CLIENT)
public static void onInitializeClient() {
System.setProperty("java.awt.headless", "false");
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
LOGGER.error("Error setting system look and feel", e);
}
try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) { LOGGER.error("Error setting system look and feel", e); }
MidnightLibConfig.init("midnightlib", MidnightLibConfig.class);
}
@Environment(EnvType.SERVER)

View File

@@ -26,16 +26,14 @@ public class MidnightConfigOverviewScreen extends Screen {
protected void init() {
this.addDrawableChild(ButtonWidget.builder(ScreenTexts.DONE, (button) -> Objects.requireNonNull(client).setScreen(parent)).dimensions(this.width / 2 - 100, this.height - 26, 200, 20).build());
this.list = new MidnightConfig.MidnightConfigListWidget(this.client, this.width, this.height - 57, 24, 25);
this.addSelectableChild(this.list);
this.addSelectableChild(this.list = new MidnightConfig.MidnightConfigListWidget(this.client, this.width, this.height - 57, 24, 25));
List<String> sortedMods = new ArrayList<>(MidnightConfig.configClass.keySet());
Collections.sort(sortedMods);
sortedMods.forEach((modid) -> {
if (!MidnightLib.hiddenMods.contains(modid)) {
list.addButton(List.of(ButtonWidget.builder(Text.translatable(modid +".midnightconfig.title"), (button) ->
Objects.requireNonNull(client).setScreen(MidnightConfig.getScreen(this,modid))).dimensions(this.width / 2 - 125, this.height - 28, 250, 20).build()), null, null);
}
});
}});
super.init();
}
@Override

View File

@@ -1,54 +1,31 @@
package eu.midnightdust.lib.config;
import com.google.common.collect.Lists;
import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.ExclusionStrategy; import com.google.gson.FieldAttributes; import com.google.gson.Gson; import com.google.gson.GsonBuilder;
import com.mojang.blaze3d.systems.RenderSystem;
import eu.midnightdust.lib.util.PlatformFunctions;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.Element;
import net.minecraft.client.gui.Selectable;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.tab.GridScreenTab;
import net.minecraft.client.gui.tab.Tab;
import net.minecraft.client.gui.tab.TabManager;
import net.minecraft.client.gui.tooltip.Tooltip;
import net.minecraft.client.gui.widget.*;
import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment;
import net.minecraft.client.MinecraftClient; import net.minecraft.client.font.TextRenderer; import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.Element; import net.minecraft.client.gui.Selectable; import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.tab.GridScreenTab; import net.minecraft.client.gui.tab.Tab; import net.minecraft.client.gui.tab.TabManager;
import net.minecraft.client.gui.tooltip.Tooltip; import net.minecraft.client.gui.widget.*;
import net.minecraft.client.resource.language.I18n;
import net.minecraft.registry.Registries;
import net.minecraft.screen.ScreenTexts;
import net.minecraft.text.OrderedText;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;
import net.minecraft.text.OrderedText; import net.minecraft.text.Style; import net.minecraft.text.Text;
import net.minecraft.util.Formatting; import net.minecraft.util.Identifier;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.*; import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.Color;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.nio.file.Files;
import java.nio.file.Path;
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;
import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.lang.reflect.ParameterizedType;
import java.nio.file.Files; import java.nio.file.Path;
import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.BiFunction; import java.util.function.Function; import java.util.function.Predicate;
import java.util.regex.Pattern;
/** MidnightConfig v2.6.0 by TeamMidnightDust & Motschen
/** MidnightConfig v2.6.0 by Martin "Motschen" Prokoph
* Single class config library - feel free to copy!
* Based on <a href="https://github.com/Minenash/TinyConfig">...</a>
* Credits to Minenash */
@@ -64,35 +41,24 @@ public abstract class MidnightConfig {
public static class EntryInfo {
Field field;
Class<?> dataType;
Object function;
int width;
int width, listIndex;
boolean centered;
Text error;
Object defaultValue;
Object value;
String tempValue; // The value visible in the config screen
Object defaultValue, value, function;
String modid, tempValue; // The value visible in the config screen
boolean inLimits = true;
String modid;
Text name;
int index;
Text name, error;
ClickableWidget actionButton; // color picker button / explorer button
Tab tab;
public void setValue(Object value) {
if (this.field.getType() != List.class) {
this.value = value;
if (this.field.getType() != List.class) { this.value = value;
this.tempValue = value.toString();
} else {
writeList(this.index, value);
this.tempValue = toTemporaryValue();
}
} else { writeList(this.listIndex, value);
this.tempValue = toTemporaryValue(); }
}
public String toTemporaryValue() {
if (this.field.getType() != List.class) return this.value.toString();
else {
try { return ((List<?>) this.value).get(this.index).toString();
} catch (Exception ignored) {return "";}
}
else try { return ((List<?>) this.value).get(this.listIndex).toString(); } catch (Exception ignored) {return "";}
}
public <T> void writeList(int index, T value) {
var list = (List<T>) this.value;
@@ -105,19 +71,15 @@ public abstract class MidnightConfig {
private static Path path;
private static final Gson gson = new GsonBuilder()
.excludeFieldsWithModifiers(Modifier.TRANSIENT)
.excludeFieldsWithModifiers(Modifier.PRIVATE)
.excludeFieldsWithModifiers(Modifier.TRANSIENT).excludeFieldsWithModifiers(Modifier.PRIVATE)
.addSerializationExclusionStrategy(new HiddenAnnotationExclusionStrategy())
.registerTypeAdapter(Identifier.class, new Identifier.Serializer())
.setPrettyPrinting().create();
public static @Nullable Object getDefaultValue(String modid, String entry) {
for (EntryInfo e : entries) {
if (modid.equals(e.modid) && entry.equals(e.field.getName())) {
return e.defaultValue;
}
}
return null;
if (modid.equals(e.modid) && entry.equals(e.field.getName())) return e.defaultValue;
} return null;
}
public static void init(String modid, Class<? extends MidnightConfig> config) {
@@ -130,16 +92,14 @@ public abstract class MidnightConfig {
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);
try { info.defaultValue = field.get(null);
} catch (IllegalAccessException ignored) {}
}
try { gson.fromJson(Files.newBufferedReader(path), config); }
catch (Exception e) { write(modid); }
for (EntryInfo info : entries) {
if (info.field.isAnnotationPresent(Entry.class))
try {
if (info.field.isAnnotationPresent(Entry.class)) try {
info.value = info.field.get(null);
info.tempValue = info.toTemporaryValue();
} catch (IllegalAccessException ignored) {}
@@ -150,14 +110,11 @@ public abstract class MidnightConfig {
info.dataType = field.getType();
Entry e = field.getAnnotation(Entry.class);
info.width = e != null ? e.width() : 0;
info.field = field;
info.modid = modid;
info.field = field; info.modid = modid;
if (info.dataType == List.class) {
Class<?> listType = (Class<?>) ((ParameterizedType) info.field.getGenericType()).getActualTypeArguments()[0];
try { info.dataType = (Class<?>) listType.getField("TYPE").get(null);
} catch (NoSuchFieldException | IllegalAccessException ignored) {
info.dataType = listType;
}
} catch (NoSuchFieldException | IllegalAccessException ignored) { info.dataType = listType; }
}
if (e != null) {
@@ -165,24 +122,20 @@ public abstract class MidnightConfig {
if (info.dataType == int.class) textField(info, Integer::parseInt, INTEGER_ONLY, (int) e.min(), (int) e.max(), true);
else if (info.dataType == float.class) textField(info, Float::parseFloat, DECIMAL_ONLY, (float) e.min(), (float) e.max(), false);
else if (info.dataType == double.class) textField(info, Double::parseDouble, DECIMAL_ONLY, e.min(), e.max(), false);
else if (info.dataType == String.class || info.dataType == Identifier.class)
textField(info, String::length, null, Math.min(e.min(), 0), Math.max(e.max(), 1), true);
else if (info.dataType == String.class || info.dataType == Identifier.class) textField(info, String::length, null, Math.min(e.min(), 0), Math.max(e.max(), 1), true);
else if (info.dataType == boolean.class) {
Function<Object, Text> func = value -> Text.translatable((Boolean) value ? "gui.yes" : "gui.no").formatted((Boolean) value ? Formatting.GREEN : Formatting.RED);
info.function = new AbstractMap.SimpleEntry<ButtonWidget.PressAction, Function<Object, Text>>(button -> {
info.setValue(!(Boolean) info.value);
button.setMessage(func.apply(info.value));
info.setValue(!(Boolean) info.value); button.setMessage(func.apply(info.value));
}, func);
} else if (info.dataType.isEnum()) {
List<?> values = Arrays.asList(field.getType().getEnumConstants());
Function<Object, Text> func = value -> Text.translatable(modid + ".midnightconfig." + "enum." + info.dataType.getSimpleName() + "." + info.toTemporaryValue());
info.function = new AbstractMap.SimpleEntry<ButtonWidget.PressAction, Function<Object, Text>>(button -> {
int index = values.indexOf(info.value) + 1;
info.value = values.get(index >= values.size() ? 0 : index);
button.setMessage(func.apply(info.value));
info.value = values.get(index >= values.size() ? 0 : index); button.setMessage(func.apply(info.value));
}, func);
}
}
}}
entries.add(info);
}
public static Tooltip getTooltip(EntryInfo info) {
@@ -197,9 +150,7 @@ public abstract class MidnightConfig {
s = s.trim();
if (!(s.isEmpty() || !isNumber || pattern.matcher(s).matches())) return false;
Number value = 0;
boolean inLimits = false;
info.error = null;
Number value = 0; boolean inLimits = false; info.error = null;
if (!(isNumber && s.isEmpty()) && !s.equals("-") && !s.equals(".")) {
try { value = f.apply(s); } catch(NumberFormatException e){ return false; }
inLimits = value.doubleValue() >= min && value.doubleValue() <= max;
@@ -215,16 +166,14 @@ public abstract class MidnightConfig {
b.active = entries.stream().allMatch(e -> e.inLimits);
if (inLimits) {
if (info.dataType == Identifier.class) {
info.setValue(Identifier.tryParse(s));
} else info.setValue(isNumber ? value : s);
if (info.dataType == Identifier.class) info.setValue(Identifier.tryParse(s));
else info.setValue(isNumber ? value : s);
}
if (info.field.getAnnotation(Entry.class).isColor()) {
if (!s.contains("#")) s = '#' + s;
if (!HEXADECIMAL_ONLY.matcher(s).matches()) return false;
try {
info.actionButton.setMessage(Text.literal("").setStyle(Style.EMPTY.withColor(Color.decode(info.tempValue).getRGB())));
try { info.actionButton.setMessage(Text.literal("").setStyle(Style.EMPTY.withColor(Color.decode(info.tempValue).getRGB())));
} catch (Exception ignored) {}
}
return true;
@@ -233,18 +182,12 @@ public abstract class MidnightConfig {
public static MidnightConfig getClass(String modid) {
try { return configClass.get(modid).getDeclaredConstructor().newInstance(); } catch (Exception e) {throw new RuntimeException(e);}
}
public static void write(String modid) {
getClass(modid).writeChanges(modid);
}
public static void write(String modid) { getClass(modid).writeChanges(modid); }
public void writeChanges(String modid) {
path = PlatformFunctions.getConfigDirectory().resolve(modid + ".json");
try {
if (!Files.exists(path)) Files.createFile(path);
try { if (!Files.exists(path = PlatformFunctions.getConfigDirectory().resolve(modid + ".json"))) Files.createFile(path);
Files.write(path, gson.toJson(getClass(modid)).getBytes());
} catch (Exception e) {
e.fillInStackTrace();
}
} catch (Exception e) { e.fillInStackTrace(); }
}
@Environment(EnvType.CLIENT)
public static Screen getScreen(Screen parent, String modid) {
@@ -254,8 +197,7 @@ public abstract class MidnightConfig {
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.parent = parent; this.modid = modid;
this.translationPrefix = modid + ".midnightconfig.";
loadValues();
@@ -277,9 +219,8 @@ public abstract class MidnightConfig {
tabNavigation.init();
prevTab = tabManager.getCurrentTab();
}
public final String translationPrefix;
public final String translationPrefix, modid;
public final Screen parent;
public final String modid;
public MidnightConfigListWidget list;
public TabManager tabManager = new TabManager(a -> {}, a -> {});
public Map<String, Tab> tabs = new HashMap<>();
@@ -294,39 +235,29 @@ public abstract class MidnightConfig {
super.tick();
if (prevTab != null && prevTab != tabManager.getCurrentTab()) {
prevTab = tabManager.getCurrentTab();
this.list.clear();
fillList();
this.list.clear(); fillList();
list.setScrollAmount(0);
}
scrollProgress = list.getScrollAmount();
for (EntryInfo info : entries) {
try {info.field.set(null, info.value);} catch (IllegalAccessException ignored) {}
}
for (EntryInfo info : entries) try {info.field.set(null, info.value);} catch (IllegalAccessException ignored) {}
updateButtons();
}
public void updateButtons() {
if (this.list != null) {
for (ButtonEntry entry : this.list.children()) {
if (entry.buttons != null) {
if (entry.buttons.size() > 1 && entry.buttons.get(0) instanceof ClickableWidget widget && (widget.isFocused() || widget.isHovered())) {
widget.setTooltip(getTooltip(entry.info));
}
if (entry.buttons.size() > 1 && entry.buttons.get(1) instanceof ButtonWidget button) {
if (entry.buttons != null && entry.buttons.size() > 1) {
if (entry.buttons.get(0) instanceof ClickableWidget widget)
if (widget.isFocused() || widget.isHovered()) widget.setTooltip(getTooltip(entry.info));
if (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)); }
catch (Exception e) { write(modid); }
for (EntryInfo info : entries) {
if (info.field.isAnnotationPresent(Entry.class))
try {
info.value = info.field.get(null);
info.tempValue = info.toTemporaryValue();
try { info.value = info.field.get(null); info.tempValue = info.toTemporaryValue();
} catch (IllegalAccessException ignored) {}
}
}
@@ -337,45 +268,29 @@ public abstract class MidnightConfig {
}
@Override
public void close() {
loadValues();
cleanup();
loadValues(); cleanup();
Objects.requireNonNull(client).setScreen(parent);
}
private void cleanup() {
entries.forEach(info -> {
info.error = null;
info.value = null;
info.tempValue = null;
info.actionButton = null;
info.index = 0;
info.tab = null;
info.inLimits = true;
info.error = null; info.value = null; info.tempValue = null; info.actionButton = null; info.listIndex = 0; info.tab = null; info.inLimits = true;
});
}
@Override
public void init() {
super.init();
tabNavigation.setWidth(this.width);
tabNavigation.init();
tabNavigation.setWidth(this.width); tabNavigation.init();
if (tabs.size() > 1) this.addDrawableChild(tabNavigation);
this.addDrawableChild(ButtonWidget.builder(ScreenTexts.CANCEL, button -> this.close()).dimensions(this.width / 2 - 154, this.height - 26, 150, 20).build());
done = this.addDrawableChild(ButtonWidget.builder(ScreenTexts.DONE, (button) -> {
for (EntryInfo info : entries)
if (info.modid.equals(modid)) {
try {
info.field.set(null, info.value);
} catch (IllegalAccessException ignored) {}
}
write(modid);
cleanup();
for (EntryInfo info : entries) if (info.modid.equals(modid)) try { info.field.set(null, info.value); } catch (IllegalAccessException ignored) {}
write(modid); cleanup();
Objects.requireNonNull(client).setScreen(parent);
}).dimensions(this.width / 2 + 4, this.height - 26, 150, 20).build());
this.list = new MidnightConfigListWidget(this.client, this.width, this.height - 57, 24, 25);
this.addSelectableChild(this.list);
fillList();
this.addSelectableChild(this.list); fillList();
if (tabs.size() > 1) list.renderHeaderSeparator = false;
}
public void fillList() {
@@ -383,11 +298,9 @@ public abstract class MidnightConfig {
if (info.modid.equals(modid) && (info.tab == null || info.tab == tabManager.getCurrentTab())) {
Text name = Objects.requireNonNullElseGet(info.name, () -> Text.translatable(translationPrefix + info.field.getName()));
TextIconButtonWidget resetButton = TextIconButtonWidget.builder(Text.translatable("controls.reset"), (button -> {
info.value = info.defaultValue;
info.value = info.defaultValue; info.listIndex = 0;
info.tempValue = info.toTemporaryValue();
info.index = 0;
list.clear();
fillList();
list.clear(); fillList();
}), true).texture(Identifier.of("midnightlib","icon/reset"), 12, 12).dimension(20, 20).build();
resetButton.setPosition(width - 205 + 150 + 25, 0);
@@ -403,12 +316,10 @@ public abstract class MidnightConfig {
}
else if (e.isSlider())
widget = new MidnightSliderWidget(width - 185, 0, 150, 20, Text.of(info.tempValue), (Double.parseDouble(info.tempValue) - e.min()) / (e.max() - e.min()), info);
else
widget = new MidnightTextFieldWidget(textRenderer, width - 185, 0, 150, 20, info);
else widget = new TextFieldWidget(textRenderer, width - 185, 0, 150, 20, Text.empty());
if (widget instanceof TextFieldWidget textField) {
textField.setMaxLength(info.width);
textField.setText(info.tempValue);
textField.setMaxLength(info.width); textField.setText(info.tempValue);
Predicate<String> processor = ((BiFunction<TextFieldWidget, ButtonWidget, Predicate<String>>) info.function).apply(textField, done);
textField.setTextPredicate(processor);
}
@@ -416,75 +327,59 @@ public abstract class MidnightConfig {
ButtonWidget cycleButton = null;
if (info.field.getType() == List.class) {
cycleButton = ButtonWidget.builder(Text.literal(String.valueOf(info.index)).formatted(Formatting.GOLD), (button -> {
cycleButton = ButtonWidget.builder(Text.literal(String.valueOf(info.listIndex)).formatted(Formatting.GOLD), (button -> {
var values = (List<?>) info.value;
values.remove("");
info.index = info.index + 1;
if (info.index > values.size()) info.index = 0;
info.listIndex = info.listIndex + 1;
if (info.listIndex > values.size()) info.listIndex = 0;
info.tempValue = info.toTemporaryValue();
if (info.index == values.size()) info.tempValue = "";
list.clear();
fillList();
if (info.listIndex == values.size()) info.tempValue = "";
list.clear(); fillList();
})).dimensions(width - 185, 0, 20, 20).build();
}
if (e.isColor()) {
ButtonWidget colorButton = ButtonWidget.builder(
Text.literal(""),
ButtonWidget colorButton = ButtonWidget.builder(Text.literal(""),
button -> new Thread(() -> {
Color newColor = JColorChooser.showDialog(null, Text.translatable("midnightconfig.colorChooser.title").getString(), Color.decode(!Objects.equals(info.tempValue, "") ? info.tempValue : "#FFFFFF"));
if (newColor != null) {
info.setValue("#" + Integer.toHexString(newColor.getRGB()).substring(2));
list.clear();
fillList();
list.clear(); fillList();
}
}).start()
).dimensions(width - 185, 0, 20, 20).build();
try {
colorButton.setMessage(Text.literal("").setStyle(Style.EMPTY.withColor(Color.decode(info.tempValue).getRGB())));
try { colorButton.setMessage(Text.literal("").setStyle(Style.EMPTY.withColor(Color.decode(info.tempValue).getRGB())));
} catch (Exception ignored) {}
info.actionButton = colorButton;
} else if (e.selectionMode() > -1) {
ButtonWidget explorerButton = TextIconButtonWidget.builder(
Text.of(""),
ButtonWidget explorerButton = TextIconButtonWidget.builder(Text.of(""),
button -> new Thread(() -> {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(e.selectionMode());
fileChooser.setDialogType(e.fileChooserType());
fileChooser.setDialogTitle(Text.translatable(translationPrefix + info.field.getName() + ".fileChooser.title").getString());
if ((e.selectionMode() == JFileChooser.FILES_ONLY || e.selectionMode() == JFileChooser.FILES_AND_DIRECTORIES) &&
Arrays.stream(e.fileExtensions()).noneMatch("*"::equals)) {
fileChooser.setFileSelectionMode(e.selectionMode()); fileChooser.setDialogType(e.fileChooserType());
fileChooser.setDialogTitle(Text.translatable(translationPrefix + info.field.getName() + ".fileChooser").getString());
if ((e.selectionMode() == JFileChooser.FILES_ONLY || e.selectionMode() == JFileChooser.FILES_AND_DIRECTORIES) && Arrays.stream(e.fileExtensions()).noneMatch("*"::equals))
fileChooser.setFileFilter(new FileNameExtensionFilter(
Text.translatable(translationPrefix + info.field.getName() + ".fileFilter.description").getString(),
e.fileExtensions()));
}
Text.translatable(translationPrefix + info.field.getName() + ".fileFilter").getString(), e.fileExtensions()));
if (fileChooser.showDialog(null, null) == JFileChooser.APPROVE_OPTION) {
info.setValue(fileChooser.getSelectedFile().getAbsolutePath());
list.clear();
fillList();
list.clear(); fillList();
}
}).start(),
true
}).start(), true
).texture(Identifier.of("midnightlib", "icon/explorer"), 12, 12).dimension(20, 20).build();
explorerButton.setPosition(width - 185, 0);
info.actionButton = explorerButton;
}
List<ClickableWidget> widgets = Lists.newArrayList(widget, resetButton);
if (info.actionButton != null) {
widget.setWidth(widget.getWidth() - 22);
widget.setX(widget.getX() + 22);
widget.setWidth(widget.getWidth() - 22); widget.setX(widget.getX() + 22);
widgets.add(info.actionButton);
} if (cycleButton != null) {
if (info.actionButton != null) info.actionButton.setX(info.actionButton.getX() + 22);
widget.setWidth(widget.getWidth() - 22);
widget.setX(widget.getX() + 22);
widget.setWidth(widget.getWidth() - 22); widget.setX(widget.getX() + 22);
widgets.add(cycleButton);
}
this.list.addButton(widgets, name, info);
} else {
this.list.addButton(List.of(), name, info);
}
}
list.setScrollAmount(scrollProgress);
} else this.list.addButton(List.of(), name, info);
} list.setScrollAmount(scrollProgress);
updateButtons();
}
}
@@ -492,56 +387,47 @@ public abstract class MidnightConfig {
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
super.render(context,mouseX,mouseY,delta);
this.list.render(context, mouseX, mouseY, delta);
if (tabs.size() < 2) context.drawCenteredTextWithShadow(textRenderer, title, width / 2, 10, 0xFFFFFF);
if (tabs.size() < 2)
context.drawCenteredTextWithShadow(textRenderer, title, width / 2, 10, 0xFFFFFF);
}
if (this.list != null) {
for (ButtonEntry entry : this.list.children()) {
if (entry.buttons != null && entry.buttons.size() > 1) {
if (entry.buttons.getFirst() instanceof ClickableWidget widget) {
int idMode = entry.info.field.getAnnotation(Entry.class).idMode();
if (idMode != -1) context.drawItem(idMode == 0 ? Registries.ITEM.get(Identifier.tryParse(entry.info.tempValue)).getDefaultStack() : Registries.BLOCK.get(Identifier.tryParse(entry.info.tempValue)).asItem().getDefaultStack(), widget.getX() + widget.getWidth() - 18, widget.getY() + 2);
}}}}}
}
@Environment(EnvType.CLIENT)
public static class MidnightConfigListWidget extends ElementListWidget<ButtonEntry> {
boolean renderHeaderSeparator = true;
public MidnightConfigListWidget(MinecraftClient client, int width, int height, int y, int itemHeight) {
super(client, width, height, y, itemHeight);
}
@Override
public int getScrollbarX() { return this.width -7; }
public MidnightConfigListWidget(MinecraftClient client, int width, int height, int y, int itemHeight) { super(client, width, height, y, itemHeight); }
@Override public int getScrollbarX() { return this.width -7; }
@Override
protected void drawHeaderAndFooterSeparators(DrawContext context) {
if (renderHeaderSeparator) super.drawHeaderAndFooterSeparators(context);
else {
RenderSystem.enableBlend();
else { RenderSystem.enableBlend();
context.drawTexture(this.client.world == null ? Screen.FOOTER_SEPARATOR_TEXTURE : Screen.INWORLD_FOOTER_SEPARATOR_TEXTURE, this.getX(), this.getBottom(), 0.0F, 0.0F, this.getWidth(), 2, 32, 2);
RenderSystem.disableBlend();
}
}
public void addButton(List<ClickableWidget> buttons, Text text, EntryInfo info) {
this.addEntry(new ButtonEntry(buttons, text, info));
RenderSystem.disableBlend(); }
}
public void addButton(List<ClickableWidget> buttons, Text text, EntryInfo info) { this.addEntry(new ButtonEntry(buttons, text, info)); }
public void clear() { this.clearEntries(); }
@Override
public int getRowWidth() { return 10000; }
@Override public int getRowWidth() { return 10000; }
}
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 List<ClickableWidget> buttons;
public final EntryInfo info;
public boolean centered = false;
public static final Map<ClickableWidget, Text> buttonsWithText = new HashMap<>();
public ButtonEntry(List<ClickableWidget> buttons, Text text, EntryInfo info) {
if (!buttons.isEmpty()) buttonsWithText.put(buttons.getFirst(),text);
this.buttons = buttons;
this.text = text;
this.info = info;
this.buttons = buttons; this.text = text; this.info = info;
if (info != null) this.centered = info.centered;
}
public void render(DrawContext context, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) {
buttons.forEach(b -> { b.setY(y); b.render(context, mouseX, mouseY, tickDelta); });
if (text != null && (!text.getString().contains("spacer") || !buttons.isEmpty())) {
int wrappedY = y;
if (text != null && (!text.getString().contains("spacer") || !buttons.isEmpty())) { int wrappedY = y;
for (Iterator<OrderedText> textIterator = textRenderer.wrapLines(text, (buttons.size() > 1 ? buttons.get(1).getX()-24 : MinecraftClient.getInstance().getWindow().getScaledWidth() - 24)).iterator(); textIterator.hasNext(); wrappedY += 9) {
context.drawTextWithShadow(textRenderer, textIterator.next(), (centered) ? (MinecraftClient.getInstance().getWindow().getScaledWidth() / 2 - (textRenderer.getWidth(text) / 2)) : 12, wrappedY + 5, 0xFFFFFF);
}
@@ -559,37 +445,19 @@ public abstract class MidnightConfig {
}
@Override
protected void updateMessage() {
this.setMessage(Text.of(info.tempValue));
}
public void updateMessage() { this.setMessage(Text.of(info.tempValue)); }
@Override
protected void applyValue() {
public void applyValue() {
if (info.dataType == int.class) info.setValue(((Number) (e.min() + value * (e.max() - e.min()))).intValue());
else if (info.field.getType() == double.class) info.setValue(Math.round((e.min() + value * (e.max() - e.min())) * (double) e.precision()) / (double) e.precision());
else if (info.field.getType() == float.class) info.setValue(Math.round((e.min() + value * (e.max() - e.min())) * (float) e.precision()) / (float) e.precision());
}
}
public static class MidnightTextFieldWidget extends TextFieldWidget {
private final EntryInfo info;
private final boolean isItem;
public MidnightTextFieldWidget(TextRenderer textRenderer, int x, int y, int width, int height, EntryInfo info) {
super(textRenderer, x, y, width, height, Text.of(info.tempValue));
this.info = info;
this.isItem = info.field.getAnnotation(Entry.class).isItem();
}
public void renderWidget(DrawContext context, int mouseX, int mouseY, float delta) {
super.renderWidget(context, mouseX, mouseY, delta);
if (isItem)
context.drawItem(Registries.ITEM.get(Identifier.tryParse(info.tempValue)).getDefaultStack(), this.getX()+this.getWidth()-18, this.getY()+2);
}
}
/**
* Entry Annotation<br>
* - <b>width</b>: The maximum character length of the {@link String} or {@link List<String>} field<br>
* - <b>width</b>: The maximum character length of the {@link String}, {@link Identifier} or String/Identifier {@link List<String>} field<br>
* - <b>min</b>: The minimum value of the <code>int</code>, <code>float</code> or <code>double</code> field<br>
* - <b>max</b>: The maximum value of the <code>int</code>, <code>float</code> or <code>double</code> field<br>
* - <b>name</b>: The name of the field in the config screen<br>
@@ -618,7 +486,7 @@ public abstract class MidnightConfig {
int selectionMode() default -1; // -1 for none, 0 for file, 1 for directory, 2 for both
int fileChooserType() default JFileChooser.OPEN_DIALOG;
String[] fileExtensions() default {"*"};
boolean isItem() default false;
int idMode() default -1; // -1 for none, 0 for item, 1 for block
boolean isColor() default false;
boolean isSlider() default false;
int precision() default 100;
@@ -635,8 +503,6 @@ public abstract class MidnightConfig {
public static class HiddenAnnotationExclusionStrategy implements ExclusionStrategy {
public boolean shouldSkipClass(Class<?> clazz) { return false; }
public boolean shouldSkipField(FieldAttributes fieldAttributes) {
return fieldAttributes.getAnnotation(Entry.class) == null;
}
public boolean shouldSkipField(FieldAttributes fieldAttributes) { return fieldAttributes.getAnnotation(Entry.class) == null; }
}
}

View File

@@ -8,9 +8,7 @@ public class MidnightColorUtil {
* @return Color as RGB
*/
public static Color hex2Rgb(String colorStr) {
try {
return Color.decode("#" + colorStr.replace("#", ""));
} catch (Exception ignored) {}
return Color.BLACK;
try { return Color.decode("#" + colorStr.replace("#", ""));
} catch (Exception ignored) { return Color.BLACK; }
}
}

View File

@@ -1,17 +0,0 @@
architectury {
common(rootProject.enabled_platforms.split(","))
}
repositories {
maven { url "https://maven.terraformersmc.com/releases" }
}
loom {
}
dependencies {
modImplementation "net.fabricmc:fabric-loader:${rootProject.fabric_loader_version}"
modApi "net.fabricmc.fabric-api:fabric-api:${rootProject.fabric_api_version}"
modImplementation ("com.terraformersmc:modmenu:${rootProject.mod_menu_version}")
compileOnly(project(path: ":common", configuration: "namedElements")) { transitive false }
}

View File

@@ -1,28 +0,0 @@
package eu.midnightdust.lib.config;
import com.terraformersmc.modmenu.api.ConfigScreenFactory;
import com.terraformersmc.modmenu.api.ModMenuApi;
import eu.midnightdust.core.MidnightLib;
import eu.midnightdust.core.config.MidnightLibConfig;
import java.util.HashMap;
import java.util.Map;
public class AutoModMenu implements ModMenuApi {
@Override
public ConfigScreenFactory<?> getModConfigScreenFactory() {
return parent -> MidnightLibConfig.getScreen(parent,"midnightlib");
}
@Override
public Map<String, ConfigScreenFactory<?>> getProvidedConfigScreenFactories() {
HashMap<String, ConfigScreenFactory<?>> map = new HashMap<>();
MidnightConfig.configClass.forEach((modid, cClass) -> {
if (!MidnightLib.hiddenMods.contains(modid))
map.put(modid, parent -> MidnightConfig.getScreen(parent, modid));
}
);
return map;
}
}

View File

@@ -21,8 +21,6 @@ public class AutoModMenu implements ModMenuApi {
MidnightConfig.configClass.forEach((modid, cClass) -> {
if (!MidnightLib.hiddenMods.contains(modid))
map.put(modid, parent -> MidnightConfig.getScreen(parent, modid));
}
);
return map;
}); return map;
}
}

View File

@@ -8,7 +8,6 @@ pluginManagement {
}
include("common")
include("fabric-like")
include("fabric")
//include("quilt")
include("neoforge")