diff --git a/src/main/java/world/bentobox/controlpanel/database/objects/ControlPanelObject.java b/src/main/java/world/bentobox/controlpanel/database/objects/ControlPanelObject.java index 6c4c8f4..08fc904 100644 --- a/src/main/java/world/bentobox/controlpanel/database/objects/ControlPanelObject.java +++ b/src/main/java/world/bentobox/controlpanel/database/objects/ControlPanelObject.java @@ -304,6 +304,48 @@ public void setCommand(String command) } + /** + * Method ControlPanelButton#getRightClickCommand returns the rightClickCommand of this object. + * + * @return the rightClickCommand (type String) of this object. + */ + public String getRightClickCommand() + { + return rightClickCommand; + } + + + /** + * Method ControlPanelButton#setRightClickCommand sets new value for the rightClickCommand of this object. + * @param rightClickCommand new value for this object. + */ + public void setRightClickCommand(String rightClickCommand) + { + this.rightClickCommand = rightClickCommand; + } + + + /** + * Method ControlPanelButton#getShiftClickCommand returns the shiftClickCommand of this object. + * + * @return the shiftClickCommand (type String) of this object. + */ + public String getShiftClickCommand() + { + return shiftClickCommand; + } + + + /** + * Method ControlPanelButton#setShiftClickCommand sets new value for the shiftClickCommand of this object. + * @param shiftClickCommand new value for this object. + */ + public void setShiftClickCommand(String shiftClickCommand) + { + this.shiftClickCommand = shiftClickCommand; + } + + /** * Method ControlPanelButton#getName returns the name of this object. * @@ -388,6 +430,18 @@ public void setDescriptionLines(List descriptionLines) @Expose private String command; + /** + * Command that will run on right click. + */ + @Expose + private String rightClickCommand; + + /** + * Command that will run on shift+left click. + */ + @Expose + private String shiftClickCommand; + /** * Name of the Button. */ diff --git a/src/main/java/world/bentobox/controlpanel/managers/ControlPanelManager.java b/src/main/java/world/bentobox/controlpanel/managers/ControlPanelManager.java index af25886..e7cc2f0 100644 --- a/src/main/java/world/bentobox/controlpanel/managers/ControlPanelManager.java +++ b/src/main/java/world/bentobox/controlpanel/managers/ControlPanelManager.java @@ -367,6 +367,8 @@ private void readControlPanel(YamlConfiguration config, @Nullable User user, fin { button.setName(buttonSection.getString("name")); button.setCommand(buttonSection.getString("command", "[user_command]")); + button.setRightClickCommand(buttonSection.getString("right_click_command")); + button.setShiftClickCommand(buttonSection.getString("shift_click_command")); // Create empty list button.setDescriptionLines(new ArrayList<>()); diff --git a/src/main/java/world/bentobox/controlpanel/panels/ControlPanelGenerator.java b/src/main/java/world/bentobox/controlpanel/panels/ControlPanelGenerator.java index 0dd7932..2e0890c 100644 --- a/src/main/java/world/bentobox/controlpanel/panels/ControlPanelGenerator.java +++ b/src/main/java/world/bentobox/controlpanel/panels/ControlPanelGenerator.java @@ -8,6 +8,7 @@ import org.bukkit.Material; +import org.bukkit.event.inventory.ClickType; import org.bukkit.inventory.ItemStack; import java.util.ArrayList; import java.util.Comparator; @@ -120,7 +121,14 @@ private PanelItem generateButton(ControlPanelButton button) icon(button.getIcon() == null ? new ItemStack(button.getMaterial() == null ? Material.PAPER : button.getMaterial()) : button.getIcon()). description(description). clickHandler((panel, user, clickType, slot) -> { - final String parsedCommand = button.getCommand(). + final String rawCommand = this.getCommandForClickType(button, clickType); + + if (rawCommand == null || rawCommand.isEmpty()) + { + return true; + } + + final String parsedCommand = rawCommand. replace("[label]", this.topLabel). replace("[server]", ""). replace("[player]", user.getName()). @@ -128,7 +136,7 @@ private PanelItem generateButton(ControlPanelButton button) if (!parsedCommand.isEmpty()) { - if (button.getCommand().startsWith("[server]")) + if (rawCommand.startsWith("[server]")) { if (!this.addon.getServer().dispatchCommand( this.addon.getServer().getConsoleSender(), @@ -154,6 +162,42 @@ private PanelItem generateButton(ControlPanelButton button) } + /** + * This method returns the command string for a given click type. + * If a specific click type command is not defined, falls back to the default command. + * @param button ControlPanelButton that contains command definitions. + * @param clickType The type of click performed. + * @return The command string to execute, or null if no command is defined. + */ + private String getCommandForClickType(ControlPanelButton button, ClickType clickType) + { + switch (clickType) + { + case RIGHT: + case SHIFT_RIGHT: + if (button.getRightClickCommand() != null && !button.getRightClickCommand().isEmpty()) + { + return button.getRightClickCommand(); + } + + break; + + case SHIFT_LEFT: + if (button.getShiftClickCommand() != null && !button.getShiftClickCommand().isEmpty()) + { + return button.getShiftClickCommand(); + } + + break; + + default: + break; + } + + return button.getCommand(); + } + + // --------------------------------------------------------------------- // Section: Variables // --------------------------------------------------------------------- diff --git a/src/main/resources/controlPanelTemplate.yml b/src/main/resources/controlPanelTemplate.yml index 912e844..58347ae 100644 --- a/src/main/resources/controlPanelTemplate.yml +++ b/src/main/resources/controlPanelTemplate.yml @@ -5,6 +5,10 @@ # [server] - Command will be run by console, f.e. '[server] op [player]' will result in '/op BONNe1704' # [label] - [label] in command will be replaced with corresponding GameMode user command, # f.e. '[label] challenges' in BSkyblock will result in 'island challenges' +# Command types: +# command - Executed on left click (default click action) +# right_click_command - Executed on right click or shift+right click. Falls back to 'command' if not set. +# shift_click_command - Executed on shift+left click. Falls back to 'command' if not set. # material is used from Material.match # icon is used by parsing BentoBox ItemParser. Replacement for material. # permission is a suffix that will be added to the end of "[gamemode].controlpanel.panel.[suffix]". diff --git a/src/test/java/world/bentobox/controlpanel/database/objects/ControlPanelObjectTest.java b/src/test/java/world/bentobox/controlpanel/database/objects/ControlPanelObjectTest.java index c6beb6c..624f54c 100644 --- a/src/test/java/world/bentobox/controlpanel/database/objects/ControlPanelObjectTest.java +++ b/src/test/java/world/bentobox/controlpanel/database/objects/ControlPanelObjectTest.java @@ -91,6 +91,14 @@ void testControlPanelButton() { button.setCommand("island go"); assertEquals("island go", button.getCommand()); + assertNull(button.getRightClickCommand()); + button.setRightClickCommand("island settings"); + assertEquals("island settings", button.getRightClickCommand()); + + assertNull(button.getShiftClickCommand()); + button.setShiftClickCommand("island team"); + assertEquals("island team", button.getShiftClickCommand()); + assertNull(button.getDescriptionLines()); List desc = new ArrayList<>(); desc.add("Line 1");