Skip to content

Commit 6ec8222

Browse files
committed
add DialogListDialog
1 parent cbd5e88 commit 6ec8222

File tree

4 files changed

+173
-0
lines changed

4 files changed

+173
-0
lines changed

core/src/main/java/io/github/projectunified/unidialog/core/DialogManager.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ public interface DialogManager<I, BB extends DialogBodyBuilder<I>, IB extends Di
5454
*/
5555
<T extends NoticeDialog<I, BB, IB, D, AB, T>> T createNoticeDialog();
5656

57+
/**
58+
* Create a dialog list dialog
59+
*
60+
* @param <T> the type of the dialog list dialog, extending DialogListDialog
61+
* @return a new instance of DialogListDialog
62+
*/
63+
<T extends DialogListDialog<I, BB, IB, D, AB, T>> T createDialogListDialog();
64+
5765
/**
5866
* Register the dialog manager
5967
*/
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package io.github.projectunified.unidialog.core.dialog;
2+
3+
import io.github.projectunified.unidialog.core.action.DialogActionBuilder;
4+
import io.github.projectunified.unidialog.core.body.DialogBodyBuilder;
5+
import io.github.projectunified.unidialog.core.input.DialogInputBuilder;
6+
import org.jetbrains.annotations.Nullable;
7+
8+
import java.util.Collection;
9+
import java.util.function.Consumer;
10+
11+
/**
12+
* Interface representing a dialog that contains a list of other dialogs.
13+
*
14+
* @param <I> the type of the item for the item body
15+
* @param <BB> the type of the dialog body builder
16+
* @param <IB> the type of the dialog input builder
17+
* @param <D> the type of the dialog contained in the list
18+
* @param <AB> the type of the dialog action builder
19+
* @param <T> the type of the dialog itself, for method chaining
20+
*/
21+
public interface DialogListDialog<I, BB extends DialogBodyBuilder<I>, IB extends DialogInputBuilder, D extends Dialog<I, BB, IB, ?>, AB extends DialogActionBuilder<D, AB>, T extends DialogListDialog<I, BB, IB, D, AB, T>> extends Dialog<I, BB, IB, T> {
22+
/**
23+
* Add a dialog to the list of dialogs in this dialog
24+
*
25+
* @param dialog the dialog to add
26+
* @return the dialog itself for method chaining
27+
*/
28+
T dialog(D dialog);
29+
30+
/**
31+
* Add a dialog to the list of dialogs in this dialog
32+
*
33+
* @param namespace the namespace of the dialog
34+
* @param dialogId the ID of the dialog
35+
* @return the dialog itself for method chaining
36+
*/
37+
T dialog(String namespace, String dialogId);
38+
39+
/**
40+
* Set the action to be performed when exiting the dialog
41+
*
42+
* @param action the action to be performed when exiting the dialog
43+
* @return the dialog itself for method chaining
44+
*/
45+
T exitAction(@Nullable Consumer<AB> action);
46+
47+
/**
48+
* Set the number of columns in the dialog
49+
*
50+
* @param columns the number of columns in the dialog
51+
* @return the dialog itself for method chaining
52+
*/
53+
T columns(int columns);
54+
55+
/**
56+
* Set the width of the buttons in the dialog
57+
*
58+
* @param buttonWidth the width of the buttons in the dialog
59+
* @return the dialog itself for method chaining
60+
*/
61+
T buttonWidth(int buttonWidth);
62+
63+
/**
64+
* Add multiple dialogs to the list of dialogs in this dialog
65+
*
66+
* @param dialogs the collection of dialogs to add
67+
* @return the dialog itself for method chaining
68+
*/
69+
default <B extends D> T dialog(Collection<B> dialogs) {
70+
for (B dialog : dialogs) {
71+
dialog(dialog);
72+
}
73+
//noinspection unchecked
74+
return (T) this;
75+
}
76+
}

packetevents/src/main/java/io/github/projectunified/unidialog/packetevents/PocketEventsDialogManager.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ public PENoticeDialog createNoticeDialog() {
6666
return new PENoticeDialog(defaultNamespace, componentDeserializer, this::getPlayer);
6767
}
6868

69+
@Override
70+
public PEDialogListDialog createDialogListDialog() {
71+
return new PEDialogListDialog(defaultNamespace, componentDeserializer, this::getPlayer);
72+
}
73+
6974
@Override
7075
public void register() {
7176
if (packetListener != null) {
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package io.github.projectunified.unidialog.packetevents.dialog;
2+
3+
import com.github.retrooper.packetevents.protocol.dialog.CommonDialogData;
4+
import com.github.retrooper.packetevents.protocol.dialog.Dialog;
5+
import com.github.retrooper.packetevents.protocol.dialog.Dialogs;
6+
import com.github.retrooper.packetevents.protocol.dialog.button.ActionButton;
7+
import com.github.retrooper.packetevents.protocol.item.ItemStack;
8+
import com.github.retrooper.packetevents.protocol.mapper.MappedEntitySet;
9+
import com.github.retrooper.packetevents.resources.ResourceLocation;
10+
import io.github.projectunified.unidialog.core.dialog.DialogListDialog;
11+
import io.github.projectunified.unidialog.packetevents.action.PEDialogActionBuilder;
12+
import io.github.projectunified.unidialog.packetevents.body.PEDialogBodyBuilder;
13+
import io.github.projectunified.unidialog.packetevents.input.PEDialogInputBuilder;
14+
import net.kyori.adventure.text.Component;
15+
import org.jetbrains.annotations.Nullable;
16+
17+
import java.util.ArrayList;
18+
import java.util.List;
19+
import java.util.UUID;
20+
import java.util.function.Consumer;
21+
import java.util.function.Function;
22+
23+
public class PEDialogListDialog extends PEDialog<PEDialogListDialog> implements DialogListDialog<ItemStack, PEDialogBodyBuilder, PEDialogInputBuilder, PEDialog<?>, PEDialogActionBuilder, PEDialogListDialog> {
24+
private List<Dialog> dialogs;
25+
private @Nullable ActionButton exitAction;
26+
private int columns;
27+
private int buttonWidth;
28+
29+
public PEDialogListDialog(String defaultNamespace, Function<String, Component> componentDeserializer, Function<UUID, @Nullable Object> playerFunction) {
30+
super(defaultNamespace, componentDeserializer, playerFunction);
31+
}
32+
33+
private void addDialog(Dialog dialog) {
34+
if (dialogs == null) {
35+
dialogs = new ArrayList<>();
36+
}
37+
dialogs.add(dialog);
38+
}
39+
40+
@Override
41+
public PEDialogListDialog dialog(PEDialog<?> dialog) {
42+
addDialog(dialog.getDialog());
43+
return this;
44+
}
45+
46+
@Override
47+
public PEDialogListDialog dialog(String namespace, String dialogId) {
48+
Dialog dialog = Dialogs.getRegistry().getByName(new ResourceLocation(namespace, dialogId));
49+
if (dialog == null) {
50+
throw new IllegalArgumentException("Dialog with namespace '" + namespace + "' and id '" + dialogId + "' does not exist.");
51+
}
52+
addDialog(dialog);
53+
return this;
54+
}
55+
56+
@Override
57+
public PEDialogListDialog exitAction(Consumer<PEDialogActionBuilder> action) {
58+
this.exitAction = action == null ? null : getAction(action);
59+
return this;
60+
}
61+
62+
@Override
63+
public PEDialogListDialog columns(int columns) {
64+
this.columns = columns;
65+
return this;
66+
}
67+
68+
@Override
69+
public PEDialogListDialog buttonWidth(int buttonWidth) {
70+
this.buttonWidth = buttonWidth;
71+
return this;
72+
}
73+
74+
@Override
75+
protected Dialog constructDialog(CommonDialogData commonDialogData) {
76+
return new com.github.retrooper.packetevents.protocol.dialog.DialogListDialog(
77+
commonDialogData,
78+
dialogs == null || dialogs.isEmpty() ? MappedEntitySet.createEmpty() : new MappedEntitySet<>(dialogs),
79+
exitAction,
80+
columns > 0 ? columns : 2,
81+
buttonWidth > 0 ? buttonWidth : 150
82+
);
83+
}
84+
}

0 commit comments

Comments
 (0)