Merge pull request #2141 from BentoBoxWorld/bbox_perms_command

Bbox admin perms command
This commit is contained in:
tastybento 2023-06-24 10:16:07 -07:00 committed by GitHub
commit 2805e5889b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
37 changed files with 381 additions and 58 deletions

View File

@ -73,10 +73,10 @@
<postgresql.version>42.2.18</postgresql.version> <postgresql.version>42.2.18</postgresql.version>
<hikaricp.version>5.0.1</hikaricp.version> <hikaricp.version>5.0.1</hikaricp.version>
<!-- More visible way to change dependency versions --> <!-- More visible way to change dependency versions -->
<spigot.version>1.20-R0.1-SNAPSHOT</spigot.version> <spigot.version>1.20.1-R0.1-SNAPSHOT</spigot.version>
<!-- Might differ from the last Spigot release for short periods <!-- Might differ from the last Spigot release for short periods
of time --> of time -->
<paper.version>1.20-R0.1-SNAPSHOT</paper.version> <paper.version>1.20.1-R0.1-SNAPSHOT</paper.version>
<bstats.version>3.0.0</bstats.version> <bstats.version>3.0.0</bstats.version>
<vault.version>1.7.1</vault.version> <vault.version>1.7.1</vault.version>
<placeholderapi.version>2.10.9</placeholderapi.version> <placeholderapi.version>2.10.9</placeholderapi.version>

View File

@ -48,6 +48,13 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
*/ */
private boolean onlyPlayer = false; private boolean onlyPlayer = false;
/**
* True if the command is only for the console
* @since 1.24.0
*/
private boolean onlyConsole = false;
/** /**
* True if command is a configurable rank * True if command is a configurable rank
*/ */
@ -256,10 +263,16 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
*/ */
public boolean call(User user, String cmdLabel, List<String> cmdArgs) { public boolean call(User user, String cmdLabel, List<String> cmdArgs) {
// Check for console and permissions // Check for console and permissions
if (onlyPlayer && !user.isPlayer()) { if (isOnlyPlayer() && !user.isPlayer()) {
user.sendMessage("general.errors.use-in-game"); user.sendMessage("general.errors.use-in-game");
return false; return false;
} }
if (isOnlyConsole() && user.isPlayer()) {
user.sendMessage("general.errors.use-in-console");
return false;
}
if (!this.runPermissionCheck(user)) if (!this.runPermissionCheck(user))
{ {
// Error message is displayed by permission check. // Error message is displayed by permission check.
@ -510,6 +523,14 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
return onlyPlayer; return onlyPlayer;
} }
/**
* Check if this command is only for consoles.
* @return true or false
*/
public boolean isOnlyConsole() {
return onlyConsole;
}
/** /**
* Sets whether this command should only be run by players. * Sets whether this command should only be run by players.
* If this is set to {@code true}, this command will only be runnable by objects implementing {@link Player}. * If this is set to {@code true}, this command will only be runnable by objects implementing {@link Player}.
@ -522,6 +543,18 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
this.onlyPlayer = onlyPlayer; this.onlyPlayer = onlyPlayer;
} }
/**
* Sets whether this command should only be run in the console.
* This is for commands that dump a lot of data or are for debugging.
* The default value provided when instantiating this CompositeCommand is {@code false}.
* Therefore, this method should only be used in case you want to explicitly edit the value.
* @param onlyConsole {@code true} if this command should only be run in the console.
* @since 1.24.0
*/
public void setOnlyConsole(boolean onlyConsole) {
this.onlyConsole = onlyConsole;
}
/** /**
* Sets locale reference to this command's description. * Sets locale reference to this command's description.
* It is used to display the help of this command. * It is used to display the help of this command.
@ -620,16 +653,17 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
@Override @Override
@NonNull @NonNull
public List<String> tabComplete(final @NonNull CommandSender sender, final @NonNull String alias, final String[] args) { public List<String> tabComplete(final @NonNull CommandSender sender, final @NonNull String alias, final String[] args) {
List<String> options = new ArrayList<>();
// Get command object based on args entered so far // Get command object based on args entered so far
CompositeCommand command = getCommandFromArgs(args); CompositeCommand command = getCommandFromArgs(args);
// Check for console and permissions // Check for console and permissions
if (command.isOnlyPlayer() && !(sender instanceof Player)) { if ((command.isOnlyPlayer() && !(sender instanceof Player))
return options; || (command.isOnlyConsole() && sender instanceof Player)) {
return List.of();
} }
if (command.getPermission() != null && !command.getPermission().isEmpty() && !sender.hasPermission(command.getPermission()) && !sender.isOp()) { if (command.getPermission() != null && !command.getPermission().isEmpty() && !sender.hasPermission(command.getPermission()) && !sender.isOp()) {
return options; return List.of();
} }
List<String> options = new ArrayList<>();
// Add any tab completion from the subcommand // Add any tab completion from the subcommand
options.addAll(command.tabComplete(User.getInstance(sender), alias, new LinkedList<>(Arrays.asList(args))).orElseGet(ArrayList::new)); options.addAll(command.tabComplete(User.getInstance(sender), alias, new LinkedList<>(Arrays.asList(args))).orElseGet(ArrayList::new));
if (command.hasSubCommands()) { if (command.hasSubCommands()) {
@ -651,17 +685,26 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
} }
/** /**
* Returns a list containing all the labels of the subcommands for the provided CompositeCommand excluding any hidden commands * Returns a list containing all the labels of the subcommands for the provided
* CompositeCommand excluding any hidden commands
* @param sender the CommandSender * @param sender the CommandSender
* @param command the CompositeCommand to get the subcommands from * @param command the CompositeCommand to get the subcommands from
* @return a list of subcommands labels or an empty list. * @return a list of subcommands labels or an empty list.
*/ */
@NonNull @NonNull
private List<String> getSubCommandLabels(@NonNull CommandSender sender, @NonNull CompositeCommand command) { private List<String> getSubCommandLabels(@NonNull CommandSender sender, @NonNull CompositeCommand command) {
return command.getSubCommands().values().stream() List<String> result = new ArrayList<>();
.filter(cmd -> !cmd.isHidden()) for (CompositeCommand cc: command.getSubCommands().values()) {
.filter(cmd -> !cmd.isOnlyPlayer() || sender.isOp() || (sender instanceof Player && cmd.getPermission() != null && (cmd.getPermission().isEmpty() || sender.hasPermission(cmd.getPermission()))) ) // Player or not
.map(CompositeCommand::getLabel).toList(); if (sender instanceof Player) {
if (!cc.isHidden() && !cc.isOnlyConsole() && (cc.getPermission().isEmpty() || sender.hasPermission(cc.getPermission()))) {
result.add(cc.getLabel());
}
} else if (!cc.isOnlyPlayer()) {
result.add(cc.getLabel());
}
}
return result;
} }
/** /**

View File

@ -19,7 +19,7 @@ public class AdminBlueprintCopyCommand extends CompositeCommand
@Override @Override
public void setup() public void setup()
{ {
inheritPermission(); setPermission("admin.blueprint.copy");
setParametersHelp("commands.admin.blueprint.copy.parameters"); setParametersHelp("commands.admin.blueprint.copy.parameters");
setDescription("commands.admin.blueprint.copy.description"); setDescription("commands.admin.blueprint.copy.description");
} }
@ -37,7 +37,7 @@ public class AdminBlueprintCopyCommand extends CompositeCommand
AdminBlueprintCommand parent = (AdminBlueprintCommand) getParent(); AdminBlueprintCommand parent = (AdminBlueprintCommand) getParent();
BlueprintClipboard clipboard = BlueprintClipboard clipboard =
parent.getClipboards().computeIfAbsent(user.getUniqueId(), v -> new BlueprintClipboard()); parent.getClipboards().computeIfAbsent(user.getUniqueId(), v -> new BlueprintClipboard());
boolean copyAir = args.stream().anyMatch(key -> key.equalsIgnoreCase("air")); boolean copyAir = args.stream().anyMatch(key -> key.equalsIgnoreCase("air"));
boolean copyBiome = args.stream().anyMatch(key -> key.equalsIgnoreCase("biome")); boolean copyBiome = args.stream().anyMatch(key -> key.equalsIgnoreCase("biome"));

View File

@ -26,7 +26,7 @@ public class AdminBlueprintDeleteCommand extends ConfirmableCommand
@Override @Override
public void setup() public void setup()
{ {
this.inheritPermission(); setPermission("admin.blueprint.delete");
this.setParametersHelp("commands.admin.blueprint.delete.parameters"); this.setParametersHelp("commands.admin.blueprint.delete.parameters");
this.setDescription("commands.admin.blueprint.delete.description"); this.setDescription("commands.admin.blueprint.delete.description");
} }
@ -47,10 +47,10 @@ public class AdminBlueprintDeleteCommand extends ConfirmableCommand
if (this.getPlugin().getBlueprintsManager().getBlueprints(this.getAddon()).containsKey(blueprintName)) if (this.getPlugin().getBlueprintsManager().getBlueprints(this.getAddon()).containsKey(blueprintName))
{ {
this.askConfirmation(user, user.getTranslation("commands.admin.blueprint.delete.confirmation"), this.askConfirmation(user, user.getTranslation("commands.admin.blueprint.delete.confirmation"),
() -> { () -> {
this.getPlugin().getBlueprintsManager().deleteBlueprint(this.getAddon(), blueprintName); this.getPlugin().getBlueprintsManager().deleteBlueprint(this.getAddon(), blueprintName);
user.sendMessage("commands.admin.blueprint.delete.success", TextVariables.NAME, blueprintName); user.sendMessage("commands.admin.blueprint.delete.success", TextVariables.NAME, blueprintName);
}); });
return true; return true;
} }
else else

View File

@ -22,7 +22,7 @@ public class AdminBlueprintListCommand extends CompositeCommand
@Override @Override
public void setup() public void setup()
{ {
this.inheritPermission(); setPermission("admin.blueprint.list");
this.setDescription("commands.admin.blueprint.list.description"); this.setDescription("commands.admin.blueprint.list.description");
} }
@ -54,8 +54,8 @@ public class AdminBlueprintListCommand extends CompositeCommand
FilenameFilter blueprintFilter = (File dir, String name) -> name.endsWith(BlueprintsManager.BLUEPRINT_SUFFIX); FilenameFilter blueprintFilter = (File dir, String name) -> name.endsWith(BlueprintsManager.BLUEPRINT_SUFFIX);
List<String> blueprintList = Arrays.stream(Objects.requireNonNull(blueprints.list(blueprintFilter))). List<String> blueprintList = Arrays.stream(Objects.requireNonNull(blueprints.list(blueprintFilter))).
map(name -> name.substring(0, name.length() - BlueprintsManager.BLUEPRINT_SUFFIX.length())). map(name -> name.substring(0, name.length() - BlueprintsManager.BLUEPRINT_SUFFIX.length())).
toList(); toList();
if (blueprintList.isEmpty()) if (blueprintList.isEmpty())
{ {

View File

@ -17,7 +17,7 @@ public class AdminBlueprintLoadCommand extends CompositeCommand {
@Override @Override
public void setup() { public void setup() {
inheritPermission(); setPermission("admin.blueprint.load");
setParametersHelp("commands.admin.blueprint.load.parameters"); setParametersHelp("commands.admin.blueprint.load.parameters");
setDescription("commands.admin.blueprint.load.description"); setDescription("commands.admin.blueprint.load.description");
} }

View File

@ -19,7 +19,7 @@ public class AdminBlueprintOriginCommand extends CompositeCommand {
@Override @Override
public void setup() { public void setup() {
inheritPermission(); setPermission("admin.blueprint.origin");
setParametersHelp("commands.admin.blueprint.origin.parameters"); setParametersHelp("commands.admin.blueprint.origin.parameters");
setDescription("commands.admin.blueprint.origin.description"); setDescription("commands.admin.blueprint.origin.description");
} }

View File

@ -15,7 +15,7 @@ public class AdminBlueprintPasteCommand extends CompositeCommand {
@Override @Override
public void setup() { public void setup() {
inheritPermission(); setPermission("admin.blueprint.paste");
setParametersHelp("commands.admin.blueprint.paste.parameters"); setParametersHelp("commands.admin.blueprint.paste.parameters");
setDescription("commands.admin.blueprint.paste.description"); setDescription("commands.admin.blueprint.paste.description");
} }

View File

@ -15,7 +15,7 @@ public class AdminBlueprintPos1Command extends CompositeCommand {
@Override @Override
public void setup() { public void setup() {
inheritPermission(); setPermission("admin.blueprint.pos1");
setParametersHelp("commands.admin.blueprint.pos1.parameters"); setParametersHelp("commands.admin.blueprint.pos1.parameters");
setDescription("commands.admin.blueprint.pos1.description"); setDescription("commands.admin.blueprint.pos1.description");
} }

View File

@ -15,7 +15,7 @@ public class AdminBlueprintPos2Command extends CompositeCommand {
@Override @Override
public void setup() { public void setup() {
inheritPermission(); setPermission("admin.blueprint.pos2");
setParametersHelp("commands.admin.blueprint.pos2.parameters"); setParametersHelp("commands.admin.blueprint.pos2.parameters");
setDescription("commands.admin.blueprint.pos2.description"); setDescription("commands.admin.blueprint.pos2.description");
} }

View File

@ -27,7 +27,7 @@ public class AdminBlueprintRenameCommand extends ConfirmableCommand
@Override @Override
public void setup() public void setup()
{ {
this.inheritPermission(); setPermission("admin.blueprint.rename");
this.setParametersHelp("commands.admin.blueprint.rename.parameters"); this.setParametersHelp("commands.admin.blueprint.rename.parameters");
this.setDescription("commands.admin.blueprint.rename.description"); this.setDescription("commands.admin.blueprint.rename.description");
} }
@ -83,8 +83,8 @@ public class AdminBlueprintRenameCommand extends ConfirmableCommand
{ {
// Ask for confirmation to overwrite // Ask for confirmation to overwrite
this.askConfirmation(user, this.askConfirmation(user,
user.getTranslation("commands.admin.blueprint.file-exists"), user.getTranslation("commands.admin.blueprint.file-exists"),
() -> this.rename(user, from, to, args.get(1))); () -> this.rename(user, from, to, args.get(1)));
} }
else else
{ {
@ -102,11 +102,11 @@ public class AdminBlueprintRenameCommand extends ConfirmableCommand
this.getPlugin().getBlueprintsManager().renameBlueprint(this.getAddon(), blueprint, fileName, displayName); this.getPlugin().getBlueprintsManager().renameBlueprint(this.getAddon(), blueprint, fileName, displayName);
user.sendMessage("commands.admin.blueprint.rename.success", user.sendMessage("commands.admin.blueprint.rename.success",
"[old]", "[old]",
blueprintName, blueprintName,
TextVariables.NAME, TextVariables.NAME,
blueprint.getName(), blueprint.getName(),
"[display]", "[display]",
blueprint.getDisplayName()); blueprint.getDisplayName());
} }
} }

View File

@ -25,7 +25,7 @@ public class AdminBlueprintSaveCommand extends ConfirmableCommand
@Override @Override
public void setup() public void setup()
{ {
this.inheritPermission(); setPermission("admin.blueprint.save");
this.setParametersHelp("commands.admin.blueprint.save.parameters"); this.setParametersHelp("commands.admin.blueprint.save.parameters");
this.setDescription("commands.admin.blueprint.save.description"); this.setDescription("commands.admin.blueprint.save.description");
} }

View File

@ -22,7 +22,7 @@ public class AdminDeathsAddCommand extends CompositeCommand {
@Override @Override
public void setup() { public void setup() {
inheritPermission(); setPermission("admin.deaths.add");
setDescription("commands.admin.deaths.add.description"); setDescription("commands.admin.deaths.add.description");
setParametersHelp("commands.admin.deaths.add.parameters"); setParametersHelp("commands.admin.deaths.add.parameters");
} }

View File

@ -22,7 +22,7 @@ public class AdminDeathsRemoveCommand extends CompositeCommand {
@Override @Override
public void setup() { public void setup() {
inheritPermission(); setPermission("admin.deaths.remove");
setDescription("commands.admin.deaths.remove.description"); setDescription("commands.admin.deaths.remove.description");
setParametersHelp("commands.admin.deaths.remove.parameters"); setParametersHelp("commands.admin.deaths.remove.parameters");
} }

View File

@ -21,7 +21,7 @@ public class AdminDeathsResetCommand extends CompositeCommand {
@Override @Override
public void setup() { public void setup() {
inheritPermission(); setPermission("admin.deaths.reset");
setDescription("commands.admin.deaths.reset.description"); setDescription("commands.admin.deaths.reset.description");
setParametersHelp("commands.admin.deaths.reset.parameters"); setParametersHelp("commands.admin.deaths.reset.parameters");
} }

View File

@ -21,7 +21,7 @@ public class AdminDeathsSetCommand extends CompositeCommand {
@Override @Override
public void setup() { public void setup() {
inheritPermission(); setPermission("admin.deaths.set");
setDescription("commands.admin.deaths.set.description"); setDescription("commands.admin.deaths.set.description");
setParametersHelp("commands.admin.deaths.set.parameters"); setParametersHelp("commands.admin.deaths.set.parameters");
} }

View File

@ -16,7 +16,7 @@ public class AdminPurgeProtectCommand extends CompositeCommand {
@Override @Override
public void setup() { public void setup() {
inheritPermission(); setPermission("admin.purge.protect");
setOnlyPlayer(true); setOnlyPlayer(true);
setDescription("commands.admin.purge.protect.description"); setDescription("commands.admin.purge.protect.description");
} }

View File

@ -19,7 +19,7 @@ public class AdminPurgeStatusCommand extends CompositeCommand {
@Override @Override
public void setup() { public void setup() {
inheritPermission(); setPermission("admin.purge.status");
setOnlyPlayer(false); setOnlyPlayer(false);
setParametersHelp("commands.admin.purge.status.parameters"); setParametersHelp("commands.admin.purge.status.parameters");
setDescription("commands.admin.purge.status.description"); setDescription("commands.admin.purge.status.description");

View File

@ -13,7 +13,7 @@ public class AdminPurgeStopCommand extends CompositeCommand {
@Override @Override
public void setup() { public void setup() {
inheritPermission(); setPermission("admin.purge.stop");
setOnlyPlayer(false); setOnlyPlayer(false);
setDescription("commands.admin.purge.stop.description"); setDescription("commands.admin.purge.stop.description");
} }

View File

@ -17,7 +17,7 @@ public class AdminPurgeUnownedCommand extends ConfirmableCommand {
@Override @Override
public void setup() { public void setup() {
inheritPermission(); setPermission("admin.purge.unowned");
setOnlyPlayer(false); setOnlyPlayer(false);
setParametersHelp("commands.admin.purge.unowned.parameters"); setParametersHelp("commands.admin.purge.unowned.parameters");
setDescription("commands.admin.purge.unowned.description"); setDescription("commands.admin.purge.unowned.description");

View File

@ -25,7 +25,7 @@ public class AdminRangeAddCommand extends CompositeCommand {
@Override @Override
public void setup() { public void setup() {
inheritPermission(); setPermission("admin.range.add");
setDescription("commands.admin.range.add.description"); setDescription("commands.admin.range.add.description");
setParametersHelp("commands.admin.range.add.parameters"); setParametersHelp("commands.admin.range.add.parameters");
} }

View File

@ -25,7 +25,7 @@ public class AdminRangeRemoveCommand extends CompositeCommand {
@Override @Override
public void setup() { public void setup() {
inheritPermission(); setPermission("admin.range.remove");
setDescription("commands.admin.range.remove.description"); setDescription("commands.admin.range.remove.description");
setParametersHelp("commands.admin.range.remove.parameters"); setParametersHelp("commands.admin.range.remove.parameters");
} }

View File

@ -22,7 +22,7 @@ public class AdminResetsAddCommand extends CompositeCommand {
@Override @Override
public void setup() { public void setup() {
inheritPermission(); setPermission("admin.resets.add");
setDescription("commands.admin.resets.add.description"); setDescription("commands.admin.resets.add.description");
setParametersHelp("commands.admin.resets.add.parameters"); setParametersHelp("commands.admin.resets.add.parameters");
} }

View File

@ -22,7 +22,7 @@ public class AdminResetsRemoveCommand extends CompositeCommand {
@Override @Override
public void setup() { public void setup() {
inheritPermission(); setPermission("admin.resets.remove");
setDescription("commands.admin.resets.remove.description"); setDescription("commands.admin.resets.remove.description");
setParametersHelp("commands.admin.resets.remove.parameters"); setParametersHelp("commands.admin.resets.remove.parameters");
} }

View File

@ -22,7 +22,7 @@ public class AdminResetsResetCommand extends ConfirmableCommand {
@Override @Override
public void setup() { public void setup() {
inheritPermission(); setPermission("admin.resets.remove");
setDescription("commands.admin.resets.reset.description"); setDescription("commands.admin.resets.reset.description");
setParametersHelp("commands.admin.resets.reset.parameters"); setParametersHelp("commands.admin.resets.reset.parameters");
} }

View File

@ -16,7 +16,7 @@ public class AdminResetsSetCommand extends CompositeCommand {
@Override @Override
public void setup() { public void setup() {
inheritPermission(); setPermission("admin.resets.set");
setDescription("commands.admin.resets.set.description"); setDescription("commands.admin.resets.set.description");
setParametersHelp("commands.admin.resets.set.parameters"); setParametersHelp("commands.admin.resets.set.parameters");
} }

View File

@ -21,7 +21,7 @@ public class AdminTeamAddCommand extends CompositeCommand {
@Override @Override
public void setup() { public void setup() {
setPermission("mod.team"); setPermission("mod.team.add");
setParametersHelp("commands.admin.team.add.parameters"); setParametersHelp("commands.admin.team.add.parameters");
setDescription("commands.admin.team.add.description"); setDescription("commands.admin.team.add.description");
} }

View File

@ -20,7 +20,7 @@ public class AdminTeamDisbandCommand extends CompositeCommand {
@Override @Override
public void setup() { public void setup() {
setPermission("mod.team"); setPermission("mod.team.disband");
setParametersHelp("commands.admin.team.disband.parameters"); setParametersHelp("commands.admin.team.disband.parameters");
setDescription("commands.admin.team.disband.description"); setDescription("commands.admin.team.disband.description");
} }

View File

@ -14,7 +14,7 @@ public class AdminTeamFixCommand extends CompositeCommand {
@Override @Override
public void setup() { public void setup() {
setPermission("mod.team"); setPermission("mod.team.fix");
setDescription("commands.admin.team.fix.description"); setDescription("commands.admin.team.fix.description");
} }

View File

@ -30,7 +30,7 @@ public class AdminTeamKickCommand extends CompositeCommand {
@Override @Override
public void setup() { public void setup() {
setPermission("mod.team"); setPermission("mod.team.kick");
setParametersHelp("commands.admin.team.kick.parameters"); setParametersHelp("commands.admin.team.kick.parameters");
setDescription("commands.admin.team.kick.description"); setDescription("commands.admin.team.kick.description");
} }

View File

@ -25,7 +25,7 @@ public class AdminTeamSetownerCommand extends CompositeCommand {
@Override @Override
public void setup() { public void setup() {
setPermission("mod.team"); setPermission("mod.team.setowner");
setParametersHelp("commands.admin.team.setowner.parameters"); setParametersHelp("commands.admin.team.setowner.parameters");
setDescription("commands.admin.team.setowner.description"); setDescription("commands.admin.team.setowner.description");
} }

View File

@ -28,7 +28,7 @@ public class BentoBoxAboutCommand extends CompositeCommand {
@Override @Override
public boolean execute(User user, String label, List<String> args) { public boolean execute(User user, String label, List<String> args) {
user.sendRawMessage("About " + BentoBox.getInstance().getDescription().getName() + " v" + BentoBox.getInstance().getDescription().getVersion() + ":"); user.sendRawMessage("About " + BentoBox.getInstance().getDescription().getName() + " v" + BentoBox.getInstance().getDescription().getVersion() + ":");
user.sendRawMessage("Copyright (c) 2017 - 2021 Tastybento, Poslovitch and the BentoBoxWorld contributors"); user.sendRawMessage("Copyright (c) 2017 - 2023 Tastybento, Poslovitch and the BentoBoxWorld contributors");
user.sendRawMessage("See https://www.eclipse.org/legal/epl-2.0/ for license information."); user.sendRawMessage("See https://www.eclipse.org/legal/epl-2.0/ for license information.");
return true; return true;
} }

View File

@ -24,6 +24,7 @@ public class BentoBoxCommand extends CompositeCommand {
new BentoBoxReloadCommand(this); new BentoBoxReloadCommand(this);
new BentoBoxLocaleCommand(this); new BentoBoxLocaleCommand(this);
new BentoBoxHelpCommand(this); new BentoBoxHelpCommand(this);
new BentoBoxPermsCommand(this);
// Database names with a 2 in them are migration databases // Database names with a 2 in them are migration databases
if (getPlugin().getSettings().getDatabaseType().name().contains("2")) { if (getPlugin().getSettings().getDatabaseType().name().contains("2")) {
new BentoBoxMigrateCommand(this); new BentoBoxMigrateCommand(this);

View File

@ -0,0 +1,82 @@
package world.bentobox.bentobox.commands;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.permissions.Permission;
import world.bentobox.bentobox.api.addons.GameModeAddon;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User;
/**
* Displays permissions that have been set by BentoBox.
*
* @author tastybento
*/
public class BentoBoxPermsCommand extends CompositeCommand {
/**
* Info command
* @param parent - command parent
*/
public BentoBoxPermsCommand(CompositeCommand parent) {
super(parent, "perms");
}
@Override
public void setup() {
setPermission("bentobox.admin.perms");
setParametersHelp("commands.bentobox.perms.parameters");
setDescription("commands.bentobox.perms.description");
this.setOnlyConsole(true);
}
@Override
public boolean execute(User user, String label, List<String> args) {
// Loop all the known top-level commands
getPlugin().getCommandsManager().getCommands().values().stream().distinct().forEach(cc -> {
if (cc.getAddon() == null) {
user.sendMessage("*** BentoBox effective perms:");
} else if (cc.getAddon() instanceof GameModeAddon gma) {
user.sendRawMessage("**** " + gma.getDescription().getName() + " effective perms:");
} else {
user.sendRawMessage("**** " + cc.getAddon().getDescription().getName() + " effective perms:");
}
user.sendRawMessage("permissions:");
printData(user, cc, cc.getLabel());
printSubCommandData(user, cc, cc.getLabel());
});
return true;
}
private void printData(User user, CompositeCommand cc, String label) {
if (cc.getPermission().isBlank()) return;
String desc = user.getTranslation(cc.getWorld(), cc.getDescription());
user.sendRawMessage(" " + cc.getPermission() + ":");
user.sendRawMessage(" description: Allow use of '/" + label + "' command - " + desc);
Permission p = Bukkit.getPluginManager().getPermission(cc.getPermission());
if (p != null) {
user.sendRawMessage(" default: " + p.getDefault().name());
} else {
user.sendRawMessage(" default: OP"); // If not def
}
}
/**
* Iterates over sub-commands
* @param user user
* @param parent parent command
* @param label
*/
private void printSubCommandData(User user, CompositeCommand parent, String label) {
for (CompositeCommand cc : parent.getSubCommands().values()) {
if (cc.getLabel().equalsIgnoreCase("help")) continue; // Ignore the help command
String newLabel = label + " " + cc.getLabel();
printData(user, cc, newLabel);
printSubCommandData(user, cc, newLabel);
}
}
}

View File

@ -21,6 +21,7 @@ general:
no-permission: "&c You don't have the permission to execute this command (&7 [permission]&c )." no-permission: "&c You don't have the permission to execute this command (&7 [permission]&c )."
insufficient-rank: "&c Your rank is not high enough to do that! (&7 [rank]&c )" insufficient-rank: "&c Your rank is not high enough to do that! (&7 [rank]&c )"
use-in-game: "&c This command is only available in-game." use-in-game: "&c This command is only available in-game."
use-in-console: "&c This command is only available in the console."
no-team: "&c You do not have a team!" no-team: "&c You do not have a team!"
no-island: "&c You do not have an island!" no-island: "&c You do not have an island!"
player-has-island: "&c Player already has an island!" player-has-island: "&c Player already has an island!"
@ -428,6 +429,8 @@ commands:
success: "&a Successfully reset [name]'s island name." success: "&a Successfully reset [name]'s island name."
bentobox: bentobox:
description: "BentoBox admin command" description: "BentoBox admin command"
perms:
description: "displays the effective perms for BentoBox and Addons in a YAML format"
about: about:
description: "displays copyright and license information" description: "displays copyright and license information"
reload: reload:

View File

@ -60,4 +60,6 @@ permissions:
bentobox.version: bentobox.version:
description: Allows to use /bentobox version description: Allows to use /bentobox version
default: op default: op
bentobox.perms:
description: Allow use of '/bentobox perms' command
default: OP

View File

@ -0,0 +1,192 @@
package world.bentobox.bentobox.commands;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionDefault;
import org.bukkit.plugin.PluginManager;
import org.eclipse.jdt.annotation.NonNull;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.managers.CommandsManager;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.LocalesManager;
import world.bentobox.bentobox.managers.PlaceholdersManager;
/**
* @author tastybento
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class, BentoBox.class, User.class })
public class BentoBoxPermsCommandTest {
@Mock
private BentoBox plugin;
@Mock
private CompositeCommand ac;
@Mock
private User user;
@Mock
private LocalesManager lm;
BentoBoxPermsCommand cmd;
@Mock
private PlaceholdersManager phm;
@Mock
private PluginManager pim;
@Mock
private Permission perm;
private PermissionDefault defaultPerm = PermissionDefault.OP;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
// Set up plugin
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
// Command manager
CommandsManager cm = mock(CommandsManager.class);
when(plugin.getCommandsManager()).thenReturn(cm);
@NonNull
Map<String, CompositeCommand> cmdMap = new HashMap<>();
cmdMap.put("test", ac);
when(cm.getCommands()).thenReturn(cmdMap);
// Parent command has no aliases
when(ac.getSubCommandAliases()).thenReturn(new HashMap<>());
when(ac.getSubCommands()).thenReturn(new HashMap<>());
when(ac.getLabel()).thenReturn("bbox");
when(ac.getTopLabel()).thenReturn("bbox");
when(ac.getPermission()).thenReturn("admin.bbox");
when(ac.getDescription()).thenReturn("description");
// User
when(user.getTranslation(Mockito.anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(0, String.class));
when(user.isPlayer()).thenReturn(false);
User.setPlugin(plugin);
// Bukkit
PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS);
when(perm.getDefault()).thenReturn(defaultPerm);
when(pim.getPermission(anyString())).thenReturn(perm);
when(Bukkit.getPluginManager()).thenReturn(pim);
// Placeholders
when(phm.replacePlaceholders(any(), anyString())).thenAnswer((Answer<String>) invocation -> invocation.getArgument(1, String.class));
// BentoBox
when(plugin.getLocalesManager()).thenReturn(lm);
when(plugin.getPlaceholdersManager()).thenReturn(phm);
IslandWorldManager iwm = mock(IslandWorldManager.class);
when(plugin.getIWM()).thenReturn(iwm);
// Commands for perms
cmd = new BentoBoxPermsCommand(ac);
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {
Mockito.framework().clearInlineMocks();
}
/**
* Test method for {@link world.bentobox.bentobox.commands.BentoBoxPermsCommand#BentoBoxPermsCommand(world.bentobox.bentobox.api.commands.CompositeCommand)}.
*/
@Test
public void testBentoBoxPermsCommand() {
assertNotNull(cmd);
}
/**
* Test method for {@link world.bentobox.bentobox.commands.BentoBoxPermsCommand#setup()}.
*/
@Test
public void testSetup() {
assertTrue(cmd.isOnlyConsole());
assertFalse(cmd.isOnlyPlayer());
assertEquals("bentobox.admin.perms", cmd.getPermission());
assertEquals("commands.bentobox.perms.description", cmd.getDescription());
assertEquals("commands.bentobox.perms.parameters", cmd.getParameters());
}
/**
* Test method for {@link world.bentobox.bentobox.commands.BentoBoxPermsCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfString() {
assertTrue(cmd.execute(user, "perms", List.of()));
verify(user).sendMessage("*** BentoBox effective perms:");
verify(user).sendRawMessage("permissions:");
verify(user).sendRawMessage(" admin.bbox:");
verify(user).sendRawMessage(" description: Allow use of '/bbox' command - null");
verify(user).sendRawMessage(" bentobox.admin.perms:");
verify(user).sendRawMessage(" description: Allow use of '/bbox perms' command - null");
verify(user, times(2)).sendRawMessage(" default: OP");
}
/**
* Test method for {@link world.bentobox.bentobox.commands.BentoBoxPermsCommand#execute(Player, java.lang.String, String[])}.
*/
@Test
public void testExecuteUserStringListOfStringConsole() {
String[] args = new String[1];
args[0] = "";
CommandSender p = mock(CommandSender.class);
assertTrue(cmd.execute(p, "perms", args));
verify(p, never()).sendMessage("general.errors.use-in-console");
}
/**
* Test method for {@link world.bentobox.bentobox.commands.BentoBoxPermsCommand#execute(Player, java.lang.String, String[])}.
*/
@Test
public void testExecuteUserStringListOfStringIsPlayer() {
when(user.isPlayer()).thenReturn(true);
String[] args = new String[1];
args[0] = "";
Player p = mock(Player.class);
assertFalse(cmd.execute(p, "perms", args));
verify(p).sendMessage("general.errors.use-in-console");
}
}