Skip to content

Commit

Permalink
MidnightLib 0.3.0 - String-List ingame support
Browse files Browse the repository at this point in the history
- String Lists can now be edited in MidnightLib config screens
- Remove some pointless clutter
  • Loading branch information
Motschen committed Oct 31, 2021
1 parent 78dcb1e commit 5dc707a
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 100 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
23 changes: 0 additions & 23 deletions src/main/java/eu/midnightdust/core/mixin/MixinEntryListWidget.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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));
Expand All @@ -65,26 +62,19 @@ public void addButton(ClickableWidget button) {
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;}
}
}
1 change: 1 addition & 0 deletions src/main/java/eu/midnightdust/hats/web/HatLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
127 changes: 75 additions & 52 deletions src/main/java/eu/midnightdust/lib/config/MidnightConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -64,6 +65,7 @@ protected static class EntryInfo {
boolean inLimits = true;
String id;
TranslatableText name;
int index;
}

public static final Map<String,Class<?>> configClass = new HashMap<>();
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -154,8 +156,12 @@ private static void textField(EntryInfo info, Function<String,Number> 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<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;
};
Expand All @@ -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;
Expand All @@ -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)); }
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -249,17 +254,37 @@ protected void init() {
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);
}
}
}
Expand All @@ -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)) {
Expand Down Expand Up @@ -307,59 +329,60 @@ 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<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();
}
}
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 -> {
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<? 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)
Expand Down
1 change: 0 additions & 1 deletion src/main/resources/midnightcore.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"compatibilityLevel": "JAVA_16",
"client": [
"MixinOptionsScreen",
"MixinEntryListWidget",
"MixinPlayerEntityRenderer"
],
"injectors": {
Expand Down

0 comments on commit 5dc707a

Please sign in to comment.