[DEV] Fix up porting patches and configs

This commit is contained in:
2024-06-11 19:51:28 +02:00
parent 68fea1db1e
commit 1ffa879f17
1023 changed files with 38304 additions and 596 deletions

View File

@@ -0,0 +1,393 @@
package com.hypherionmc.craterlib.client.gui.config;
import com.hypherionmc.craterlib.CraterConstants;
import com.hypherionmc.craterlib.client.gui.config.widgets.*;
import com.hypherionmc.craterlib.core.config.ModuleConfig;
import com.hypherionmc.craterlib.core.config.annotations.HideFromScreen;
import com.hypherionmc.craterlib.core.config.annotations.SubConfig;
import com.hypherionmc.craterlib.core.config.annotations.Tooltip;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.*;
import me.hypherionmc.moonconfig.core.conversion.SpecComment;
import net.minecraft.ChatFormatting;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.ConfirmScreen;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Mth;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.joml.Matrix4f;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* @author HypherionSA
*/
public class CraterConfigScreen extends Screen {
public static final float SCROLLBAR_BOTTOM_COLOR = .5f;
public static final float SCROLLBAR_TOP_COLOR = .67f;
private static final int TOP = 26;
private static final int BOTTOM = 24;
private final Screen parent;
private final List<Option<?>> options = new ArrayList<>();
private final ModuleConfig config;
public double scrollerAmount;
private boolean dragging;
public CraterConfigScreen(ModuleConfig config, Screen parent, Object subConfig) {
super(Component.translatable("cl." + config.getClass().getSimpleName().toLowerCase() + ".title"));
this.parent = parent;
this.config = config;
if (subConfig != null) {
setupScreenFromConfig(subConfig, subConfig.getClass());
} else {
setupScreenFromConfig(config, config.getClass());
}
}
public CraterConfigScreen(ModuleConfig config, Screen parent) {
this(config, parent, null);
}
private static Component toText(Enum<?> val) {
return Component.translatable(val.toString());
}
private static Component toText(Boolean bool) {
return Component.translatable(bool.toString());
}
private void setupScreenFromConfig(Object object, Class<?> clazz) {
while (clazz != Object.class) {
for (Field field : clazz.getDeclaredFields()) {
final int fieldModifiers = field.getModifiers();
if (object == null || Modifier.isStatic(fieldModifiers) || Modifier.isTransient(fieldModifiers)) {
continue;
}
try {
if (!field.isAccessible()) {
field.setAccessible(true);
}
if (field.isAnnotationPresent(HideFromScreen.class))
return;
Object val = field.get(object);
/* Lang Stuff */
String baseLangKey = "cl." + clazz.getSimpleName().toLowerCase() + "." + field.getName().toLowerCase();
String[] tooltipLang = field.isAnnotationPresent(SpecComment.class) ? new String[]{field.getAnnotation(SpecComment.class).value()} : new String[0];
if (field.isAnnotationPresent(Tooltip.class)) {
tooltipLang = field.getAnnotation(Tooltip.class).value();
}
add(Component.translatable(baseLangKey),
val,
() -> val,
(ret) -> {
try {
field.set(object, ret);
config.saveConfig(config);
} catch (IllegalAccessException e) {
CraterConstants.LOG.error("Failed to update value for field {} in config {}", field.getName(), config.getConfigName(), e);
}
},
field.isAnnotationPresent(SubConfig.class),
tooltipLang
);
} catch (IllegalAccessException e) {
CraterConstants.LOG.error("Failed to access value for field {} in config {}", field.getName(), config.getConfigName(), e);
}
}
clazz = clazz.getSuperclass();
}
}
public <T> void add(Component text, T value, @Nullable Supplier<T> defaultValue, Consumer<T> savingConsumer, boolean isSubConfig, String... langKeys) {
Option<T> option = (Option<T>) createOption(value, isSubConfig);
option.text = text;
option.defaultValue = defaultValue;
option.savingConsumer = savingConsumer;
option.originalValue = value;
option.value = value;
option.setLangKeys(List.of(langKeys));
options.add(option);
option.onAdd();
}
private <T> Option<?> createOption(T value, boolean isSubConfig) {
if (value instanceof Enum) {
Object[] objects = value.getClass().getEnumConstants();
return new ToggleButton<Enum<?>>((List) Arrays.asList(objects), CraterConfigScreen::toText);
}
if (value instanceof Boolean) {
return new ToggleButton<>(Arrays.asList(Boolean.TRUE, Boolean.FALSE), CraterConfigScreen::toText);
}
if (value instanceof String) {
return new TextConfigOption<>(Function.identity(), Function.identity());
}
if (value instanceof Integer) {
return new TextConfigOption<>(Objects::toString, Integer::valueOf);
}
if (value instanceof Long) {
return new TextConfigOption<>(Objects::toString, Long::valueOf);
}
if (value instanceof Double) {
return new TextConfigOption<>(Objects::toString, Double::valueOf);
}
if (value instanceof Float) {
return new TextConfigOption<>(Objects::toString, Float::valueOf);
}
if (value instanceof BigInteger) {
return new TextConfigOption<>(Objects::toString, BigInteger::new);
}
if (value instanceof BigDecimal) {
return new TextConfigOption<>(Objects::toString, BigDecimal::new);
}
if (value instanceof ResourceLocation) {
return new TextConfigOption<>(Objects::toString, ResourceLocation::new);
}
if (isSubConfig) {
return new SubConfigWidget<>(config, this, value);
}
throw new IllegalArgumentException(String.valueOf(value));
}
@Override
protected void init() {
super.init();
((List) children()).addAll(options);
int buttonWidths = Math.min(200, (width - 50 - 12) / 3);
addRenderableWidget(new InternalConfigButton(this, width / 2 - buttonWidths - 3, height - 22, buttonWidths, 20, Component.empty(), true));
addRenderableWidget(new InternalConfigButton(this, width / 2 + 3, height - 22, buttonWidths, 20, Component.empty(), false));
}
@Override
public void render(@NotNull GuiGraphics matrices, int mouseX, int mouseY, float delta) {
overlayBackground(matrices.pose(), TOP, height - BOTTOM, 32);
renderScrollBar();
matrices.pose().pushPose();
matrices.pose().translate(0, 0, 500.0);
overlayBackground(matrices.pose(), 0, TOP, 64);
overlayBackground(matrices.pose(), height - BOTTOM, height, 64);
renderShadow(matrices.pose());
matrices.drawCenteredString(font, getTitle(), width / 2, 9, 0xFFFFFF);
super.render(matrices, mouseX, mouseY, delta);
matrices.pose().popPose();
int y = (int) (TOP + 4 - Math.round(scrollerAmount));
for (Option<?> option : options) {
int height1 = option.height();
option.render(minecraft, font, 40, y, width - 80, height1, matrices, mouseX, mouseY, delta);
renderConfigTooltip(matrices, font, mouseX, mouseY, 40, y, font.width(option.text), height1, option.text.getString(), option.getLangKeys().toArray(new String[0]));
y += height1;
}
}
private void renderScrollBar() {
int listHeight = height - BOTTOM - TOP;
int totalHeight = totalHeight();
if (totalHeight > listHeight) {
int maxScroll = Math.max(0, totalHeight - listHeight);
int height = listHeight * listHeight / totalHeight;
height = Mth.clamp(height, 32, listHeight);
height = Math.max(10, height);
int minY = Math.min(Math.max((int) scrollerAmount * (listHeight - height) / maxScroll + TOP, TOP), this.height - BOTTOM - height);
int scrollbarPositionMaxX = width;
int scrollbarPositionMinX = scrollbarPositionMaxX - 6;
int maxY = this.height - BOTTOM;
//RenderSystem.disableTexture();
Tesselator tesselator = Tesselator.getInstance();
BufferBuilder buffer = tesselator.getBuilder();
RenderSystem.setShader(GameRenderer::getPositionColorShader);
buffer.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_COLOR);
buffer.vertex(scrollbarPositionMinX, maxY, 0.0D).color(0, 0, 0, 255).endVertex();
buffer.vertex(scrollbarPositionMaxX, maxY, 0.0D).color(0, 0, 0, 255).endVertex();
buffer.vertex(scrollbarPositionMaxX, TOP, 0.0D).color(0, 0, 0, 255).endVertex();
buffer.vertex(scrollbarPositionMinX, TOP, 0.0D).color(0, 0, 0, 255).endVertex();
buffer.vertex(scrollbarPositionMinX, minY + height, 0.0D).color(SCROLLBAR_BOTTOM_COLOR, SCROLLBAR_BOTTOM_COLOR, SCROLLBAR_BOTTOM_COLOR, 1).endVertex();
buffer.vertex(scrollbarPositionMaxX, minY + height, 0.0D).color(SCROLLBAR_BOTTOM_COLOR, SCROLLBAR_BOTTOM_COLOR, SCROLLBAR_BOTTOM_COLOR, 1).endVertex();
buffer.vertex(scrollbarPositionMaxX, minY, 0.0D).color(SCROLLBAR_BOTTOM_COLOR, SCROLLBAR_BOTTOM_COLOR, SCROLLBAR_BOTTOM_COLOR, 1).endVertex();
buffer.vertex(scrollbarPositionMinX, minY, 0.0D).color(SCROLLBAR_BOTTOM_COLOR, SCROLLBAR_BOTTOM_COLOR, SCROLLBAR_BOTTOM_COLOR, 1).endVertex();
buffer.vertex(scrollbarPositionMinX, (minY + height - 1), 0.0D).color(SCROLLBAR_TOP_COLOR, SCROLLBAR_TOP_COLOR, SCROLLBAR_TOP_COLOR, 1).endVertex();
buffer.vertex((scrollbarPositionMaxX - 1), (minY + height - 1), 0.0D).color(SCROLLBAR_TOP_COLOR, SCROLLBAR_TOP_COLOR, SCROLLBAR_TOP_COLOR, 1).endVertex();
buffer.vertex((scrollbarPositionMaxX - 1), minY, 0.0D).color(SCROLLBAR_TOP_COLOR, SCROLLBAR_TOP_COLOR, SCROLLBAR_TOP_COLOR, 1).endVertex();
buffer.vertex(scrollbarPositionMinX, minY, 0.0D).color(SCROLLBAR_TOP_COLOR, SCROLLBAR_TOP_COLOR, SCROLLBAR_TOP_COLOR, 1).endVertex();
tesselator.end();
RenderSystem.disableBlend();
//RenderSystem.enableTexture();
}
}
private void renderShadow(PoseStack matrices) {
Tesselator tesselator = Tesselator.getInstance();
BufferBuilder buffer = tesselator.getBuilder();
RenderSystem.enableBlend();
RenderSystem.blendFuncSeparate(770, 771, 0, 1);
//RenderSystem.disableTexture();
RenderSystem.setShader(GameRenderer::getPositionTexColorShader);
Matrix4f matrix = matrices.last().pose();
buffer.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_TEX_COLOR);
buffer.vertex(matrix, 0, TOP + 4, 0.0F).uv(0, 1).color(0, 0, 0, 0).endVertex();
buffer.vertex(matrix, width, TOP + 4, 0.0F).uv(1, 1).color(0, 0, 0, 0).endVertex();
buffer.vertex(matrix, width, TOP, 0.0F).uv(1, 0).color(0, 0, 0, 185).endVertex();
buffer.vertex(matrix, 0, TOP, 0.0F).uv(0, 0).color(0, 0, 0, 185).endVertex();
buffer.vertex(matrix, 0, height - BOTTOM, 0.0F).uv(0, 1).color(0, 0, 0, 185).endVertex();
buffer.vertex(matrix, width, height - BOTTOM, 0.0F).uv(1, 1).color(0, 0, 0, 185).endVertex();
buffer.vertex(matrix, width, height - BOTTOM - 4, 0.0F).uv(1, 0).color(0, 0, 0, 0).endVertex();
buffer.vertex(matrix, 0, height - BOTTOM - 4, 0.0F).uv(0, 0).color(0, 0, 0, 0).endVertex();
tesselator.end();
//RenderSystem.enableTexture();
RenderSystem.disableBlend();
}
protected void overlayBackground(PoseStack matrices, int h1, int h2, int color) {
overlayBackground(matrices.last().pose(), 0, h1, width, h2, color, color, color, 255, 255);
}
protected void overlayBackground(Matrix4f matrix, int minX, int minY, int maxX, int maxY, int red, int green, int blue, int startAlpha, int endAlpha) {
Tesselator tesselator = Tesselator.getInstance();
BufferBuilder buffer = tesselator.getBuilder();
RenderSystem.setShaderTexture(0, Screen.BACKGROUND_LOCATION);
RenderSystem.setShaderColor(1.0f, 1.0f, 1.0f, 1.0f);
RenderSystem.setShader(GameRenderer::getPositionTexColorShader);
buffer.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_TEX_COLOR);
buffer.vertex(matrix, minX, maxY, 0.0F).uv(minX / 32.0F, maxY / 32.0F).color(red, green, blue, endAlpha).endVertex();
buffer.vertex(matrix, maxX, maxY, 0.0F).uv(maxX / 32.0F, maxY / 32.0F).color(red, green, blue, endAlpha).endVertex();
buffer.vertex(matrix, maxX, minY, 0.0F).uv(maxX / 32.0F, minY / 32.0F).color(red, green, blue, startAlpha).endVertex();
buffer.vertex(matrix, minX, minY, 0.0F).uv(minX / 32.0F, minY / 32.0F).color(red, green, blue, startAlpha).endVertex();
tesselator.end();
}
public int scrollHeight() {
int totalHeight = totalHeight();
int listHeight = height - BOTTOM - TOP;
if (totalHeight <= listHeight) {
return 0;
}
return totalHeight - listHeight;
}
public int totalHeight() {
int i = 8;
for (Option<?> option : options) {
i += option.height();
}
return i;
}
public boolean hasErrors() {
for (Option<?> option : options) {
if (option.hasErrors) {
return true;
}
}
return false;
}
public boolean isEdited() {
for (Option<?> option : options) {
if (option.isEdited()) {
return true;
}
}
return false;
}
public void save() {
for (Option option : options) {
option.save();
option.originalValue = option.value;
}
}
@Override
public void onClose() {
if (isEdited()) {
minecraft.setScreen(new ConfirmScreen(this::acceptConfirm, Component.translatable("t.clc.quit_config"),
Component.translatable("t.clc.quit_config_sure"),
Component.translatable("t.clc.quit_discard"),
Component.translatable("gui.cancel")));
} else {
minecraft.setScreen(parent);
}
}
@Override
public boolean mouseScrolled(double d, double e, double f) {
if (e >= TOP && e <= height - BOTTOM) {
scrollerAmount = Mth.clamp(scrollerAmount - f * 16.0D, 0, scrollHeight());
return true;
}
return super.mouseScrolled(d, e, f);
}
@Override
public boolean mouseClicked(double d, double e, int i) {
this.dragging = i == 0 && d >= width - 6 && d < width;
return super.mouseClicked(d, e, i) || dragging;
}
@Override
public boolean mouseDragged(double d, double e, int i, double f, double g) {
if (super.mouseDragged(d, e, i, f, g)) {
return true;
}
if (i != 0 || !this.dragging) {
return false;
}
if (e < TOP) {
scrollerAmount = 0;
} else if (e > height - BOTTOM) {
scrollerAmount = scrollHeight();
} else {
double h = Math.max(1, this.scrollHeight());
int j = height - BOTTOM - TOP;
int k = Mth.clamp((int) ((float) (j * j) / (float) this.scrollHeight()), 32, j - 8);
double l = Math.max(1.0, h / (double) (j - k));
scrollerAmount = Mth.clamp(scrollerAmount + g * l, 0, scrollHeight());
}
return true;
}
private void acceptConfirm(boolean t) {
if (!t) {
minecraft.setScreen(this);
} else {
minecraft.setScreen(parent);
}
}
private void renderConfigTooltip(GuiGraphics stack, Font font, int mouseX, int mouseY, int startX, int startY, int sizeX, int sizeY, String title, String... description) {
if (mouseX > startX && mouseX < startX + sizeX) {
if (mouseY > startY && mouseY < startY + sizeY) {
List<Component> list = new ArrayList<>();
list.add(Component.translatable(ChatFormatting.BOLD + "" + ChatFormatting.YELLOW + title));
for (String desc : description) {
list.add(Component.translatable(desc));
}
stack.renderComponentTooltip(font, list, mouseX, mouseY);
}
}
}
}

View File

@@ -0,0 +1,27 @@
package com.hypherionmc.craterlib.client.gui.config.widgets;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.components.EditBox;
/**
* Copied from Cloth Config Lite
* <a href="https://github.com/shedaniel/cloth-config-lite/blob/1.17/src/main/java/me/shedaniel/clothconfiglite/impl/option/AbstractWidgetOption.java">...</a>
*/
public class AbstractConfigWidget<T, W extends AbstractWidget> extends BaseWidget<T> {
public static final int buttonWidth = 200;
public static final int buttonHeight = 20;
public W widget;
@Override
public void render(Minecraft minecraft, Font font, int x, int y, int width, int height, GuiGraphics matrices, int mouseX, int mouseY, float delta) {
super.render(minecraft, font, x, y, width, height, matrices, mouseX, mouseY, delta);
int i = (widget instanceof EditBox ? 1 : 0);
widget.setX(x + width - 200 - resetButtonOffset + i);
widget.setY(y + i + 1);
widget.render(matrices, mouseX, mouseY, delta);
}
}

View File

@@ -0,0 +1,61 @@
package com.hypherionmc.craterlib.client.gui.config.widgets;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Button;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.chat.TextColor;
/**
* Copied from Cloth Config Lite
* <a href="https://github.com/shedaniel/cloth-config-lite/blob/1.17/src/main/java/me/shedaniel/clothconfiglite/impl/option/BaseOption.java">...</a>
*/
public class BaseWidget<T> extends Option<T> {
public static final int resetButtonOffset = 48;
private final Button resetButton = addChild(Button.builder(Component.literal("Reset"), this::onResetPressed).size(46, 20).build());
private boolean hideReset = false;
private boolean isSubConfig = false;
private void onResetPressed(Button button) {
value = defaultValue.get();
reset();
}
public void hideReset() {
this.hideReset = true;
}
public boolean isSubConfig() {
return isSubConfig;
}
public void setSubConfig(boolean subConfig) {
isSubConfig = subConfig;
}
@Override
public void render(Minecraft minecraft, Font font, int x, int y, int width, int height, GuiGraphics matrices, int mouseX, int mouseY, float delta) {
MutableComponent text = Component.literal(this.text.getString());
boolean edited = isEdited() || hasErrors;
if (edited) {
text.withStyle(ChatFormatting.ITALIC);
if (hasErrors) {
text.withStyle(style -> style.withColor(TextColor.fromRgb(16733525)));
}
} else {
text.withStyle(ChatFormatting.GRAY);
}
matrices.drawString(font, text, x, y + font.lineHeight - 2, 0xFFFFFF);
resetButton.setX(x + width - 46);
resetButton.setY(y + 1);
resetButton.active = isNotDefault();
if (!hideReset) {
resetButton.render(matrices, mouseX, mouseY, delta);
}
}
}

View File

@@ -0,0 +1,52 @@
package com.hypherionmc.craterlib.client.gui.config.widgets;
import com.hypherionmc.craterlib.client.gui.config.CraterConfigScreen;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.AbstractButton;
import net.minecraft.client.gui.narration.NarratedElementType;
import net.minecraft.client.gui.narration.NarrationElementOutput;
import net.minecraft.network.chat.Component;
import org.jetbrains.annotations.NotNull;
/**
* @author HypherionSA
*/
public class InternalConfigButton extends AbstractButton {
CraterConfigScreen screen;
boolean cancel;
public InternalConfigButton(CraterConfigScreen screen, int i, int j, int k, int l, Component component, boolean cancel) {
super(i, j, k, l, component);
this.screen = screen;
this.cancel = cancel;
}
@Override
public void render(@NotNull GuiGraphics poseStack, int i, int j, float f) {
if (cancel) {
setMessage(Component.translatable(screen.isEdited() ? "t.clc.cancel_discard" : "gui.cancel"));
} else {
boolean hasErrors = screen.hasErrors();
active = screen.isEdited() && !hasErrors;
setMessage(Component.translatable(hasErrors ? "t.clc.error" : "t.clc.save"));
}
super.render(poseStack, i, j, f);
}
@Override
protected void updateWidgetNarration(NarrationElementOutput narrationElementOutput) {
narrationElementOutput.add(NarratedElementType.USAGE, getMessage());
}
@Override
public void onPress() {
if (cancel) {
screen.onClose();
} else {
screen.save();
}
}
}

View File

@@ -0,0 +1,71 @@
package com.hypherionmc.craterlib.client.gui.config.widgets;
import lombok.Getter;
import lombok.Setter;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.events.AbstractContainerEventHandler;
import net.minecraft.client.gui.components.events.GuiEventListener;
import net.minecraft.network.chat.Component;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Supplier;
/**
* Copied from Cloth Config Lite
* <a href="https://github.com/shedaniel/cloth-config-lite/blob/1.17/src/main/java/me/shedaniel/clothconfiglite/impl/option/Option.java">...</a>
*/
public abstract class Option<T> extends AbstractContainerEventHandler {
public Component text;
@Nullable
public Supplier<T> defaultValue;
public Consumer<T> savingConsumer;
public T originalValue;
public T value;
public boolean hasErrors;
public List<? extends GuiEventListener> children = new ArrayList<>();
@Setter
@Getter
private List<String> langKeys = new ArrayList<>();
public abstract void render(Minecraft minecraft, Font font, int x, int y, int width, int height, GuiGraphics matrices, int mouseX, int mouseY, float delta);
public int height() {
return 22;
}
@Override
public List<? extends GuiEventListener> children() {
return children;
}
protected <R extends GuiEventListener> R addChild(R listener) {
((List) children).add(listener);
return listener;
}
public void onAdd() {
}
protected void reset() {
onAdd();
}
public boolean isEdited() {
return !Objects.equals(originalValue, value);
}
protected boolean isNotDefault() {
return defaultValue != null && !Objects.equals(defaultValue.get(), value);
}
public void save() {
savingConsumer.accept(value);
}
}

View File

@@ -0,0 +1,40 @@
package com.hypherionmc.craterlib.client.gui.config.widgets;
import com.hypherionmc.craterlib.client.gui.config.CraterConfigScreen;
import com.hypherionmc.craterlib.core.config.ModuleConfig;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
/**
* @author HypherionSA
*/
public class SubConfigWidget<T> extends AbstractConfigWidget<T, Button> {
private final Object subConfig;
private final ModuleConfig config;
private final Screen screen;
public SubConfigWidget(ModuleConfig config, Screen screen, Object subConfig) {
this.config = config;
this.subConfig = subConfig;
this.screen = screen;
this.widget = addChild(Button.builder(Component.translatable("t.clc.opensubconfig"), this::openSubConfig).size(200, buttonHeight).build());
}
@Override
public void render(Minecraft minecraft, Font font, int x, int y, int width, int height, GuiGraphics matrices, int mouseX, int mouseY, float delta) {
this.text = Component.literal(subConfig.getClass().getSimpleName().toLowerCase());
this.hideReset();
super.render(minecraft, font, x, y, width, height, matrices, mouseX, mouseY, delta);
}
private void openSubConfig(Button button) {
Minecraft.getInstance().setScreen(new CraterConfigScreen(config, screen, subConfig));
}
}

View File

@@ -0,0 +1,45 @@
package com.hypherionmc.craterlib.client.gui.config.widgets;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import java.util.function.Function;
/**
* Copied from Cloth Config Lite
* <a href="https://github.com/shedaniel/cloth-config-lite/blob/1.17/src/main/java/me/shedaniel/clothconfiglite/impl/option/TextFieldOption.java">...</a>
*/
public class TextConfigOption<T> extends AbstractConfigWidget<T, WrappedEditBox> {
private final Function<T, String> toString;
private final Function<String, T> fromString;
public TextConfigOption(Function<T, String> toString, Function<String, T> fromString) {
this.toString = toString;
this.fromString = fromString;
this.widget = addChild(new WrappedEditBox(Minecraft.getInstance().font, 0, 0, 198, 18, null));
}
@Override
public void onAdd() {
widget.setMaxLength(1000000);
widget.setValue(toString.apply(value));
widget.setResponder(this::update);
}
@Override
public void render(Minecraft minecraft, Font font, int x, int y, int width, int height, GuiGraphics matrices, int mouseX, int mouseY, float delta) {
widget.setTextColor(hasErrors ? 16733525 : 14737632);
super.render(minecraft, font, x, y, width, height, matrices, mouseX, mouseY, delta);
}
private void update(String s) {
try {
this.value = fromString.apply(s);
this.hasErrors = false;
} catch (Exception e) {
this.hasErrors = true;
}
}
}

View File

@@ -0,0 +1,33 @@
package com.hypherionmc.craterlib.client.gui.config.widgets;
import net.minecraft.client.gui.components.Button;
import net.minecraft.network.chat.Component;
import java.util.List;
import java.util.function.Function;
/**
* Copied from Cloth Config Lite
* <a href="https://github.com/shedaniel/cloth-config-lite/blob/1.17/src/main/java/me/shedaniel/clothconfiglite/impl/option/ToggleOption.java">...</a>
*/
public class ToggleButton<T> extends AbstractConfigWidget<T, Button> {
private final List<T> options;
private final Function<T, Component> toComponent;
public ToggleButton(List<T> options, Function<T, Component> toComponent) {
this.options = options;
this.toComponent = toComponent;
this.widget = addChild(Button.builder(Component.empty(), this::switchNext).size(buttonWidth, buttonHeight).build());
}
@Override
public void onAdd() {
widget.setMessage(toComponent.apply(value));
}
private void switchNext(Button button) {
value = options.get((options.indexOf(value) + 1) % options.size());
onAdd();
}
}

View File

@@ -0,0 +1,29 @@
package com.hypherionmc.craterlib.client.gui.config.widgets;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.components.EditBox;
import net.minecraft.client.gui.components.events.GuiEventListener;
import net.minecraft.network.chat.Component;
import org.jetbrains.annotations.NotNull;
/**
* @author HypherionSA
*/
public class WrappedEditBox extends EditBox {
public WrappedEditBox(Font font, int i, int j, int k, int l, @NotNull Component component) {
super(font, i, j, k, l, component);
}
@Override
public void setFocused(boolean bl) {
for (GuiEventListener child : Minecraft.getInstance().screen.children()) {
if (child instanceof TextConfigOption<?> option) {
WrappedEditBox box = option.widget;
super.setFocused(box == this);
}
}
super.setFocused(bl);
}
}

View File

@@ -0,0 +1,13 @@
package com.hypherionmc.craterlib.client.mentions;
/**
* Based on <a href="https://github.com/SarahIsWeird/MoreChatSuggestions/blob/main/src/main/java/com/sarahisweird/morechatsuggestions/SuggestionCondition.java">...</a>
*/
@FunctionalInterface
public interface MentionCondition {
boolean shouldAddMention(String currentWord);
MentionCondition ALWAYS = currentWord -> true;
}

View File

@@ -0,0 +1,47 @@
package com.hypherionmc.craterlib.client.mentions;
import com.hypherionmc.craterlib.nojang.resources.ResourceIdentifier;
import lombok.Getter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* Based on <a href="https://github.com/SarahIsWeird/MoreChatSuggestions/blob/main/src/main/java/com/sarahisweird/morechatsuggestions/client/MoreChatSuggestions.java">...</a>
*/
public class MentionsController {
private static final Map<ResourceIdentifier, Collection<String>> mentions = new LinkedHashMap<>();
private static final Map<ResourceIdentifier, MentionCondition> mentionConditions = new LinkedHashMap<>();
@Getter
private static boolean lastMentionConditional = true;
public static void registerMention(ResourceIdentifier mentionClass, Collection<String> suggestions, MentionCondition condition) {
mentions.put(mentionClass, suggestions);
mentionConditions.put(mentionClass, condition);
}
public static Collection<String> getMentions(String currentWord) {
ArrayList<String> applicableMentions = new ArrayList<>();
lastMentionConditional = false;
mentionConditions.forEach((mention, condition) -> {
boolean shouldSuggest = condition.shouldAddMention(currentWord);
if (!shouldSuggest) return;
if (!lastMentionConditional && condition != MentionCondition.ALWAYS) {
lastMentionConditional = true;
}
applicableMentions.addAll(mentions.get(mention));
});
return applicableMentions;
}
public static boolean hasMentions() {
return !mentions.isEmpty();
}
}