Added ability to hide command rank commands by Ops.

This commit is contained in:
tastybento 2024-02-03 11:32:28 -08:00
parent 9acb79fc60
commit 43db8d346c
2 changed files with 33 additions and 7 deletions

View File

@ -5,6 +5,7 @@ import org.bukkit.World;
import org.bukkit.event.inventory.ClickType;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.panels.Panel;
import world.bentobox.bentobox.api.panels.PanelItem.ClickHandler;
import world.bentobox.bentobox.api.user.User;
@ -18,6 +19,7 @@ import world.bentobox.bentobox.managers.RanksManager;
*/
public class CommandCycleClick implements ClickHandler {
protected static final String COMMAND_RANK_PREFIX = "COMMAND_RANK:";
private final BentoBox plugin = BentoBox.getInstance();
private final String command;
private final CommandRankClickListener commandRankClickListener;
@ -53,6 +55,8 @@ public class CommandCycleClick implements ClickHandler {
island.setRankCommand(command, RanksManager.getInstance().getRankDownValue(currentRank));
}
user.getPlayer().playSound(user.getLocation(), Sound.BLOCK_STONE_BUTTON_CLICK_ON, 1F, 1F);
} else if (click.equals(ClickType.SHIFT_LEFT) && user.isOp()) {
leftShiftClick(user);
}
// Apply change to panel
panel.getInventory().setItem(slot, commandRankClickListener.getPanelItem(command, user, world).getItem());
@ -65,4 +69,22 @@ public class CommandCycleClick implements ClickHandler {
return true;
}
/**
* Adds or removes the command rank from visibility by non-Ops
* @param user - the Op requesting the change
*/
private void leftShiftClick(User user) {
String configSetting = COMMAND_RANK_PREFIX + command;
if (!plugin.getIWM().getHiddenFlags(user.getWorld()).contains(configSetting)) {
plugin.getIWM().getHiddenFlags(user.getWorld()).add(configSetting);
user.getPlayer().playSound(user.getLocation(), Sound.BLOCK_GLASS_BREAK, 1F, 1F);
} else {
plugin.getIWM().getHiddenFlags(user.getWorld()).remove(configSetting);
user.getPlayer().playSound(user.getLocation(), Sound.BLOCK_NOTE_BLOCK_CHIME, 1F, 1F);
}
// Save changes
plugin.getIWM().getAddon(user.getWorld()).ifPresent(GameModeAddon::saveWorldSettings);
}
}

View File

@ -77,7 +77,7 @@ public class CommandRankClickListener implements ClickHandler {
if (panel.getName().equals(panelName)) {
// This is a click on the panel
// Slot relates to the command
String c = getCommands(panel.getWorld().orElse(user.getWorld())).get(slot);
String c = getCommands(panel.getWorld().orElse(user.getWorld()), user).get(slot);
// Apply change to panel
panel.getInventory().setItem(slot, getPanelItem(c, user, panel.getWorld().orElse(user.getWorld())).getItem());
} else {
@ -94,7 +94,7 @@ public class CommandRankClickListener implements ClickHandler {
PanelBuilder pb = new PanelBuilder();
pb.user(user).name(panelName).world(world);
// Make panel items
getCommands(world).forEach(c -> pb.item(getPanelItem(c, user, world)));
getCommands(world, user).forEach(c -> pb.item(getPanelItem(c, user, world)));
pb.build();
}
@ -123,14 +123,18 @@ public class CommandRankClickListener implements ClickHandler {
pib.description(user.getTranslation("protection.panel.flag-item.minimal-rank") + user.getTranslation(reference));
}
});
pib.invisible(plugin.getIWM().getHiddenFlags(world).contains(CommandCycleClick.COMMAND_RANK_PREFIX + c));
return pib.build();
}
private List<String> getCommands(World world) {
List<String> result = new ArrayList<>();
plugin.getCommandsManager().getCommands().values().stream()
.filter(c -> c.getWorld() != null && c.getWorld().equals(world))
.forEach(c -> result.addAll(getCmdRecursively("/", c)));
private List<String> getCommands(World world, User user) {
List<String> hiddenItems = plugin.getIWM().getHiddenFlags(world);
List<String> result = plugin.getCommandsManager().getCommands().values().stream()
.filter(c -> c.getWorld() != null && c.getWorld().equals(world)) // Only allow commands in this world
.filter(c -> c.testPermission(user.getSender())) // Only allow them to see commands they have permission to see
.flatMap(c -> getCmdRecursively("/", c).stream())
.filter(label -> user.isOp() || !hiddenItems.contains(CommandCycleClick.COMMAND_RANK_PREFIX + label))
.toList();
if (result.size() > 49) {
plugin.logError("Number of rank setting commands is too big for GUI");
result.subList(49, result.size()).clear();