Add /bbox rank command for ranks management

This commit is contained in:
tastybento 2023-11-05 13:24:24 -08:00
parent 2655a98aad
commit 456082ac24
5 changed files with 169 additions and 29 deletions

View File

@ -16,6 +16,7 @@ import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.bentobox.util.Util;
/**
* Tells the rank of the player
* @author tastybento
*
*/

View File

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

View File

@ -0,0 +1,141 @@
package world.bentobox.bentobox.commands;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
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.managers.RanksManager;
/**
* Manages ranks
*
* @author tastybento
* @since 2.0.0
*/
public class BentoBoxRankCommand extends CompositeCommand {
private int rankValue;
private String firstElement;
private final RanksManager rm;
/**
* Rank management. Add and remove
* @param parent command parent
*/
public BentoBoxRankCommand(CompositeCommand parent) {
super(parent, "rank");
rm = getPlugin().getRanksManager();
}
@Override
public void setup() {
setPermission("bentobox.admin.rank");
setDescription("commands.bentobox.rank.description");
this.setParametersHelp("commands.bentobox.rank.parameters");
}
@Override
public boolean canExecute(User user, String label, List<String> args) {
if (args.isEmpty()) {
// Show help
showHelp(this, user);
return false;
}
// Check if the first element is "add" or "remove" or "list"
firstElement = args.get(0);
if (!("list".equals(firstElement) || "add".equals(firstElement) || "remove".equals(firstElement))) {
// Show help
showHelp(this, user);
return false;
}
if ("remove".equals(firstElement) && args.size() != 2) {
// Show help
showHelp(this, user);
return false;
}
// If the first element is "add", then check if the third element is an integer
if ("add".equals(firstElement)) {
// Check if there is a third element
if (args.size() < 3) {
// Show help
showHelp(this, user);
return false;
}
// Check if the third element is an integer
String thirdElement = args.get(2);
try {
rankValue = Integer.parseInt(thirdElement);
} catch (NumberFormatException e) {
// Show help
showHelp(this, user);
return false;
}
}
// If all checks passed, return true
return true;
}
@Override
public boolean execute(User user, String label, List<String> args) {
if ("list".equals(firstElement)) {
showRanks(user);
return true;
}
if ("add".equals(firstElement)) {
if (rm.addRank(args.get(1), rankValue)) {
user.sendMessage("commands.bentobox.rank.add.success", "[rank]", args.get(1), TextVariables.NUMBER, String.valueOf(rankValue));
showRanks(user);
} else {
user.sendMessage("commands.bentobox.rank.add.failure", "[rank]", args.get(1), TextVariables.NUMBER, String.valueOf(rankValue));
return false;
}
} else {
if (rm.removeRank(args.get(1))) {
user.sendMessage("commands.bentobox.rank.remove.success", "[rank]", args.get(1));
showRanks(user);
} else {
user.sendMessage("commands.bentobox.rank.remove.failure", "[rank]", args.get(1));
return false;
}
}
return true;
}
private void showRanks(User user) {
user.sendMessage("commands.bentobox.rank.list");
rm.getRanks().forEach((ref, rank) -> {
user.sendRawMessage(user.getTranslation(ref) + ": " + ref + " " + String.valueOf(rank));
});
}
@Override
public Optional<List<String>> tabComplete(User user, String alias, List<String> args) {
if (args.size() <=1) {
return Optional.empty();
}
firstElement = args.get(1);
if (args.size() <= 2) {
return Optional.of(List.of("add","remove","list"));
}
if (args.size() > 1 && "add".equals(firstElement)) {
List<String> options = new ArrayList<>(RanksManager.DEFAULT_RANKS.keySet());
options.removeIf(rm.getRanks().keySet()::contains);
return Optional.of(options);
}
if (args.size() > 1 && "remove".equals(firstElement)) {
return Optional.of(new ArrayList<>(rm.getRanks().keySet()));
}
return Optional.empty();
}
}

View File

@ -3,6 +3,7 @@ package world.bentobox.bentobox.managers;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.stream.Collectors;
import org.eclipse.jdt.annotation.NonNull;
@ -41,6 +42,15 @@ public class RanksManager {
// The store of ranks
private LinkedHashMap<String, Integer> ranks = new LinkedHashMap<>();
public static final Map<String, Integer> DEFAULT_RANKS = Map.of(ADMIN_RANK_REF, ADMIN_RANK,
MOD_RANK_REF, MOD_RANK,
OWNER_RANK_REF, OWNER_RANK,
SUB_OWNER_RANK_REF, SUB_OWNER_RANK,
MEMBER_RANK_REF, MEMBER_RANK,
TRUSTED_RANK_REF, TRUSTED_RANK,
COOP_RANK_REF, COOP_RANK,
VISITOR_RANK_REF, VISITOR_RANK,
BANNED_RANK_REF, BANNED_RANK);
@NonNull
private Database<RankObject> handler;
@ -50,19 +60,13 @@ public class RanksManager {
handler = new Database<>(BentoBox.getInstance(), RankObject.class);
if (!handler.objectExists(RankObject.ID)) {
// Make the initial object
ranksPut(ADMIN_RANK_REF, ADMIN_RANK);
ranksPut(MOD_RANK_REF, MOD_RANK);
ranksPut(OWNER_RANK_REF, OWNER_RANK);
ranksPut(SUB_OWNER_RANK_REF, SUB_OWNER_RANK);
ranksPut(MEMBER_RANK_REF, MEMBER_RANK);
ranksPut(TRUSTED_RANK_REF, TRUSTED_RANK);
ranksPut(COOP_RANK_REF, COOP_RANK);
ranksPut(VISITOR_RANK_REF, VISITOR_RANK);
ranksPut(BANNED_RANK_REF, BANNED_RANK);
DEFAULT_RANKS.forEach((ref, rank) -> ranksPut(ref, rank));
handler.saveObject(new RankObject(ranks));
} else {
handler.loadObject(RankObject.ID).getRankReference().forEach(this::ranksPut);
// Load the ranks from the database
Objects.requireNonNull(handler.loadObject(RankObject.ID)).getRankReference().forEach(this::ranksPut);
}
}
/**
@ -81,15 +85,7 @@ public class RanksManager {
* @return true if the rank was successfully added
*/
public boolean addRank(String reference, int value) {
if (reference.equalsIgnoreCase(OWNER_RANK_REF)
|| reference.equalsIgnoreCase(SUB_OWNER_RANK_REF)
|| reference.equalsIgnoreCase(TRUSTED_RANK_REF)
|| reference.equalsIgnoreCase(COOP_RANK_REF)
|| reference.equalsIgnoreCase(MEMBER_RANK_REF)
|| reference.equalsIgnoreCase(VISITOR_RANK_REF)
|| reference.equalsIgnoreCase(BANNED_RANK_REF)
|| reference.equalsIgnoreCase(ADMIN_RANK_REF)
|| reference.equalsIgnoreCase(MOD_RANK_REF)) {
if (rankExists(reference)) {
return false;
}
ranksPut(reference, value);
@ -112,15 +108,7 @@ public class RanksManager {
* @return true if removed
*/
public boolean removeRank(String reference) {
return !reference.equalsIgnoreCase(OWNER_RANK_REF)
&& !reference.equalsIgnoreCase(SUB_OWNER_RANK_REF)
&& !reference.equalsIgnoreCase(TRUSTED_RANK_REF)
&& !reference.equalsIgnoreCase(COOP_RANK_REF)
&& !reference.equalsIgnoreCase(MEMBER_RANK_REF)
&& !reference.equalsIgnoreCase(VISITOR_RANK_REF)
&& !reference.equalsIgnoreCase(BANNED_RANK_REF)
&& !reference.equalsIgnoreCase(ADMIN_RANK_REF)
&& !reference.equalsIgnoreCase(MOD_RANK_REF) && (ranks.remove(reference) != null);
return ranks.remove(reference) != null;
}

View File

@ -498,7 +498,16 @@ commands:
addons: '[prefix_bentobox]&6 Migrating addons'
class: '[prefix_bentobox]&6 Migrating [description]'
migrated: '[prefix_bentobox]&a Migrated'
rank:
description: 'list, add, or remove ranks'
parameters: '&a [list | add | remove] [rank reference] [rank value]'
add:
success: '&a Added [rank] with value [number]'
failure: '&c Failed to add [rank] with value [number]. Maybe a duplicate?'
remove:
success: '&a Removed [rank]'
failure: '&c Failed to remove [rank]. Unknown rank.'
list: '&a Registered ranks are as follows:'
confirmation:
confirm: '&c Type command again within &b [seconds]s&c to confirm.'
previous-request-cancelled: '&6 Previous confirmation request cancelled.'