Extend ranked commands (#1928)

* Add CompositeCommand#defaultCommandRank variable.

This variable stores default rank for a command. This adds more flexibility to the Island#commandRanks object. This change allows specifying default rank for each command.

* Add more commands for users to configure on their islands.

Owners now can specify who can use:
- sethome command
- renamehome command
- deletehome command
- setname command
- resetname command
This commit is contained in:
BONNe 2022-01-29 04:41:20 +02:00 committed by GitHub
parent 7b94aa0ea2
commit da98bbcf38
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 142 additions and 21 deletions

View File

@ -30,6 +30,7 @@ import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.PlayersManager;
import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.bentobox.util.Util;
/**
@ -53,6 +54,12 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
*/
private boolean configurableRankCommand = false;
/**
* Make default command rank as owner rank.
* @since 1.20.0
*/
private int defaultCommandRank = RanksManager.OWNER_RANK;
/**
* True if command is hidden from help and tab complete
* @since 1.13.0
@ -784,6 +791,26 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
this.configurableRankCommand = true;
}
/**
* Sets default command rank.
*
* @param rank the rank
* @since 1.20.0
*/
public void setDefaultCommandRank(int rank) {
this.defaultCommandRank = rank;
}
/**
* Gets default command rank.
*
* @return the default command rank
* @since 1.20.0
*/
public int getDefaultCommandRank() {
return this.defaultCommandRank;
}
/**
* Checks if a command is hidden
* @return the hidden

View File

@ -2,6 +2,7 @@ package world.bentobox.bentobox.api.commands.island;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import org.eclipse.jdt.annotation.Nullable;
@ -11,6 +12,7 @@ import world.bentobox.bentobox.api.commands.ConfirmableCommand;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.bentobox.util.Util;
/**
@ -32,6 +34,8 @@ public class IslandDeletehomeCommand extends ConfirmableCommand {
setOnlyPlayer(true);
setParametersHelp("commands.island.deletehome.parameters");
setDescription("commands.island.deletehome.description");
setConfigurableRankCommand();
setDefaultCommandRank(RanksManager.MEMBER_RANK);
}
@Override
@ -46,6 +50,15 @@ public class IslandDeletehomeCommand extends ConfirmableCommand {
user.sendMessage("general.errors.no-island");
return false;
}
// check command permission
int rank = Objects.requireNonNull(island).getRank(user);
if (rank < island.getRankCommand(getUsage())) {
user.sendMessage("general.errors.insufficient-rank",
TextVariables.RANK, user.getTranslation(getPlugin().getRanksManager().getRank(rank)));
return false;
}
// Check if the name is known
if (!getIslands().isHomeLocation(island, String.join(" ", args))) {
user.sendMessage("commands.island.go.unknown-home");

View File

@ -2,6 +2,7 @@ package world.bentobox.bentobox.api.commands.island;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import org.bukkit.conversations.ConversationFactory;
@ -14,6 +15,7 @@ import world.bentobox.bentobox.api.commands.admin.conversations.NamePrompt;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.bentobox.util.Util;
/**
@ -35,6 +37,8 @@ public class IslandRenamehomeCommand extends ConfirmableCommand {
setOnlyPlayer(true);
setParametersHelp("commands.island.renamehome.parameters");
setDescription("commands.island.renamehome.description");
setConfigurableRankCommand();
setDefaultCommandRank(RanksManager.MEMBER_RANK);
}
@Override
@ -57,6 +61,14 @@ public class IslandRenamehomeCommand extends ConfirmableCommand {
this.showHelp(this, user);
return false;
}
// check command permission
int rank = Objects.requireNonNull(island).getRank(user);
if (rank < island.getRankCommand(getUsage())) {
user.sendMessage("general.errors.insufficient-rank", TextVariables.RANK, user.getTranslation(getPlugin().getRanksManager().getRank(rank)));
return false;
}
return true;
}

View File

@ -1,10 +1,13 @@
package world.bentobox.bentobox.api.commands.island;
import java.util.List;
import java.util.UUID;
import java.util.Objects;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
/**
* @author tastybento
@ -23,22 +26,34 @@ public class IslandResetnameCommand extends CompositeCommand {
setDescription("commands.island.resetname.description");
}
@Override
public boolean execute(User user, String label, List<String> args) {
UUID playerUUID = user.getUniqueId();
if (!getIslands().hasIsland(getWorld(), playerUUID)) {
@Override
public boolean canExecute(User user, String label, List<String> args)
{
Island island = getIslands().getIsland(getWorld(), user);
if (island == null) {
user.sendMessage("general.errors.no-island");
return false;
}
if (!getIslands().isOwner(getWorld(), playerUUID)) {
user.sendMessage("general.errors.not-owner");
// Check command rank.
int rank = Objects.requireNonNull(island).getRank(user);
if (rank < island.getRankCommand(getUsage())) {
user.sendMessage("general.errors.insufficient-rank",
TextVariables.RANK, user.getTranslation(getPlugin().getRanksManager().getRank(rank)));
return false;
}
// Resets the island name
getIslands().getIsland(getWorld(), playerUUID).setName(null);
return true;
}
@Override
public boolean execute(User user, String label, List<String> args) {
// Resets the island name
Objects.requireNonNull(getIslands().getIsland(getWorld(), user)).setName(null);
user.sendMessage("commands.island.resetname.success");
return true;
}

View File

@ -1,6 +1,7 @@
package world.bentobox.bentobox.api.commands.island;
import java.util.List;
import java.util.Objects;
import org.eclipse.jdt.annotation.Nullable;
@ -10,6 +11,8 @@ import world.bentobox.bentobox.api.configuration.WorldSettings;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.RanksManager;
public class IslandSethomeCommand extends ConfirmableCommand {
@ -24,6 +27,8 @@ public class IslandSethomeCommand extends ConfirmableCommand {
setPermission("island.sethome");
setOnlyPlayer(true);
setDescription("commands.island.sethome.description");
setConfigurableRankCommand();
setDefaultCommandRank(RanksManager.MEMBER_RANK);
}
@Override
@ -38,6 +43,13 @@ public class IslandSethomeCommand extends ConfirmableCommand {
user.sendMessage("commands.island.sethome.must-be-on-your-island");
return false;
}
int rank = Objects.requireNonNull(island).getRank(user);
if (rank < island.getRankCommand(getUsage())) {
user.sendMessage("general.errors.insufficient-rank", TextVariables.RANK, user.getTranslation(getPlugin().getRanksManager().getRank(rank)));
return false;
}
// Check number of homes
int maxHomes = getIslands().getMaxHomes(island);
if (getIslands().getNumberOfHomesIfAdded(island, String.join(" ", args)) > maxHomes) {

View File

@ -1,13 +1,15 @@
package world.bentobox.bentobox.api.commands.island;
import java.util.List;
import java.util.UUID;
import java.util.Objects;
import org.bukkit.ChatColor;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
/**
* @author tastybento
@ -25,24 +27,30 @@ public class IslandSetnameCommand extends CompositeCommand {
setOnlyPlayer(true);
setParametersHelp("commands.island.setname.parameters");
setDescription("commands.island.setname.description");
setConfigurableRankCommand();
}
@Override
public boolean execute(User user, String label, List<String> args) {
public boolean canExecute(User user, String label, List<String> args)
{
// Explain command
if (args.isEmpty()) {
showHelp(this, user);
return false;
}
UUID playerUUID = user.getUniqueId();
Island island = getIslands().getIsland(getWorld(), user);
if (!getIslands().hasIsland(getWorld(), playerUUID)) {
if (island == null) {
user.sendMessage("general.errors.no-island");
return false;
}
if (!getIslands().isOwner(getWorld(), playerUUID)) {
user.sendMessage("general.errors.not-owner");
// Check command rank.
int rank = Objects.requireNonNull(island).getRank(user);
if (rank < island.getRankCommand(getUsage())) {
user.sendMessage("general.errors.insufficient-rank", TextVariables.RANK, user.getTranslation(getPlugin().getRanksManager().getRank(rank)));
return false;
}
@ -70,8 +78,22 @@ public class IslandSetnameCommand extends CompositeCommand {
return false;
}
return true;
}
@Override
public boolean execute(User user, String label, List<String> args) {
// Naming the island - join all the arguments with spaces.
String name = String.join(" ", args);
// Apply colors
if (user.hasPermission(getPermissionPrefix() + "island.name.format")) {
name = ChatColor.translateAlternateColorCodes('&', name);
}
// Everything's good!
getIslands().getIsland(getWorld(), playerUUID).setName(name);
Objects.requireNonNull(getIslands().getIsland(getWorld(), user)).setName(name);
user.sendMessage("commands.island.setname.success", TextVariables.NAME, name);
return true;
}

View File

@ -32,7 +32,6 @@ import com.google.gson.annotations.Expose;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.configuration.WorldSettings;
import world.bentobox.bentobox.api.events.island.IslandEvent;
import world.bentobox.bentobox.api.flags.Flag;
import world.bentobox.bentobox.api.logs.LogEntry;
import world.bentobox.bentobox.api.metadata.MetaDataAble;
@ -301,7 +300,7 @@ public class Island implements DataObject, MetaDataAble {
* Bans the target player from this Island.
* If the player is a member, coop or trustee, they will be removed from those lists.
* <br/>
* Calling this method won't call the {@link world.bentobox.bentobox.api.events.island.IslandEvent.IslandBanEvent}.
* Calling this method won't call the {@link world.bentobox.bentobox.api.events.island.IslandBanEvent}.
* @param issuer UUID of the issuer, may be null.
* Whenever possible, one should be provided.
* @param target UUID of the target, must be provided.
@ -996,7 +995,7 @@ public class Island implements DataObject, MetaDataAble {
/**
* Sets player's rank to an arbitrary rank value.
* Calling this method won't call the {@link IslandEvent.IslandRankChangeEvent}.
* Calling this method won't call the {@link world.bentobox.bentobox.api.events.island.IslandRankChangeEvent}.
* @param uuid UUID of the player
* @param rank rank value
* @since 1.1
@ -1313,10 +1312,31 @@ public class Island implements DataObject, MetaDataAble {
* Get the rank required to run command on this island.
* The command must have been registered with a rank.
* @param command - the string given by {@link CompositeCommand#getUsage()}
* @return Rank value required, or if command is not set {@link RanksManager#OWNER_RANK}
* @return Rank value required, or if command is not set {@link CompositeCommand#getDefaultCommandRank()}
*/
public int getRankCommand(String command) {
return commandRanks == null ? RanksManager.OWNER_RANK : commandRanks.getOrDefault(command, RanksManager.OWNER_RANK);
if (this.commandRanks == null) {
this.commandRanks = new HashMap<>();
}
// Return or calculate default rank for a command.
return this.commandRanks.computeIfAbsent(command, key -> {
// Need to find default value for the command.
String[] labels = key.replaceFirst("/", "").split(" ");
// Get first command label.
CompositeCommand compositeCommand = this.getPlugin().getCommandsManager().getCommand(labels[0]);
for (int i = 1; i < labels.length && compositeCommand != null; i++)
{
compositeCommand = compositeCommand.getSubCommand(labels[i]).orElse(null);
}
// Return default command rank or owner rank, if command does not exist.
return compositeCommand == null ? RanksManager.OWNER_RANK : compositeCommand.getDefaultCommandRank();
});
}
/**