MidnightLib 0.3.0 - String-List ingame support

- String Lists can now be edited in MidnightLib config screens
- Remove some pointless clutter
This commit is contained in:
Motschen
2021-10-31 15:46:34 +01:00
parent 78dcb1e988
commit 5dc707a6d8
7 changed files with 88 additions and 100 deletions

View File

@@ -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

View File

@@ -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;

View File

@@ -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));
}
}

View File

@@ -40,10 +40,7 @@ public class MidnightConfigOverviewScreen extends Screen {
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 class MidnightConfigOverviewScreen extends Screen {
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 class MidnightConfigOverviewScreen extends Screen {
public int getRowWidth() { return 400; }
}
public static class OverviewButtonEntry extends ElementListWidget.Entry<OverviewButtonEntry> {
private final List<ClickableWidget> buttons = new ArrayList<>();
private final ClickableWidget button;
private final List<ClickableWidget> 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<? extends Element> children() {
return buttons;
}
public List<? extends Selectable> selectableChildren() {
return buttons;
button.y = y;
button.render(matrices, mouseX, mouseY, tickDelta);
}
public List<? extends Element> children() {return buttonList;}
public List<? extends Selectable> selectableChildren() {return buttonList;}
}
}

View File

@@ -17,6 +17,7 @@ import java.net.URL;
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";

View File

@@ -24,6 +24,7 @@ import net.minecraft.text.LiteralText;
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.Function;
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 @@ public abstract class MidnightConfig {
boolean inLimits = true;
String id;
TranslatableText name;
int index;
}
public static final Map<String,Class<?>> configClass = new HashMap<>();
@@ -108,7 +110,7 @@ public abstract class MidnightConfig {
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 @@ public abstract class MidnightConfig {
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<String>) info.value).size() == info.index) ((List<String>) info.value).add("");
((List<String>) info.value).set(info.index, Arrays.stream(info.tempValue.replace("[", "").replace("]", "").split(", ")).toList().get(0));
}
return true;
};
@@ -176,7 +182,6 @@ public abstract class MidnightConfig {
}
@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 @@ public abstract class MidnightConfig {
@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 @@ public abstract class MidnightConfig {
try {
info.value = info.field.get(null);
info.tempValue = info.value.toString();
} catch (IllegalAccessException ignored) {
}
} catch (IllegalAccessException ignored) {}
}
}
@Override
@@ -237,9 +241,10 @@ public abstract class MidnightConfig {
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 @@ public abstract class MidnightConfig {
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 -> 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<String>)info.value).size()) widget.setText((String.valueOf(((List<String>)info.value).get(info.index))));
else widget.setText("");
Predicate<String> processor = ((BiFunction<TextFieldWidget, ButtonWidget, Predicate<String>>) 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<String>)info.value).remove("");
double scrollAmount = list.getScrollAmount();
this.reload = true;
info.index = info.index + 1;
if (info.index > ((List<String>)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<String> processor = ((BiFunction<TextFieldWidget, ButtonWidget, Predicate<String>>) 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 @@ public abstract class MidnightConfig {
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 abstract class MidnightConfig {
@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<ClickableWidget> 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 abstract class MidnightConfig {
}
public static class ButtonEntry extends ElementListWidget.Entry<ButtonEntry> {
private static final TextRenderer textRenderer = MinecraftClient.getInstance().textRenderer;
private final List<ClickableWidget> buttons = new ArrayList<>();
private final List<ClickableWidget> resetButtons = new ArrayList<>();
private final List<Text> texts = new ArrayList<>();
private final List<ClickableWidget> buttonsWithResetButtons = new ArrayList<>();
public final ClickableWidget button;
private final ClickableWidget resetButton;
private final ClickableWidget indexButton;
private final Text text;
private final List<ClickableWidget> children = new ArrayList<>();
public static final Map<ClickableWidget, Text> 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 -> {
if (button != null) {
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) -> {
button.y = y;
button.render(matrices, mouseX, mouseY, tickDelta);
});
}
public List<? extends Element> children() {
return buttonsWithResetButtons;
}
public List<? extends Selectable> 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<? extends Element> children() {return children;}
public List<? extends Selectable> selectableChildren() {return children;}
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)

View File

@@ -4,7 +4,6 @@
"compatibilityLevel": "JAVA_16",
"client": [
"MixinOptionsScreen",
"MixinEntryListWidget",
"MixinPlayerEntityRenderer"
],
"injectors": {