From cf13130713afbf57569e92328ac0e9250535b15a Mon Sep 17 00:00:00 2001 From: BlitzOffline <52609756+BlitzOffline@users.noreply.github.com> Date: Sun, 26 May 2024 00:17:36 +0300 Subject: [PATCH] Added `parse_placeholders_after_arguments` menu option --- .../deluxemenus/action/ClickActionTask.java | 12 ++++++++++-- .../deluxemenus/commands/DeluxeMenusCommands.java | 1 + .../deluxemenus/config/DeluxeMenusConfig.java | 10 ++++------ .../com/extendedclip/deluxemenus/menu/Menu.java | 1 + .../extendedclip/deluxemenus/menu/MenuHolder.java | 12 ++++++++++++ .../deluxemenus/menu/options/MenuOptions.java | 13 +++++++++++++ .../extendedclip/deluxemenus/utils/StringUtils.java | 8 +++++++- 7 files changed, 48 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/extendedclip/deluxemenus/action/ClickActionTask.java b/src/main/java/com/extendedclip/deluxemenus/action/ClickActionTask.java index b2aef50..2814927 100644 --- a/src/main/java/com/extendedclip/deluxemenus/action/ClickActionTask.java +++ b/src/main/java/com/extendedclip/deluxemenus/action/ClickActionTask.java @@ -33,6 +33,7 @@ public class ClickActionTask extends BukkitRunnable { // Ugly hack to get around the fact that arguments are not available at task execution time private final Map arguments; private final boolean parsePlaceholdersInArguments; + private final boolean parsePlaceholdersAfterArguments; public ClickActionTask( @NotNull final DeluxeMenus plugin, @@ -40,7 +41,8 @@ public ClickActionTask( @NotNull final ActionType actionType, @NotNull final String exec, @NotNull final Map arguments, - final boolean parsePlaceholdersInArguments + final boolean parsePlaceholdersInArguments, + final boolean parsePlaceholdersAfterArguments ) { this.plugin = plugin; this.uuid = uuid; @@ -48,6 +50,7 @@ public ClickActionTask( this.exec = exec; this.arguments = arguments; this.parsePlaceholdersInArguments = parsePlaceholdersInArguments; + this.parsePlaceholdersAfterArguments = parsePlaceholdersAfterArguments; } @Override @@ -58,7 +61,12 @@ public void run() { } final Optional holder = Menu.getMenuHolder(player); - final String executable = StringUtils.replacePlaceholdersAndArguments(this.exec, this.arguments, player, this.parsePlaceholdersInArguments); + final String executable = StringUtils.replacePlaceholdersAndArguments( + this.exec, + this.arguments, + player, + this.parsePlaceholdersInArguments, + this.parsePlaceholdersAfterArguments); switch (actionType) { diff --git a/src/main/java/com/extendedclip/deluxemenus/commands/DeluxeMenusCommands.java b/src/main/java/com/extendedclip/deluxemenus/commands/DeluxeMenusCommands.java index feea319..e7d1b80 100644 --- a/src/main/java/com/extendedclip/deluxemenus/commands/DeluxeMenusCommands.java +++ b/src/main/java/com/extendedclip/deluxemenus/commands/DeluxeMenusCommands.java @@ -182,6 +182,7 @@ public boolean onCommand( action.getType(), action.getExecutable(), holder.getTypedArgs(), + true, true ); diff --git a/src/main/java/com/extendedclip/deluxemenus/config/DeluxeMenusConfig.java b/src/main/java/com/extendedclip/deluxemenus/config/DeluxeMenusConfig.java index a6bd348..038747a 100644 --- a/src/main/java/com/extendedclip/deluxemenus/config/DeluxeMenusConfig.java +++ b/src/main/java/com/extendedclip/deluxemenus/config/DeluxeMenusConfig.java @@ -666,11 +666,8 @@ public void loadMenu(FileConfiguration c, String key, boolean mainConfig) { return; } - final boolean argumentsSupportPlaceholders = c.getBoolean(pre + "arguments_support_placeholders", false); - - if (argumentsSupportPlaceholders) { - builder.parsePlaceholdersInArguments(argumentsSupportPlaceholders); - } + builder.parsePlaceholdersInArguments(c.getBoolean(pre + "arguments_support_placeholders", false)); + builder.parsePlaceholdersAfterArguments(c.getBoolean(pre + "parse_placeholders_after_arguments", false)); // Don't need to register the menu since it's done in the constructor new Menu(builder.build(), items); @@ -1489,7 +1486,8 @@ public void onClick(@NotNull final MenuHolder holder) { action.getType(), action.getExecutable(), holder.getTypedArgs(), - holder.parsePlaceholdersInArguments() + holder.parsePlaceholdersInArguments(), + holder.parsePlaceholdersAfterArguments() ); if (action.hasDelay()) { diff --git a/src/main/java/com/extendedclip/deluxemenus/menu/Menu.java b/src/main/java/com/extendedclip/deluxemenus/menu/Menu.java index 005485b..bc1d201 100644 --- a/src/main/java/com/extendedclip/deluxemenus/menu/Menu.java +++ b/src/main/java/com/extendedclip/deluxemenus/menu/Menu.java @@ -337,6 +337,7 @@ public void openMenu(final @NotNull Player viewer, final @Nullable Map typedArgs; public MenuHolder(Player viewer) { @@ -91,6 +92,9 @@ public Optional getMenu() { } public @NotNull String setPlaceholdersAndArguments(final @NotNull String string) { + if (parsePlaceholdersAfterArguments) { + return setPlaceholders(setArguments(string)); + } return setArguments(setPlaceholders(string)); } @@ -318,10 +322,18 @@ public void parsePlaceholdersInArguments(final boolean parsePlaceholdersInArgume this.parsePlaceholdersInArguments = parsePlaceholdersInArguments; } + public void parsePlaceholdersAfterArguments(final boolean parsePlaceholdersAfterArguments) { + this.parsePlaceholdersAfterArguments = parsePlaceholdersAfterArguments; + } + public boolean parsePlaceholdersInArguments() { return parsePlaceholdersInArguments; } + public boolean parsePlaceholdersAfterArguments() { + return parsePlaceholdersAfterArguments; + } + public void setPlaceholderPlayer(Player placeholderPlayer) { this.placeholderPlayer = placeholderPlayer; } diff --git a/src/main/java/com/extendedclip/deluxemenus/menu/options/MenuOptions.java b/src/main/java/com/extendedclip/deluxemenus/menu/options/MenuOptions.java index 17ab4bf..fc1af76 100644 --- a/src/main/java/com/extendedclip/deluxemenus/menu/options/MenuOptions.java +++ b/src/main/java/com/extendedclip/deluxemenus/menu/options/MenuOptions.java @@ -17,6 +17,7 @@ public class MenuOptions { private final int size; private final int updateInterval; private final boolean parsePlaceholdersInArguments; + private final boolean parsePlaceholdersAfterArguments; private final List commands; private final boolean registerCommands; @@ -35,6 +36,7 @@ private MenuOptions(final @NotNull MenuOptionsBuilder builder) { this.size = builder.size; this.updateInterval = builder.updateInterval; this.parsePlaceholdersInArguments = builder.parsePlaceholdersInArguments; + this.parsePlaceholdersAfterArguments = builder.parsePlaceholdersAfterArguments; this.commands = builder.commands; this.registerCommands = builder.registerCommands; @@ -75,6 +77,10 @@ public boolean parsePlaceholdersInArguments() { return this.parsePlaceholdersInArguments; } + public boolean parsePlaceholdersAfterArguments() { + return this.parsePlaceholdersAfterArguments; + } + public @NotNull List<@NotNull String> commands() { return this.commands; } @@ -113,6 +119,7 @@ public boolean registerCommands() { .size(this.size) .updateInterval(this.updateInterval) .parsePlaceholdersInArguments(this.parsePlaceholdersInArguments) + .parsePlaceholdersAfterArguments(this.parsePlaceholdersAfterArguments) .commands(this.commands) .registerCommands(this.registerCommands) .arguments(this.arguments) @@ -131,6 +138,7 @@ public static class MenuOptionsBuilder { private int size = 9; private int updateInterval = 10; private boolean parsePlaceholdersInArguments = false; + private boolean parsePlaceholdersAfterArguments = false; private List commands = List.of(); private boolean registerCommands = false; @@ -177,6 +185,11 @@ public MenuOptionsBuilder parsePlaceholdersInArguments(final boolean parsePlaceh return this; } + public MenuOptionsBuilder parsePlaceholdersAfterArguments(final boolean parsePlaceholdersAfterArguments) { + this.parsePlaceholdersAfterArguments = parsePlaceholdersAfterArguments; + return this; + } + public MenuOptionsBuilder commands(final @NotNull List<@NotNull String> commands) { this.commands = commands; return this; diff --git a/src/main/java/com/extendedclip/deluxemenus/utils/StringUtils.java b/src/main/java/com/extendedclip/deluxemenus/utils/StringUtils.java index ba677c7..0304c7e 100644 --- a/src/main/java/com/extendedclip/deluxemenus/utils/StringUtils.java +++ b/src/main/java/com/extendedclip/deluxemenus/utils/StringUtils.java @@ -38,11 +38,17 @@ public static String color(@NotNull String input) { @NotNull public static String replacePlaceholdersAndArguments(@NotNull String input, final @Nullable Map arguments, - final @Nullable Player player, boolean parsePlaceholdersInsideArguments) { + final @Nullable Player player, + final boolean parsePlaceholdersInsideArguments, + final boolean parsePlaceholdersAfterArguments) { if (player == null) { return replaceArguments(input, arguments, null, parsePlaceholdersInsideArguments); } + if (parsePlaceholdersAfterArguments) { + return replacePlaceholders(replaceArguments(input, arguments, player, parsePlaceholdersInsideArguments), player); + } + return replaceArguments(replacePlaceholders(input, player), arguments, player, parsePlaceholdersInsideArguments); }