From d44889e7b050e95eda8549000402f8fb4f750e50 Mon Sep 17 00:00:00 2001 From: tastybento Date: Sat, 11 Aug 2018 21:08:04 -0700 Subject: [PATCH 1/9] Added command rank settings and extra ranks. --- .gitignore | 1 + .../world/bentobox/bentobox/BentoBox.java | 4 - .../world/bentobox/bentobox/Settings.java | 29 ++++ .../api/commands/CompositeCommand.java | 58 +++++++- .../api/commands/island/IslandBanCommand.java | 6 +- .../commands/island/IslandUnbanCommand.java | 6 +- .../island/team/IslandTeamInviteCommand.java | 9 +- .../island/team/IslandTeamKickCommand.java | 1 + .../island/team/IslandTeamPromoteCommand.java | 15 +- .../listeners/flags/CommandCycleClick.java | 66 +++++++++ .../flags/CommandRankClickListener.java | 135 ++++++++++++++++++ .../world/bentobox/bentobox/lists/Flags.java | 4 + .../bentobox/managers/CommandsManager.java | 7 + .../bentobox/managers/RanksManager.java | 11 +- .../bentobox/panels/SettingsPanel.java | 4 +- src/main/resources/locales/en-US.yml | 5 + 16 files changed, 335 insertions(+), 26 deletions(-) create mode 100644 src/main/java/world/bentobox/bentobox/listeners/flags/CommandCycleClick.java create mode 100644 src/main/java/world/bentobox/bentobox/listeners/flags/CommandRankClickListener.java diff --git a/.gitignore b/.gitignore index 7f75d6573..8e8b3cca3 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ /.idea /database/ /.gitignore +/dependency-reduced-pom.xml diff --git a/src/main/java/world/bentobox/bentobox/BentoBox.java b/src/main/java/world/bentobox/bentobox/BentoBox.java index 187e36f55..c1df11730 100644 --- a/src/main/java/world/bentobox/bentobox/BentoBox.java +++ b/src/main/java/world/bentobox/bentobox/BentoBox.java @@ -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. diff --git a/src/main/java/world/bentobox/bentobox/Settings.java b/src/main/java/world/bentobox/bentobox/Settings.java index aa19d2765..a5ed6a3af 100644 --- a/src/main/java/world/bentobox/bentobox/Settings.java +++ b/src/main/java/world/bentobox/bentobox/Settings.java @@ -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 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 getRankCommand() { + return rankCommand; + } + + /** + * @param rankCommand the rankCommand to set + */ + public void setRankCommand(Map rankCommand) { + this.rankCommand = rankCommand; + } + } \ No newline at end of file diff --git a/src/main/java/world/bentobox/bentobox/api/commands/CompositeCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/CompositeCommand.java index da7beed22..198ad8958 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/CompositeCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/CompositeCommand.java @@ -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; + } + } diff --git a/src/main/java/world/bentobox/bentobox/api/commands/island/IslandBanCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/island/IslandBanCommand.java index 70ac0bd1a..6637e3cdb 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/island/IslandBanCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/island/IslandBanCommand.java @@ -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 diff --git a/src/main/java/world/bentobox/bentobox/api/commands/island/IslandUnbanCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/island/IslandUnbanCommand.java index a5c9d548c..92e004cdb 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/island/IslandUnbanCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/island/IslandUnbanCommand.java @@ -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 diff --git a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamInviteCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamInviteCommand.java index 1c4fa05a6..1658e6747 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamInviteCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamInviteCommand.java @@ -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 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) { diff --git a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamKickCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamKickCommand.java index f784b6ad8..9a8365433 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamKickCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamKickCommand.java @@ -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 diff --git a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamPromoteCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamPromoteCommand.java index b1f7e9749..d21da3cbe 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamPromoteCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamPromoteCommand.java @@ -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); diff --git a/src/main/java/world/bentobox/bentobox/listeners/flags/CommandCycleClick.java b/src/main/java/world/bentobox/bentobox/listeners/flags/CommandCycleClick.java new file mode 100644 index 000000000..53ff47f37 --- /dev/null +++ b/src/main/java/world/bentobox/bentobox/listeners/flags/CommandCycleClick.java @@ -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; + } + +} diff --git a/src/main/java/world/bentobox/bentobox/listeners/flags/CommandRankClickListener.java b/src/main/java/world/bentobox/bentobox/listeners/flags/CommandRankClickListener.java new file mode 100644 index 000000000..8b6499b6a --- /dev/null +++ b/src/main/java/world/bentobox/bentobox/listeners/flags/CommandRankClickListener.java @@ -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 getCommands(World world) { + List 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 getCmdRecursively(String labels, CompositeCommand cc) { + List 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; + } + +} diff --git a/src/main/java/world/bentobox/bentobox/lists/Flags.java b/src/main/java/world/bentobox/bentobox/lists/Flags.java index 6c348c057..7385c9101 100644 --- a/src/main/java/world/bentobox/bentobox/lists/Flags.java +++ b/src/main/java/world/bentobox/bentobox/lists/Flags.java @@ -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 diff --git a/src/main/java/world/bentobox/bentobox/managers/CommandsManager.java b/src/main/java/world/bentobox/bentobox/managers/CommandsManager.java index 4c73f47b6..017c253d1 100644 --- a/src/main/java/world/bentobox/bentobox/managers/CommandsManager.java +++ b/src/main/java/world/bentobox/bentobox/managers/CommandsManager.java @@ -44,4 +44,11 @@ public class CommandsManager { return commands.keySet(); } + /** + * @return the commands + */ + public HashMap getCommands() { + return commands; + } + } diff --git a/src/main/java/world/bentobox/bentobox/managers/RanksManager.java b/src/main/java/world/bentobox/bentobox/managers/RanksManager.java index 436542e88..b67219afa 100644 --- a/src/main/java/world/bentobox/bentobox/managers/RanksManager.java +++ b/src/main/java/world/bentobox/bentobox/managers/RanksManager.java @@ -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(); diff --git a/src/main/java/world/bentobox/bentobox/panels/SettingsPanel.java b/src/main/java/world/bentobox/bentobox/panels/SettingsPanel.java index 1b1dae9ca..85d05fee3 100644 --- a/src/main/java/world/bentobox/bentobox/panels/SettingsPanel.java +++ b/src/main/java/world/bentobox/bentobox/panels/SettingsPanel.java @@ -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); diff --git a/src/main/resources/locales/en-US.yml b/src/main/resources/locales/en-US.yml index 223b741f9..240d3db6b 100644 --- a/src/main/resources/locales/en-US.yml +++ b/src/main/resources/locales/en-US.yml @@ -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" From ca4db42e8d9b1c41a5b4a984fc36e4a90865b474 Mon Sep 17 00:00:00 2001 From: tastybento Date: Sun, 12 Aug 2018 08:02:54 -0700 Subject: [PATCH 2/9] Fixed bug with locale files being saved from BentoBox into addon locales --- .../bentobox/managers/AddonsManager.java | 1 - .../bentobox/managers/LocalesManager.java | 24 ++++++++++++------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/managers/AddonsManager.java b/src/main/java/world/bentobox/bentobox/managers/AddonsManager.java index 6fd92e79f..5b92fcabd 100644 --- a/src/main/java/world/bentobox/bentobox/managers/AddonsManager.java +++ b/src/main/java/world/bentobox/bentobox/managers/AddonsManager.java @@ -192,7 +192,6 @@ public class AddonsManager { * @param jar - the jar file * @param folderPath - the path within the jar * @return a list of files - - if the file cannot be read */ public List listJarYamlFiles(JarFile jar, String folderPath) { List result = new ArrayList<>(); diff --git a/src/main/java/world/bentobox/bentobox/managers/LocalesManager.java b/src/main/java/world/bentobox/bentobox/managers/LocalesManager.java index d1b9aba6b..cf4cf430e 100644 --- a/src/main/java/world/bentobox/bentobox/managers/LocalesManager.java +++ b/src/main/java/world/bentobox/bentobox/managers/LocalesManager.java @@ -30,6 +30,7 @@ public class LocalesManager { public LocalesManager(BentoBox plugin) { this.plugin = plugin; + loadLocalesFromJar("BentoBox"); loadLocales("BentoBox"); // Default } @@ -55,15 +56,7 @@ public class LocalesManager { return null; } - /** - * Loads all the locales available. If the locale folder does not exist, one will be created and - * filled with locale files from the jar. - */ - public void loadLocales(String parent) { - // Describe the filter - we only want files that are correctly named - // Files must be 9 chars long - FilenameFilter ymlFilter = (dir, name) -> name.toLowerCase(java.util.Locale.ENGLISH).endsWith(".yml") && name.length() == 9; - + public void loadLocalesFromJar(String parent) { // Run through the files and store the locales File localeDir = new File(plugin.getDataFolder(), LOCALE_FOLDER + File.separator + parent); // If the folder does not exist, then make it and fill with the locale files from the jar @@ -83,6 +76,19 @@ public class LocalesManager { plugin.logError("Could not copy locale files from jar " + e.getMessage()); } } + } + + /** + * Loads all the locales available. If the locale folder does not exist, one will be created and + * filled with locale files from the jar. + */ + public void loadLocales(String parent) { + // Describe the filter - we only want files that are correctly named + // Files must be 9 chars long + FilenameFilter ymlFilter = (dir, name) -> name.toLowerCase(java.util.Locale.ENGLISH).endsWith(".yml") && name.length() == 9; + + // Run through the files and store the locales + File localeDir = new File(plugin.getDataFolder(), LOCALE_FOLDER + File.separator + parent); // Store all the locales available for (File language : Objects.requireNonNull(localeDir.listFiles(ymlFilter))) { From 82c35f9aa94fa5057ea226cd3d4e340f58e3381c Mon Sep 17 00:00:00 2001 From: tastybento Date: Sun, 12 Aug 2018 14:28:25 -0700 Subject: [PATCH 3/9] Fixed test breaks. --- .../admin/AdminClearResetsAllCommand.java | 1 - .../admin/AdminClearResetsAllCommandTest.java | 2 +- .../commands/admin/AdminDeleteCommandTest.java | 2 +- .../api/commands/island/IslandBanCommandTest.java | 9 +++++++-- .../commands/island/IslandResetCommandTest.java | 2 +- .../commands/island/IslandUnbanCommandTest.java | 9 +++++++-- .../island/team/IslandTeamInviteCommandTest.java | 15 ++++++++++++--- .../island/team/IslandTeamKickCommandTest.java | 2 +- .../island/team/IslandTeamLeaveCommandTest.java | 2 +- .../api/flags/clicklisteners/CycleClickTest.java | 3 +-- .../clicklisteners/IslandToggleClickTest.java | 3 +-- .../clicklisteners/WorldToggleClickTest.java | 3 +-- .../bentobox/managers/RanksManagerTest.java | 8 ++++---- 13 files changed, 38 insertions(+), 23 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminClearResetsAllCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminClearResetsAllCommand.java index 364078ccd..a4cd16540 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminClearResetsAllCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminClearResetsAllCommand.java @@ -22,7 +22,6 @@ public class AdminClearResetsAllCommand extends CompositeCommand { @Override public boolean execute(User user, String label, List args) { - // If args are not right, show help if (!args.isEmpty()) { showHelp(this, user); return false; diff --git a/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminClearResetsAllCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminClearResetsAllCommandTest.java index a6a101468..8d539a5fa 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminClearResetsAllCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminClearResetsAllCommandTest.java @@ -113,7 +113,7 @@ public class AdminClearResetsAllCommandTest { AdminClearResetsAllCommand itl = new AdminClearResetsAllCommand(ac); assertFalse(itl.execute(user, itl.getLabel(), new ArrayList<>(Arrays.asList("tastybento", "bobby")))); assertTrue(itl.execute(user, itl.getLabel(), new ArrayList<>())); - Mockito.verify(user).sendMessage(Mockito.eq("general.confirm"), Mockito.eq("[seconds]"), Mockito.any()); + Mockito.verify(user).sendMessage(Mockito.eq("commands.confirmation.confirm"), Mockito.eq("[seconds]"), Mockito.any()); } } diff --git a/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminDeleteCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminDeleteCommandTest.java index 6ea414f61..3f3c1f4e5 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminDeleteCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminDeleteCommandTest.java @@ -189,7 +189,7 @@ public class AdminDeleteCommandTest { AdminDeleteCommand itl = new AdminDeleteCommand(ac); // First requires confirmation assertFalse(itl.execute(user, itl.getLabel(), Arrays.asList(name))); - Mockito.verify(user).sendMessage("general.confirm", "[seconds]", "0"); + Mockito.verify(user).sendMessage("commands.confirmation.confirm", "[seconds]", "0"); // Confirm itl.execute(user, itl.getLabel(), Arrays.asList(name)); } diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandBanCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandBanCommandTest.java index 517f471c8..12efdaecc 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandBanCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandBanCommandTest.java @@ -44,6 +44,7 @@ import world.bentobox.bentobox.managers.CommandsManager; import world.bentobox.bentobox.managers.IslandWorldManager; import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.PlayersManager; +import world.bentobox.bentobox.managers.RanksManager; /** * @author tastybento @@ -76,6 +77,7 @@ public class IslandBanCommandTest { // Settings Settings s = mock(Settings.class); when(s.getResetWait()).thenReturn(0L); + when(s.getRankCommand(Mockito.anyString())).thenReturn(RanksManager.OWNER_RANK); when(plugin.getSettings()).thenReturn(s); // Player @@ -112,6 +114,8 @@ public class IslandBanCommandTest { island = mock(Island.class); when(island.getBanned()).thenReturn(new HashSet<>()); when(island.isBanned(Mockito.any())).thenReturn(false); + when(island.getRank(Mockito.any())).thenReturn(RanksManager.OWNER_RANK); + when(im.getIsland(Mockito.any(), Mockito.any(User.class))).thenReturn(island); when(im.getIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(island); // IWM friendly name @@ -153,11 +157,12 @@ public class IslandBanCommandTest { } @Test - public void testNotOwner() { + public void testTooLowRank() { IslandBanCommand ibc = new IslandBanCommand(ic); when(im.hasIsland(Mockito.any(), Mockito.eq(uuid))).thenReturn(true); + when(island.getRank(Mockito.any())).thenReturn(RanksManager.MEMBER_RANK); assertFalse(ibc.execute(user, ibc.getLabel(), Collections.singletonList("bill"))); - Mockito.verify(user).sendMessage("general.errors.not-leader"); + Mockito.verify(user).sendMessage("general.errors.no-permission"); } @Test diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandResetCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandResetCommandTest.java index 3176339f5..6b06fe649 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandResetCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandResetCommandTest.java @@ -276,7 +276,7 @@ public class IslandResetCommandTest { // Reset assertTrue(irc.execute(user, irc.getLabel(), new ArrayList<>())); // Check for message - Mockito.verify(user).sendMessage("general.confirm", "[seconds]", String.valueOf(s.getConfirmationTime())); + Mockito.verify(user).sendMessage("commands.confirmation.confirm", "[seconds]", String.valueOf(s.getConfirmationTime())); // Send command again to confirm assertTrue(irc.execute(user, irc.getLabel(), new ArrayList<>())); diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandUnbanCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandUnbanCommandTest.java index 2043d53eb..121ad690d 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandUnbanCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandUnbanCommandTest.java @@ -41,6 +41,7 @@ import world.bentobox.bentobox.managers.CommandsManager; import world.bentobox.bentobox.managers.IslandWorldManager; import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.PlayersManager; +import world.bentobox.bentobox.managers.RanksManager; /** * @author tastybento @@ -73,6 +74,7 @@ public class IslandUnbanCommandTest { // Settings Settings s = mock(Settings.class); when(s.getResetWait()).thenReturn(0L); + when(s.getRankCommand(Mockito.anyString())).thenReturn(RanksManager.OWNER_RANK); when(plugin.getSettings()).thenReturn(s); @@ -110,6 +112,8 @@ public class IslandUnbanCommandTest { island = mock(Island.class); when(island.getBanned()).thenReturn(new HashSet<>()); when(island.isBanned(Mockito.any())).thenReturn(false); + when(island.getRank(Mockito.any())).thenReturn(RanksManager.OWNER_RANK); + when(im.getIsland(Mockito.any(), Mockito.any(User.class))).thenReturn(island); when(im.getIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(island); // IWM friendly name @@ -147,11 +151,12 @@ public class IslandUnbanCommandTest { } @Test - public void testNotOwner() { + public void testTooLowRank() { IslandUnbanCommand iubc = new IslandUnbanCommand(ic); when(im.hasIsland(Mockito.any(), Mockito.eq(uuid))).thenReturn(true); + when(island.getRank(Mockito.any())).thenReturn(RanksManager.MEMBER_RANK); assertFalse(iubc.execute(user, iubc.getLabel(), Collections.singletonList("bill"))); - Mockito.verify(user).sendMessage("general.errors.not-leader"); + Mockito.verify(user).sendMessage("general.errors.no-permission"); } @Test diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamInviteCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamInviteCommandTest.java index 144a9e4ab..9e1a5520a 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamInviteCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamInviteCommandTest.java @@ -28,11 +28,13 @@ import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.user.User; +import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.managers.CommandsManager; import world.bentobox.bentobox.managers.IslandWorldManager; import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.LocalesManager; import world.bentobox.bentobox.managers.PlayersManager; +import world.bentobox.bentobox.managers.RanksManager; /** * @author tastybento @@ -49,6 +51,7 @@ public class IslandTeamInviteCommandTest { private PlayersManager pm; private UUID notUUID; private Settings s; + private Island island; /** * @throws java.lang.Exception @@ -66,6 +69,7 @@ public class IslandTeamInviteCommandTest { // Settings s = mock(Settings.class); when(s.getResetWait()).thenReturn(0L); + when(s.getRankCommand(Mockito.anyString())).thenReturn(RanksManager.OWNER_RANK); when(plugin.getSettings()).thenReturn(s); @@ -91,8 +95,12 @@ public class IslandTeamInviteCommandTest { // Player has island to begin with im = mock(IslandsManager.class); when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true); + when(im.inTeam(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true); when(im.isOwner(Mockito.any(), Mockito.any())).thenReturn(true); when(im.getTeamLeader(Mockito.any(), Mockito.any())).thenReturn(uuid); + island = mock(Island.class); + when(island.getRank(Mockito.any())).thenReturn(RanksManager.OWNER_RANK); + when(im.getIsland(Mockito.any(), Mockito.any(User.class))).thenReturn(island); when(plugin.getIslands()).thenReturn(im); // Has team @@ -125,6 +133,7 @@ public class IslandTeamInviteCommandTest { @Test public void testExecuteNoIsland() { when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(false); + when(im.inTeam(Mockito.any(), Mockito.any(UUID.class))).thenReturn(false); IslandTeamInviteCommand itl = new IslandTeamInviteCommand(ic); assertFalse(itl.execute(user, itl.getLabel(), new ArrayList<>())); Mockito.verify(user).sendMessage(Mockito.eq("general.errors.no-island")); @@ -134,11 +143,11 @@ public class IslandTeamInviteCommandTest { * Test method for . */ @Test - public void testExecuteNotTeamLeader() { - when(im.getTeamLeader(Mockito.any(), Mockito.any())).thenReturn(notUUID); + public void testExecuteLowRank() { + when(island.getRank(Mockito.any())).thenReturn(RanksManager.MEMBER_RANK); IslandTeamInviteCommand itl = new IslandTeamInviteCommand(ic); assertFalse(itl.execute(user, itl.getLabel(), new ArrayList<>())); - Mockito.verify(user).sendMessage(Mockito.eq("general.errors.not-leader")); + Mockito.verify(user).sendMessage(Mockito.eq("general.errors.no-permission")); } /** diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamKickCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamKickCommandTest.java index 8b3d0133e..ff9ab37f5 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamKickCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamKickCommandTest.java @@ -236,7 +236,7 @@ public class IslandTeamKickCommandTest { IslandTeamKickCommand itl = new IslandTeamKickCommand(ic); assertFalse(itl.execute(user, itl.getLabel(), Arrays.asList(name))); // Confirmation required - Mockito.verify(user).sendMessage(Mockito.eq("general.confirm"), Mockito.eq("[seconds]"), Mockito.eq("0")); + Mockito.verify(user).sendMessage(Mockito.eq("commands.confirmation.confirm"), Mockito.eq("[seconds]"), Mockito.eq("0")); } /** diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamLeaveCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamLeaveCommandTest.java index 29572a702..4752069d9 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamLeaveCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamLeaveCommandTest.java @@ -159,7 +159,7 @@ public class IslandTeamLeaveCommandTest { IslandTeamLeaveCommand itl = new IslandTeamLeaveCommand(ic); assertFalse(itl.execute(user, itl.getLabel(), new ArrayList<>())); // Confirmation required - Mockito.verify(user).sendMessage(Mockito.eq("general.confirm"), Mockito.eq("[seconds]"), Mockito.eq("0")); + Mockito.verify(user).sendMessage(Mockito.eq("commands.confirmation.confirm"), Mockito.eq("[seconds]"), Mockito.eq("0")); } /** diff --git a/src/test/java/world/bentobox/bentobox/api/flags/clicklisteners/CycleClickTest.java b/src/test/java/world/bentobox/bentobox/api/flags/clicklisteners/CycleClickTest.java index 0a9a48f7e..53b707c0a 100644 --- a/src/test/java/world/bentobox/bentobox/api/flags/clicklisteners/CycleClickTest.java +++ b/src/test/java/world/bentobox/bentobox/api/flags/clicklisteners/CycleClickTest.java @@ -207,8 +207,7 @@ public class CycleClickTest { when(user.hasPermission(Mockito.anyString())).thenReturn(false); CycleClick udc = new CycleClick("LOCK"); assertTrue(udc.onClick(panel, user, ClickType.LEFT, 5)); - Mockito.verify(user).sendMessage(Mockito.eq("general.errors.no-permission")); - Mockito.verify(user).sendMessage(Mockito.eq("general.errors.you-need"), Mockito.eq("[permission]"), Mockito.eq("bskyblock.settings.LOCK")); + Mockito.verify(user).sendMessage(Mockito.eq("general.errors.no-permission"), Mockito.eq("[permission]"), Mockito.eq("bskyblock.settings.LOCK")); } @Test diff --git a/src/test/java/world/bentobox/bentobox/api/flags/clicklisteners/IslandToggleClickTest.java b/src/test/java/world/bentobox/bentobox/api/flags/clicklisteners/IslandToggleClickTest.java index 6853a6e82..7e5e9a727 100644 --- a/src/test/java/world/bentobox/bentobox/api/flags/clicklisteners/IslandToggleClickTest.java +++ b/src/test/java/world/bentobox/bentobox/api/flags/clicklisteners/IslandToggleClickTest.java @@ -102,8 +102,7 @@ public class IslandToggleClickTest { public void testOnClickNoPermission() { when(user.hasPermission(Mockito.anyString())).thenReturn(false); listener.onClick(panel, user, ClickType.LEFT, 0); - Mockito.verify(user).sendMessage("general.errors.no-permission"); - Mockito.verify(user).sendMessage("general.errors.you-need", "[permission]", "bskyblock.settings.test"); + Mockito.verify(user).sendMessage("general.errors.no-permission", "[permission]", "bskyblock.settings.test"); } @Test diff --git a/src/test/java/world/bentobox/bentobox/api/flags/clicklisteners/WorldToggleClickTest.java b/src/test/java/world/bentobox/bentobox/api/flags/clicklisteners/WorldToggleClickTest.java index 2709f76ce..fc191096b 100644 --- a/src/test/java/world/bentobox/bentobox/api/flags/clicklisteners/WorldToggleClickTest.java +++ b/src/test/java/world/bentobox/bentobox/api/flags/clicklisteners/WorldToggleClickTest.java @@ -86,8 +86,7 @@ public class WorldToggleClickTest { public void testOnClickNoPermission() { when(user.hasPermission(Mockito.anyString())).thenReturn(false); listener.onClick(panel, user, ClickType.LEFT, 0); - Mockito.verify(user).sendMessage("general.errors.no-permission"); - Mockito.verify(user).sendMessage("general.errors.you-need", "[permission]", "bskyblock.admin.world.settings.test"); + Mockito.verify(user).sendMessage("general.errors.no-permission", "[permission]", "bskyblock.admin.world.settings.test"); } @Test diff --git a/src/test/java/world/bentobox/bentobox/managers/RanksManagerTest.java b/src/test/java/world/bentobox/bentobox/managers/RanksManagerTest.java index 0d7add716..f15f69269 100644 --- a/src/test/java/world/bentobox/bentobox/managers/RanksManagerTest.java +++ b/src/test/java/world/bentobox/bentobox/managers/RanksManagerTest.java @@ -89,8 +89,8 @@ public class RanksManagerTest { public void testGetNextRankValue() { assertEquals(RanksManager.BANNED_RANK, ranksManager.getRankUpValue(-20)); assertEquals(RanksManager.VISITOR_RANK, ranksManager.getRankUpValue(RanksManager.BANNED_RANK)); - assertEquals(RanksManager.MEMBER_RANK, ranksManager.getRankUpValue(RanksManager.VISITOR_RANK)); - assertEquals(RanksManager.OWNER_RANK, ranksManager.getRankUpValue(RanksManager.MEMBER_RANK)); + assertEquals(RanksManager.COOP_RANK, ranksManager.getRankUpValue(RanksManager.VISITOR_RANK)); + assertEquals(RanksManager.SUB_OWNER_RANK, ranksManager.getRankUpValue(RanksManager.MEMBER_RANK)); assertEquals(RanksManager.OWNER_RANK, ranksManager.getRankUpValue(RanksManager.OWNER_RANK)); assertEquals(RanksManager.OWNER_RANK, ranksManager.getRankUpValue(2000)); } @@ -103,8 +103,8 @@ public class RanksManagerTest { // Lowest rank is Visitor assertEquals(RanksManager.VISITOR_RANK, ranksManager.getRankDownValue(-20)); assertEquals(RanksManager.VISITOR_RANK, ranksManager.getRankDownValue(RanksManager.VISITOR_RANK)); - assertEquals(RanksManager.VISITOR_RANK, ranksManager.getRankDownValue(RanksManager.MEMBER_RANK)); - assertEquals(RanksManager.MEMBER_RANK, ranksManager.getRankDownValue(RanksManager.OWNER_RANK)); + assertEquals(RanksManager.TRUSTED_RANK, ranksManager.getRankDownValue(RanksManager.MEMBER_RANK)); + assertEquals(RanksManager.SUB_OWNER_RANK, ranksManager.getRankDownValue(RanksManager.OWNER_RANK)); } /** From c1a6a233a973855d47b165421953b2763fcf8253 Mon Sep 17 00:00:00 2001 From: tastybento Date: Sun, 12 Aug 2018 16:43:47 -0700 Subject: [PATCH 4/9] Added team coop, uncoop, trust and untrust commands. --- pom.xml | 2 +- .../api/commands/island/IslandBanCommand.java | 4 +- .../commands/island/IslandUnbanCommand.java | 4 +- .../island/team/IslandTeamCommand.java | 7 ++ .../island/team/IslandTeamCoopCommand.java | 106 ++++++++++++++++ .../island/team/IslandTeamInviteCommand.java | 2 +- .../island/team/IslandTeamTrustCommand.java | 111 +++++++++++++++++ .../island/team/IslandTeamUncoopCommand.java | 114 ++++++++++++++++++ .../island/team/IslandTeamUntrustCommand.java | 114 ++++++++++++++++++ .../bentobox/database/objects/Island.java | 28 ++--- .../CommandCycleClick.java | 2 +- .../CommandRankClickListener.java | 8 +- .../GeoLimitClickListener.java | 2 +- .../world/bentobox/bentobox/lists/Flags.java | 4 +- src/main/resources/locales/en-US.yml | 29 ++++- .../commands/island/IslandBanCommandTest.java | 1 + .../island/IslandUnbanCommandTest.java | 1 + 17 files changed, 508 insertions(+), 31 deletions(-) create mode 100644 src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamCoopCommand.java create mode 100644 src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamTrustCommand.java create mode 100644 src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamUncoopCommand.java create mode 100644 src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamUntrustCommand.java rename src/main/java/world/bentobox/bentobox/listeners/flags/{ => clicklisteners}/CommandCycleClick.java (97%) rename src/main/java/world/bentobox/bentobox/listeners/flags/{ => clicklisteners}/CommandRankClickListener.java (95%) rename src/main/java/world/bentobox/bentobox/listeners/flags/{ => clicklisteners}/GeoLimitClickListener.java (98%) diff --git a/pom.xml b/pom.xml index d8bfade1a..05dddf1a9 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ world.bentobox bentobox - FC-0.92 + FC-0.95 BentoBox BentoBox is an expandable Minecraft Spigot plugin for island-type games like ASkyBlock or AcidIsland. diff --git a/src/main/java/world/bentobox/bentobox/api/commands/island/IslandBanCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/island/IslandBanCommand.java index 6637e3cdb..ba7d37937 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/island/IslandBanCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/island/IslandBanCommand.java @@ -38,8 +38,8 @@ public class IslandBanCommand extends CompositeCommand { return false; } UUID playerUUID = user.getUniqueId(); - // Player issuing the command must have an island - if (!getIslands().hasIsland(getWorld(), playerUUID)) { + // Player issuing the command must have an island or be in a team + if (!getIslands().inTeam(getWorld(), user.getUniqueId()) && !getIslands().hasIsland(getWorld(), user.getUniqueId())) { user.sendMessage("general.errors.no-island"); return false; } diff --git a/src/main/java/world/bentobox/bentobox/api/commands/island/IslandUnbanCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/island/IslandUnbanCommand.java index 92e004cdb..773a0722d 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/island/IslandUnbanCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/island/IslandUnbanCommand.java @@ -34,8 +34,8 @@ public class IslandUnbanCommand extends CompositeCommand { return false; } UUID playerUUID = user.getUniqueId(); - // Player issuing the command must have an island - if (!getIslands().hasIsland(getWorld(), playerUUID)) { + // Player issuing the command must have an island or be in a team + if (!getIslands().inTeam(getWorld(), user.getUniqueId()) && !getIslands().hasIsland(getWorld(), user.getUniqueId())) { user.sendMessage("general.errors.no-island"); return false; } diff --git a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamCommand.java index b7ef12129..476692dfb 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamCommand.java @@ -30,6 +30,13 @@ public class IslandTeamCommand extends CompositeCommand { new IslandTeamKickCommand(this); new IslandTeamInviteAcceptCommand(this); new IslandTeamInviteRejectCommand(this); + new IslandTeamCoopCommand(this); + new IslandTeamUncoopCommand(this); + new IslandTeamTrustCommand(this); + new IslandTeamUntrustCommand(this); + new IslandTeamPromoteCommand(this, "promote"); + new IslandTeamPromoteCommand(this, "demote"); + } @Override diff --git a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamCoopCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamCoopCommand.java new file mode 100644 index 000000000..bf359135f --- /dev/null +++ b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamCoopCommand.java @@ -0,0 +1,106 @@ +package world.bentobox.bentobox.api.commands.island.team; + +import java.util.List; +import java.util.Optional; +import java.util.UUID; +import java.util.stream.Collectors; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; + +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; +import world.bentobox.bentobox.managers.RanksManager; +import world.bentobox.bentobox.util.Util; + +/** + * Command to coop another player + * @author tastybento + * + */ +public class IslandTeamCoopCommand extends CompositeCommand { + + public IslandTeamCoopCommand(CompositeCommand parentCommand) { + super(parentCommand, "coop"); + } + + @Override + public void setup() { + setPermission("island.team.coop"); + setOnlyPlayer(true); + setParametersHelp("commands.island.team.coop.parameters"); + setDescription("commands.island.team.coop.description"); + setConfigurableRankCommand(); + } + + @Override + public boolean execute(User user, String label, List args) { + if (args.size() != 1) { + // Show help + showHelp(this, user); + return false; + } + // Player issuing the command must have an island or be in a team + if (!getIslands().inTeam(getWorld(), user.getUniqueId()) && !getIslands().hasIsland(getWorld(), user.getUniqueId())) { + user.sendMessage("general.errors.no-island"); + return false; + } + // 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 + UUID targetUUID = getPlayers().getUUID(args.get(0)); + if (targetUUID == null) { + user.sendMessage("general.errors.unknown-player"); + return false; + } + if (getSettings().getInviteWait() > 0 && checkCooldown(user, targetUUID)) { + return false; + } + return coopCmd(user, targetUUID); + } + + private boolean coopCmd(User user, UUID targetUUID) { + // Player cannot coop themselves + if (user.getUniqueId().equals(targetUUID)) { + user.sendMessage("commands.island.team.coop.cannot-coop-yourself"); + return false; + } + if (getIslands().getMembers(getWorld(), user.getUniqueId()).contains(targetUUID)) { + user.sendMessage("commands.island.team.coop.already-has-rank"); + return false; + } + User target = User.getInstance(targetUUID); + Island island = getIslands().getIsland(getWorld(), user.getUniqueId()); + if (island != null) { + island.setRank(target, RanksManager.COOP_RANK); + user.sendMessage("general.success"); + target.sendMessage("commands.island.team.coop.you-are-a-coop-member", TextVariables.NAME, user.getName()); + return true; + } else { + // Should not happen + user.sendMessage("general.errors.general"); + return false; + } + } + + @Override + public Optional> tabComplete(User user, String alias, List args) { + if (args.isEmpty()) { + // Don't show every player on the server. Require at least the first letter + return Optional.empty(); + } + Island island = getIslands().getIsland(getWorld(), user.getUniqueId()); + List options = Bukkit.getOnlinePlayers().stream() + .filter(p -> !p.getUniqueId().equals(user.getUniqueId())) + .filter(p -> !island.getMemberSet().contains(p.getUniqueId())) + .filter(p -> user.getPlayer().canSee(p)) + .map(Player::getName).collect(Collectors.toList()); + String lastArg = !args.isEmpty() ? args.get(args.size()-1) : ""; + return Optional.of(Util.tabLimit(options, lastArg)); + } +} diff --git a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamInviteCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamInviteCommand.java index 1658e6747..1ec8d379b 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamInviteCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamInviteCommand.java @@ -38,7 +38,7 @@ public class IslandTeamInviteCommand extends CompositeCommand { @Override public boolean execute(User user, String label, List args) { UUID playerUUID = user.getUniqueId(); - // Player issuing the command must have an island + // Player issuing the command must have an island or be in a team if (!getIslands().inTeam(getWorld(), user.getUniqueId()) && !getIslands().hasIsland(getWorld(), user.getUniqueId())) { user.sendMessage("general.errors.no-island"); return false; diff --git a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamTrustCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamTrustCommand.java new file mode 100644 index 000000000..a45429de4 --- /dev/null +++ b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamTrustCommand.java @@ -0,0 +1,111 @@ +package world.bentobox.bentobox.api.commands.island.team; + +import java.util.List; +import java.util.Optional; +import java.util.UUID; +import java.util.stream.Collectors; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; + +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; +import world.bentobox.bentobox.managers.RanksManager; +import world.bentobox.bentobox.util.Util; + +/** + * Command to trust another player + * @author tastybento + * + */ +public class IslandTeamTrustCommand extends CompositeCommand { + + public IslandTeamTrustCommand(CompositeCommand parentCommand) { + super(parentCommand, "trust"); + } + + @Override + public void setup() { + setPermission("island.team.trust"); + setOnlyPlayer(true); + setParametersHelp("commands.island.team.trust.parameters"); + setDescription("commands.island.team.trust.description"); + setConfigurableRankCommand(); + } + + @Override + public boolean execute(User user, String label, List args) { + if (args.size() != 1) { + // Show help + showHelp(this, user); + return false; + } + // Player issuing the command must have an island or be in a team + if (!getIslands().inTeam(getWorld(), user.getUniqueId()) && !getIslands().hasIsland(getWorld(), user.getUniqueId())) { + user.sendMessage("general.errors.no-island"); + return false; + } + // 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 + UUID targetUUID = getPlayers().getUUID(args.get(0)); + if (targetUUID == null) { + user.sendMessage("general.errors.unknown-player"); + return false; + } + if (getSettings().getInviteWait() > 0 && checkCooldown(user, targetUUID)) { + return false; + } + return trustCmd(user, targetUUID); + } + + private boolean trustCmd(User user, UUID targetUUID) { + // Player cannot trust themselves + if (user.getUniqueId().equals(targetUUID)) { + user.sendMessage("commands.island.team.trust.trust-in-yourself"); + return false; + } + if (getIslands().getMembers(getWorld(), user.getUniqueId()).contains(targetUUID)) { + user.sendMessage("commands.island.team.trust.members-trusted"); + return false; + } + User target = User.getInstance(targetUUID); + int rank = getIslands().getIsland(getWorld(), user).getRank(target); + if (rank >= RanksManager.TRUSTED_RANK) { + user.sendMessage("commands.island.team.trust.player-already-trusted"); + return false; + } + Island island = getIslands().getIsland(getWorld(), user.getUniqueId()); + if (island != null) { + island.setRank(target, RanksManager.TRUSTED_RANK); + user.sendMessage("general.success"); + target.sendMessage("commands.island.team.trust.you-are-trusted", TextVariables.NAME, user.getName()); + return true; + } else { + // Should not happen + user.sendMessage("general.errors.general"); + return false; + } + } + + @Override + public Optional> tabComplete(User user, String alias, List args) { + if (args.isEmpty()) { + // Don't show every player on the server. Require at least the first letter + return Optional.empty(); + } + Island island = getIslands().getIsland(getWorld(), user.getUniqueId()); + List options = Bukkit.getOnlinePlayers().stream() + .filter(p -> !p.getUniqueId().equals(user.getUniqueId())) + .filter(p -> !island.getMemberSet().contains(p.getUniqueId())) + .filter(p -> user.getPlayer().canSee(p)) + .map(Player::getName).collect(Collectors.toList()); + String lastArg = !args.isEmpty() ? args.get(args.size()-1) : ""; + return Optional.of(Util.tabLimit(options, lastArg)); + } +} diff --git a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamUncoopCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamUncoopCommand.java new file mode 100644 index 000000000..1da9a4c4c --- /dev/null +++ b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamUncoopCommand.java @@ -0,0 +1,114 @@ +package world.bentobox.bentobox.api.commands.island.team; + +import java.util.List; +import java.util.Optional; +import java.util.UUID; +import java.util.stream.Collectors; + +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; + +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; +import world.bentobox.bentobox.managers.RanksManager; +import world.bentobox.bentobox.util.Util; + +/** + * Command to uncoop a player + * @author tastybento + * + */ +public class IslandTeamUncoopCommand extends CompositeCommand { + + public IslandTeamUncoopCommand(CompositeCommand parentCommand) { + super(parentCommand, "uncoop"); + } + + @Override + public void setup() { + setPermission("island.team.coop"); + setOnlyPlayer(true); + setParametersHelp("commands.island.team.uncoop.parameters"); + setDescription("commands.island.team.uncoop.description"); + setConfigurableRankCommand(); + } + + @Override + public boolean execute(User user, String label, List args) { + if (args.size() != 1) { + // Show help + showHelp(this, user); + return false; + } + // Player issuing the command must have an island or be in a team + if (!getIslands().inTeam(getWorld(), user.getUniqueId()) && !getIslands().hasIsland(getWorld(), user.getUniqueId())) { + user.sendMessage("general.errors.no-island"); + return false; + } + // 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 + UUID targetUUID = getPlayers().getUUID(args.get(0)); + if (targetUUID == null) { + user.sendMessage("general.errors.unknown-player"); + return false; + } + // Uncoop + return unCoopCmd(user, targetUUID); + } + + private boolean unCoopCmd(User user, UUID targetUUID) { + // Player cannot uncoop themselves + if (user.getUniqueId().equals(targetUUID)) { + user.sendMessage("commands.island.team.coop.cannot-uncoop-yourself"); + return false; + } + if (getIslands().getMembers(getWorld(), user.getUniqueId()).contains(targetUUID)) { + user.sendMessage("commands.island.team.coop.cannot-uncoop-member"); + return false; + } + User target = User.getInstance(targetUUID); + int rank = getIslands().getIsland(getWorld(), user).getRank(target); + if (rank != RanksManager.COOP_RANK) { + user.sendMessage("commands.island.team.uncoop.player-not-coop"); + return false; + } + Island island = getIslands().getIsland(getWorld(), user.getUniqueId()); + if (island != null) { + island.removeMember(targetUUID); + user.sendMessage("general.success"); + target.sendMessage("commands.island.team.coop.you-are-no-longer-a-coop-member", TextVariables.NAME, user.getName()); + // Set cooldown + if (getSettings().getInviteWait() > 0 && getParent() != null) { + getParent().getSubCommand("coop").ifPresent(subCommand -> + subCommand.setCooldown(user.getUniqueId(), targetUUID, getSettings().getInviteWait() * 60)); + } + return true; + } else { + // Should not happen + user.sendMessage("general.errors.general"); + return false; + } + } + + @Override + public Optional> tabComplete(User user, String alias, List args) { + if (args.isEmpty()) { + // Don't show every player on the server. Require at least the first letter + return Optional.empty(); + } + Island island = getIslands().getIsland(getWorld(), user.getUniqueId()); + List options = island.getMemberSet().stream() + .filter(uuid -> island.getRank(User.getInstance(uuid)) == RanksManager.COOP_RANK) + .map(Bukkit::getOfflinePlayer) + .map(OfflinePlayer::getName).collect(Collectors.toList()); + + String lastArg = !args.isEmpty() ? args.get(args.size()-1) : ""; + return Optional.of(Util.tabLimit(options, lastArg)); + } +} diff --git a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamUntrustCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamUntrustCommand.java new file mode 100644 index 000000000..6d369b574 --- /dev/null +++ b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamUntrustCommand.java @@ -0,0 +1,114 @@ +package world.bentobox.bentobox.api.commands.island.team; + +import java.util.List; +import java.util.Optional; +import java.util.UUID; +import java.util.stream.Collectors; + +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; + +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; +import world.bentobox.bentobox.managers.RanksManager; +import world.bentobox.bentobox.util.Util; + +/** + * Command to untrust a player + * @author tastybento + * + */ +public class IslandTeamUntrustCommand extends CompositeCommand { + + public IslandTeamUntrustCommand(CompositeCommand parentCommand) { + super(parentCommand, "untrust"); + } + + @Override + public void setup() { + setPermission("island.team.coop"); + setOnlyPlayer(true); + setParametersHelp("commands.island.team.untrust.parameters"); + setDescription("commands.island.team.untrust.description"); + setConfigurableRankCommand(); + } + + @Override + public boolean execute(User user, String label, List args) { + if (args.size() != 1) { + // Show help + showHelp(this, user); + return false; + } + // Player issuing the command must have an island or be in a team + if (!getIslands().inTeam(getWorld(), user.getUniqueId()) && !getIslands().hasIsland(getWorld(), user.getUniqueId())) { + user.sendMessage("general.errors.no-island"); + return false; + } + // 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 + UUID targetUUID = getPlayers().getUUID(args.get(0)); + if (targetUUID == null) { + user.sendMessage("general.errors.unknown-player"); + return false; + } + // untrust + return unTrustCmd(user, targetUUID); + } + + private boolean unTrustCmd(User user, UUID targetUUID) { + // Player cannot untrust themselves + if (user.getUniqueId().equals(targetUUID)) { + user.sendMessage("commands.island.team.untrust.cannot-untrust-yourself"); + return false; + } + if (getIslands().getMembers(getWorld(), user.getUniqueId()).contains(targetUUID)) { + user.sendMessage("commands.island.team.untrust.cannot-untrust-member"); + return false; + } + User target = User.getInstance(targetUUID); + int rank = getIslands().getIsland(getWorld(), user).getRank(target); + if (rank != RanksManager.TRUSTED_RANK) { + user.sendMessage("commands.island.team.untrust.player-not-trusted"); + return false; + } + Island island = getIslands().getIsland(getWorld(), user.getUniqueId()); + if (island != null) { + island.removeMember(targetUUID); + user.sendMessage("general.success"); + target.sendMessage("commands.island.team.untrust.you-are-no-longer-trusted", TextVariables.NAME, user.getName()); + // Set cooldown + if (getSettings().getInviteWait() > 0 && getParent() != null) { + getParent().getSubCommand("trust").ifPresent(subCommand -> + subCommand.setCooldown(user.getUniqueId(), targetUUID, getSettings().getInviteWait() * 60)); + } + return true; + } else { + // Should not happen + user.sendMessage("general.errors.general"); + return false; + } + } + + @Override + public Optional> tabComplete(User user, String alias, List args) { + if (args.isEmpty()) { + // Don't show every player on the server. Require at least the first letter + return Optional.empty(); + } + Island island = getIslands().getIsland(getWorld(), user.getUniqueId()); + List options = island.getMemberSet().stream() + .filter(uuid -> island.getRank(User.getInstance(uuid)) == RanksManager.TRUSTED_RANK) + .map(Bukkit::getOfflinePlayer) + .map(OfflinePlayer::getName).collect(Collectors.toList()); + + String lastArg = !args.isEmpty() ? args.get(args.size()-1) : ""; + return Optional.of(Util.tabLimit(options, lastArg)); + } +} diff --git a/src/main/java/world/bentobox/bentobox/database/objects/Island.java b/src/main/java/world/bentobox/bentobox/database/objects/Island.java index e6cea2d47..f459e2c1d 100644 --- a/src/main/java/world/bentobox/bentobox/database/objects/Island.java +++ b/src/main/java/world/bentobox/bentobox/database/objects/Island.java @@ -403,8 +403,8 @@ public class Island implements DataObject { } /** - * Removes a player from the team member map. Do not call this directly. - * Use {@link world.bentobox.bentobox.managers.IslandsManager#removePlayer(World, UUID)} + * Removes a player from the team member map. Generally, you should + * use {@link world.bentobox.bentobox.managers.IslandsManager#removePlayer(World, UUID)} * @param playerUUID - uuid of player */ public void removeMember(UUID playerUUID) { @@ -636,20 +636,16 @@ public class Island implements DataObject { * @param world - world to check */ public void showMembers(BentoBox plugin, User user, World world) { - if (plugin.getIslands().inTeam(world, user.getUniqueId())) { - user.sendMessage("commands.admin.info.team-members-title"); - members.forEach((u, i) -> { - if (owner.equals(u)) { - user.sendMessage("commands.admin.info.team-owner-format", TextVariables.NAME, plugin.getPlayers().getName(u) - , "[rank]", user.getTranslation(plugin.getRanksManager().getRank(i))); - } else if (i > RanksManager.VISITOR_RANK){ - user.sendMessage("commands.admin.info.team-member-format", TextVariables.NAME, plugin.getPlayers().getName(u) - , "[rank]", user.getTranslation(plugin.getRanksManager().getRank(i))); - } - }); - } - - + user.sendMessage("commands.admin.info.team-members-title"); + members.forEach((u, i) -> { + if (owner.equals(u)) { + user.sendMessage("commands.admin.info.team-owner-format", TextVariables.NAME, plugin.getPlayers().getName(u) + , "[rank]", user.getTranslation(plugin.getRanksManager().getRank(i))); + } else if (i > RanksManager.VISITOR_RANK){ + user.sendMessage("commands.admin.info.team-member-format", TextVariables.NAME, plugin.getPlayers().getName(u) + , "[rank]", user.getTranslation(plugin.getRanksManager().getRank(i))); + } + }); } /** diff --git a/src/main/java/world/bentobox/bentobox/listeners/flags/CommandCycleClick.java b/src/main/java/world/bentobox/bentobox/listeners/flags/clicklisteners/CommandCycleClick.java similarity index 97% rename from src/main/java/world/bentobox/bentobox/listeners/flags/CommandCycleClick.java rename to src/main/java/world/bentobox/bentobox/listeners/flags/clicklisteners/CommandCycleClick.java index 53ff47f37..b04b042ee 100644 --- a/src/main/java/world/bentobox/bentobox/listeners/flags/CommandCycleClick.java +++ b/src/main/java/world/bentobox/bentobox/listeners/flags/clicklisteners/CommandCycleClick.java @@ -1,7 +1,7 @@ /** * */ -package world.bentobox.bentobox.listeners.flags; +package world.bentobox.bentobox.listeners.flags.clicklisteners; import org.bukkit.Sound; import org.bukkit.event.inventory.ClickType; diff --git a/src/main/java/world/bentobox/bentobox/listeners/flags/CommandRankClickListener.java b/src/main/java/world/bentobox/bentobox/listeners/flags/clicklisteners/CommandRankClickListener.java similarity index 95% rename from src/main/java/world/bentobox/bentobox/listeners/flags/CommandRankClickListener.java rename to src/main/java/world/bentobox/bentobox/listeners/flags/clicklisteners/CommandRankClickListener.java index 8b6499b6a..a8b2d854f 100644 --- a/src/main/java/world/bentobox/bentobox/listeners/flags/CommandRankClickListener.java +++ b/src/main/java/world/bentobox/bentobox/listeners/flags/clicklisteners/CommandRankClickListener.java @@ -1,7 +1,7 @@ /** * */ -package world.bentobox.bentobox.listeners.flags; +package world.bentobox.bentobox.listeners.flags.clicklisteners; import java.util.ArrayList; import java.util.List; @@ -94,11 +94,11 @@ public class CommandRankClickListener implements ClickHandler { 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)); + 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)); + 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)); + pib.description(user.getTranslation("protection.panel.flag-item.minimal-rank") + user.getTranslation(reference)); } }); return pib.build(); diff --git a/src/main/java/world/bentobox/bentobox/listeners/flags/GeoLimitClickListener.java b/src/main/java/world/bentobox/bentobox/listeners/flags/clicklisteners/GeoLimitClickListener.java similarity index 98% rename from src/main/java/world/bentobox/bentobox/listeners/flags/GeoLimitClickListener.java rename to src/main/java/world/bentobox/bentobox/listeners/flags/clicklisteners/GeoLimitClickListener.java index 6d766ab19..9d15cc68e 100644 --- a/src/main/java/world/bentobox/bentobox/listeners/flags/GeoLimitClickListener.java +++ b/src/main/java/world/bentobox/bentobox/listeners/flags/clicklisteners/GeoLimitClickListener.java @@ -1,7 +1,7 @@ /** * */ -package world.bentobox.bentobox.listeners.flags; +package world.bentobox.bentobox.listeners.flags.clicklisteners; import java.util.Arrays; import java.util.Comparator; diff --git a/src/main/java/world/bentobox/bentobox/lists/Flags.java b/src/main/java/world/bentobox/bentobox/lists/Flags.java index 7385c9101..97785b4bd 100644 --- a/src/main/java/world/bentobox/bentobox/lists/Flags.java +++ b/src/main/java/world/bentobox/bentobox/lists/Flags.java @@ -16,7 +16,6 @@ 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; @@ -24,7 +23,6 @@ import world.bentobox.bentobox.listeners.flags.EndermanListener; import world.bentobox.bentobox.listeners.flags.EnterExitListener; import world.bentobox.bentobox.listeners.flags.EntityInteractListener; import world.bentobox.bentobox.listeners.flags.FireListener; -import world.bentobox.bentobox.listeners.flags.GeoLimitClickListener; import world.bentobox.bentobox.listeners.flags.GeoLimitMobsListener; import world.bentobox.bentobox.listeners.flags.HurtingListener; import world.bentobox.bentobox.listeners.flags.InventoryListener; @@ -45,6 +43,8 @@ import world.bentobox.bentobox.listeners.flags.RemoveMobsListener; import world.bentobox.bentobox.listeners.flags.ShearingListener; import world.bentobox.bentobox.listeners.flags.TNTListener; import world.bentobox.bentobox.listeners.flags.TeleportationListener; +import world.bentobox.bentobox.listeners.flags.clicklisteners.CommandRankClickListener; +import world.bentobox.bentobox.listeners.flags.clicklisteners.GeoLimitClickListener; import world.bentobox.bentobox.managers.RanksManager; public class Flags { diff --git a/src/main/resources/locales/en-US.yml b/src/main/resources/locales/en-US.yml index 92d772ed3..92b055c59 100644 --- a/src/main/resources/locales/en-US.yml +++ b/src/main/resources/locales/en-US.yml @@ -226,12 +226,39 @@ commands: description: "manage your team" info: description: "display detailed info about your team" + coop: + description: "make a player coop rank on your island" + paramters: "" + cannot-coop-yourself: "&cYou cannot coop yourself!" + already-has-rank: "&cPlayer already has a rank!" + you-are-a-coop-member: "&2You were cooped by [name]" + uncoop: + description: "remove a coop rank from player" + paramters: "" + cannot-uncoop-yourself: "&cYou cannot uncoop yourself!" + cannot-uncoop-member: "&cYou cannot uncoop a team member!" + player-not-cooped: "&cPlayer is not cooped!" + you-are-no-longer-a-coop-member: "&cYou are no longer a coop member of [name]'s island" + trust: + description: "give a player trusted rank on your island" + paramters: "" + trust-in-yourself: "&cTrust in yourself!" + members-trusted: "&cMembers are already trusted" + player-already-trusted: "&cPlayer is already trusted!" + you-are-trusted: "&2You are trusted by [name]!" + untrust: + description: "remove trusted player rank from player" + paramters: "" + cannot-untrust-yourself: "&cYou cannot untrust yourself!" + cannot-untrust-member: "&cYou cannot untrust a team member!" + player-not-trusted: "&cPlayer is not trusted!" + you-are-no-longer-trusted: "&cYou are no longer trusted by [name]!" invite: description: "invite a player to join your island" invitation-sent: "Invitation sent to [name]" removing-invite: "Removing invite" name-has-invited-you: "[name] has invited you to join their island." - to-accept-or-reject: "Do /island team accept to accept, or /island team reject to reject" + to-accept-or-reject: "Do /[label] team accept to accept, or /[label] team reject to reject" you-will-lose-your-island: "&cWARNING! You will lose your island if you accept!" errors: cannot-invite-self: "&cYou cannot invite yourself!" diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandBanCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandBanCommandTest.java index 12efdaecc..a14044f56 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandBanCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandBanCommandTest.java @@ -151,6 +151,7 @@ public class IslandBanCommandTest { @Test public void testNoIsland() { + when(im.inTeam(Mockito.any(), Mockito.eq(uuid))).thenReturn(false); IslandBanCommand ibc = new IslandBanCommand(ic); assertFalse(ibc.execute(user, ibc.getLabel(), Collections.singletonList("bill"))); Mockito.verify(user).sendMessage("general.errors.no-island"); diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandUnbanCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandUnbanCommandTest.java index 121ad690d..23849c550 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandUnbanCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandUnbanCommandTest.java @@ -145,6 +145,7 @@ public class IslandUnbanCommandTest { @Test public void testNoIsland() { + when(im.inTeam(Mockito.any(), Mockito.eq(uuid))).thenReturn(false); IslandUnbanCommand iubc = new IslandUnbanCommand(ic); assertFalse(iubc.execute(user, iubc.getLabel(), Collections.singletonList("bill"))); Mockito.verify(user).sendMessage("general.errors.no-island"); From 6eeff3f3832fcf2011e4204ae6c1b6a5ae873383 Mon Sep 17 00:00:00 2001 From: tastybento Date: Sun, 12 Aug 2018 17:15:11 -0700 Subject: [PATCH 5/9] Added test classes for coop, uncoop, trust and untrust commands. --- .../island/team/IslandTeamCoopCommand.java | 12 +- .../island/team/IslandTeamUncoopCommand.java | 6 +- .../team/IslandTeamCoopCommandTest.java | 220 ++++++++++++++++++ .../team/IslandTeamTrustCommandTest.java | 220 ++++++++++++++++++ .../team/IslandTeamUncoopCommandTest.java | 220 ++++++++++++++++++ .../team/IslandTeamUntrustCommandTest.java | 220 ++++++++++++++++++ 6 files changed, 884 insertions(+), 14 deletions(-) create mode 100644 src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamCoopCommandTest.java create mode 100644 src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamTrustCommandTest.java create mode 100644 src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamUncoopCommandTest.java create mode 100644 src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamUntrustCommandTest.java diff --git a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamCoopCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamCoopCommand.java index bf359135f..d304bb48b 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamCoopCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamCoopCommand.java @@ -3,10 +3,6 @@ package world.bentobox.bentobox.api.commands.island.team; import java.util.List; import java.util.Optional; import java.util.UUID; -import java.util.stream.Collectors; - -import org.bukkit.Bukkit; -import org.bukkit.entity.Player; import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.localization.TextVariables; @@ -94,13 +90,7 @@ public class IslandTeamCoopCommand extends CompositeCommand { // Don't show every player on the server. Require at least the first letter return Optional.empty(); } - Island island = getIslands().getIsland(getWorld(), user.getUniqueId()); - List options = Bukkit.getOnlinePlayers().stream() - .filter(p -> !p.getUniqueId().equals(user.getUniqueId())) - .filter(p -> !island.getMemberSet().contains(p.getUniqueId())) - .filter(p -> user.getPlayer().canSee(p)) - .map(Player::getName).collect(Collectors.toList()); String lastArg = !args.isEmpty() ? args.get(args.size()-1) : ""; - return Optional.of(Util.tabLimit(options, lastArg)); + return Optional.of(Util.tabLimit(Util.getOnlinePlayerList(user), lastArg)); } } diff --git a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamUncoopCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamUncoopCommand.java index 1da9a4c4c..c54f79861 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamUncoopCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamUncoopCommand.java @@ -65,11 +65,11 @@ public class IslandTeamUncoopCommand extends CompositeCommand { private boolean unCoopCmd(User user, UUID targetUUID) { // Player cannot uncoop themselves if (user.getUniqueId().equals(targetUUID)) { - user.sendMessage("commands.island.team.coop.cannot-uncoop-yourself"); + user.sendMessage("commands.island.team.uncoop.cannot-uncoop-yourself"); return false; } if (getIslands().getMembers(getWorld(), user.getUniqueId()).contains(targetUUID)) { - user.sendMessage("commands.island.team.coop.cannot-uncoop-member"); + user.sendMessage("commands.island.team.uncoop.cannot-uncoop-member"); return false; } User target = User.getInstance(targetUUID); @@ -82,7 +82,7 @@ public class IslandTeamUncoopCommand extends CompositeCommand { if (island != null) { island.removeMember(targetUUID); user.sendMessage("general.success"); - target.sendMessage("commands.island.team.coop.you-are-no-longer-a-coop-member", TextVariables.NAME, user.getName()); + target.sendMessage("commands.island.team.uncoop.you-are-no-longer-a-coop-member", TextVariables.NAME, user.getName()); // Set cooldown if (getSettings().getInviteWait() > 0 && getParent() != null) { getParent().getSubCommand("coop").ifPresent(subCommand -> diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamCoopCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamCoopCommandTest.java new file mode 100644 index 000000000..e1661d579 --- /dev/null +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamCoopCommandTest.java @@ -0,0 +1,220 @@ +/** + * + */ +package world.bentobox.bentobox.api.commands.island.team; + +import static org.junit.Assert.assertFalse; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.UUID; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.scheduler.BukkitScheduler; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +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.Settings; +import world.bentobox.bentobox.api.commands.CompositeCommand; +import world.bentobox.bentobox.api.user.User; +import world.bentobox.bentobox.database.objects.Island; +import world.bentobox.bentobox.managers.CommandsManager; +import world.bentobox.bentobox.managers.IslandWorldManager; +import world.bentobox.bentobox.managers.IslandsManager; +import world.bentobox.bentobox.managers.LocalesManager; +import world.bentobox.bentobox.managers.PlayersManager; +import world.bentobox.bentobox.managers.RanksManager; + +/** + * @author tastybento + * + */ +@RunWith(PowerMockRunner.class) +@PrepareForTest({Bukkit.class, BentoBox.class, User.class }) +public class IslandTeamCoopCommandTest { + + private CompositeCommand ic; + private UUID uuid; + private User user; + private IslandsManager im; + private PlayersManager pm; + private UUID notUUID; + private Settings s; + private Island island; + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + // Set up plugin + BentoBox plugin = mock(BentoBox.class); + Whitebox.setInternalState(BentoBox.class, "instance", plugin); + + // Command manager + CommandsManager cm = mock(CommandsManager.class); + when(plugin.getCommandsManager()).thenReturn(cm); + + // Settings + s = mock(Settings.class); + when(s.getResetWait()).thenReturn(0L); + when(s.getRankCommand(Mockito.anyString())).thenReturn(RanksManager.OWNER_RANK); + + when(plugin.getSettings()).thenReturn(s); + + // Player + Player p = mock(Player.class); + // Sometimes use Mockito.withSettings().verboseLogging() + user = mock(User.class); + when(user.isOp()).thenReturn(false); + uuid = UUID.randomUUID(); + notUUID = UUID.randomUUID(); + while(notUUID.equals(uuid)) { + notUUID = UUID.randomUUID(); + } + when(user.getUniqueId()).thenReturn(uuid); + when(user.getPlayer()).thenReturn(p); + when(user.getName()).thenReturn("tastybento"); + User.setPlugin(plugin); + + // Parent command has no aliases + ic = mock(CompositeCommand.class); + when(ic.getSubCommandAliases()).thenReturn(new HashMap<>()); + + // Player has island to begin with + im = mock(IslandsManager.class); + when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true); + when(im.inTeam(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true); + when(im.isOwner(Mockito.any(), Mockito.any())).thenReturn(true); + when(im.getTeamLeader(Mockito.any(), Mockito.any())).thenReturn(uuid); + island = mock(Island.class); + when(island.getRank(Mockito.any())).thenReturn(RanksManager.OWNER_RANK); + when(im.getIsland(Mockito.any(), Mockito.any(User.class))).thenReturn(island); + when(im.getIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(island); + when(plugin.getIslands()).thenReturn(im); + + // Has team + when(im.inTeam(Mockito.any(), Mockito.eq(uuid))).thenReturn(true); + + // Player Manager + pm = mock(PlayersManager.class); + + when(plugin.getPlayers()).thenReturn(pm); + + // Server & Scheduler + BukkitScheduler sch = mock(BukkitScheduler.class); + PowerMockito.mockStatic(Bukkit.class); + when(Bukkit.getScheduler()).thenReturn(sch); + + // Locales + LocalesManager lm = mock(LocalesManager.class); + when(lm.get(Mockito.any(), Mockito.any())).thenReturn("mock translation"); + when(plugin.getLocalesManager()).thenReturn(lm); + + // IWM friendly name + IslandWorldManager iwm = mock(IslandWorldManager.class); + when(iwm.getFriendlyName(Mockito.any())).thenReturn("BSkyBlock"); + when(plugin.getIWM()).thenReturn(iwm); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamCoopCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecuteNoisland() { + when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(false); + when(im.inTeam(Mockito.any(), Mockito.any(UUID.class))).thenReturn(false); + IslandTeamCoopCommand itl = new IslandTeamCoopCommand(ic); + assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("bill"))); + Mockito.verify(user).sendMessage(Mockito.eq("general.errors.no-island")); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamCoopCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecuteLowRank() { + when(island.getRank(Mockito.any())).thenReturn(RanksManager.MEMBER_RANK); + IslandTeamCoopCommand itl = new IslandTeamCoopCommand(ic); + assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("bill"))); + Mockito.verify(user).sendMessage(Mockito.eq("general.errors.no-permission")); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamCoopCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecuteNoTarget() { + IslandTeamCoopCommand itl = new IslandTeamCoopCommand(ic); + assertFalse(itl.execute(user, itl.getLabel(), new ArrayList<>())); + // Show help + } + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamCoopCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecuteUnknownPlayer() { + IslandTeamCoopCommand itl = new IslandTeamCoopCommand(ic); + when(pm.getUUID(Mockito.any())).thenReturn(null); + assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("tastybento"))); + Mockito.verify(user).sendMessage(Mockito.eq("general.errors.unknown-player")); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamCoopCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecuteSamePlayer() { + PowerMockito.mockStatic(User.class); + when(User.getInstance(Mockito.any(UUID.class))).thenReturn(user); + when(user.isOnline()).thenReturn(true); + IslandTeamCoopCommand itl = new IslandTeamCoopCommand(ic); + when(pm.getUUID(Mockito.any())).thenReturn(uuid); + assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("tastybento"))); + Mockito.verify(user).sendMessage(Mockito.eq("commands.island.team.coop.cannot-coop-yourself")); + } + + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamCoopCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecutePlayerHasRank() { + PowerMockito.mockStatic(User.class); + when(User.getInstance(Mockito.any(UUID.class))).thenReturn(user); + when(user.isOnline()).thenReturn(true); + IslandTeamCoopCommand itl = new IslandTeamCoopCommand(ic); + when(pm.getUUID(Mockito.any())).thenReturn(notUUID); + when(im.inTeam(Mockito.any(), Mockito.any())).thenReturn(true); + when(im.getMembers(Mockito.any(), Mockito.any())).thenReturn(Collections.singleton(notUUID)); + assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("bento"))); + Mockito.verify(user).sendMessage(Mockito.eq("commands.island.team.coop.already-has-rank")); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamCoopCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecuteCoolDownActive() { + // 10 minutes = 600 seconds + when(s.getInviteWait()).thenReturn(10); + IslandTeamCoopCommand itl = new IslandTeamCoopCommand(ic); + String[] name = {"tastybento"}; + itl.execute(user, itl.getLabel(), Arrays.asList(name)); + } + + +} diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamTrustCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamTrustCommandTest.java new file mode 100644 index 000000000..1fd7559c2 --- /dev/null +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamTrustCommandTest.java @@ -0,0 +1,220 @@ +/** + * + */ +package world.bentobox.bentobox.api.commands.island.team; + +import static org.junit.Assert.assertFalse; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.UUID; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.scheduler.BukkitScheduler; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +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.Settings; +import world.bentobox.bentobox.api.commands.CompositeCommand; +import world.bentobox.bentobox.api.user.User; +import world.bentobox.bentobox.database.objects.Island; +import world.bentobox.bentobox.managers.CommandsManager; +import world.bentobox.bentobox.managers.IslandWorldManager; +import world.bentobox.bentobox.managers.IslandsManager; +import world.bentobox.bentobox.managers.LocalesManager; +import world.bentobox.bentobox.managers.PlayersManager; +import world.bentobox.bentobox.managers.RanksManager; + +/** + * @author tastybento + * + */ +@RunWith(PowerMockRunner.class) +@PrepareForTest({Bukkit.class, BentoBox.class, User.class }) +public class IslandTeamTrustCommandTest { + + private CompositeCommand ic; + private UUID uuid; + private User user; + private IslandsManager im; + private PlayersManager pm; + private UUID notUUID; + private Settings s; + private Island island; + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + // Set up plugin + BentoBox plugin = mock(BentoBox.class); + Whitebox.setInternalState(BentoBox.class, "instance", plugin); + + // Command manager + CommandsManager cm = mock(CommandsManager.class); + when(plugin.getCommandsManager()).thenReturn(cm); + + // Settings + s = mock(Settings.class); + when(s.getResetWait()).thenReturn(0L); + when(s.getRankCommand(Mockito.anyString())).thenReturn(RanksManager.OWNER_RANK); + + when(plugin.getSettings()).thenReturn(s); + + // Player + Player p = mock(Player.class); + // Sometimes use Mockito.withSettings().verboseLogging() + user = mock(User.class); + when(user.isOp()).thenReturn(false); + uuid = UUID.randomUUID(); + notUUID = UUID.randomUUID(); + while(notUUID.equals(uuid)) { + notUUID = UUID.randomUUID(); + } + when(user.getUniqueId()).thenReturn(uuid); + when(user.getPlayer()).thenReturn(p); + when(user.getName()).thenReturn("tastybento"); + User.setPlugin(plugin); + + // Parent command has no aliases + ic = mock(CompositeCommand.class); + when(ic.getSubCommandAliases()).thenReturn(new HashMap<>()); + + // Player has island to begin with + im = mock(IslandsManager.class); + when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true); + when(im.inTeam(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true); + when(im.isOwner(Mockito.any(), Mockito.any())).thenReturn(true); + when(im.getTeamLeader(Mockito.any(), Mockito.any())).thenReturn(uuid); + island = mock(Island.class); + when(island.getRank(Mockito.any())).thenReturn(RanksManager.OWNER_RANK); + when(im.getIsland(Mockito.any(), Mockito.any(User.class))).thenReturn(island); + when(im.getIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(island); + when(plugin.getIslands()).thenReturn(im); + + // Has team + when(im.inTeam(Mockito.any(), Mockito.eq(uuid))).thenReturn(true); + + // Player Manager + pm = mock(PlayersManager.class); + + when(plugin.getPlayers()).thenReturn(pm); + + // Server & Scheduler + BukkitScheduler sch = mock(BukkitScheduler.class); + PowerMockito.mockStatic(Bukkit.class); + when(Bukkit.getScheduler()).thenReturn(sch); + + // Locales + LocalesManager lm = mock(LocalesManager.class); + when(lm.get(Mockito.any(), Mockito.any())).thenReturn("mock translation"); + when(plugin.getLocalesManager()).thenReturn(lm); + + // IWM friendly name + IslandWorldManager iwm = mock(IslandWorldManager.class); + when(iwm.getFriendlyName(Mockito.any())).thenReturn("BSkyBlock"); + when(plugin.getIWM()).thenReturn(iwm); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamTrustCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecuteNoisland() { + when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(false); + when(im.inTeam(Mockito.any(), Mockito.any(UUID.class))).thenReturn(false); + IslandTeamTrustCommand itl = new IslandTeamTrustCommand(ic); + assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("bill"))); + Mockito.verify(user).sendMessage(Mockito.eq("general.errors.no-island")); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamTrustCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecuteLowRank() { + when(island.getRank(Mockito.any())).thenReturn(RanksManager.MEMBER_RANK); + IslandTeamTrustCommand itl = new IslandTeamTrustCommand(ic); + assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("bill"))); + Mockito.verify(user).sendMessage(Mockito.eq("general.errors.no-permission")); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamTrustCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecuteNoTarget() { + IslandTeamTrustCommand itl = new IslandTeamTrustCommand(ic); + assertFalse(itl.execute(user, itl.getLabel(), new ArrayList<>())); + // Show help + } + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamTrustCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecuteUnknownPlayer() { + IslandTeamTrustCommand itl = new IslandTeamTrustCommand(ic); + when(pm.getUUID(Mockito.any())).thenReturn(null); + assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("tastybento"))); + Mockito.verify(user).sendMessage(Mockito.eq("general.errors.unknown-player")); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamTrustCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecuteSamePlayer() { + PowerMockito.mockStatic(User.class); + when(User.getInstance(Mockito.any(UUID.class))).thenReturn(user); + when(user.isOnline()).thenReturn(true); + IslandTeamTrustCommand itl = new IslandTeamTrustCommand(ic); + when(pm.getUUID(Mockito.any())).thenReturn(uuid); + assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("tastybento"))); + Mockito.verify(user).sendMessage(Mockito.eq("commands.island.team.trust.trust-in-yourself")); + } + + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamTrustCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecutePlayerHasRank() { + PowerMockito.mockStatic(User.class); + when(User.getInstance(Mockito.any(UUID.class))).thenReturn(user); + when(user.isOnline()).thenReturn(true); + IslandTeamTrustCommand itl = new IslandTeamTrustCommand(ic); + when(pm.getUUID(Mockito.any())).thenReturn(notUUID); + when(im.inTeam(Mockito.any(), Mockito.any())).thenReturn(true); + when(im.getMembers(Mockito.any(), Mockito.any())).thenReturn(Collections.singleton(notUUID)); + assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("bento"))); + Mockito.verify(user).sendMessage(Mockito.eq("commands.island.team.trust.members-trusted")); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamTrustCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecuteCoolDownActive() { + // 10 minutes = 600 seconds + when(s.getInviteWait()).thenReturn(10); + IslandTeamTrustCommand itl = new IslandTeamTrustCommand(ic); + String[] name = {"tastybento"}; + itl.execute(user, itl.getLabel(), Arrays.asList(name)); + } + + +} diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamUncoopCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamUncoopCommandTest.java new file mode 100644 index 000000000..fb8e0de8e --- /dev/null +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamUncoopCommandTest.java @@ -0,0 +1,220 @@ +/** + * + */ +package world.bentobox.bentobox.api.commands.island.team; + +import static org.junit.Assert.assertFalse; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.UUID; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.scheduler.BukkitScheduler; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +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.Settings; +import world.bentobox.bentobox.api.commands.CompositeCommand; +import world.bentobox.bentobox.api.user.User; +import world.bentobox.bentobox.database.objects.Island; +import world.bentobox.bentobox.managers.CommandsManager; +import world.bentobox.bentobox.managers.IslandWorldManager; +import world.bentobox.bentobox.managers.IslandsManager; +import world.bentobox.bentobox.managers.LocalesManager; +import world.bentobox.bentobox.managers.PlayersManager; +import world.bentobox.bentobox.managers.RanksManager; + +/** + * @author tastybento + * + */ +@RunWith(PowerMockRunner.class) +@PrepareForTest({Bukkit.class, BentoBox.class, User.class }) +public class IslandTeamUncoopCommandTest { + + private CompositeCommand ic; + private UUID uuid; + private User user; + private IslandsManager im; + private PlayersManager pm; + private UUID notUUID; + private Settings s; + private Island island; + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + // Set up plugin + BentoBox plugin = mock(BentoBox.class); + Whitebox.setInternalState(BentoBox.class, "instance", plugin); + + // Command manager + CommandsManager cm = mock(CommandsManager.class); + when(plugin.getCommandsManager()).thenReturn(cm); + + // Settings + s = mock(Settings.class); + when(s.getResetWait()).thenReturn(0L); + when(s.getRankCommand(Mockito.anyString())).thenReturn(RanksManager.OWNER_RANK); + + when(plugin.getSettings()).thenReturn(s); + + // Player + Player p = mock(Player.class); + // Sometimes use Mockito.withSettings().verboseLogging() + user = mock(User.class); + when(user.isOp()).thenReturn(false); + uuid = UUID.randomUUID(); + notUUID = UUID.randomUUID(); + while(notUUID.equals(uuid)) { + notUUID = UUID.randomUUID(); + } + when(user.getUniqueId()).thenReturn(uuid); + when(user.getPlayer()).thenReturn(p); + when(user.getName()).thenReturn("tastybento"); + User.setPlugin(plugin); + + // Parent command has no aliases + ic = mock(CompositeCommand.class); + when(ic.getSubCommandAliases()).thenReturn(new HashMap<>()); + + // Player has island to begin with + im = mock(IslandsManager.class); + when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true); + when(im.inTeam(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true); + when(im.isOwner(Mockito.any(), Mockito.any())).thenReturn(true); + when(im.getTeamLeader(Mockito.any(), Mockito.any())).thenReturn(uuid); + island = mock(Island.class); + when(island.getRank(Mockito.any())).thenReturn(RanksManager.OWNER_RANK); + when(im.getIsland(Mockito.any(), Mockito.any(User.class))).thenReturn(island); + when(im.getIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(island); + when(plugin.getIslands()).thenReturn(im); + + // Has team + when(im.inTeam(Mockito.any(), Mockito.eq(uuid))).thenReturn(true); + + // Player Manager + pm = mock(PlayersManager.class); + + when(plugin.getPlayers()).thenReturn(pm); + + // Server & Scheduler + BukkitScheduler sch = mock(BukkitScheduler.class); + PowerMockito.mockStatic(Bukkit.class); + when(Bukkit.getScheduler()).thenReturn(sch); + + // Locales + LocalesManager lm = mock(LocalesManager.class); + when(lm.get(Mockito.any(), Mockito.any())).thenReturn("mock translation"); + when(plugin.getLocalesManager()).thenReturn(lm); + + // IWM friendly name + IslandWorldManager iwm = mock(IslandWorldManager.class); + when(iwm.getFriendlyName(Mockito.any())).thenReturn("BSkyBlock"); + when(plugin.getIWM()).thenReturn(iwm); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamUncoopCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecuteNoisland() { + when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(false); + when(im.inTeam(Mockito.any(), Mockito.any(UUID.class))).thenReturn(false); + IslandTeamUncoopCommand itl = new IslandTeamUncoopCommand(ic); + assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("bill"))); + Mockito.verify(user).sendMessage(Mockito.eq("general.errors.no-island")); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamUncoopCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecuteLowRank() { + when(island.getRank(Mockito.any())).thenReturn(RanksManager.MEMBER_RANK); + IslandTeamUncoopCommand itl = new IslandTeamUncoopCommand(ic); + assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("bill"))); + Mockito.verify(user).sendMessage(Mockito.eq("general.errors.no-permission")); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamUncoopCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecuteNoTarget() { + IslandTeamUncoopCommand itl = new IslandTeamUncoopCommand(ic); + assertFalse(itl.execute(user, itl.getLabel(), new ArrayList<>())); + // Show help + } + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamUncoopCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecuteUnknownPlayer() { + IslandTeamUncoopCommand itl = new IslandTeamUncoopCommand(ic); + when(pm.getUUID(Mockito.any())).thenReturn(null); + assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("tastybento"))); + Mockito.verify(user).sendMessage(Mockito.eq("general.errors.unknown-player")); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamUncoopCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecuteSamePlayer() { + PowerMockito.mockStatic(User.class); + when(User.getInstance(Mockito.any(UUID.class))).thenReturn(user); + when(user.isOnline()).thenReturn(true); + IslandTeamUncoopCommand itl = new IslandTeamUncoopCommand(ic); + when(pm.getUUID(Mockito.any())).thenReturn(uuid); + assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("tastybento"))); + Mockito.verify(user).sendMessage(Mockito.eq("commands.island.team.uncoop.cannot-uncoop-yourself")); + } + + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamUncoopCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecutePlayerHasRank() { + PowerMockito.mockStatic(User.class); + when(User.getInstance(Mockito.any(UUID.class))).thenReturn(user); + when(user.isOnline()).thenReturn(true); + IslandTeamUncoopCommand itl = new IslandTeamUncoopCommand(ic); + when(pm.getUUID(Mockito.any())).thenReturn(notUUID); + when(im.inTeam(Mockito.any(), Mockito.any())).thenReturn(true); + when(im.getMembers(Mockito.any(), Mockito.any())).thenReturn(Collections.singleton(notUUID)); + assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("bento"))); + Mockito.verify(user).sendMessage(Mockito.eq("commands.island.team.uncoop.cannot-uncoop-member")); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamUncoopCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecuteCoolDownActive() { + // 10 minutes = 600 seconds + when(s.getInviteWait()).thenReturn(10); + IslandTeamUncoopCommand itl = new IslandTeamUncoopCommand(ic); + String[] name = {"tastybento"}; + itl.execute(user, itl.getLabel(), Arrays.asList(name)); + } + + +} diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamUntrustCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamUntrustCommandTest.java new file mode 100644 index 000000000..80e03090e --- /dev/null +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamUntrustCommandTest.java @@ -0,0 +1,220 @@ +/** + * + */ +package world.bentobox.bentobox.api.commands.island.team; + +import static org.junit.Assert.assertFalse; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.UUID; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.scheduler.BukkitScheduler; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +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.Settings; +import world.bentobox.bentobox.api.commands.CompositeCommand; +import world.bentobox.bentobox.api.user.User; +import world.bentobox.bentobox.database.objects.Island; +import world.bentobox.bentobox.managers.CommandsManager; +import world.bentobox.bentobox.managers.IslandWorldManager; +import world.bentobox.bentobox.managers.IslandsManager; +import world.bentobox.bentobox.managers.LocalesManager; +import world.bentobox.bentobox.managers.PlayersManager; +import world.bentobox.bentobox.managers.RanksManager; + +/** + * @author tastybento + * + */ +@RunWith(PowerMockRunner.class) +@PrepareForTest({Bukkit.class, BentoBox.class, User.class }) +public class IslandTeamUntrustCommandTest { + + private CompositeCommand ic; + private UUID uuid; + private User user; + private IslandsManager im; + private PlayersManager pm; + private UUID notUUID; + private Settings s; + private Island island; + + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { + // Set up plugin + BentoBox plugin = mock(BentoBox.class); + Whitebox.setInternalState(BentoBox.class, "instance", plugin); + + // Command manager + CommandsManager cm = mock(CommandsManager.class); + when(plugin.getCommandsManager()).thenReturn(cm); + + // Settings + s = mock(Settings.class); + when(s.getResetWait()).thenReturn(0L); + when(s.getRankCommand(Mockito.anyString())).thenReturn(RanksManager.OWNER_RANK); + + when(plugin.getSettings()).thenReturn(s); + + // Player + Player p = mock(Player.class); + // Sometimes use Mockito.withSettings().verboseLogging() + user = mock(User.class); + when(user.isOp()).thenReturn(false); + uuid = UUID.randomUUID(); + notUUID = UUID.randomUUID(); + while(notUUID.equals(uuid)) { + notUUID = UUID.randomUUID(); + } + when(user.getUniqueId()).thenReturn(uuid); + when(user.getPlayer()).thenReturn(p); + when(user.getName()).thenReturn("tastybento"); + User.setPlugin(plugin); + + // Parent command has no aliases + ic = mock(CompositeCommand.class); + when(ic.getSubCommandAliases()).thenReturn(new HashMap<>()); + + // Player has island to begin with + im = mock(IslandsManager.class); + when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true); + when(im.inTeam(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true); + when(im.isOwner(Mockito.any(), Mockito.any())).thenReturn(true); + when(im.getTeamLeader(Mockito.any(), Mockito.any())).thenReturn(uuid); + island = mock(Island.class); + when(island.getRank(Mockito.any())).thenReturn(RanksManager.OWNER_RANK); + when(im.getIsland(Mockito.any(), Mockito.any(User.class))).thenReturn(island); + when(im.getIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(island); + when(plugin.getIslands()).thenReturn(im); + + // Has team + when(im.inTeam(Mockito.any(), Mockito.eq(uuid))).thenReturn(true); + + // Player Manager + pm = mock(PlayersManager.class); + + when(plugin.getPlayers()).thenReturn(pm); + + // Server & Scheduler + BukkitScheduler sch = mock(BukkitScheduler.class); + PowerMockito.mockStatic(Bukkit.class); + when(Bukkit.getScheduler()).thenReturn(sch); + + // Locales + LocalesManager lm = mock(LocalesManager.class); + when(lm.get(Mockito.any(), Mockito.any())).thenReturn("mock translation"); + when(plugin.getLocalesManager()).thenReturn(lm); + + // IWM friendly name + IslandWorldManager iwm = mock(IslandWorldManager.class); + when(iwm.getFriendlyName(Mockito.any())).thenReturn("BSkyBlock"); + when(plugin.getIWM()).thenReturn(iwm); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamUntrustCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecuteNoisland() { + when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(false); + when(im.inTeam(Mockito.any(), Mockito.any(UUID.class))).thenReturn(false); + IslandTeamUntrustCommand itl = new IslandTeamUntrustCommand(ic); + assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("bill"))); + Mockito.verify(user).sendMessage(Mockito.eq("general.errors.no-island")); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamUntrustCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecuteLowRank() { + when(island.getRank(Mockito.any())).thenReturn(RanksManager.MEMBER_RANK); + IslandTeamUntrustCommand itl = new IslandTeamUntrustCommand(ic); + assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("bill"))); + Mockito.verify(user).sendMessage(Mockito.eq("general.errors.no-permission")); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamUntrustCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecuteNoTarget() { + IslandTeamUntrustCommand itl = new IslandTeamUntrustCommand(ic); + assertFalse(itl.execute(user, itl.getLabel(), new ArrayList<>())); + // Show help + } + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamUntrustCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecuteUnknownPlayer() { + IslandTeamUntrustCommand itl = new IslandTeamUntrustCommand(ic); + when(pm.getUUID(Mockito.any())).thenReturn(null); + assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("tastybento"))); + Mockito.verify(user).sendMessage(Mockito.eq("general.errors.unknown-player")); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamUntrustCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecuteSamePlayer() { + PowerMockito.mockStatic(User.class); + when(User.getInstance(Mockito.any(UUID.class))).thenReturn(user); + when(user.isOnline()).thenReturn(true); + IslandTeamUntrustCommand itl = new IslandTeamUntrustCommand(ic); + when(pm.getUUID(Mockito.any())).thenReturn(uuid); + assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("tastybento"))); + Mockito.verify(user).sendMessage(Mockito.eq("commands.island.team.untrust.cannot-untrust-yourself")); + } + + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamUntrustCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecutePlayerHasRank() { + PowerMockito.mockStatic(User.class); + when(User.getInstance(Mockito.any(UUID.class))).thenReturn(user); + when(user.isOnline()).thenReturn(true); + IslandTeamUntrustCommand itl = new IslandTeamUntrustCommand(ic); + when(pm.getUUID(Mockito.any())).thenReturn(notUUID); + when(im.inTeam(Mockito.any(), Mockito.any())).thenReturn(true); + when(im.getMembers(Mockito.any(), Mockito.any())).thenReturn(Collections.singleton(notUUID)); + assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("bento"))); + Mockito.verify(user).sendMessage(Mockito.eq("commands.island.team.untrust.cannot-untrust-member")); + } + + /** + * Test method for {@link world.bentobox.bentobox.api.commands.island.team.IslandTeamUntrustCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}. + */ + @Test + public void testExecuteCoolDownActive() { + // 10 minutes = 600 seconds + when(s.getInviteWait()).thenReturn(10); + IslandTeamUntrustCommand itl = new IslandTeamUntrustCommand(ic); + String[] name = {"tastybento"}; + itl.execute(user, itl.getLabel(), Arrays.asList(name)); + } + + +} From 94c5ac8e79c22c9de0a2a451e9098c0e041a15e9 Mon Sep 17 00:00:00 2001 From: tastybento Date: Sun, 12 Aug 2018 18:53:56 -0700 Subject: [PATCH 6/9] Fixed bug with loading add-on with no locales. Fixed bug with no permission error in click listeners. --- .../flags/clicklisteners/CommandRankClickListener.java | 3 +-- .../listeners/flags/clicklisteners/GeoLimitClickListener.java | 3 +-- .../java/world/bentobox/bentobox/managers/LocalesManager.java | 4 +++- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/listeners/flags/clicklisteners/CommandRankClickListener.java b/src/main/java/world/bentobox/bentobox/listeners/flags/clicklisteners/CommandRankClickListener.java index a8b2d854f..dc4f0081b 100644 --- a/src/main/java/world/bentobox/bentobox/listeners/flags/clicklisteners/CommandRankClickListener.java +++ b/src/main/java/world/bentobox/bentobox/listeners/flags/clicklisteners/CommandRankClickListener.java @@ -46,8 +46,7 @@ public class CommandRankClickListener implements ClickHandler { 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.sendMessage("general.errors.no-permission", "[permission]", reqPerm); user.getPlayer().playSound(user.getLocation(), Sound.BLOCK_METAL_HIT, 1F, 1F); return true; } diff --git a/src/main/java/world/bentobox/bentobox/listeners/flags/clicklisteners/GeoLimitClickListener.java b/src/main/java/world/bentobox/bentobox/listeners/flags/clicklisteners/GeoLimitClickListener.java index 9d15cc68e..7ec288b33 100644 --- a/src/main/java/world/bentobox/bentobox/listeners/flags/clicklisteners/GeoLimitClickListener.java +++ b/src/main/java/world/bentobox/bentobox/listeners/flags/clicklisteners/GeoLimitClickListener.java @@ -49,8 +49,7 @@ public class GeoLimitClickListener implements ClickHandler { IslandWorldManager iwm = BentoBox.getInstance().getIWM(); String reqPerm = iwm.getPermissionPrefix(Util.getWorld(user.getWorld())) + ".admin.settings.GEO_LIMIT_MOBS"; if (!user.hasPermission(reqPerm)) { - user.sendMessage("general.errors.no-permission"); - user.sendMessage("general.errors.you-need", "[permission]", reqPerm); + user.sendMessage("general.errors.no-permission", "[permission]", reqPerm); user.getPlayer().playSound(user.getLocation(), Sound.BLOCK_METAL_HIT, 1F, 1F); return true; } diff --git a/src/main/java/world/bentobox/bentobox/managers/LocalesManager.java b/src/main/java/world/bentobox/bentobox/managers/LocalesManager.java index cf4cf430e..30f3e456e 100644 --- a/src/main/java/world/bentobox/bentobox/managers/LocalesManager.java +++ b/src/main/java/world/bentobox/bentobox/managers/LocalesManager.java @@ -89,7 +89,9 @@ public class LocalesManager { // Run through the files and store the locales File localeDir = new File(plugin.getDataFolder(), LOCALE_FOLDER + File.separator + parent); - + if (!localeDir.exists()) { + return; + } // Store all the locales available for (File language : Objects.requireNonNull(localeDir.listFiles(ymlFilter))) { Locale localeObject = Locale.forLanguageTag(language.getName().substring(0, language.getName().length() - 4)); From 4681a0bd7d2d4c3ca3ebedc02d854fc57a7d5dd2 Mon Sep 17 00:00:00 2001 From: tastybento Date: Sun, 12 Aug 2018 19:52:13 -0700 Subject: [PATCH 7/9] Added BentoBox reload command Only reloads locales for now. --- .../commands/admin/AdminReloadCommand.java | 31 ------------- .../bentobox/commands/BentoBoxCommand.java | 2 + .../commands/BentoBoxReloadCommand.java | 44 +++++++++++++++++++ .../commands/BentoBoxVersionCommand.java | 2 +- .../bentobox/managers/AddonsManager.java | 2 +- .../bentobox/managers/LocalesManager.java | 39 +++++++++++----- src/main/resources/locales/en-US.yml | 2 + src/main/resources/plugin.yml | 5 +++ 8 files changed, 82 insertions(+), 45 deletions(-) delete mode 100644 src/main/java/world/bentobox/bentobox/api/commands/admin/AdminReloadCommand.java create mode 100644 src/main/java/world/bentobox/bentobox/commands/BentoBoxReloadCommand.java diff --git a/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminReloadCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminReloadCommand.java deleted file mode 100644 index 69b716912..000000000 --- a/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminReloadCommand.java +++ /dev/null @@ -1,31 +0,0 @@ -package world.bentobox.bentobox.api.commands.admin; - -import java.util.List; - -import world.bentobox.bentobox.api.commands.CompositeCommand; -import world.bentobox.bentobox.api.user.User; - -/** - * @author tastybento - * - */ -public class AdminReloadCommand extends CompositeCommand { - - /** - * @param parent - parent command - */ - public AdminReloadCommand(CompositeCommand parent) { - super(parent, "reload", "rl"); - } - - @Override - public void setup() { - setDescription("commands.admin.reload.description"); - } - - @Override - public boolean execute(User user, String label, List args) { - return true; - } - -} diff --git a/src/main/java/world/bentobox/bentobox/commands/BentoBoxCommand.java b/src/main/java/world/bentobox/bentobox/commands/BentoBoxCommand.java index fe77af717..00fcaf27f 100644 --- a/src/main/java/world/bentobox/bentobox/commands/BentoBoxCommand.java +++ b/src/main/java/world/bentobox/bentobox/commands/BentoBoxCommand.java @@ -16,8 +16,10 @@ public class BentoBoxCommand extends CompositeCommand { @Override public void setup() { + setPermission("bentobox.admin"); new BentoBoxVersionCommand(this); new BentoBoxAboutCommand(this); + new BentoBoxReloadCommand(this); } @Override diff --git a/src/main/java/world/bentobox/bentobox/commands/BentoBoxReloadCommand.java b/src/main/java/world/bentobox/bentobox/commands/BentoBoxReloadCommand.java new file mode 100644 index 000000000..605371d9e --- /dev/null +++ b/src/main/java/world/bentobox/bentobox/commands/BentoBoxReloadCommand.java @@ -0,0 +1,44 @@ +package world.bentobox.bentobox.commands; + +import java.util.List; + +import world.bentobox.bentobox.api.commands.CompositeCommand; +import world.bentobox.bentobox.api.user.User; + +/** + * Displays information about Gamemodes, Addons and versioning. + * + * @author tastybento + */ +public class BentoBoxReloadCommand extends CompositeCommand { + + /** + * Reloads locales command + * @param parent - command parent + */ + public BentoBoxReloadCommand(CompositeCommand parent) { + super(parent, "reload"); + } + + @Override + public void setup() { + setPermission("admin.reload"); + setDescription("commands.bentobox.reload.description"); + } + + @Override + public boolean execute(User user, String label, List args) { + this.askConfirmation(user, () -> reloadLocales(user)); + return false; + } + + /** + * Reloads the languages + * @param user + */ + public void reloadLocales(User user) { + getPlugin().getLocalesManager().reloadLanguages(); + user.sendMessage("commands.bentobox.reload.locales-reloaded"); + } + +} diff --git a/src/main/java/world/bentobox/bentobox/commands/BentoBoxVersionCommand.java b/src/main/java/world/bentobox/bentobox/commands/BentoBoxVersionCommand.java index d86fb54c7..4a48a9e7e 100644 --- a/src/main/java/world/bentobox/bentobox/commands/BentoBoxVersionCommand.java +++ b/src/main/java/world/bentobox/bentobox/commands/BentoBoxVersionCommand.java @@ -23,7 +23,7 @@ public class BentoBoxVersionCommand extends CompositeCommand { @Override public void setup() { - // Useless : this command uses the default values. + setPermission("bentobox.admin"); } @Override diff --git a/src/main/java/world/bentobox/bentobox/managers/AddonsManager.java b/src/main/java/world/bentobox/bentobox/managers/AddonsManager.java index 5b92fcabd..6f08848f9 100644 --- a/src/main/java/world/bentobox/bentobox/managers/AddonsManager.java +++ b/src/main/java/world/bentobox/bentobox/managers/AddonsManager.java @@ -115,7 +115,7 @@ public class AddonsManager { for (String localeFile : listJarYamlFiles(jar, LOCALE_FOLDER)) { addon.saveResource(localeFile, localeDir, false, true); } - plugin.getLocalesManager().loadLocales(addon.getDescription().getName()); + plugin.getLocalesManager().loadLocalesFromFile(addon.getDescription().getName()); // Fire the load event Bukkit.getPluginManager().callEvent(AddonEvent.builder().addon(addon).reason(AddonEvent.Reason.LOAD).build()); // Add it to the list of addons diff --git a/src/main/java/world/bentobox/bentobox/managers/LocalesManager.java b/src/main/java/world/bentobox/bentobox/managers/LocalesManager.java index 30f3e456e..0d05ca14b 100644 --- a/src/main/java/world/bentobox/bentobox/managers/LocalesManager.java +++ b/src/main/java/world/bentobox/bentobox/managers/LocalesManager.java @@ -30,8 +30,8 @@ public class LocalesManager { public LocalesManager(BentoBox plugin) { this.plugin = plugin; - loadLocalesFromJar("BentoBox"); - loadLocales("BentoBox"); // Default + copyLocalesFromJar("BentoBox"); + loadLocalesFromFile("BentoBox"); // Default } /** @@ -56,9 +56,14 @@ public class LocalesManager { return null; } - public void loadLocalesFromJar(String parent) { + /** + * Copies all the locale files from the plugin jar to the filesystem. + * Only done if the locale folder does not already exist. + * @param folderName - the name of the destination folder + */ + private void copyLocalesFromJar(String folderName) { // Run through the files and store the locales - File localeDir = new File(plugin.getDataFolder(), LOCALE_FOLDER + File.separator + parent); + File localeDir = new File(plugin.getDataFolder(), LOCALE_FOLDER + File.separator + folderName); // If the folder does not exist, then make it and fill with the locale files from the jar // If it does exist, then new files will NOT be written! if (!localeDir.exists()) { @@ -79,20 +84,21 @@ public class LocalesManager { } /** - * Loads all the locales available. If the locale folder does not exist, one will be created and - * filled with locale files from the jar. + * Loads all the locales available in the locale folder given. Used for loading all locales from plugin and addons + * + * @param localeFolder - locale folder location relative to the plugin's data folder */ - public void loadLocales(String parent) { - // Describe the filter - we only want files that are correctly named - // Files must be 9 chars long + public void loadLocalesFromFile(String localeFolder) { + // Filter for files of length 9 and ending with .yml FilenameFilter ymlFilter = (dir, name) -> name.toLowerCase(java.util.Locale.ENGLISH).endsWith(".yml") && name.length() == 9; - // Run through the files and store the locales - File localeDir = new File(plugin.getDataFolder(), LOCALE_FOLDER + File.separator + parent); + // Get the folder + File localeDir = new File(plugin.getDataFolder(), LOCALE_FOLDER + File.separator + localeFolder); if (!localeDir.exists()) { + // If there is no locale folder, then return return; } - // Store all the locales available + // Run through the files and store the locales for (File language : Objects.requireNonNull(localeDir.listFiles(ymlFilter))) { Locale localeObject = Locale.forLanguageTag(language.getName().substring(0, language.getName().length() - 4)); @@ -145,4 +151,13 @@ public class LocalesManager { public Map getLanguages() { return this.languages; } + + /** + * Reloads all the language files from the filesystem + */ + public void reloadLanguages() { + languages.clear(); + loadLocalesFromFile("BentoBox"); + plugin.getAddonsManager().getAddons().forEach(addon -> loadLocalesFromFile(addon.getDescription().getName())); + } } diff --git a/src/main/resources/locales/en-US.yml b/src/main/resources/locales/en-US.yml index 92b055c59..e7acf22cb 100644 --- a/src/main/resources/locales/en-US.yml +++ b/src/main/resources/locales/en-US.yml @@ -175,6 +175,8 @@ commands: description: "BentoBox admin command" about: description: "display copyright and license info" + reload: + description: "reloads all locale files" version: description: "display info" loaded-addons: "Loaded Add-Ons" diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 9048349de..7a8abb772 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -8,3 +8,8 @@ authors: [tastybento, Poslovitch] softdepend: [PlaceholderAPI, MVdWPlaceholderAPI] loadbefore: [Multiverse-Core] + +permissions: + bentobox.admin: + description: Allow bentobox command usage + default: op From ec3e956d76571a030f3706f18f0aefb59a0cc87c Mon Sep 17 00:00:00 2001 From: tastybento Date: Sun, 12 Aug 2018 20:09:04 -0700 Subject: [PATCH 8/9] Enforced onlyConsole setting. Removed extraneous test code method --- .../bentobox/api/commands/CompositeCommand.java | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/api/commands/CompositeCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/CompositeCommand.java index 8473db5c1..5379ce1ce 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/CompositeCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/CompositeCommand.java @@ -424,16 +424,6 @@ 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 @@ -635,7 +625,7 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi // Get command object based on args entered so far CompositeCommand cmd = getCommandFromArgs(args); // Check for console and permissions - if (cmd.onlyPlayer && !(sender instanceof Player)) { + if ((cmd.onlyPlayer && !(sender instanceof Player)) || (cmd.onlyConsole && sender instanceof Player)) { return options; } if (!cmd.getPermission().isEmpty() && !sender.hasPermission(cmd.getPermission()) && !sender.isOp()) { From 82ee5d8928160a37be668b93d554ff5f25526673 Mon Sep 17 00:00:00 2001 From: Florian CUNY Date: Mon, 13 Aug 2018 13:53:58 +0200 Subject: [PATCH 9/9] Brought back FC-0.92 I need to merge this pull request in order to continue working on BentoBox (some changes might cause conflicts). Therefore, I just put the version back to FC-0.92. --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 05dddf1a9..d8bfade1a 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ world.bentobox bentobox - FC-0.95 + FC-0.92 BentoBox BentoBox is an expandable Minecraft Spigot plugin for island-type games like ASkyBlock or AcidIsland.