Skip to content

Commit 30e63eb

Browse files
Improve multiple text widgets follow one another in dialogs (#1044)
1 parent 217c945 commit 30e63eb

File tree

1 file changed

+36
-5
lines changed

1 file changed

+36
-5
lines changed

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

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import java.util.List;
5050
import org.checkerframework.checker.nullness.qual.Nullable;
5151

52+
import static com.viaversion.viabackwards.utils.ChatUtil.fixStyle;
5253
import static com.viaversion.viabackwards.utils.ChatUtil.text;
5354
import static com.viaversion.viabackwards.utils.ChatUtil.translate;
5455

@@ -74,6 +75,18 @@ public void openDialog(final UserConnection connection, final Dialog dialog) {
7475
return;
7576
}
7677

78+
// Batch text widgets following one another into a single MultiTextWidget to be display properly.
79+
final List<Tag> texts = new ArrayList<>();
80+
for (final Widget widget : new ArrayList<>(dialog.widgets())) {
81+
if (widget instanceof final TextWidget textWidget) {
82+
texts.add(textWidget.label());
83+
dialog.widgets().remove(textWidget);
84+
} else if (!texts.isEmpty()) {
85+
dialog.widgets().add(dialog.widgets().indexOf(widget), new MultiTextWidget(texts.toArray(Tag[]::new)));
86+
texts.clear();
87+
}
88+
}
89+
7790
final ChestDialogStorage previousStorage = connection.get(ChestDialogStorage.class);
7891
final ChestDialogStorage storage = new ChestDialogStorage(this, dialog);
7992
if (previousStorage != null) {
@@ -252,8 +265,18 @@ protected Item getItemWidget(final ItemWidget itemWidget) {
252265
return item;
253266
}
254267

255-
protected Item getTextWidget(final UserConnection connection, final TextWidget textWidget) {
256-
return createItem("minecraft:paper", handleTag(connection, textWidget.label()));
268+
protected Item getMultiTextWidget(final UserConnection connection, final MultiTextWidget multiTextWidget) {
269+
final Tag name = handleTag(connection, multiTextWidget.labels()[0]);
270+
final int length = multiTextWidget.labels().length;
271+
if (length == 1) {
272+
return createItem("minecraft:paper", name);
273+
}
274+
275+
final Tag[] lore = new Tag[length - 1];
276+
for (int i = 1; i < length; i++) {
277+
lore[i - 1] = fixStyle(handleTag(connection, multiTextWidget.labels()[i]));
278+
}
279+
return createItem("minecraft:paper", name, lore);
257280
}
258281

259282
protected Item getBooleanInput(final UserConnection connection, final BooleanInput booleanInput) {
@@ -270,7 +293,7 @@ protected Item getBooleanInput(final UserConnection connection, final BooleanInp
270293
} else {
271294
final Tag[] lore = new Tag[label.length];
272295
for (int i = 1; i < label.length; i++) {
273-
lore[i - 1] = ChatUtil.fixStyle(handleTag(connection, label[i]));
296+
lore[i - 1] = fixStyle(handleTag(connection, label[i]));
274297
}
275298
lore[lore.length - 1] = text("§9Left click: §6Toggle value");
276299
return createItem(
@@ -445,8 +468,8 @@ protected void clickDialogButton(final UserConnection connection, final Dialog d
445468
protected Item getItem(final UserConnection connection, final Widget widget) {
446469
if (widget instanceof final ItemWidget itemWidget) {
447470
return getItemWidget(itemWidget);
448-
} else if (widget instanceof final TextWidget textWidget) {
449-
return getTextWidget(connection, textWidget);
471+
} else if (widget instanceof final MultiTextWidget multiTextWidget) {
472+
return getMultiTextWidget(connection, multiTextWidget);
450473
} else if (widget instanceof final BooleanInput booleanInput) {
451474
return getBooleanInput(connection, booleanInput);
452475
} else if (widget instanceof final NumberRangeInput numberRangeInput) {
@@ -529,6 +552,14 @@ protected Item[] getItems(final UserConnection connection, final ChestDialogStor
529552
return tag;
530553
}
531554

555+
/**
556+
* This doesn't actually exist in Minecraft but is created if multiple {@link TextWidget} follow one another in order
557+
* to improve readability in chest inventories.
558+
*/
559+
public record MultiTextWidget(Tag[] labels) implements Widget {
560+
561+
}
562+
532563
// -------------------------------------------------------------------------------------
533564

534565
protected Item createItem(final String identifier, final Tag name) {

0 commit comments

Comments
 (0)