fancydialogs: Add dialog action system

This commit is contained in:
Oliver
2025-06-19 12:55:09 +02:00
parent d61b3af90b
commit d3850310ce
5 changed files with 78 additions and 14 deletions

View File

@@ -1,5 +1,6 @@
package com.fancyinnovations.fancydialogs; package com.fancyinnovations.fancydialogs;
import com.fancyinnovations.fancydialogs.actions.ActionRegistry;
import com.fancyinnovations.fancydialogs.api.Dialog; import com.fancyinnovations.fancydialogs.api.Dialog;
import com.fancyinnovations.fancydialogs.api.FancyDialogs; import com.fancyinnovations.fancydialogs.api.FancyDialogs;
import com.fancyinnovations.fancydialogs.api.data.DialogData; import com.fancyinnovations.fancydialogs.api.data.DialogData;
@@ -48,6 +49,7 @@ public class FancyDialogsPlugin extends JavaPlugin implements FancyDialogs {
private Translator translator; private Translator translator;
private DialogRegistry dialogRegistry; private DialogRegistry dialogRegistry;
private DialogStorage dialogStorage; private DialogStorage dialogStorage;
private ActionRegistry actionRegistry;
public FancyDialogsPlugin() { public FancyDialogsPlugin() {
INSTANCE = this; INSTANCE = this;
@@ -108,6 +110,8 @@ public class FancyDialogsPlugin extends JavaPlugin implements FancyDialogs {
DefaultDialogs.registerDefaultDialogs(); DefaultDialogs.registerDefaultDialogs();
actionRegistry = new ActionRegistry();
fancyLogger.info("Successfully loaded FancyDialogs version %s".formatted(getDescription().getVersion())); fancyLogger.info("Successfully loaded FancyDialogs version %s".formatted(getDescription().getVersion()));
} }
@@ -202,4 +206,8 @@ public class FancyDialogsPlugin extends JavaPlugin implements FancyDialogs {
public DialogStorage getDialogStorage() { public DialogStorage getDialogStorage() {
return dialogStorage; return dialogStorage;
} }
public ActionRegistry getActionRegistry() {
return actionRegistry;
}
} }

View File

@@ -0,0 +1,31 @@
package com.fancyinnovations.fancydialogs.actions;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class ActionRegistry {
private final Map<String, DialogAction> actions;
public ActionRegistry() {
this.actions = new ConcurrentHashMap<>();
registerDefaultActions();
}
private void registerDefaultActions() {
}
public void registerAction(String actionId, DialogAction action) {
if (actions.containsKey(actionId)) {
throw new IllegalArgumentException("Action with ID " + actionId + " is already registered.");
}
actions.put(actionId, action);
}
public DialogAction getAction(String actionId) {
return actions.get(actionId);
}
}

View File

@@ -0,0 +1,10 @@
package com.fancyinnovations.fancydialogs.actions;
import com.fancyinnovations.fancydialogs.api.Dialog;
import org.bukkit.entity.Player;
public interface DialogAction {
void execute(Player player, Dialog dialog, String data);
}

View File

@@ -1,5 +1,8 @@
package com.fancyinnovations.fancydialogs.listener; package com.fancyinnovations.fancydialogs.listener;
import com.fancyinnovations.fancydialogs.FancyDialogsPlugin;
import com.fancyinnovations.fancydialogs.actions.DialogAction;
import com.fancyinnovations.fancydialogs.api.Dialog;
import de.oliver.fancysitula.api.packets.FS_ServerboundCustomClickActionPacket; import de.oliver.fancysitula.api.packets.FS_ServerboundCustomClickActionPacket;
import de.oliver.fancysitula.api.packets.FS_ServerboundPacket; import de.oliver.fancysitula.api.packets.FS_ServerboundPacket;
import de.oliver.fancysitula.api.utils.FS_PacketListener; import de.oliver.fancysitula.api.utils.FS_PacketListener;
@@ -36,7 +39,19 @@ public class CustomClickActionPacketListener {
String actionId = packet.getPayload().get("action_id"); String actionId = packet.getPayload().get("action_id");
String actionData = packet.getPayload().get("action_data"); String actionData = packet.getPayload().get("action_data");
// TODO process the dialog action 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
}
DialogAction action = FancyDialogsPlugin.get().getActionRegistry().getAction(actionId);
if (action == null) {
FancyDialogsPlugin.get().getFancyLogger().warn("Received unknown action: " + actionId);
return; // Ignore unknown actions
}
action.execute(event.player(), dialog, actionData);
} }
public FS_PacketListener getPacketListener() { public FS_PacketListener getPacketListener() {

View File

@@ -44,13 +44,13 @@ 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",
"" ""
), ),
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:", "console_command",
"give {player} apple 1" "give {player} apple 1"
) )
) )
@@ -74,26 +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", "open_dialog",
"<click:open_url:'{LINK TO RULES}'>Visit our rules</click>!" "rules"
), ),
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",
"" ""
), ),
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", "message",
"<click:open_url:'{LINK TO DISC SERVER}'>Join our Discord server</click>!" "Join our Discord server here: LINK TO DISCORD"
), ),
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", "message",
"<click:open_url:'{LINK TO WEBSITE}'>Visit our website</click>!" "Visit our website here: LINK TO WEBSITE"
) )
) )
); );
@@ -114,30 +114,30 @@ public class DefaultDialogs {
"<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", "message",
"<click:open_url:'{LINK TO WEBSITE}'>Visit our website</click>!" "Visit our website here: LINK TO WEBSITE"
), ),
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", "open_dialog",
"<click:open_url:'{LINK TO RULES}'>Visit our rules</click>!" "rules"
), ),
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", "message",
"<click:open_url:'{LINK TO DISCORD}'>Join our Discord server</click>!" "Join our Discord server here: LINK TO DISCORD"
), ),
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", "message",
"<click:open_url:'{LINK TO SUPPORT}'>Support us</click>!" "Support us here: LINK TO SUPPORT"
), ),
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",
"" ""
) )
) )