Added command rank settings and extra ranks.

This commit is contained in:
tastybento 2018-08-11 21:08:04 -07:00
parent 203998d000
commit d44889e7b0
16 changed files with 335 additions and 26 deletions

1
.gitignore vendored
View File

@ -8,3 +8,4 @@
/.idea
/database/
/.gitignore
/dependency-reduced-pom.xml

View File

@ -74,10 +74,6 @@ public class BentoBox extends JavaPlugin {
// get a commodore instance
commodore = CommodoreProvider.getCommodore(this);
}
// Not loaded
isLoaded = false;
// Store the current millis time so we can tell how many ms it took for BSB to fully load.

View File

@ -10,6 +10,7 @@ import world.bentobox.bentobox.api.configuration.ConfigEntry;
import world.bentobox.bentobox.api.configuration.StoreAt;
import world.bentobox.bentobox.database.DatabaseSetup.DatabaseType;
import world.bentobox.bentobox.database.objects.DataObject;
import world.bentobox.bentobox.managers.RanksManager;
/**
* All the plugin settings are here
@ -109,6 +110,10 @@ public class Settings implements DataObject {
@ConfigComment("Time in seconds that players have to confirm sensitive commands, e.g. island reset")
@ConfigEntry(path = "general.confirmation-time")
private int confirmationTime = 20;
@ConfigComment("Rank required to use a command. e.g., use the invite command. Default is owner rank is required.")
@ConfigEntry(path = "general.rank-command")
private Map<String, Integer> rankCommand = new HashMap<>();
@ConfigEntry(path = "panel.close-on-click-outside")
private boolean closePanelOnClickOutside = true;
@ -586,4 +591,28 @@ public class Settings implements DataObject {
this.banWait = banWait;
}
public int getRankCommand(String command) {
return rankCommand.getOrDefault(command, RanksManager.OWNER_RANK);
}
public void setRankCommand(String command, int rank) {
rankCommand.put(command, rank);
}
/**
* @return the rankCommand
*/
public Map<String, Integer> getRankCommand() {
return rankCommand;
}
/**
* @param rankCommand the rankCommand to set
*/
public void setRankCommand(Map<String, Integer> rankCommand) {
this.rankCommand = rankCommand;
}
}

View File

@ -17,16 +17,10 @@ import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand;
import org.bukkit.command.PluginIdentifiableCommand;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitTask;
import com.mojang.brigadier.arguments.BoolArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import me.lucko.commodore.Commodore;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.Settings;
import world.bentobox.bentobox.api.addons.Addon;
@ -51,6 +45,17 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
* True if the command is for the player only (not for the console)
*/
private boolean onlyPlayer = false;
/**
* True if the command is only for the console to use
*/
private boolean onlyConsole = false;
/**
* True if command is a configurable rank
*/
private boolean configurableRankCommand = false;
/**
* The parameters string for this command. It is the commands followed by a locale reference.
*/
@ -201,8 +206,8 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
this.permissionPrefix = parent.getPermissionPrefix();
// Inherit world
this.world = parent.getWorld();
// Default references to description and parameters
StringBuilder reference = new StringBuilder();
reference.append(label);
CompositeCommand p = this;
@ -420,6 +425,16 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
return "/" + usage;
}
public String getFullUsage() {
String result = usage;
CompositeCommand c = this.parent;
while (c != null) {
result = c.getUsage() + " " + result;
c = c.getParent();
}
return "/" + result;
}
/**
* Check if this command has a specific sub command.
* @param subCommand - sub command
@ -822,4 +837,33 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
user.sendMessage("general.errors.you-must-wait", TextVariables.NUMBER, String.valueOf(timeToGo));
return true;
}
/**
* @return the onlyConsole
*/
public boolean isOnlyConsole() {
return onlyConsole;
}
/**
* This command is only for console use
*/
public void setOnlyConsole() {
this.onlyConsole = true;
}
/**
* @return the configurableRankCommand
*/
public boolean isConfigurableRankCommand() {
return configurableRankCommand;
}
/**
* This command can be configured for use by different ranks
*/
public void setConfigurableRankCommand() {
this.configurableRankCommand = true;
}
}

View File

@ -27,6 +27,7 @@ public class IslandBanCommand extends CompositeCommand {
setOnlyPlayer(true);
setParametersHelp("commands.island.ban.parameters");
setDescription("commands.island.ban.description");
setConfigurableRankCommand();
}
@Override
@ -42,8 +43,9 @@ public class IslandBanCommand extends CompositeCommand {
user.sendMessage("general.errors.no-island");
return false;
}
if (!getIslands().isOwner(getWorld(), playerUUID)) {
user.sendMessage("general.errors.not-leader");
// Check rank to use command
if (getIslands().getIsland(getWorld(), user).getRank(user) < getPlugin().getSettings().getRankCommand(getUsage())) {
user.sendMessage("general.errors.no-permission");
return false;
}
// Get target player

View File

@ -23,6 +23,7 @@ public class IslandUnbanCommand extends CompositeCommand {
setOnlyPlayer(true);
setParametersHelp("commands.island.unban.parameters");
setDescription("commands.island.unban.description");
setConfigurableRankCommand();
}
@Override
@ -38,8 +39,9 @@ public class IslandUnbanCommand extends CompositeCommand {
user.sendMessage("general.errors.no-island");
return false;
}
if (!getIslands().isOwner(getWorld(), playerUUID)) {
user.sendMessage("general.errors.not-leader");
// Check rank to use command
if (getIslands().getIsland(getWorld(), user).getRank(user) < getPlugin().getSettings().getRankCommand(getUsage())) {
user.sendMessage("general.errors.no-permission");
return false;
}
// Get target player

View File

@ -32,19 +32,20 @@ public class IslandTeamInviteCommand extends CompositeCommand {
setOnlyPlayer(true);
setDescription("commands.island.team.invite.description");
inviteList = HashBiMap.create();
setConfigurableRankCommand();
}
@Override
public boolean execute(User user, String label, List<String> args) {
UUID playerUUID = user.getUniqueId();
// Player issuing the command must have an island
if (!getIslands().hasIsland(getWorld(), user.getUniqueId())) {
if (!getIslands().inTeam(getWorld(), user.getUniqueId()) && !getIslands().hasIsland(getWorld(), user.getUniqueId())) {
user.sendMessage("general.errors.no-island");
return false;
}
UUID teamLeaderUUID = getTeamLeader(getWorld(), user);
if (!(teamLeaderUUID.equals(playerUUID))) {
user.sendMessage("general.errors.not-leader");
// Check rank to use command
if (getIslands().getIsland(getWorld(), user).getRank(user) < getPlugin().getSettings().getRankCommand(getUsage())) {
user.sendMessage("general.errors.no-permission");
return false;
}
if (args.isEmpty() || args.size() > 1) {

View File

@ -18,6 +18,7 @@ public class IslandTeamKickCommand extends CompositeCommand {
setOnlyPlayer(true);
setParametersHelp("commands.island.team.kick.parameters");
setDescription("commands.island.team.kick.description");
setConfigurableRankCommand();
}
@Override

View File

@ -5,6 +5,7 @@ import java.util.List;
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;
public class IslandTeamPromoteCommand extends CompositeCommand {
@ -31,10 +32,12 @@ public class IslandTeamPromoteCommand extends CompositeCommand {
user.sendMessage("general.errors.no-team");
return true;
}
if (!getTeamLeader(getWorld(), user).equals(user.getUniqueId())) {
user.sendMessage("general.errors.not-leader");
return true;
// Check rank to use command
if (getIslands().getIsland(getWorld(), user).getRank(user) < getPlugin().getSettings().getRankCommand(getUsage())) {
user.sendMessage("general.errors.no-permission");
return false;
}
// If args are not right, show help
if (args.size() != 1) {
showHelp(this, user);
@ -58,7 +61,8 @@ public class IslandTeamPromoteCommand extends CompositeCommand {
int currentRank = getIslands().getIsland(getWorld(), user.getUniqueId()).getRank(target);
if (this.getLabel().equals("promote")) {
int nextRank = getPlugin().getRanksManager().getRankUpValue(currentRank);
if (nextRank > currentRank) {
// Stop short of owner
if (nextRank != RanksManager.OWNER_RANK && nextRank > currentRank) {
getIslands().getIsland(getWorld(), user.getUniqueId()).setRank(target, nextRank);
String rankName = user.getTranslation(getPlugin().getRanksManager().getRank(nextRank));
user.sendMessage("commands.island.team.promote.success", TextVariables.NAME, target.getName(), TextVariables.RANK, rankName);
@ -70,7 +74,8 @@ public class IslandTeamPromoteCommand extends CompositeCommand {
} else {
// Demote
int prevRank = getPlugin().getRanksManager().getRankDownValue(currentRank);
if (prevRank < currentRank) {
// Lowest is Member
if (prevRank >= RanksManager.MEMBER_RANK && prevRank < currentRank) {
getIslands().getIsland(getWorld(), user.getUniqueId()).setRank(target, prevRank);
String rankName = user.getTranslation(getPlugin().getRanksManager().getRank(prevRank));
user.sendMessage("commands.island.team.demote.success", TextVariables.NAME, target.getName(), TextVariables.RANK, rankName);

View File

@ -0,0 +1,66 @@
/**
*
*/
package world.bentobox.bentobox.listeners.flags;
import org.bukkit.Sound;
import org.bukkit.event.inventory.ClickType;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.panels.Panel;
import world.bentobox.bentobox.api.panels.PanelItem.ClickHandler;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.managers.RanksManager;
/**
* @author tastybento
*
*/
public class CommandCycleClick implements ClickHandler {
private BentoBox plugin = BentoBox.getInstance();
private String command;
private CommandRankClickListener commandRankClickListener;
public CommandCycleClick(CommandRankClickListener commandRankClickListener, String c) {
this.commandRankClickListener = commandRankClickListener;
this.command = c;
}
/* (non-Javadoc)
* @see world.bentobox.bentobox.api.panels.PanelItem.ClickHandler#onClick(world.bentobox.bentobox.api.panels.Panel, world.bentobox.bentobox.api.user.User, org.bukkit.event.inventory.ClickType, int)
*/
@Override
public boolean onClick(Panel panel, User user, ClickType click, int slot) {
// Left clicking increases the rank required
// Right clicking decreases the rank required
// Get the user's island
Island island = plugin.getIslands().getIsland(user.getWorld(), user.getUniqueId());
if (island != null && island.getOwner().equals(user.getUniqueId())) {
RanksManager rm = plugin.getRanksManager();
int currentRank = plugin.getSettings().getRankCommand(command);
if (click.equals(ClickType.LEFT)) {
if (currentRank == RanksManager.OWNER_RANK) {
plugin.getSettings().setRankCommand(command, RanksManager.MEMBER_RANK);
} else {
plugin.getSettings().setRankCommand(command, rm.getRankUpValue(currentRank));
}
user.getPlayer().playSound(user.getLocation(), Sound.BLOCK_STONE_BUTTON_CLICK_ON, 1F, 1F);
} else if (click.equals(ClickType.RIGHT)) {
if (currentRank == RanksManager.MEMBER_RANK) {
plugin.getSettings().setRankCommand(command, RanksManager.OWNER_RANK);
} else {
plugin.getSettings().setRankCommand(command, rm.getRankDownValue(currentRank));
}
user.getPlayer().playSound(user.getLocation(), Sound.BLOCK_STONE_BUTTON_CLICK_ON, 1F, 1F);
}
// Apply change to panel
panel.getInventory().setItem(slot, commandRankClickListener.getPanelItem(command, user).getItem());
} else {
user.getPlayer().playSound(user.getLocation(), Sound.BLOCK_METAL_HIT, 1F, 1F);
}
return true;
}
}

View File

@ -0,0 +1,135 @@
/**
*
*/
package world.bentobox.bentobox.listeners.flags;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.World;
import org.bukkit.event.inventory.ClickType;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.panels.Panel;
import world.bentobox.bentobox.api.panels.PanelItem;
import world.bentobox.bentobox.api.panels.PanelItem.ClickHandler;
import world.bentobox.bentobox.api.panels.builders.PanelBuilder;
import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.bentobox.util.Util;
/**
* @author tastybento
*
*/
public class CommandRankClickListener implements ClickHandler {
private BentoBox plugin = BentoBox.getInstance();
/* (non-Javadoc)
* @see world.bentobox.bentobox.api.panels.PanelItem.ClickHandler#onClick(world.bentobox.bentobox.api.panels.Panel, world.bentobox.bentobox.api.user.User, org.bukkit.event.inventory.ClickType, int)
*/
@Override
public boolean onClick(Panel panel, User user, ClickType clickType, int slot) {
// Get the world
if (!user.inWorld()) {
user.sendMessage("general.errors.wrong-world");
return true;
}
IslandWorldManager iwm = plugin.getIWM();
String reqPerm = iwm.getPermissionPrefix(Util.getWorld(user.getWorld())) + ".admin.settings.COMMAND_RANKS";
if (!user.hasPermission(reqPerm)) {
user.sendMessage("general.errors.no-permission");
user.sendMessage("general.errors.you-need", "[permission]", reqPerm);
user.getPlayer().playSound(user.getLocation(), Sound.BLOCK_METAL_HIT, 1F, 1F);
return true;
}
String panelName = user.getTranslation("protection.flags.COMMAND_RANKS.name");
if (panel.getName().equals(panelName)) {
// This is a click on the panel
// Slot relates to the command
String c = getCommands(user.getWorld()).get(slot);
// Apply change to panel
panel.getInventory().setItem(slot, getPanelItem(c, user).getItem());
} else {
// Open the Sub Settings panel
openPanel(user, panelName);
}
return true;
}
private void openPanel(User user, String panelName) {
// Close the current panel
user.closeInventory();
// Open a new panel
PanelBuilder pb = new PanelBuilder();
pb.user(user).name(panelName);
// Make panel items
getCommands(user.getWorld()).forEach(c -> pb.item(getPanelItem(c, user)));
pb.build();
}
/**
* Gets the rank command panel item
* @param c - rank string
* @param user - user
* @return panel item for this command
*/
public PanelItem getPanelItem(String c, User user) {
PanelItemBuilder pib = new PanelItemBuilder();
pib.name(c);
pib.clickHandler(new CommandCycleClick(this, c));
pib.icon(Material.MAP);
// TODO: use specific layout
String d = user.getTranslation("protection.panel.flag-item.description-layout", TextVariables.DESCRIPTION, "");
pib.description(d);
plugin.getRanksManager().getRanks().forEach((reference, score) -> {
if (score >= RanksManager.MEMBER_RANK && score < plugin.getSettings().getRankCommand(c)) {
pib.description(user.getTranslation("protection.panel.flag-item.blocked_rank") + user.getTranslation(reference));
} else if (score <= RanksManager.OWNER_RANK && score > plugin.getSettings().getRankCommand(c)) {
pib.description(user.getTranslation("protection.panel.flag-item.allowed_rank") + user.getTranslation(reference));
} else if (score == plugin.getSettings().getRankCommand(c)) {
pib.description(user.getTranslation("protection.panel.flag-item.minimal_rank") + user.getTranslation(reference));
}
});
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)));
if (result.size() > 49) {
Bukkit.getLogger().severe("Number of rank setting commands is too big for GUI");
result.subList(49, result.size()).clear();
}
return result;
}
/**
* Recursively traverses the command tree looking for any configurable rank command and returns a string list of commands
* @param labels - preceding command's label list
* @param cc - composite command
* @return string list of commands
*/
private List<String> getCmdRecursively(String labels, CompositeCommand cc) {
List<String> result = new ArrayList<>();
String newLabel = labels + cc.getName();
if (cc.isConfigurableRankCommand()) {
result.add(newLabel);
}
cc.getSubCommands().values().forEach(s -> result.addAll(getCmdRecursively(newLabel + " ", s)));
return result;
}
}

View File

@ -16,6 +16,7 @@ import world.bentobox.bentobox.listeners.flags.BreedingListener;
import world.bentobox.bentobox.listeners.flags.BucketListener;
import world.bentobox.bentobox.listeners.flags.ChestDamageListener;
import world.bentobox.bentobox.listeners.flags.CleanSuperFlatListener;
import world.bentobox.bentobox.listeners.flags.CommandRankClickListener;
import world.bentobox.bentobox.listeners.flags.CreeperListener;
import world.bentobox.bentobox.listeners.flags.EggListener;
import world.bentobox.bentobox.listeners.flags.EnderChestListener;
@ -222,6 +223,9 @@ public class Flags {
*/
public static final Flag CREEPER_GRIEFING = new FlagBuilder().id("CREEPER_GRIEFING").icon(Material.CREEPER_HEAD).type(Type.WORLD_SETTING)
.allowedByDefault(false).build();
public static final Flag COMMAND_RANKS = new FlagBuilder().id("COMMAND_RANKS").icon(Material.PLAYER_HEAD).type(Type.WORLD_SETTING)
.onClick(new CommandRankClickListener()).subPanel(true).build();
/**
* @return List of all the flags in this class

View File

@ -44,4 +44,11 @@ public class CommandsManager {
return commands.keySet();
}
/**
* @return the commands
*/
public HashMap<String, CompositeCommand> getCommands() {
return commands;
}
}

View File

@ -13,13 +13,19 @@ public class RanksManager {
public static final String ADMIN_RANK_REF = "ranks.admin";
public static final String MOD_RANK_REF = "ranks.mod";
public static final String OWNER_RANK_REF = "ranks.owner";
public static final String SUB_OWNER_RANK_REF = "ranks.sub-owner";
public static final String MEMBER_RANK_REF = "ranks.member";
public static final String TRUSTED_RANK_REF = "ranks.trusted";
public static final String COOP_RANK_REF = "ranks.coop";
public static final String VISITOR_RANK_REF = "ranks.visitor";
public static final String BANNED_RANK_REF = "ranks.banned";
public static final int ADMIN_RANK = 10000;
public static final int MOD_RANK = 5000;
public static final int OWNER_RANK = 1000;
public static final int MEMBER_RANK = 900;
public static final int SUB_OWNER_RANK = 900;
public static final int MEMBER_RANK = 500;
public static final int TRUSTED_RANK = 400;
public static final int COOP_RANK = 200;
public static final int VISITOR_RANK = 0;
public static final int BANNED_RANK = -1;
@ -38,7 +44,10 @@ public class RanksManager {
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);
loadCustomRanks();

View File

@ -26,7 +26,9 @@ public class SettingsPanel {
/**
* Dynamically creates the panel.
* @param plugin - plugin
* @param user the User to show the panel to
* @param user - user to show panel to
* @param flagType - initial view
* @param world - world
*/
public static void openPanel(BentoBox plugin, User user, Flag.Type flagType, World world) {
String friendlyWorldName = plugin.getIWM().getFriendlyName(world);

View File

@ -307,7 +307,9 @@ commands:
ranks:
owner: "Owner"
sub-owner: "Sub-Owner"
member: "Member"
trusted: "Trusted"
coop: "Coop"
visitor: "Visitor"
banned: "Banned"
@ -385,6 +387,9 @@ protection:
&a(override Buckets)
name: "Collect water"
hint: "No water collection"
COMMAND_RANKS:
name: "Command Ranks"
description: "&aConfigure command ranks"
CRAFTING:
description: "Toggle use"
name: "Workbenches"