Skip to content

Commit f50613e

Browse files
committed
introduce DialogPayload
This is a way to lazily get value from a dialog payload that is sent to the server when the player triggers custom action in the dialog
1 parent f503cc7 commit f50613e

File tree

9 files changed

+251
-66
lines changed

9 files changed

+251
-66
lines changed

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
import io.github.projectunified.unidialog.core.body.DialogBodyBuilder;
55
import io.github.projectunified.unidialog.core.dialog.*;
66
import io.github.projectunified.unidialog.core.input.DialogInputBuilder;
7+
import io.github.projectunified.unidialog.core.payload.DialogPayload;
78

89
import java.util.Map;
910
import java.util.UUID;
1011
import java.util.function.BiConsumer;
12+
import java.util.function.Consumer;
1113

1214
/**
1315
* The main interface for managing dialogs in the UniDialog framework.
@@ -72,13 +74,31 @@ public interface DialogManager<I, BB extends DialogBodyBuilder<I>, IB extends Di
7274
*/
7375
void unregister();
7476

77+
/**
78+
* Register a custom action with a unique identifier.
79+
*
80+
* @param id the unique identifier for the custom action
81+
* @param action the action to be executed, taking a payload
82+
*/
83+
void registerCustomAction(String id, Consumer<DialogPayload> action);
84+
85+
/**
86+
* Register a custom action with a unique identifier.
87+
*
88+
* @param id the unique identifier for the custom action
89+
* @param action the action to be executed, taking a payload
90+
*/
91+
void registerCustomAction(String namespace, String id, Consumer<DialogPayload> action);
92+
7593
/**
7694
* Register a custom action with a unique identifier.
7795
*
7896
* @param id the unique identifier for the custom action
7997
* @param action the action to be executed, taking a UUID and a map of parameters
8098
*/
81-
void registerCustomAction(String id, BiConsumer<UUID, Map<String, String>> action);
99+
default void registerCustomAction(String id, BiConsumer<UUID, Map<String, String>> action) {
100+
registerCustomAction(id, payload -> action.accept(payload.owner(), payload.map()));
101+
}
82102

83103
/**
84104
* Register a custom action with a namespace and a unique identifier.
@@ -87,7 +107,9 @@ public interface DialogManager<I, BB extends DialogBodyBuilder<I>, IB extends Di
87107
* @param id the unique identifier for the custom action
88108
* @param action the action to be executed, taking a UUID and a map of parameters
89109
*/
90-
void registerCustomAction(String namespace, String id, BiConsumer<UUID, Map<String, String>> action);
110+
default void registerCustomAction(String namespace, String id, BiConsumer<UUID, Map<String, String>> action) {
111+
registerCustomAction(namespace, id, payload -> action.accept(payload.owner(), payload.map()));
112+
}
91113

92114
/**
93115
* Unregister a custom action by its unique identifier.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package io.github.projectunified.unidialog.core.payload;
2+
3+
import org.jetbrains.annotations.Nullable;
4+
5+
import java.util.Map;
6+
import java.util.UUID;
7+
8+
/**
9+
* The payload which is sent back to the server when the player run a custom action in the dialog
10+
*/
11+
public interface DialogPayload {
12+
/**
13+
* Get the owner of the payload
14+
*
15+
* @return the id of the owner
16+
*/
17+
UUID owner();
18+
19+
/**
20+
* Get the text value from the payload
21+
*
22+
* @param key the key to search for
23+
* @return the text, or null if the key doesn't exist in the payload
24+
*/
25+
@Nullable String textValue(String key);
26+
27+
/**
28+
* Get the boolean value from the payload
29+
*
30+
* @param key the key to search for
31+
* @return the boolean, or null if the key doesn't exist in the payload
32+
*/
33+
@Nullable Boolean booleanValue(String key);
34+
35+
/**
36+
* Get the number value from the payload
37+
*
38+
* @param key the key to search for
39+
* @return the number, or null if the key doesn't exist in the payload or the value is not a number
40+
*/
41+
@Nullable Number numberValue(String key);
42+
43+
/**
44+
* Get the map of all values in the payload
45+
*
46+
* @return the map
47+
*/
48+
Map<String, String> map();
49+
}

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

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
import com.github.retrooper.packetevents.event.PacketReceiveEvent;
77
import com.github.retrooper.packetevents.protocol.ConnectionState;
88
import com.github.retrooper.packetevents.protocol.item.ItemStack;
9-
import com.github.retrooper.packetevents.protocol.nbt.*;
9+
import com.github.retrooper.packetevents.protocol.nbt.NBT;
10+
import com.github.retrooper.packetevents.protocol.nbt.NBTCompound;
1011
import com.github.retrooper.packetevents.protocol.packettype.PacketType;
1112
import com.github.retrooper.packetevents.protocol.player.User;
1213
import com.github.retrooper.packetevents.resources.ResourceLocation;
@@ -17,10 +18,12 @@
1718
import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientCustomClickAction;
1819
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerClearDialog;
1920
import io.github.projectunified.unidialog.core.DialogManager;
21+
import io.github.projectunified.unidialog.core.payload.DialogPayload;
2022
import io.github.projectunified.unidialog.packetevents.action.PEDialogActionBuilder;
2123
import io.github.projectunified.unidialog.packetevents.body.PEDialogBodyBuilder;
2224
import io.github.projectunified.unidialog.packetevents.dialog.*;
2325
import io.github.projectunified.unidialog.packetevents.input.PEDialogInputBuilder;
26+
import io.github.projectunified.unidialog.packetevents.payload.PEDialogPayload;
2427
import io.github.retrooper.packetevents.adventure.serializer.legacy.LegacyComponentSerializer;
2528
import net.kyori.adventure.text.Component;
2629
import org.jetbrains.annotations.NotNull;
@@ -30,13 +33,14 @@
3033
import java.util.Map;
3134
import java.util.UUID;
3235
import java.util.function.BiConsumer;
36+
import java.util.function.Consumer;
3337
import java.util.function.Function;
3438

3539
@SuppressWarnings("unchecked")
3640
public abstract class PocketEventsDialogManager implements DialogManager<ItemStack, PEDialogBodyBuilder, PEDialogInputBuilder, PEDialog<?>, PEDialogActionBuilder> {
3741
private final String defaultNamespace;
3842
private final Function<String, Component> componentDeserializer;
39-
private final Map<ResourceLocation, BiConsumer<UUID, Map<String, String>>> actions = new HashMap<>();
43+
private final Map<ResourceLocation, Consumer<DialogPayload>> actions = new HashMap<>();
4044
private PacketListenerCommon packetListener;
4145

4246
/**
@@ -122,28 +126,12 @@ public void onPacketReceive(@NotNull PacketReceiveEvent event) {
122126
ResourceLocation namespacedId = packet.getId();
123127
NBT data = packet.getPayload();
124128

125-
BiConsumer<UUID, Map<String, String>> action = actions.get(namespacedId);
129+
Consumer<DialogPayload> action = actions.get(namespacedId);
126130
if (action == null) return;
127131

128132
UUID uuid = getPlayerId(event.getPlayer());
129-
Map<String, String> payload = new HashMap<>();
130-
if (data instanceof NBTCompound nbtCompound) {
131-
for (Map.Entry<String, NBT> entry : nbtCompound.getTags().entrySet()) {
132-
String key = entry.getKey();
133-
String value = switch (entry.getValue()) {
134-
case NBTInt nbtInt -> Integer.toString(nbtInt.getAsInt());
135-
case NBTLong nbtLong -> Long.toString(nbtLong.getAsLong());
136-
case NBTFloat nbtFloat -> Float.toString(nbtFloat.getAsFloat());
137-
case NBTDouble nbtDouble -> Double.toString(nbtDouble.getAsDouble());
138-
case NBTString nbtString -> nbtString.getValue();
139-
case NBTByte nbtByte -> Boolean.toString(nbtByte.getAsBool());
140-
case NBT nbt -> nbt.toString(); // Fallback to string representation
141-
};
142-
payload.put(key, value);
143-
}
144-
}
145-
146-
action.accept(uuid, payload);
133+
NBTCompound compound = data instanceof NBTCompound nbtCompound ? nbtCompound : new NBTCompound();
134+
action.accept(new PEDialogPayload(uuid, compound));
147135
}
148136
};
149137
PacketEvents.getAPI().getEventManager().registerListener(packetListener);
@@ -159,12 +147,12 @@ public void unregister() {
159147
}
160148

161149
@Override
162-
public void registerCustomAction(String id, BiConsumer<UUID, Map<String, String>> action) {
150+
public void registerCustomAction(String id, Consumer<DialogPayload> action) {
163151
registerCustomAction(defaultNamespace, id, action);
164152
}
165153

166154
@Override
167-
public void registerCustomAction(String namespace, String id, BiConsumer<UUID, Map<String, String>> action) {
155+
public void registerCustomAction(String namespace, String id, Consumer<DialogPayload> action) {
168156
actions.put(new ResourceLocation(namespace, id), action);
169157
}
170158

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package io.github.projectunified.unidialog.packetevents.payload;
2+
3+
import com.github.retrooper.packetevents.protocol.nbt.*;
4+
import io.github.projectunified.unidialog.core.payload.DialogPayload;
5+
import org.jetbrains.annotations.Nullable;
6+
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
import java.util.UUID;
10+
11+
public record PEDialogPayload(UUID owner, NBTCompound compound) implements DialogPayload {
12+
@Override
13+
public @Nullable String textValue(String key) {
14+
return compound.getStringTagValueOrNull(key);
15+
}
16+
17+
@Override
18+
public @Nullable Boolean booleanValue(String key) {
19+
NBTByte nbt = compound.getTagOfTypeOrNull(key, NBTByte.class);
20+
return nbt == null ? null : nbt.getAsBool();
21+
}
22+
23+
@Override
24+
public @Nullable Number numberValue(String key) {
25+
NBTNumber nbt = compound.getTagOfTypeOrNull(key, NBTNumber.class);
26+
return nbt == null ? null : nbt.getAsNumber();
27+
}
28+
29+
@Override
30+
public Map<String, String> map() {
31+
Map<String, String> payload = new HashMap<>();
32+
for (Map.Entry<String, NBT> entry : compound.getTags().entrySet()) {
33+
String key = entry.getKey();
34+
String value = switch (entry.getValue()) {
35+
case NBTInt nbtInt -> Integer.toString(nbtInt.getAsInt());
36+
case NBTLong nbtLong -> Long.toString(nbtLong.getAsLong());
37+
case NBTFloat nbtFloat -> Float.toString(nbtFloat.getAsFloat());
38+
case NBTDouble nbtDouble -> Double.toString(nbtDouble.getAsDouble());
39+
case NBTString nbtString -> nbtString.getValue();
40+
case NBTByte nbtByte -> Boolean.toString(nbtByte.getAsBool());
41+
case NBT nbt -> nbt.toString(); // Fallback to string representation
42+
};
43+
payload.put(key, value);
44+
}
45+
return payload;
46+
}
47+
}

paper/src/main/java/io/github/projectunified/unidialog/paper/PaperDialogManager.java

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package io.github.projectunified.unidialog.paper;
22

33
import io.github.projectunified.unidialog.core.DialogManager;
4+
import io.github.projectunified.unidialog.core.payload.DialogPayload;
45
import io.github.projectunified.unidialog.paper.action.PaperDialogActionBuilder;
56
import io.github.projectunified.unidialog.paper.body.PaperDialogBodyBuilder;
67
import io.github.projectunified.unidialog.paper.dialog.*;
78
import io.github.projectunified.unidialog.paper.input.PaperDialogInputBuilder;
9+
import io.github.projectunified.unidialog.paper.payload.PaperDialogPayload;
810
import io.papermc.paper.connection.PlayerConfigurationConnection;
911
import io.papermc.paper.connection.PlayerGameConnection;
10-
import io.papermc.paper.dialog.DialogResponseView;
1112
import io.papermc.paper.event.player.PlayerCustomClickEvent;
1213
import net.kyori.adventure.key.Key;
1314
import net.kyori.adventure.text.Component;
@@ -19,16 +20,20 @@
1920
import org.bukkit.inventory.ItemStack;
2021
import org.bukkit.plugin.Plugin;
2122

22-
import java.util.*;
23+
import java.util.HashMap;
24+
import java.util.Locale;
25+
import java.util.Map;
26+
import java.util.UUID;
2327
import java.util.function.BiConsumer;
28+
import java.util.function.Consumer;
2429
import java.util.function.Function;
2530

2631
@SuppressWarnings("unchecked")
2732
public class PaperDialogManager implements DialogManager<ItemStack, PaperDialogBodyBuilder, PaperDialogInputBuilder, PaperDialog<?>, PaperDialogActionBuilder>, Listener {
2833
private final Plugin plugin;
2934
private final String defaultNamespace;
3035
private final Function<String, Component> componentDeserializer;
31-
private final Map<Key, BiConsumer<UUID, Map<String, String>>> customActions = new HashMap<>();
36+
private final Map<Key, Consumer<DialogPayload>> customActions = new HashMap<>();
3237

3338
/**
3439
* Constructor for PaperDialogManager
@@ -113,7 +118,7 @@ public void unregister() {
113118
@EventHandler
114119
public void onCustomClick(PlayerCustomClickEvent event) {
115120
Key key = event.getIdentifier();
116-
BiConsumer<UUID, Map<String, String>> action = customActions.get(key);
121+
Consumer<DialogPayload> action = customActions.get(key);
117122
if (action == null) return;
118123

119124
UUID uuid = switch (event.getCommonConnection()) {
@@ -123,15 +128,7 @@ public void onCustomClick(PlayerCustomClickEvent event) {
123128
};
124129
if (uuid == null) return;
125130

126-
Map<String, String> data;
127-
DialogResponseView responseView = event.getDialogResponseView();
128-
if (responseView != null) {
129-
data = PaperUtil.convertDialogResponseToMap(responseView);
130-
} else {
131-
data = Collections.emptyMap();
132-
}
133-
134-
action.accept(uuid, data);
131+
action.accept(new PaperDialogPayload(uuid, event.getDialogResponseView()));
135132
}
136133

137134
@Override
@@ -150,13 +147,13 @@ public void unregisterAllCustomActions() {
150147
}
151148

152149
@Override
153-
public void registerCustomAction(String namespace, String id, BiConsumer<UUID, Map<String, String>> action) {
154-
customActions.put(Key.key(namespace, id), action);
150+
public void registerCustomAction(String id, Consumer<DialogPayload> action) {
151+
registerCustomAction(defaultNamespace, id, action);
155152
}
156153

157154
@Override
158-
public void registerCustomAction(String id, BiConsumer<UUID, Map<String, String>> action) {
159-
registerCustomAction(defaultNamespace, id, action);
155+
public void registerCustomAction(String namespace, String id, Consumer<DialogPayload> action) {
156+
customActions.put(Key.key(namespace, id), action);
160157
}
161158

162159
@Override
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package io.github.projectunified.unidialog.paper.payload;
2+
3+
import io.github.projectunified.unidialog.core.payload.DialogPayload;
4+
import io.papermc.paper.dialog.DialogResponseView;
5+
import org.jetbrains.annotations.Nullable;
6+
7+
import java.util.Map;
8+
import java.util.UUID;
9+
10+
public record PaperDialogPayload(UUID owner, @Nullable DialogResponseView view) implements DialogPayload {
11+
@Override
12+
public @Nullable String textValue(String key) {
13+
if (view == null) return null;
14+
return view.getText(key);
15+
}
16+
17+
@Override
18+
public @Nullable Boolean booleanValue(String key) {
19+
if (view == null) return null;
20+
return view.getBoolean(key);
21+
}
22+
23+
@Override
24+
public @Nullable Number numberValue(String key) {
25+
if (view == null) return null;
26+
return view.getFloat(key);
27+
}
28+
29+
@Override
30+
public Map<String, String> map() {
31+
if (view == null) return Map.of();
32+
return PaperDialogPayloadMap.convertDialogResponseToMap(view);
33+
}
34+
}

paper/src/main/java/io/github/projectunified/unidialog/paper/PaperUtil.java renamed to paper/src/main/java/io/github/projectunified/unidialog/paper/payload/PaperDialogPayloadMap.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.projectunified.unidialog.paper;
1+
package io.github.projectunified.unidialog.paper.payload;
22

33
import io.papermc.paper.dialog.DialogResponseView;
44

@@ -8,7 +8,7 @@
88
import java.util.Map;
99
import java.util.Set;
1010

11-
final class PaperUtil {
11+
final class PaperDialogPayloadMap {
1212
private static final Field compoundField;
1313
private static final Method keySetMethod;
1414

0 commit comments

Comments
 (0)