Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -388,6 +430,18 @@ public void setDescriptionLines(List<String> 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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<>());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -120,15 +121,22 @@
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()).
trim();

if (!parsedCommand.isEmpty())
{
if (button.getCommand().startsWith("[server]"))
if (rawCommand.startsWith("[server]"))
{
if (!this.addon.getServer().dispatchCommand(
this.addon.getServer().getConsoleSender(),
Expand All @@ -154,6 +162,42 @@
}


/**
* 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:

Check warning on line 177 in src/main/java/world/bentobox/controlpanel/panels/ControlPanelGenerator.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Merge the previous cases into this one using comma-separated label.

See more on https://sonarcloud.io/project/issues?id=BentoBoxWorld_ControlPanel&issues=AZ1PLfu0P8eqLqF6p9Nx&open=AZ1PLfu0P8eqLqF6p9Nx&pullRequest=35
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
// ---------------------------------------------------------------------
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/controlPanelTemplate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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]".
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> desc = new ArrayList<>();
desc.add("Line 1");
Expand Down
Loading