Skip to content

Commit

Permalink
Add /bbox rank command for ranks management
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Nov 5, 2023
1 parent 2655a98 commit 456082a
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import world.bentobox.bentobox.util.Util;

/**
* Tells the rank of the player
* @author tastybento
*
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public void setup() {
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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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();

}
}
44 changes: 16 additions & 28 deletions src/main/java/world/bentobox/bentobox/managers/RanksManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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;
Expand Down Expand Up @@ -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;
Expand All @@ -50,19 +60,13 @@ public 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);
}

}

/**
Expand All @@ -81,15 +85,7 @@ public boolean rankExists(String reference) {
* @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);
Expand All @@ -112,15 +108,7 @@ private void ranksPut(String reference, int value) {
* @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;

}

Expand Down
11 changes: 10 additions & 1 deletion src/main/resources/locales/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
Expand Down

0 comments on commit 456082a

Please sign in to comment.