fancydialogs: Add support for multiple actions

This commit is contained in:
Oliver
2025-06-19 14:36:23 +02:00
parent bd4a8f2f23
commit b8f57fda38
7 changed files with 96 additions and 46 deletions

View File

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

View File

@@ -1,4 +0,0 @@
package com.fancyinnovations.fancydialogs.api.data;
public abstract class DialogAction {
}

View File

@@ -1,9 +1,43 @@
package com.fancyinnovations.fancydialogs.api.data;
public record DialogButton(
String label,
String tooltip,
String action,
String actionData
) {
import java.util.List;
import java.util.UUID;
public class DialogButton {
private final transient String id;
private final String label;
private final String tooltip;
private final List<DialogAction> actions;
public DialogButton(String label, String tooltip, List<DialogAction> actions) {
this.id = UUID.randomUUID().toString();
this.label = label;
this.tooltip = tooltip;
this.actions = actions;
}
public String id() {
return id;
}
public String label() {
return label;
}
public String tooltip() {
return tooltip;
}
public List<DialogAction> actions() {
return actions;
}
public record DialogAction(
String name,
String data
) {
}
}

View File

@@ -12,4 +12,14 @@ public record DialogData(
@NotNull List<DialogButton> buttons
) {
public DialogButton getButtonById(@NotNull String buttonId) {
for (DialogButton button : buttons) {
if (button.id().equals(buttonId)) {
return button;
}
}
return null;
}
}

View File

@@ -51,8 +51,7 @@ public class DialogImpl extends Dialog {
"fancydialogs_dialog_action",
Map.of(
"dialog_id", id,
"action_id", button.action(),
"action_data", button.actionData()
"button_id", button.id()
)
)
);

View File

@@ -3,6 +3,7 @@ package com.fancyinnovations.fancydialogs.listener;
import com.fancyinnovations.fancydialogs.FancyDialogsPlugin;
import com.fancyinnovations.fancydialogs.actions.DialogAction;
import com.fancyinnovations.fancydialogs.api.Dialog;
import com.fancyinnovations.fancydialogs.api.data.DialogButton;
import de.oliver.fancysitula.api.packets.FS_ServerboundCustomClickActionPacket;
import de.oliver.fancysitula.api.packets.FS_ServerboundPacket;
import de.oliver.fancysitula.api.utils.FS_PacketListener;
@@ -36,22 +37,29 @@ public class CustomClickActionPacketListener {
}
String dialogId = packet.getPayload().get("dialog_id");
String actionId = packet.getPayload().get("action_id");
String actionData = packet.getPayload().get("action_data");
String buttonId = packet.getPayload().get("button_id");
Dialog dialog = FancyDialogsPlugin.get().getDialogRegistry().get(dialogId);
if (dialog == null) {
FancyDialogsPlugin.get().getFancyLogger().warn("Received action for unknown dialog: " + dialogId);
return; // Ignore actions for unknown dialogs
return;
}
DialogAction action = FancyDialogsPlugin.get().getActionRegistry().getAction(actionId);
if (action == null) {
FancyDialogsPlugin.get().getFancyLogger().warn("Received unknown action: " + actionId);
return; // Ignore unknown actions
DialogButton btn = dialog.getData().getButtonById(buttonId);
if (btn == null) {
FancyDialogsPlugin.get().getFancyLogger().warn("Received action for unknown button: " + buttonId + " in dialog: " + dialogId);
return;
}
action.execute(event.player(), dialog, actionData);
for (DialogButton.DialogAction btnAction : btn.actions()) {
DialogAction action = FancyDialogsPlugin.get().getActionRegistry().getAction(btnAction.name());
if (action == null) {
FancyDialogsPlugin.get().getFancyLogger().warn("Received action for unknown action: " + btnAction.name() + " in button: " + buttonId);
continue;
}
action.execute(event.player(), dialog, btnAction.data());
}
}
public FS_PacketListener getPacketListener() {

View File

@@ -44,14 +44,14 @@ public class DefaultDialogs {
new DialogButton(
"<color:#ff4f19>Close</color>",
"<color:#ff4f19>Enjoy using FancyDialogs</color>",
"close",
""
List.of()
),
new DialogButton(
"<color:#ffd000>Run command</color>",
"<color:#ff4f19>Click to give yourself an apple :)</color>",
"console_command",
"give {player} apple 1"
List.of(
new DialogButton.DialogAction("console_command", "give @p minecraft:apple 1")
)
)
)
);
@@ -74,26 +74,28 @@ public class DefaultDialogs {
new DialogButton(
"<color:red>Read the rules</color>",
"<color:red>Click to read our rules!</color>",
"open_dialog",
"rules"
List.of(
new DialogButton.DialogAction("open_dialog", "rules")
)
),
new DialogButton(
"<color:#00ff5e>Start playing</color>",
"<color:#00ff5e>Click to start playing!</color>",
"close",
""
List.of()
),
new DialogButton(
"<color:#1787ff>Join our Discord</color>",
"<color:#1787ff>Click to join our Discord server!</color>",
"message",
"Join our Discord server here: LINK TO DISCORD"
List.of(
new DialogButton.DialogAction("message", "Join our Discord server here: LINK TO DISCORD")
)
),
new DialogButton(
"<color:#ffee00>Visit our website</color>",
"<color:#ffee00>Click to visit our website!</color>",
"message",
"Visit our website here: LINK TO WEBSITE"
List.of(
new DialogButton.DialogAction("message", "Visit our website here: LINK TO WEBSITE")
)
)
)
);
@@ -113,32 +115,35 @@ public class DefaultDialogs {
new DialogButton(
"<color:#ffee00>Visit our website</color>",
"<color:#ffee00>Click to visit our website!</color>",
"message",
"Visit our website here: LINK TO WEBSITE"
List.of(
new DialogButton.DialogAction("message", "Visit our website here: LINK TO WEBSITE")
)
),
new DialogButton(
"<color:#ffee00>Read the rules</color>",
"<color:#ffee00>Click to read our rules!</color>",
"open_dialog",
"rules"
List.of(
new DialogButton.DialogAction("open_dialog", "rules")
)
),
new DialogButton(
"<color:#ffee00>Join our Discord</color>",
"<color:#ffee00>Click to join our Discord server!</color>",
"message",
"Join our Discord server here: LINK TO DISCORD"
List.of(
new DialogButton.DialogAction("message", "Join our Discord server here: LINK TO DISCORD")
)
),
new DialogButton(
"<color:#ffee00>Support us</color>",
"<color:#ffee00>Click to support us!</color>",
"message",
"Support us here: LINK TO SUPPORT"
List.of(
new DialogButton.DialogAction("message", "Support us by donating here: LINK TO DONATE")
)
),
new DialogButton(
"<color:red>Close</color>",
"<color:red>Click to close this dialog!</color>",
"close",
""
List.of()
)
)
);