Skip to content

Commit 3705bb6

Browse files
authored
Merge pull request #35 from BentoBoxWorld/copilot/add-new-command-styles
Add right-click and shift-click command support for panel buttons
2 parents 00eb108 + 95e8474 commit 3705bb6

5 files changed

Lines changed: 114 additions & 2 deletions

File tree

src/main/java/world/bentobox/controlpanel/database/objects/ControlPanelObject.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,48 @@ public void setCommand(String command)
304304
}
305305

306306

307+
/**
308+
* Method ControlPanelButton#getRightClickCommand returns the rightClickCommand of this object.
309+
*
310+
* @return the rightClickCommand (type String) of this object.
311+
*/
312+
public String getRightClickCommand()
313+
{
314+
return rightClickCommand;
315+
}
316+
317+
318+
/**
319+
* Method ControlPanelButton#setRightClickCommand sets new value for the rightClickCommand of this object.
320+
* @param rightClickCommand new value for this object.
321+
*/
322+
public void setRightClickCommand(String rightClickCommand)
323+
{
324+
this.rightClickCommand = rightClickCommand;
325+
}
326+
327+
328+
/**
329+
* Method ControlPanelButton#getShiftClickCommand returns the shiftClickCommand of this object.
330+
*
331+
* @return the shiftClickCommand (type String) of this object.
332+
*/
333+
public String getShiftClickCommand()
334+
{
335+
return shiftClickCommand;
336+
}
337+
338+
339+
/**
340+
* Method ControlPanelButton#setShiftClickCommand sets new value for the shiftClickCommand of this object.
341+
* @param shiftClickCommand new value for this object.
342+
*/
343+
public void setShiftClickCommand(String shiftClickCommand)
344+
{
345+
this.shiftClickCommand = shiftClickCommand;
346+
}
347+
348+
307349
/**
308350
* Method ControlPanelButton#getName returns the name of this object.
309351
*
@@ -388,6 +430,18 @@ public void setDescriptionLines(List<String> descriptionLines)
388430
@Expose
389431
private String command;
390432

433+
/**
434+
* Command that will run on right click.
435+
*/
436+
@Expose
437+
private String rightClickCommand;
438+
439+
/**
440+
* Command that will run on shift+left click.
441+
*/
442+
@Expose
443+
private String shiftClickCommand;
444+
391445
/**
392446
* Name of the Button.
393447
*/

src/main/java/world/bentobox/controlpanel/managers/ControlPanelManager.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,8 @@ private void readControlPanel(YamlConfiguration config, @Nullable User user, fin
367367
{
368368
button.setName(buttonSection.getString("name"));
369369
button.setCommand(buttonSection.getString("command", "[user_command]"));
370+
button.setRightClickCommand(buttonSection.getString("right_click_command"));
371+
button.setShiftClickCommand(buttonSection.getString("shift_click_command"));
370372

371373
// Create empty list
372374
button.setDescriptionLines(new ArrayList<>());

src/main/java/world/bentobox/controlpanel/panels/ControlPanelGenerator.java

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99

1010
import org.bukkit.Material;
11+
import org.bukkit.event.inventory.ClickType;
1112
import org.bukkit.inventory.ItemStack;
1213
import java.util.ArrayList;
1314
import java.util.Comparator;
@@ -120,15 +121,22 @@ private PanelItem generateButton(ControlPanelButton button)
120121
icon(button.getIcon() == null ? new ItemStack(button.getMaterial() == null ? Material.PAPER : button.getMaterial()) : button.getIcon()).
121122
description(description).
122123
clickHandler((panel, user, clickType, slot) -> {
123-
final String parsedCommand = button.getCommand().
124+
final String rawCommand = this.getCommandForClickType(button, clickType);
125+
126+
if (rawCommand == null || rawCommand.isEmpty())
127+
{
128+
return true;
129+
}
130+
131+
final String parsedCommand = rawCommand.
124132
replace("[label]", this.topLabel).
125133
replace("[server]", "").
126134
replace("[player]", user.getName()).
127135
trim();
128136

129137
if (!parsedCommand.isEmpty())
130138
{
131-
if (button.getCommand().startsWith("[server]"))
139+
if (rawCommand.startsWith("[server]"))
132140
{
133141
if (!this.addon.getServer().dispatchCommand(
134142
this.addon.getServer().getConsoleSender(),
@@ -154,6 +162,42 @@ private PanelItem generateButton(ControlPanelButton button)
154162
}
155163

156164

165+
/**
166+
* This method returns the command string for a given click type.
167+
* If a specific click type command is not defined, falls back to the default command.
168+
* @param button ControlPanelButton that contains command definitions.
169+
* @param clickType The type of click performed.
170+
* @return The command string to execute, or null if no command is defined.
171+
*/
172+
private String getCommandForClickType(ControlPanelButton button, ClickType clickType)
173+
{
174+
switch (clickType)
175+
{
176+
case RIGHT:
177+
case SHIFT_RIGHT:
178+
if (button.getRightClickCommand() != null && !button.getRightClickCommand().isEmpty())
179+
{
180+
return button.getRightClickCommand();
181+
}
182+
183+
break;
184+
185+
case SHIFT_LEFT:
186+
if (button.getShiftClickCommand() != null && !button.getShiftClickCommand().isEmpty())
187+
{
188+
return button.getShiftClickCommand();
189+
}
190+
191+
break;
192+
193+
default:
194+
break;
195+
}
196+
197+
return button.getCommand();
198+
}
199+
200+
157201
// ---------------------------------------------------------------------
158202
// Section: Variables
159203
// ---------------------------------------------------------------------

src/main/resources/controlPanelTemplate.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
# [server] - Command will be run by console, f.e. '[server] op [player]' will result in '/op BONNe1704'
66
# [label] - [label] in command will be replaced with corresponding GameMode user command,
77
# f.e. '[label] challenges' in BSkyblock will result in 'island challenges'
8+
# Command types:
9+
# command - Executed on left click (default click action)
10+
# right_click_command - Executed on right click or shift+right click. Falls back to 'command' if not set.
11+
# shift_click_command - Executed on shift+left click. Falls back to 'command' if not set.
812
# material is used from Material.match
913
# icon is used by parsing BentoBox ItemParser. Replacement for material.
1014
# permission is a suffix that will be added to the end of "[gamemode].controlpanel.panel.[suffix]".

src/test/java/world/bentobox/controlpanel/database/objects/ControlPanelObjectTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ void testControlPanelButton() {
9191
button.setCommand("island go");
9292
assertEquals("island go", button.getCommand());
9393

94+
assertNull(button.getRightClickCommand());
95+
button.setRightClickCommand("island settings");
96+
assertEquals("island settings", button.getRightClickCommand());
97+
98+
assertNull(button.getShiftClickCommand());
99+
button.setShiftClickCommand("island team");
100+
assertEquals("island team", button.getShiftClickCommand());
101+
94102
assertNull(button.getDescriptionLines());
95103
List<String> desc = new ArrayList<>();
96104
desc.add("Line 1");

0 commit comments

Comments
 (0)