fancydialogs: Add open_random_dialog action

This commit is contained in:
Oliver
2025-06-28 19:01:01 +02:00
parent fa90dde62f
commit f08fa67a84
5 changed files with 49 additions and 1 deletions

View File

@@ -5,6 +5,10 @@ order: 1
# FancyDialogs v0.x.x
## v0.0.9 [!badge variant="info" text="2025-06-28"]
- Added `open_random_dialog` action to open a random dialog from a list
## v0.0.8 [!badge variant="info" text="2025-06-28"]
- Fixed permission for dialog command

View File

@@ -127,4 +127,5 @@ Available actions include:
- `console_command`: Executes a command as the console (set `data` to the command)
- `player_command`: Executes a command as the player (set `data` to the command)
- `open_dialog`: Opens another dialog (set `data` to the ID of the dialog to open)
- `open_random_dialog`: Opens another dialog (set `data` to a list of dialog IDs separated by commas: 'dialog1,dialog2,dialog3')
- `send_to_server`: Sends the player to another server (requires BungeeCord or Velocity) (set `data` to the server name)

View File

@@ -1 +1 @@
0.0.8
0.0.9

View File

@@ -19,6 +19,7 @@ public class ActionRegistryImpl implements DialogActionRegistry {
private void registerDefaultActions() {
registerAction("open_dialog", OpenDialogDialogAction.INSTANCE);
registerAction("open_random_dialog", OpenRandomDialogDialogAction.INSTANCE);
registerAction("message", MessageDialogAction.INSTANCE);
registerAction("console_command", ConsoleCommandDialogAction.INSTANCE);
registerAction("player_command", PlayerCommandDialogAction.INSTANCE);

View File

@@ -0,0 +1,42 @@
package com.fancyinnovations.fancydialogs.actions.defaultActions;
import com.fancyinnovations.fancydialogs.FancyDialogsPlugin;
import com.fancyinnovations.fancydialogs.api.Dialog;
import com.fancyinnovations.fancydialogs.api.DialogAction;
import org.bukkit.entity.Player;
public class OpenRandomDialogDialogAction implements DialogAction {
public static final OpenRandomDialogDialogAction INSTANCE = new OpenRandomDialogDialogAction();
private OpenRandomDialogDialogAction() {
}
private static String pickRandomDialogId(String[] ids) {
int randomIndex = (int) (Math.random() * ids.length);
return ids[randomIndex].trim();
}
@Override
public void execute(Player player, Dialog dialog, String data) {
if (data == null || data.isEmpty()) {
return;
}
String[] ids = data.split(",");
if (ids.length == 0) {
FancyDialogsPlugin.get().getFancyLogger().warn("No dialog IDs provided in data: " + data);
return;
}
String randomDialogId = pickRandomDialogId(ids);
Dialog targetDialog = FancyDialogsPlugin.get().getDialogRegistry().get(randomDialogId);
if (targetDialog == null) {
FancyDialogsPlugin.get().getFancyLogger().warn("Dialog with ID '" + data + "' not found.");
return;
}
targetDialog.open(player);
}
}