Update to 1.19.4 & Fix invalid value tooltips

This commit is contained in:
Motschen
2023-05-18 21:33:17 +02:00
parent e9e1bec91f
commit 8109eeb74e
9 changed files with 56 additions and 62 deletions

View File

@@ -9,7 +9,7 @@ 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.DrawableHelper;
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;
@@ -19,8 +19,8 @@ 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.client.util.math.MatrixStack;
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;
@@ -41,10 +41,9 @@ import java.util.function.Function;
import java.util.function.Predicate;
import java.util.regex.Pattern;
/** MidnightConfig v2.4.0 by TeamMidnightDust & Motschen
/** MidnightConfig v2.4.1 by TeamMidnightDust & Motschen
* Single class config library - feel free to copy!
* Based on https://github.com/Minenash/TinyConfig
* Based on <a href="https://github.com/Minenash/TinyConfig">...</a>
* Credits to Minenash */
@SuppressWarnings("unchecked")
@@ -115,9 +114,8 @@ public abstract class MidnightConfig {
if (type == int.class) textField(info, Integer::parseInt, INTEGER_ONLY, (int) e.min(), (int) e.max(), true);
else if (type == float.class) textField(info, Float::parseFloat, DECIMAL_ONLY, (float) e.min(), (float) e.max(), false);
else if (type == double.class) textField(info, Double::parseDouble, DECIMAL_ONLY, e.min(), e.max(), false);
else if (type == String.class || type == List.class) {
textField(info, String::length, null, Math.min(e.min(), 0), Math.max(e.max(), 1), true);
} else if (type == boolean.class) {
else if (type == String.class || type == List.class) 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.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;
@@ -135,6 +133,9 @@ public abstract class MidnightConfig {
}
entries.add(info);
}
public static Tooltip getTooltip(EntryInfo info) {
return Tooltip.of(info.error != null ? info.error : I18n.hasTranslation(info.id + ".midnightconfig."+info.field.getName()+".tooltip") ? Text.translatable(info.id + ".midnightconfig."+info.field.getName()+".tooltip") : Text.empty());
}
private static void textField(EntryInfo info, Function<String,Number> f, Pattern pattern, double min, double max, boolean cast) {
boolean isNumber = pattern != null;
@@ -151,6 +152,7 @@ public abstract class MidnightConfig {
info.error = inLimits? null : Text.literal(value.doubleValue() < min ?
"§cMinimum " + (isNumber? "value" : "length") + (cast? " is " + (int)min : " is " + min) :
"§cMaximum " + (isNumber? "value" : "length") + (cast? " is " + (int)max : " is " + max)).formatted(Formatting.RED);
t.setTooltip(getTooltip(info));
}
info.tempValue = s;
@@ -248,9 +250,6 @@ public abstract class MidnightConfig {
if (this.tabNavigation.trySwitchTabsWithKey(keyCode)) return true;
return super.keyPressed(keyCode, scanCode, modifiers);
}
public Tooltip getTooltip(EntryInfo info) {
return Tooltip.of(info.error != null ? info.error : I18n.hasTranslation(translationPrefix+info.field.getName()+".tooltip") ? Text.translatable(translationPrefix+info.field.getName()+".tooltip") : Text.empty());
}
public void refresh() {
double scrollAmount = list.getScrollAmount();
list.clear();
@@ -269,8 +268,8 @@ public abstract class MidnightConfig {
String name = translationPrefix + "category." + tabId;
if (!I18n.hasTranslation(name) && tabId.equals("default"))
name = translationPrefix + "title";
Tab tab = new GridScreenTab(Text.translatable(name));
if (!tabs.containsKey(name)) {
Tab tab = new GridScreenTab(Text.translatable(name));
e.tab = tab;
tabs.put(name, tab);
} else e.tab = tabs.get(name);
@@ -374,12 +373,12 @@ public abstract class MidnightConfig {
}
}
@Override
public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
this.renderBackground(matrices);
this.list.render(matrices, mouseX, mouseY, delta);
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
this.renderBackground(context);
this.list.render(context, mouseX, mouseY, delta);
drawCenteredTextWithShadow(matrices, textRenderer, title, width / 2, 15, 0xFFFFFF);
super.render(matrices,mouseX,mouseY,delta);
context.drawCenteredTextWithShadow(textRenderer, title, width / 2, 15, 0xFFFFFF);
super.render(context,mouseX,mouseY,delta);
}
}
@Environment(EnvType.CLIENT)
@@ -418,11 +417,17 @@ public abstract class MidnightConfig {
this.info = info;
children.addAll(buttons);
}
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.setY(y); b.render(matrices, mouseX, mouseY, tickDelta); });
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())) {
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);
if (info.centered) context.drawTextWithShadow(textRenderer, text, MinecraftClient.getInstance().getWindow().getScaledWidth() / 2 - (textRenderer.getWidth(text) / 2), y + 5, 0xFFFFFF);
else {
int wrappedY = y;
for(Iterator<OrderedText> iterator = textRenderer.wrapLines(text, (buttons.size() > 1 ? buttons.get(1).getX()-24 : MinecraftClient.getInstance().getWindow().getScaledWidth() - 24)).iterator(); iterator.hasNext(); wrappedY += 9) {
OrderedText orderedText = iterator.next();
context.drawTextWithShadow(textRenderer, orderedText, 12, wrappedY + 5, 0xFFFFFF);
}
}
}
}
public List<? extends Element> children() {return children;}