From 5dc707a6d81c3b2ab0708e3370bcbf5bdba6f981 Mon Sep 17 00:00:00 2001 From: Motschen Date: Sun, 31 Oct 2021 15:46:34 +0100 Subject: [PATCH] MidnightLib 0.3.0 - String-List ingame support - String Lists can now be edited in MidnightLib config screens - Remove some pointless clutter --- gradle.properties | 2 +- .../core/config/MidnightLibConfig.java | 2 - .../core/mixin/MixinEntryListWidget.java | 23 ---- .../screen/MidnightConfigOverviewScreen.java | 32 ++--- .../eu/midnightdust/hats/web/HatLoader.java | 1 + .../lib/config/MidnightConfig.java | 127 +++++++++++------- src/main/resources/midnightcore.mixins.json | 1 - 7 files changed, 88 insertions(+), 100 deletions(-) delete mode 100755 src/main/java/eu/midnightdust/core/mixin/MixinEntryListWidget.java diff --git a/gradle.properties b/gradle.properties index 9938991..abf1a58 100755 --- a/gradle.properties +++ b/gradle.properties @@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G loader_version=0.11.7 # Mod Properties - mod_version = 0.2.9 + mod_version = 0.3.0 maven_group = eu.midnightdust archives_base_name = midnightlib diff --git a/src/main/java/eu/midnightdust/core/config/MidnightLibConfig.java b/src/main/java/eu/midnightdust/core/config/MidnightLibConfig.java index a2971fb..3bdb5dc 100755 --- a/src/main/java/eu/midnightdust/core/config/MidnightLibConfig.java +++ b/src/main/java/eu/midnightdust/core/config/MidnightLibConfig.java @@ -7,8 +7,6 @@ public class MidnightLibConfig extends MidnightConfig { @Comment 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; - @Entry // Change the texture of the background in MidnightConfig - public static String background_texture = "minecraft:textures/block/deepslate.png"; @Comment public static Comment midnighthats_description; @Entry // Enable or disable hats for contributors, friends and donors. public static boolean special_hats = true; diff --git a/src/main/java/eu/midnightdust/core/mixin/MixinEntryListWidget.java b/src/main/java/eu/midnightdust/core/mixin/MixinEntryListWidget.java deleted file mode 100755 index c1421b9..0000000 --- a/src/main/java/eu/midnightdust/core/mixin/MixinEntryListWidget.java +++ /dev/null @@ -1,23 +0,0 @@ -package eu.midnightdust.core.mixin; - -import com.mojang.blaze3d.systems.RenderSystem; -import eu.midnightdust.core.config.MidnightLibConfig; -import net.minecraft.client.gui.widget.EntryListWidget; -import net.minecraft.client.util.math.MatrixStack; -import net.minecraft.util.Identifier; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -import java.util.Locale; - -@Mixin(EntryListWidget.class) -public abstract class MixinEntryListWidget { - - @Inject(at = @At(value = "INVOKE",target = "Lcom/mojang/blaze3d/systems/RenderSystem;setShaderTexture(ILnet/minecraft/util/Identifier;)V",shift = At.Shift.AFTER), method = "render", cancellable = true) - private void custom_background(MatrixStack matrices, int mouseX, int mouseY, float delta, CallbackInfo ci) { - if (!MidnightLibConfig.background_texture.equals("") && this.getClass().toString().toLowerCase(Locale.ROOT).contains("midnight")) - RenderSystem.setShaderTexture(0, Identifier.tryParse(MidnightLibConfig.background_texture)); - } -} diff --git a/src/main/java/eu/midnightdust/core/screen/MidnightConfigOverviewScreen.java b/src/main/java/eu/midnightdust/core/screen/MidnightConfigOverviewScreen.java index 119111d..df70d19 100755 --- a/src/main/java/eu/midnightdust/core/screen/MidnightConfigOverviewScreen.java +++ b/src/main/java/eu/midnightdust/core/screen/MidnightConfigOverviewScreen.java @@ -40,10 +40,7 @@ protected void init() { public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) { this.renderBackground(matrices); this.list.render(matrices, mouseX, mouseY, delta); - - int stringWidth = title.getString().length() + 47; - renderTooltip(matrices, title, width/2 - stringWidth, 27); - //drawCenteredText(matrices, textRenderer, title, width / 2, 15, 0xFFFFFF); + drawCenteredText(matrices, textRenderer, title, width / 2, 15, 0xFFFFFF); super.render(matrices, mouseX, mouseY, delta); } @Environment(EnvType.CLIENT) @@ -56,7 +53,7 @@ public MidnightOverviewListWidget(MinecraftClient minecraftClient, int i, int j, textRenderer = minecraftClient.textRenderer; } @Override - public int getScrollbarPositionX() { return this.width -7; } + public int getScrollbarPositionX() {return this.width -7;} public void addButton(ClickableWidget button) { this.addEntry(OverviewButtonEntry.create(button)); @@ -65,26 +62,19 @@ public void addButton(ClickableWidget button) { public int getRowWidth() { return 400; } } public static class OverviewButtonEntry extends ElementListWidget.Entry { - private final List buttons = new ArrayList<>(); + private final ClickableWidget button; + private final List buttonList = new ArrayList<>(); private OverviewButtonEntry(ClickableWidget button) { - this.buttons.add(button); - } - public static OverviewButtonEntry create(ClickableWidget button) { - return new OverviewButtonEntry(button); + this.button = button; + this.buttonList.add(button); } + public static OverviewButtonEntry create(ClickableWidget button) {return new OverviewButtonEntry(button);} public void render(MatrixStack matrices, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) { - this.buttons.forEach((button) -> { - button.y = y; - button.render(matrices, mouseX, mouseY, tickDelta); - }); - } - public List children() { - return buttons; - } - - public List selectableChildren() { - return buttons; + button.y = y; + button.render(matrices, mouseX, mouseY, tickDelta); } + public List children() {return buttonList;} + public List selectableChildren() {return buttonList;} } } \ No newline at end of file diff --git a/src/main/java/eu/midnightdust/hats/web/HatLoader.java b/src/main/java/eu/midnightdust/hats/web/HatLoader.java index 38e8eea..f50732b 100755 --- a/src/main/java/eu/midnightdust/hats/web/HatLoader.java +++ b/src/main/java/eu/midnightdust/hats/web/HatLoader.java @@ -17,6 +17,7 @@ import java.util.*; import java.util.concurrent.CompletableFuture; +@SuppressWarnings("UnstableApiUsage") public class HatLoader { public static final Logger logger = LogManager.getLogger("MidnightLib"); private final static String HATS_URL = "https://raw.githubusercontent.com/TeamMidnightDust/MidnightHats/master/hats.json"; diff --git a/src/main/java/eu/midnightdust/lib/config/MidnightConfig.java b/src/main/java/eu/midnightdust/lib/config/MidnightConfig.java index 3b69531..18d642e 100755 --- a/src/main/java/eu/midnightdust/lib/config/MidnightConfig.java +++ b/src/main/java/eu/midnightdust/lib/config/MidnightConfig.java @@ -24,6 +24,7 @@ import net.minecraft.text.Text; import net.minecraft.text.TranslatableText; import net.minecraft.util.Formatting; +import org.apache.logging.log4j.LogManager; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; @@ -39,7 +40,7 @@ import java.util.function.Predicate; import java.util.regex.Pattern; -/** MidnightConfig v1.0.8 by TeamMidnightDust & Motschen +/** MidnightConfig v2.0.0 by TeamMidnightDust & Motschen * Single class config library - feel free to copy! * * Based on https://github.com/Minenash/TinyConfig @@ -64,6 +65,7 @@ protected static class EntryInfo { boolean inLimits = true; String id; TranslatableText name; + int index; } public static final Map> configClass = new HashMap<>(); @@ -108,7 +110,7 @@ private static void initClient(String modid, Field field, EntryInfo info) { if (!e.name().equals("")) info.name = new TranslatableText(e.name()); if (type == int.class) textField(info, Integer::parseInt, INTEGER_ONLY, e.min(), e.max(), true); else if (type == double.class) textField(info, Double::parseDouble, DECIMAL_ONLY, e.min(), e.max(), false); - else if (type == String.class) { + 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) { @@ -154,8 +156,12 @@ private static void textField(EntryInfo info, Function f, Pattern info.inLimits = inLimits; b.active = entries.stream().allMatch(e -> e.inLimits); - if (inLimits) + if (inLimits && info.field.getType() != List.class) info.value = isNumber? value : s; + else if (inLimits) { + if (((List) info.value).size() == info.index) ((List) info.value).add(""); + ((List) info.value).set(info.index, Arrays.stream(info.tempValue.replace("[", "").replace("]", "").split(", ")).toList().get(0)); + } return true; }; @@ -176,7 +182,6 @@ public static Screen getScreen(Screen parent, String modid) { } @Environment(EnvType.CLIENT) private static class MidnightConfigScreen extends Screen { - protected MidnightConfigScreen(Screen parent, String modid) { super(new TranslatableText(modid + ".midnightconfig." + "title")); this.parent = parent; @@ -193,9 +198,9 @@ protected MidnightConfigScreen(Screen parent, String modid) { @Override public void tick() { super.tick(); - 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) {} + } } private void loadValues() { try { gson.fromJson(Files.newBufferedReader(path), configClass.get(modid)); } @@ -206,8 +211,7 @@ private void loadValues() { try { info.value = info.field.get(null); info.tempValue = info.value.toString(); - } catch (IllegalAccessException ignored) { - } + } catch (IllegalAccessException ignored) {} } } @Override @@ -237,9 +241,10 @@ protected void init() { for (EntryInfo info : entries) { if (info.id.equals(modid)) { TranslatableText name = Objects.requireNonNullElseGet(info.name, () -> new TranslatableText(translationPrefix + info.field.getName())); - ButtonWidget resetButton = new ButtonWidget(width - 155, 0, 40, 20, new LiteralText("Reset").formatted(Formatting.RED), (button -> { + ButtonWidget resetButton = new ButtonWidget(width - 205, 0, 40, 20, new LiteralText("Reset").formatted(Formatting.RED), (button -> { info.value = info.defaultValue; - info.tempValue = info.value.toString(); + info.tempValue = info.defaultValue.toString(); + info.index = 0; double scrollAmount = list.getScrollAmount(); this.reload = true; Objects.requireNonNull(client).setScreen(this); @@ -249,17 +254,37 @@ protected void init() { if (info.widget instanceof Map.Entry) { Map.Entry> widget = (Map.Entry>) info.widget; if (info.field.getType().isEnum()) widget.setValue(value -> new TranslatableText(translationPrefix + "enum." + info.field.getType().getSimpleName() + "." + info.value.toString())); - this.list.addButton(new ButtonWidget(width - 110, 0,100, 20, widget.getValue().apply(info.value), widget.getKey()),resetButton,name); + this.list.addButton(new ButtonWidget(width - 160, 0,150, 20, widget.getValue().apply(info.value), widget.getKey()),resetButton, null,name); + } else if (info.field.getType() == List.class) { + if (!reload) info.index = 0; + TextFieldWidget widget = new TextFieldWidget(textRenderer, width - 160, 0, 150, 20, null); + widget.setMaxLength(info.width); + if (info.index < ((List)info.value).size()) widget.setText((String.valueOf(((List)info.value).get(info.index)))); + else widget.setText(""); + Predicate processor = ((BiFunction>) info.widget).apply(widget, done); + widget.setTextPredicate(processor); + resetButton.setWidth(20); + resetButton.setMessage(new LiteralText("R").formatted(Formatting.RED)); + ButtonWidget cycleButton = new ButtonWidget(width - 185, 0, 20, 20, new LiteralText(String.valueOf(info.index)).formatted(Formatting.GOLD), (button -> { + LogManager.getLogger("1").info(info.value); + ((List)info.value).remove(""); + double scrollAmount = list.getScrollAmount(); + this.reload = true; + info.index = info.index + 1; + if (info.index > ((List)info.value).size()) info.index = 0; + Objects.requireNonNull(client).setScreen(this); + list.setScrollAmount(scrollAmount); + })); + this.list.addButton(widget, resetButton, cycleButton, name); } else if (info.widget != null) { - TextFieldWidget widget = new TextFieldWidget(textRenderer, width - 110, 0, 100, 20, null); + TextFieldWidget widget = new TextFieldWidget(textRenderer, width - 160, 0, 150, 20, null); widget.setMaxLength(info.width); widget.setText(info.tempValue); Predicate processor = ((BiFunction>) info.widget).apply(widget, done); widget.setTextPredicate(processor); - this.list.addButton(widget, resetButton, name); + this.list.addButton(widget, resetButton, null, name); } else { - ButtonWidget dummy = new ButtonWidget(-10, 0, 0, 0, Text.of(""), null); - this.list.addButton(dummy,dummy,name); + this.list.addButton(null,null,null,name); } } } @@ -269,10 +294,7 @@ protected void init() { public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) { this.renderBackground(matrices); this.list.render(matrices, mouseX, mouseY, delta); - - int stringWidth = (int) (title.getString().length() * 2.75f); - renderTooltip(matrices, title, width/2 - stringWidth, 27); - //drawCenteredText(matrices, textRenderer, title, width / 2, 15, 0xFFFFFF); + drawCenteredText(matrices, textRenderer, title, width / 2, 15, 0xFFFFFF); for (EntryInfo info : entries) { if (info.id.equals(modid)) { @@ -307,17 +329,15 @@ public MidnightConfigListWidget(MinecraftClient minecraftClient, int i, int j, i @Override public int getScrollbarPositionX() { return this.width -7; } - public void addButton(ClickableWidget button, ClickableWidget resetButton, Text text) { - this.addEntry(ButtonEntry.create(button, text, resetButton)); + public void addButton(ClickableWidget button, ClickableWidget resetButton, ClickableWidget indexButton, Text text) { + this.addEntry(ButtonEntry.create(button, text, resetButton, indexButton)); } @Override public int getRowWidth() { return 10000; } public Optional getHoveredButton(double mouseX, double mouseY) { for (ButtonEntry buttonEntry : this.children()) { - for (ClickableWidget ClickableWidget : buttonEntry.buttons) { - if (ClickableWidget.isMouseOver(mouseX, mouseY)) { - return Optional.of(ClickableWidget); - } + if (buttonEntry.button != null && buttonEntry.button.isMouseOver(mouseX, mouseY)) { + return Optional.of(buttonEntry.button); } } return Optional.empty(); @@ -325,41 +345,44 @@ public Optional getHoveredButton(double mouseX, double mouseY) } public static class ButtonEntry extends ElementListWidget.Entry { private static final TextRenderer textRenderer = MinecraftClient.getInstance().textRenderer; - private final List buttons = new ArrayList<>(); - private final List resetButtons = new ArrayList<>(); - private final List texts = new ArrayList<>(); - private final List buttonsWithResetButtons = new ArrayList<>(); + public final ClickableWidget button; + private final ClickableWidget resetButton; + private final ClickableWidget indexButton; + private final Text text; + private final List children = new ArrayList<>(); public static final Map buttonsWithText = new HashMap<>(); - private ButtonEntry(ClickableWidget button, Text text, ClickableWidget resetButton) { + private ButtonEntry(ClickableWidget button, Text text, ClickableWidget resetButton, ClickableWidget indexButton) { buttonsWithText.put(button,text); - this.buttons.add(button); - this.resetButtons.add(resetButton); - this.texts.add(text); - this.buttonsWithResetButtons.add(button); - this.buttonsWithResetButtons.add(resetButton); + this.button = button; + this.resetButton = resetButton; + this.text = text; + this.indexButton = indexButton; + if (button != null) children.add(button); + if (resetButton != null) children.add(resetButton); + if (indexButton != null) children.add(indexButton); } - public static ButtonEntry create(ClickableWidget button, Text text, ClickableWidget resetButton) { - return new ButtonEntry(button, text, resetButton); + public static ButtonEntry create(ClickableWidget button, Text text, ClickableWidget resetButton, ClickableWidget indexButton) { + return new ButtonEntry(button, text, resetButton, indexButton); } public void render(MatrixStack matrices, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) { - this.buttons.forEach(button -> { - button.y = y; - button.render(matrices, mouseX, mouseY, tickDelta); - }); - this.texts.forEach(text -> DrawableHelper.drawTextWithShadow(matrices,textRenderer, text,12,y+5,0xFFFFFF)); - this.resetButtons.forEach((button) -> { + if (button != null) { button.y = y; button.render(matrices, mouseX, mouseY, tickDelta); - }); - } - public List children() { - return buttonsWithResetButtons; - } - - public List selectableChildren() { - return buttonsWithResetButtons; + } + if (resetButton != null) { + resetButton.y = y; + resetButton.render(matrices, mouseX, mouseY, tickDelta); + } + if (indexButton != null) { + indexButton.y = y; + indexButton.render(matrices, mouseX, mouseY, tickDelta); + } + if (text != null && (!text.getString().contains("spacer") || button != null)) + DrawableHelper.drawTextWithShadow(matrices,textRenderer, text,12,y+5,0xFFFFFF); } + public List children() {return children;} + public List selectableChildren() {return children;} } @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) diff --git a/src/main/resources/midnightcore.mixins.json b/src/main/resources/midnightcore.mixins.json index 15cf298..5060852 100755 --- a/src/main/resources/midnightcore.mixins.json +++ b/src/main/resources/midnightcore.mixins.json @@ -4,7 +4,6 @@ "compatibilityLevel": "JAVA_16", "client": [ "MixinOptionsScreen", - "MixinEntryListWidget", "MixinPlayerEntityRenderer" ], "injectors": {