Files
src
build.gradle
gradle.properties
manifest.kod
settings.gradle
Code
13 472
src/client/java/com/scopedprefixes/client/PrefixScreen.java
package com.scopedprefixes.client;
import io.wispforest.owo.ui.base.BaseOwoScreen;
import io.wispforest.owo.ui.component.LabelComponent;
import io.wispforest.owo.ui.component.TextBoxComponent;
import io.wispforest.owo.ui.component.UIComponents;
import io.wispforest.owo.ui.container.FlowLayout;
import io.wispforest.owo.ui.container.UIContainers;
import io.wispforest.owo.ui.core.HorizontalAlignment;
import io.wispforest.owo.ui.core.Insets;
import io.wispforest.owo.ui.core.OwoUIAdapter;
import io.wispforest.owo.ui.core.Sizing;
import io.wispforest.owo.ui.core.Surface;
import io.wispforest.owo.ui.core.VerticalAlignment;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import java.util.ArrayList;
import java.util.List;
public class PrefixScreen extends BaseOwoScreen<FlowLayout> {
private final Screen parent;
private final PrefixConfig config = PrefixConfig.INSTANCE;
private final List<Runnable> previews = new ArrayList<>();
private FlowLayout list;
private TextBoxComponent username;
private TextBoxComponent prefix;
private TextBoxComponent server;
private LabelComponent preview;
private LabelComponent status;
private PrefixConfig.Entry editing;
public PrefixScreen(Screen parent) {
super(Component.literal("ScopedPrefixes"));
this.parent = parent;
}
@Override
protected OwoUIAdapter<FlowLayout> createAdapter() {
return OwoUIAdapter.create(this, UIContainers::verticalFlow);
}
@Override
protected void build(FlowLayout root) {
root.surface(Surface.VANILLA_TRANSLUCENT);
root.alignment(HorizontalAlignment.CENTER, VerticalAlignment.CENTER);
FlowLayout panel = UIContainers.verticalFlow(Sizing.fixed(330), Sizing.fill(94));
panel.padding(Insets.of(8));
panel.gap(4);
panel.surface(Surface.DARK_PANEL);
panel.child(UIComponents.label(Component.literal("ScopedPrefixes")));
panel.child(UIComponents.checkbox(Component.literal("Mod enabled")).checked(config.enabled).onChanged(value -> {
config.enabled = value;
config.save();
}));
username = UIComponents.textBox(Sizing.fill(100));
username.setHint(Component.literal("Target username (required)"));
username.setMaxLength(16);
prefix = UIComponents.textBox(Sizing.fill(100));
prefix.setHint(Component.literal("Nickname / prefix (required)"));
prefix.setMaxLength(128);
server = UIComponents.textBox(Sizing.fill(100));
server.setHint(Component.literal("Server address (optional; blank = global)"));
server.setMaxLength(255);
panel.child(username);
panel.child(prefix);
panel.child(server);
preview = UIComponents.label(Component.literal("Preview"));
panel.child(preview);
status = UIComponents.label(Component.literal("Create an entry"));
panel.child(status);
FlowLayout actions = UIContainers.horizontalFlow(Sizing.fill(100), Sizing.content());
actions.gap(4);
actions.child(UIComponents.button(Component.literal("Save entry"), button -> saveEntry()));
actions.child(UIComponents.button(Component.literal("Clear form"), button -> clearForm()));
panel.child(actions);
panel.child(UIComponents.label(Component.literal("Saved entries")));
list = UIContainers.verticalFlow(Sizing.fill(100), Sizing.content());
list.gap(5);
list.margins(Insets.right(5));
panel.child(UIContainers.verticalScroll(Sizing.fill(100), Sizing.expand(), list));
panel.child(UIComponents.button(Component.literal("Save & Close"), button -> onClose()));
root.child(panel);
refreshList();
refreshPreview();
}
private void saveEntry() {
String name = username.getValue().trim();
String text = prefix.getValue().trim();
if (name.isEmpty() || text.isEmpty() || !name.matches("[A-Za-z0-9_]{1,16}")) {
status.text(Component.literal("Enter a valid username and a non-empty prefix."));
return;
}
if (editing == null) {
editing = new PrefixConfig.Entry();
config.entries.add(editing);
}
editing.username = name;
editing.prefix = text;
editing.server = server.getValue().trim();
config.save();
clearForm();
refreshList();
}
private void clearForm() {
editing = null;
username.text("");
prefix.text("");
server.text("");
status.text(Component.literal("Create an entry"));
}
private void refreshList() {
list.clearChildren();
previews.clear();
for (PrefixConfig.Entry entry : config.entries) {
FlowLayout row = UIContainers.verticalFlow(Sizing.fill(100), Sizing.content());
row.gap(2);
LabelComponent label = UIComponents.label(Component.literal(""));
row.child(label);
previews.add(() -> label.text(Prefixes.format(entry.prefix).append(" ").append(Component.literal(entry.username))));
row.child(UIComponents.label(Component.literal(entry.server == null || entry.server.isBlank() ? "Global" : entry.server)));
FlowLayout buttons = UIContainers.horizontalFlow(Sizing.fill(100), Sizing.content());
buttons.gap(4);
buttons.child(UIComponents.button(Component.literal(entry.enabled ? "Enabled" : "Disabled"), button -> {
entry.enabled = !entry.enabled;
button.setMessage(Component.literal(entry.enabled ? "Enabled" : "Disabled"));
config.save();
}));
buttons.child(UIComponents.button(Component.literal("Edit"), button -> {
editing = entry;
username.text(entry.username);
prefix.text(entry.prefix);
server.text(entry.server == null ? "" : entry.server);
status.text(Component.literal("Editing entry"));
}));
buttons.child(UIComponents.button(Component.literal("Delete"), button -> {
config.entries.remove(entry);
if (editing == entry) clearForm();
config.save();
refreshList();
}));
row.child(buttons);
list.child(row);
}
for (Runnable update : previews) update.run();
}
private void refreshPreview() {
preview.text(Prefixes.format(prefix.getValue()).append(" ").append(Component.literal(username.getValue().isBlank() ? "Username" : username.getValue())));
}
@Override
public void tick() {
super.tick();
refreshPreview();
for (Runnable update : previews) update.run();
}
@Override
public void onClose() {
config.save();
this.minecraft.gui.setScreen(parent);
}
}