packets, fancydialogs: Refactor custom action handling to use a map for additional data

This commit is contained in:
Oliver
2025-06-17 21:50:34 +02:00
parent 31f03ae252
commit d61b3af90b
10 changed files with 65 additions and 29 deletions

View File

@@ -245,7 +245,7 @@ public class ClientboundShowDialogPacketImpl extends FS_ClientboundShowDialogPac
Optional<CompoundTag> additions; Optional<CompoundTag> additions;
if (customAction.getAdditions() != null) { if (customAction.getAdditions() != null) {
CompoundTag tag = new CompoundTag(); CompoundTag tag = new CompoundTag();
tag.putString("data", customAction.getAdditions()); customAction.getAdditions().forEach(tag::putString);
additions = Optional.of(tag); additions = Optional.of(tag);
} else { } else {
additions = Optional.empty(); additions = Optional.empty();

View File

@@ -13,8 +13,9 @@ import net.minecraft.server.level.ServerPlayer;
import org.bukkit.craftbukkit.entity.CraftPlayer; import org.bukkit.craftbukkit.entity.CraftPlayer;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Map;
public class PacketListenerImpl extends FS_PacketListener { public class PacketListenerImpl extends FS_PacketListener {
@@ -75,12 +76,16 @@ public class PacketListenerImpl extends FS_PacketListener {
switch (type) { switch (type) {
case CUSTOM_CLICK_ACTION -> { case CUSTOM_CLICK_ACTION -> {
ServerboundCustomClickActionPacket customClickActionPacket = (ServerboundCustomClickActionPacket) packet; ServerboundCustomClickActionPacket customClickActionPacket = (ServerboundCustomClickActionPacket) packet;
Map<String, String> payload = new HashMap<>();
customClickActionPacket.payload().get().asCompound().get().forEach((k, t) -> {
payload.put(k, t.asString().get());
});
return new FS_ServerboundCustomClickActionPacket( return new FS_ServerboundCustomClickActionPacket(
type, type,
PaperAdventure.asAdventure(customClickActionPacket.id()), PaperAdventure.asAdventure(customClickActionPacket.id()),
customClickActionPacket.payload().isPresent() ? payload
customClickActionPacket.payload().get().asCompound().get().getString("data") :
Optional.empty()
); );
} }
// Add more cases for other packet types as needed // Add more cases for other packet types as needed

View File

@@ -3,12 +3,14 @@ package de.oliver.fancysitula.api.dialogs.actions;
import org.intellij.lang.annotations.Subst; import org.intellij.lang.annotations.Subst;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.Map;
public class FS_DialogCustomAction implements FS_DialogActionButtonAction { public class FS_DialogCustomAction implements FS_DialogActionButtonAction {
private String id; private String id;
private @Nullable String additions; private Map<String, String> additions;
public FS_DialogCustomAction(String id, String additions) { public FS_DialogCustomAction(String id, Map<String, String> additions) {
this.id = id; this.id = id;
this.additions = additions; this.additions = additions;
} }
@@ -22,11 +24,11 @@ public class FS_DialogCustomAction implements FS_DialogActionButtonAction {
this.id = id; this.id = id;
} }
public @Nullable String getAdditions() { public @Nullable Map<String, String> getAdditions() {
return additions; return additions;
} }
public void setAdditions(@Nullable String additions) { public void setAdditions(@Nullable Map<String, String> additions) {
this.additions = additions; this.additions = additions;
} }
} }

View File

@@ -2,14 +2,14 @@ package de.oliver.fancysitula.api.packets;
import net.kyori.adventure.key.Key; import net.kyori.adventure.key.Key;
import java.util.Optional; import java.util.Map;
public class FS_ServerboundCustomClickActionPacket extends FS_ServerboundPacket { public class FS_ServerboundCustomClickActionPacket extends FS_ServerboundPacket {
private final Key id; private final Key id;
private final Optional<String> payload; private final Map<String, String> payload;
public FS_ServerboundCustomClickActionPacket(Type type, Key id, Optional<String> payload) { public FS_ServerboundCustomClickActionPacket(Type type, Key id, Map<String, String> payload) {
super(type); super(type);
this.id = id; this.id = id;
this.payload = payload; this.payload = payload;
@@ -19,7 +19,7 @@ public class FS_ServerboundCustomClickActionPacket extends FS_ServerboundPacket
return id; return id;
} }
public Optional<String> getPayload() { public Map<String, String> getPayload() {
return payload; return payload;
} }

View File

@@ -26,6 +26,7 @@ import org.jetbrains.annotations.NotNull;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.UUID; import java.util.UUID;
public class FancySitulaCMD extends Command { public class FancySitulaCMD extends Command {
@@ -105,7 +106,7 @@ public class FancySitulaCMD extends Command {
), ),
new FS_DialogCustomAction( new FS_DialogCustomAction(
"my-custom-action-1", "my-custom-action-1",
"someAdditionalData1" Map.of("foo", "bar")
) )
) )
); );

View File

@@ -35,12 +35,14 @@ public class ConfirmationDialog {
new DialogButton( new DialogButton(
this.confirmText, this.confirmText,
this.confirmText, this.confirmText,
"confirm" "confirm",
""
), ),
new DialogButton( new DialogButton(
this.cancelText, this.cancelText,
this.cancelText, this.cancelText,
"cancel" "cancel",
""
) )
) )
); );

View File

@@ -3,6 +3,7 @@ package com.fancyinnovations.fancydialogs.api.data;
public record DialogButton( public record DialogButton(
String label, String label,
String tooltip, String tooltip,
String action String action,
String actionData
) { ) {
} }

View File

@@ -18,6 +18,7 @@ import org.bukkit.entity.Player;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
public class DialogImpl extends Dialog { public class DialogImpl extends Dialog {
@@ -46,7 +47,14 @@ public class DialogImpl extends Dialog {
button.tooltip(), button.tooltip(),
150 // default button width 150 // default button width
), ),
new FS_DialogCustomAction("fancydialogs_dialog_action", button.action()) new FS_DialogCustomAction(
"fancydialogs_dialog_action",
Map.of(
"dialog_id", id,
"action_id", button.action(),
"action_data", button.actionData()
)
)
); );
actions.add(fsDialogActionButton); actions.add(fsDialogActionButton);
} }

View File

@@ -31,6 +31,12 @@ public class CustomClickActionPacketListener {
if (!packet.getId().namespace().equals("fancysitula") && !packet.getId().namespace().equals("fancydialogs_dialog_action")) { if (!packet.getId().namespace().equals("fancysitula") && !packet.getId().namespace().equals("fancydialogs_dialog_action")) {
return; // Ignore packets not related to FancyDialogs return; // Ignore packets not related to FancyDialogs
} }
String dialogId = packet.getPayload().get("dialog_id");
String actionId = packet.getPayload().get("action_id");
String actionData = packet.getPayload().get("action_data");
// TODO process the dialog action
} }
public FS_PacketListener getPacketListener() { public FS_PacketListener getPacketListener() {

View File

@@ -44,12 +44,14 @@ public class DefaultDialogs {
new DialogButton( new DialogButton(
"<color:#ff4f19>Close</color>", "<color:#ff4f19>Close</color>",
"<color:#ff4f19>Enjoy using FancyDialogs</color>", "<color:#ff4f19>Enjoy using FancyDialogs</color>",
"close_dialog" "close_dialog",
""
), ),
new DialogButton( new DialogButton(
"<color:#ffd000>Run command</color>", "<color:#ffd000>Run command</color>",
"<color:#ff4f19>Click to give yourself an apple :)</color>", "<color:#ff4f19>Click to give yourself an apple :)</color>",
"run_command:/give {player} apple 1" "run_command:",
"give {player} apple 1"
) )
) )
); );
@@ -72,22 +74,26 @@ public class DefaultDialogs {
new DialogButton( new DialogButton(
"<color:red>Read the rules</color>", "<color:red>Read the rules</color>",
"<color:red>Click to read our rules!</color>", "<color:red>Click to read our rules!</color>",
"message:<click:open_url:'{LINK TO RULES}'>Visit our rules</click>!" "message",
"<click:open_url:'{LINK TO RULES}'>Visit our rules</click>!"
), ),
new DialogButton( new DialogButton(
"<color:#00ff5e>Start playing</color>", "<color:#00ff5e>Start playing</color>",
"<color:#00ff5e>Click to start playing!</color>", "<color:#00ff5e>Click to start playing!</color>",
"close_dialog" "close_dialog",
""
), ),
new DialogButton( new DialogButton(
"<color:#1787ff>Join our Discord</color>", "<color:#1787ff>Join our Discord</color>",
"<color:#1787ff>Click to join our Discord server!</color>", "<color:#1787ff>Click to join our Discord server!</color>",
"message:<click:open_url:'{LINK TO DISC SERVER}'>Join our Discord server</click>!" "message",
"<click:open_url:'{LINK TO DISC SERVER}'>Join our Discord server</click>!"
), ),
new DialogButton( new DialogButton(
"<color:#ffee00>Visit our website</color>", "<color:#ffee00>Visit our website</color>",
"<color:#ffee00>Click to visit our website!</color>", "<color:#ffee00>Click to visit our website!</color>",
"message:<click:open_url:'{LINK TO WEBSITE}'>Visit our website</click>!" "message",
"<click:open_url:'{LINK TO WEBSITE}'>Visit our website</click>!"
) )
) )
); );
@@ -107,27 +113,32 @@ public class DefaultDialogs {
new DialogButton( new DialogButton(
"<color:#ffee00>Visit our website</color>", "<color:#ffee00>Visit our website</color>",
"<color:#ffee00>Click to visit our website!</color>", "<color:#ffee00>Click to visit our website!</color>",
"message:<click:open_url:'{LINK TO WEBSITE}'>Visit our website</click>!" "message",
"<click:open_url:'{LINK TO WEBSITE}'>Visit our website</click>!"
), ),
new DialogButton( new DialogButton(
"<color:#ffee00>Read the rules</color>", "<color:#ffee00>Read the rules</color>",
"<color:#ffee00>Click to read our rules!</color>", "<color:#ffee00>Click to read our rules!</color>",
"message:<click:open_url:'{LINK TO RULES}'>Visit our rules</click>!" "message",
"<click:open_url:'{LINK TO RULES}'>Visit our rules</click>!"
), ),
new DialogButton( new DialogButton(
"<color:#ffee00>Join our Discord</color>", "<color:#ffee00>Join our Discord</color>",
"<color:#ffee00>Click to join our Discord server!</color>", "<color:#ffee00>Click to join our Discord server!</color>",
"message:<click:open_url:'{LINK TO DISCORD}'>Join our Discord server</click>!" "message",
"<click:open_url:'{LINK TO DISCORD}'>Join our Discord server</click>!"
), ),
new DialogButton( new DialogButton(
"<color:#ffee00>Support us</color>", "<color:#ffee00>Support us</color>",
"<color:#ffee00>Click to support us!</color>", "<color:#ffee00>Click to support us!</color>",
"message:<click:open_url:'{LINK TO SUPPORT}'>Support us</click>!" "message",
"<click:open_url:'{LINK TO SUPPORT}'>Support us</click>!"
), ),
new DialogButton( new DialogButton(
"<color:red>Close</color>", "<color:red>Close</color>",
"<color:red>Click to close this dialog!</color>", "<color:red>Click to close this dialog!</color>",
"close_dialog" "close_dialog",
""
) )
) )
); );