Skip to content

Commit f288f2d

Browse files
Add dialog styling options (#1072)
1 parent 61a4cea commit f288f2d

File tree

8 files changed

+115
-30
lines changed

8 files changed

+115
-30
lines changed

common/src/main/java/com/viaversion/viabackwards/ViaBackwardsConfig.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
*/
1818
package com.viaversion.viabackwards;
1919

20+
import com.viaversion.viabackwards.api.DialogStyleConfig;
21+
import com.viaversion.viaversion.util.ChatColorUtil;
2022
import com.viaversion.viaversion.util.Config;
23+
import com.viaversion.viaversion.util.ConfigSection;
2124
import java.io.File;
2225
import java.io.InputStream;
2326
import java.net.URL;
@@ -41,6 +44,7 @@ public class ViaBackwardsConfig extends Config implements com.viaversion.viaback
4144
private boolean mapCustomModelData;
4245
private boolean mapDisplayEntities;
4346
private boolean suppressEmulationWarnings;
47+
private DialogStyleConfig dialogStyleConfig;
4448

4549
public ViaBackwardsConfig(File configFile, Logger logger) {
4650
super(configFile, logger);
@@ -66,6 +70,29 @@ private void loadFields() {
6670
mapCustomModelData = getBoolean("map-custom-model-data", true);
6771
mapDisplayEntities = getBoolean("map-display-entities", true);
6872
suppressEmulationWarnings = getBoolean("suppress-emulation-warnings", false);
73+
dialogStyleConfig = loadDialogStyleConfig(getSection("dialog-style"));
74+
}
75+
76+
private DialogStyleConfig loadDialogStyleConfig(final ConfigSection section) {
77+
return new DialogStyleConfig(
78+
getString(section, "page-navigation-title", "&9&lPage navigation"),
79+
getString(section, "page-navigation-next", "&9Left click: &6Go to next page"),
80+
getString(section, "page-navigation-previous", "&9Right click: &6Go to previous page"),
81+
getString(section, "increase-value", "&9Left click: &6Increase value by %s"),
82+
getString(section, "decrease-value", "&9Right click: &6Decrease value by %s"),
83+
getString(section, "value-range", "&7(Value between &a%s &7and &a%s&7)"),
84+
getString(section, "next-option", "&9Left click: &6Go to next option"),
85+
getString(section, "previous-option", "&9Right click: &6Go to previous option"),
86+
getString(section, "current-value", "&7Current value: &a%s"),
87+
getString(section, "edit-value", "&9Left click: &6Edit text"),
88+
getString(section, "set-text", "&9Left click/close: &6Set text"),
89+
getString(section, "close", "&9Left click: &6Close"),
90+
getString(section, "toggle-value", "&9Left click: &6Toggle value")
91+
);
92+
}
93+
94+
protected String getString(final ConfigSection section, final String key, final String def) {
95+
return ChatColorUtil.translateAlternateColorCodes(section.getString(key, def));
6996
}
7097

7198
@Override
@@ -133,6 +160,11 @@ public boolean suppressEmulationWarnings() {
133160
return suppressEmulationWarnings;
134161
}
135162

163+
@Override
164+
public DialogStyleConfig dialogStyleConfig() {
165+
return dialogStyleConfig;
166+
}
167+
136168
@Override
137169
public URL getDefaultConfigURL() {
138170
return getClass().getClassLoader().getResource("assets/viabackwards/config.yml");
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* This file is part of ViaBackwards - https://github.com/ViaVersion/ViaBackwards
3+
* Copyright (C) 2016-2025 ViaVersion and contributors
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package com.viaversion.viabackwards.api;
19+
20+
public record DialogStyleConfig(String pageNavigationTitle, String pageNavigationNext, String pageNavigationPrevious,
21+
String increaseValue, String decreaseValue, String valueRange, String nextOption,
22+
String previousOption, String currentValue, String editValue, String setText,
23+
String close, String toggleValue) {
24+
25+
}

common/src/main/java/com/viaversion/viabackwards/api/ViaBackwardsConfig.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,11 @@ public interface ViaBackwardsConfig extends Config {
112112
* @return true if enabled
113113
*/
114114
boolean suppressEmulationWarnings();
115+
116+
/**
117+
* Returns the dialog style configuration.
118+
*
119+
* @return the dialog style configuration
120+
*/
121+
DialogStyleConfig dialogStyleConfig();
115122
}

common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_6to1_21_5/data/input/NumberRangeInput.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import com.viaversion.nbt.tag.FloatTag;
2222
import com.viaversion.nbt.tag.Tag;
2323

24-
import static com.viaversion.viabackwards.utils.ChatUtil.text;
2524
import static com.viaversion.viabackwards.utils.ChatUtil.translate;
2625

2726
public final class NumberRangeInput implements Input {
@@ -109,6 +108,6 @@ public void setClampedValue(final float value) {
109108
}
110109

111110
public Tag displayName() {
112-
return translate(labelFormat, label, text(valueAsString()));
111+
return translate(labelFormat, label, translate(valueAsString()));
113112
}
114113
}

common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_6to1_21_5/provider/ChestDialogViewProvider.java

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
import com.viaversion.nbt.tag.CompoundTag;
2121
import com.viaversion.nbt.tag.Tag;
22+
import com.viaversion.viabackwards.ViaBackwards;
23+
import com.viaversion.viabackwards.api.DialogStyleConfig;
2224
import com.viaversion.viabackwards.protocol.v1_21_6to1_21_5.Protocol1_21_6To1_21_5;
2325
import com.viaversion.viabackwards.protocol.v1_21_6to1_21_5.data.Button;
2426
import com.viaversion.viabackwards.protocol.v1_21_6to1_21_5.data.Dialog;
@@ -52,7 +54,6 @@
5254
import org.checkerframework.checker.nullness.qual.Nullable;
5355

5456
import static com.viaversion.viabackwards.utils.ChatUtil.fixStyle;
55-
import static com.viaversion.viabackwards.utils.ChatUtil.text;
5657
import static com.viaversion.viabackwards.utils.ChatUtil.translate;
5758

5859
/**
@@ -225,12 +226,14 @@ public void updateDialog(final UserConnection connection, final Dialog dialog) {
225226
}
226227

227228
protected Item createPageNavigationItem() {
229+
final DialogStyleConfig config = ViaBackwards.getConfig().dialogStyleConfig();
230+
228231
return createItem(
229232
"minecraft:arrow",
230-
text("§9§lPage navigation"),
233+
translate(config.pageNavigationTitle()),
231234

232-
"§9Left click: §6Go to next page",
233-
"§9Right click: §6Go to previous page"
235+
config.pageNavigationNext(),
236+
config.pageNavigationPrevious()
234237
);
235238
}
236239

@@ -251,7 +254,7 @@ protected Item getItemWidget(final UserConnection connection, final ItemWidget i
251254
final String identifier = itemWidget.item().getString("id");
252255
final int count = itemWidget.item().getInt("count", 1);
253256

254-
final Tag label = text(Key.stripMinecraftNamespace(identifier));
257+
final Tag label = translate(Key.stripMinecraftNamespace(identifier));
255258
final Item item = createItem(identifier, label);
256259
item.setAmount(count);
257260
if (itemWidget.description() != null) {
@@ -282,6 +285,8 @@ protected Item getMultiTextWidget(final UserConnection connection, final MultiTe
282285
}
283286

284287
protected Item getBooleanInput(final UserConnection connection, final BooleanInput booleanInput) {
288+
final DialogStyleConfig config = ViaBackwards.getConfig().dialogStyleConfig();
289+
285290
final String item = booleanInput.value() ? "minecraft:lime_dye" : "minecraft:gray_dye";
286291
final Tag[] label = ChatUtil.split(booleanInput.label(), "\n");
287292

@@ -290,14 +295,14 @@ protected Item getBooleanInput(final UserConnection connection, final BooleanInp
290295
return createItem(
291296
item,
292297
handleTag(connection, booleanInput.label()),
293-
text("§9Left click: §6Toggle value")
298+
translate(config.toggleValue())
294299
);
295300
} else {
296301
final Tag[] lore = new Tag[label.length];
297302
for (int i = 1; i < label.length; i++) {
298303
lore[i - 1] = handleTag(connection, fixStyle(label[i]));
299304
}
300-
lore[lore.length - 1] = text("§9Left click: §6Toggle value");
305+
lore[lore.length - 1] = translate(config.toggleValue());
301306
return createItem(
302307
item,
303308
handleTag(connection, label[0]),
@@ -311,14 +316,16 @@ protected void clickBooleanInput(final BooleanInput booleanInput) {
311316
}
312317

313318
protected Item getNumberRangeInput(final UserConnection connection, final NumberRangeInput numberRangeInput) {
319+
final DialogStyleConfig config = ViaBackwards.getConfig().dialogStyleConfig();
320+
314321
final Tag label = handleTag(connection, numberRangeInput.displayName());
315322
return createItem(
316323
"minecraft:clock",
317324
label,
318325

319-
"§9Left click: §6Increase value by " + numberRangeInput.step(),
320-
"§9Right click: §6Decrease value by " + numberRangeInput.step(),
321-
"§7(Value between §a" + numberRangeInput.start() + " §7and §a" + numberRangeInput.end() + "§7)"
326+
String.format(config.increaseValue(), numberRangeInput.step()),
327+
String.format(config.decreaseValue(), numberRangeInput.step()),
328+
String.format(config.valueRange(), numberRangeInput.start(), numberRangeInput.end())
322329
);
323330
}
324331

@@ -333,21 +340,25 @@ protected void clickNumberRangeInput(final NumberRangeInput numberRangeInput, fi
333340
}
334341

335342
protected Item getTextInput(final UserConnection connection, final TextInput textInput) {
336-
final Tag currentValue = text("§7Current value: §a" + textInput.value());
343+
final DialogStyleConfig config = ViaBackwards.getConfig().dialogStyleConfig();
344+
345+
final Tag currentValue = translate(String.format(config.currentValue(), textInput.value()));
337346
if (textInput.label() == null) {
338347
return createItem("minecraft:writable_book", currentValue);
339348
} else {
340349
final Tag label = handleTag(connection, textInput.label());
341-
return createItem("minecraft:writable_book", label, currentValue, text("§9Left click: §6Edit text"));
350+
return createItem("minecraft:writable_book", label, currentValue, translate(config.editValue()));
342351
}
343352
}
344353

345354
protected void clickTextInput(final UserConnection connection, final TextInput textInput) {
346355
final ChestDialogStorage storage = connection.get(ChestDialogStorage.class);
347-
openAnvilView(connection, storage, text("§7Edit text"), textInput.value(), textInput);
356+
openAnvilView(connection, storage, translate("§7Edit text"), textInput.value(), textInput);
348357
}
349358

350359
protected Item getSingleOptionInput(final UserConnection connection, final SingleOptionInput singleOptionInput) {
360+
final DialogStyleConfig config = ViaBackwards.getConfig().dialogStyleConfig();
361+
351362
final Tag displayName = singleOptionInput.options()[singleOptionInput.value()].computeDisplay();
352363
final Tag label;
353364
if (singleOptionInput.label() != null) {
@@ -358,8 +369,8 @@ protected Item getSingleOptionInput(final UserConnection connection, final Singl
358369
return createItem(
359370
"minecraft:bookshelf",
360371
handleTag(connection, label),
361-
"§9Left click: §6Go to next option",
362-
"§9Right click: §6Go to previous option"
372+
config.nextOption(),
373+
config.previousOption()
363374
);
364375
}
365376

@@ -388,7 +399,7 @@ public void clickButton(final UserConnection connection, final Dialog.AfterActio
388399
case "open_url" -> {
389400
// We can't open a URL for the client, so roughly emulate by opening an Anvil containing the URL.
390401
final String url = clickEvent.getString("url");
391-
openAnvilView(connection, storage, text("Open URL"), url, null);
402+
openAnvilView(connection, storage, translate("Open URL"), url, null);
392403
}
393404
case "run_command" -> {
394405
// The vanilla client validates for signed argument types and has more requirements for this click event,
@@ -404,19 +415,21 @@ public void clickButton(final UserConnection connection, final Dialog.AfterActio
404415
case "copy_to_clipboard" -> {
405416
// Same as above, we can't access the clipboard
406417
final String value = clickEvent.getString("value");
407-
openAnvilView(connection, storage, text("Copy to clipboard"), value, null);
418+
openAnvilView(connection, storage, translate("Copy to clipboard"), value, null);
408419
}
409420
}
410421

411422
ClickEvents.handleClickEvent(connection, clickEvent); // Handle show_dialog and custom
412423
}
413424

414425
protected Item createTextInputItem(final String value) {
415-
return createItem("minecraft:paper", text(value), "§9Left click/close: §6Set text");
426+
final DialogStyleConfig config = ViaBackwards.getConfig().dialogStyleConfig();
427+
return createItem("minecraft:paper", translate(value), config.setText());
416428
}
417429

418430
protected Item createTextCopyItem(final String value) {
419-
return createItem("minecraft:paper", text(value), "§9Left click: §6Close");
431+
final DialogStyleConfig config = ViaBackwards.getConfig().dialogStyleConfig();
432+
return createItem("minecraft:paper", translate(value), config.close());
420433
}
421434

422435
private void openAnvilView(
@@ -589,7 +602,7 @@ protected Item createItem(final String identifier, final Tag name, final String.
589602
if (description.length > 0) {
590603
final List<Tag> lore = new ArrayList<>();
591604
for (final String s : description) {
592-
lore.add(text(s));
605+
lore.add(translate(s));
593606
}
594607
data.set(StructuredDataKey.LORE, lore.toArray(new Tag[0]));
595608
}

common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_6to1_21_5/storage/ChestDialogStorage.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import java.util.concurrent.atomic.AtomicInteger;
3232
import org.checkerframework.checker.nullness.qual.Nullable;
3333

34-
import static com.viaversion.viabackwards.utils.ChatUtil.text;
3534
import static com.viaversion.viabackwards.utils.ChatUtil.translate;
3635

3736
/**
@@ -44,7 +43,7 @@ public final class ChestDialogStorage implements StorableObject {
4443
private static final AtomicInteger FAKE_ID_COUNTER = new AtomicInteger(MIN_FAKE_ID);
4544

4645
private static final Tag[] RESPONSE_BUTTON_LABELS = new Tag[]{
47-
text(""),
46+
translate(""),
4847
translate("gui.waitingForResponse.button.inactive", 4),
4948
translate("gui.waitingForResponse.button.inactive", 3),
5049
translate("gui.waitingForResponse.button.inactive", 2),

common/src/main/java/com/viaversion/viabackwards/utils/ChatUtil.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,6 @@ public static Tag fixStyle(final Tag tag) {
6969
public static CompoundTag translate(final String key) {
7070
final CompoundTag tag = new CompoundTag();
7171
tag.putString("translate", key);
72-
return tag;
73-
}
74-
75-
public static CompoundTag text(final String text) {
76-
final CompoundTag tag = new CompoundTag();
77-
tag.putString("text", text);
7872
tag.putString("color", "white");
7973
tag.putBoolean("italic", false);
8074
return tag;

common/src/main/resources/assets/viabackwards/config.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,19 @@ map-display-entities: true
4444
#
4545
# Suppresses warnings of missing emulations for certain features that are not supported (e.g. world height in 1.17+).
4646
suppress-emulation-warnings: false
47+
#
48+
# Dialog styling. You can also use translation keys here.
49+
dialog-style:
50+
page-navigation-title: "&9&lPage navigation"
51+
page-navigation-next: "&9Left click: &6Go to next page"
52+
page-navigation-previous: "&9Right click: &6Go to previous page"
53+
toggle-value: "&9Left click: &6Toggle value"
54+
increase-value: "&9Left click: &6Increase value by %s"
55+
decrease-value: "&9Right click: &6Decrease value by %s"
56+
value-range: "&7(Value between &a%s &7and &a%s&7)"
57+
current-value: "&7Current value: &a%s"
58+
edit-value: "&9Left click: &6Edit text"
59+
next-option: "&9Left click: &6Go to next option"
60+
previous-option: "&9Right click: &6Go to previous option"
61+
set-text: "&9Left click/close: &6Set text"
62+
close: "&9Left click: &6Close"

0 commit comments

Comments
 (0)