From 2b19d43c856023d185ef5defb63d5811f1862fae Mon Sep 17 00:00:00 2001 From: tastybento Date: Fri, 29 Mar 2024 19:38:44 -0700 Subject: [PATCH] Remove restrictions on having multiple islands for team members. Added API to enable checking for teams on islands easier. --- pom.xml | 2 +- .../api/commands/CompositeCommand.java | 1 + .../commands/admin/AdminDeleteCommand.java | 2 +- .../admin/team/AdminTeamAddCommand.java | 6 +++--- .../admin/team/AdminTeamDisbandCommand.java | 10 ++++++---- .../api/commands/island/IslandBanCommand.java | 2 +- .../commands/island/IslandCreateCommand.java | 3 ++- .../commands/island/IslandExpelCommand.java | 2 +- .../team/IslandTeamInviteAcceptCommand.java | 16 ++++++++++------ .../island/team/IslandTeamInviteCommand.java | 6 ++++-- .../island/team/IslandTeamKickCommand.java | 2 +- .../island/team/IslandTeamPromoteCommand.java | 2 +- .../team/IslandTeamSetownerCommand.java | 4 ++-- .../island/team/IslandTeamUncoopCommand.java | 2 +- .../island/team/IslandTeamUntrustCommand.java | 2 +- .../api/configuration/WorldSettings.java | 8 ++++++++ .../bentobox/database/objects/Island.java | 19 +++++++++++++++++++ .../bentobox/listeners/JoinLeaveListener.java | 2 +- .../worldsettings/EnterExitListener.java | 4 ++-- .../worldsettings/PetTeleportListener.java | 2 +- .../VisitorsStartingRaidListener.java | 2 +- .../bentobox/managers/IslandsManager.java | 9 +++++---- .../admin/AdminDeleteCommandTest.java | 4 ++++ .../admin/team/AdminTeamAddCommandTest.java | 8 +++++++- .../team/AdminTeamDisbandCommandTest.java | 2 +- .../commands/island/IslandBanCommandTest.java | 1 + .../island/IslandExpelCommandTest.java | 4 ++++ .../IslandTeamInviteAcceptCommandTest.java | 3 +++ .../team/IslandTeamInviteCommandTest.java | 5 +++++ .../team/IslandTeamKickCommandTest.java | 4 ++-- .../team/IslandTeamPromoteCommandTest.java | 6 ++++++ .../team/IslandTeamSetownerCommandTest.java | 11 ++++++++--- .../team/IslandTeamUncoopCommandTest.java | 1 + .../team/IslandTeamUntrustCommandTest.java | 1 + .../PetTeleportListenerTest.java | 2 ++ .../bentobox/managers/IslandsManagerTest.java | 3 +++ 36 files changed, 121 insertions(+), 42 deletions(-) diff --git a/pom.xml b/pom.xml index 181e7cc25..8aa65b84f 100644 --- a/pom.xml +++ b/pom.xml @@ -88,7 +88,7 @@ -LOCAL - 2.2.1 + 2.3.0 bentobox-world https://sonarcloud.io ${project.basedir}/lib 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 abf71ea5e..57081bac1 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/CompositeCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/CompositeCommand.java @@ -524,6 +524,7 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi * @param world - the world to check * @param user - the User * @return true if player is in a team + * @see Consider checking the island itself {@link Island#inTeam(UUID)} */ protected boolean inTeam(World world, User user) { return plugin.getIslands().inTeam(world, user.getUniqueId()); diff --git a/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminDeleteCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminDeleteCommand.java index 117785ac8..1c3a4c652 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminDeleteCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminDeleteCommand.java @@ -46,7 +46,7 @@ public class AdminDeleteCommand extends ConfirmableCommand { } // Team members should be kicked before deleting otherwise the whole team will become weird - if (getIslands().inTeam(getWorld(), targetUUID) && user.getUniqueId().equals(island.getOwner())) { + if (island.hasTeam() && user.getUniqueId().equals(island.getOwner())) { user.sendMessage("commands.admin.delete.cannot-delete-owner"); return false; } diff --git a/src/main/java/world/bentobox/bentobox/api/commands/admin/team/AdminTeamAddCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/admin/team/AdminTeamAddCommand.java index 98d6dd2d0..29d3599d7 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/admin/team/AdminTeamAddCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/admin/team/AdminTeamAddCommand.java @@ -44,17 +44,17 @@ public class AdminTeamAddCommand extends CompositeCommand { user.sendMessage("general.errors.unknown-player", TextVariables.NAME, args.get(1)); return false; } - if (!getIslands().hasIsland(getWorld(), ownerUUID)) { + Island island = getIslands().getPrimaryIsland(getWorld(), ownerUUID); + if (island == null || !getIslands().hasIsland(getWorld(), ownerUUID)) { user.sendMessage("general.errors.player-has-no-island"); return false; } - Island island = getIslands().getPrimaryIsland(getWorld(), ownerUUID); if (getIslands().inTeam(getWorld(), ownerUUID) && island != null && !ownerUUID.equals(island.getOwner())) { user.sendMessage("commands.admin.team.add.name-not-owner", TextVariables.NAME, args.get(0)); new IslandInfo(island).showMembers(user); return false; } - if (getIslands().inTeam(getWorld(), targetUUID)) { + if (getIWM().getWorldSettings(getWorld()).isTeamMembersDropIsland() && island.inTeam(targetUUID)) { user.sendMessage("commands.island.team.invite.errors.already-on-team"); return false; } diff --git a/src/main/java/world/bentobox/bentobox/api/commands/admin/team/AdminTeamDisbandCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/admin/team/AdminTeamDisbandCommand.java index 727eae5f3..64db4dd52 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/admin/team/AdminTeamDisbandCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/admin/team/AdminTeamDisbandCommand.java @@ -52,10 +52,7 @@ public class AdminTeamDisbandCommand extends CompositeCommand { user.sendMessage("general.errors.unknown-player", TextVariables.NAME, args.get(0)); return false; } - if (!getIslands().inTeam(getWorld(), targetUUID)) { - user.sendMessage("general.errors.player-is-not-owner", TextVariables.NAME, args.get(0)); - return false; - } + // Find the island the player is an owner of Map islands = getIslandsXYZ(targetUUID); if (islands.isEmpty()) { @@ -77,6 +74,11 @@ public class AdminTeamDisbandCommand extends CompositeCommand { // Get the only island island = islands.values().iterator().next(); } + // Check that the target owns the island + if (!island.getOwner().equals(targetUUID)) { + user.sendMessage("general.errors.player-is-not-owner", TextVariables.NAME, args.get(0)); + return false; + } return 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 403eecc88..24645119e 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 @@ -70,7 +70,7 @@ public class IslandBanCommand extends CompositeCommand { user.sendMessage("commands.island.ban.cannot-ban-yourself"); return false; } - if (getIslands().getPrimaryIsland(getWorld(), user.getUniqueId()).getMemberSet().contains(targetUUID)) { + if (getIslands().getPrimaryIsland(getWorld(), user.getUniqueId()).inTeam(targetUUID)) { user.sendMessage("commands.island.ban.cannot-ban-member"); return false; } diff --git a/src/main/java/world/bentobox/bentobox/api/commands/island/IslandCreateCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/island/IslandCreateCommand.java index 54d3c7a12..15655d4ce 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/island/IslandCreateCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/island/IslandCreateCommand.java @@ -51,7 +51,8 @@ public class IslandCreateCommand extends CompositeCommand { } } // Check if this player is on a team in this world - if (getIslands().inTeam(getWorld(), user.getUniqueId()) && island != null + if (getIWM().getWorldSettings(getWorld()).isTeamMembersDropIsland() + && getIslands().inTeam(getWorld(), user.getUniqueId()) && island != null && !user.getUniqueId().equals(island.getOwner())) { // Team members who are not owners cannot make additional islands user.sendMessage("commands.island.create.you-cannot-make-team"); diff --git a/src/main/java/world/bentobox/bentobox/api/commands/island/IslandExpelCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/island/IslandExpelCommand.java index 6d9de5fc4..b463d9667 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/island/IslandExpelCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/island/IslandExpelCommand.java @@ -76,7 +76,7 @@ public class IslandExpelCommand extends CompositeCommand { return false; } // Or team member - if (island.getMemberSet().contains(targetUUID)) { + if (island.inTeam(targetUUID)) { user.sendMessage("commands.island.expel.cannot-expel-member"); return false; } diff --git a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamInviteAcceptCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamInviteAcceptCommand.java index c11ff606a..35c3188b3 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamInviteAcceptCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamInviteAcceptCommand.java @@ -61,7 +61,8 @@ public class IslandTeamInviteAcceptCommand extends ConfirmableCommand { } // Check if player is already in a team - if (getIslands().inTeam(getWorld(), playerUUID)) { + if (getIWM().getWorldSettings(getWorld()).isTeamMembersDropIsland() + && getIslands().inTeam(getWorld(), playerUUID)) { user.sendMessage("commands.island.team.invite.errors.you-already-are-in-team"); return false; } @@ -155,17 +156,20 @@ public class IslandTeamInviteAcceptCommand extends ConfirmableCommand { user.sendMessage("commands.island.team.invite.errors.island-is-full"); return; } - // Remove the player's other islands - getIslands().removePlayer(getWorld(), user.getUniqueId()); + if (getIWM().getWorldSettings(getWorld()).isTeamMembersDropIsland()) { + // Remove the player's other islands + getIslands().removePlayer(getWorld(), user.getUniqueId()); + } // Remove money inventory etc. for leaving cleanPlayer(user); // Add the player as a team member of the new island getIslands().setJoinTeam(teamIsland, user.getUniqueId()); // Move player to team's island getIslands().homeTeleportAsync(getWorld(), user.getPlayer()).thenRun(() -> { - // Delete the old islands - islands.forEach(island -> getIslands().deleteIsland(island, true, user.getUniqueId())); - + if (getIWM().getWorldSettings(getWorld()).isTeamMembersDropIsland()) { + // Delete the old islands + islands.forEach(island -> getIslands().deleteIsland(island, true, user.getUniqueId())); + } // Put player back into normal mode user.setGameMode(getIWM().getDefaultGameMode(getWorld())); 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 c291e65ca..30cfbfea8 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 @@ -105,7 +105,8 @@ public class IslandTeamInviteCommand extends CompositeCommand { } // Player cannot invite someone already on a team - if (getIslands().inTeam(getWorld(), invitedPlayerUUID)) { + if (getIWM().getWorldSettings(getWorld()).isTeamMembersDropIsland() + && getIslands().inTeam(getWorld(), invitedPlayerUUID)) { user.sendMessage("commands.island.team.invite.errors.already-on-team"); return false; } @@ -170,7 +171,8 @@ public class IslandTeamInviteCommand extends CompositeCommand { // Send message to online player invitedPlayer.sendMessage("commands.island.team.invite.name-has-invited-you", TextVariables.NAME, user.getName(), TextVariables.DISPLAY_NAME, user.getDisplayName()); invitedPlayer.sendMessage("commands.island.team.invite.to-accept-or-reject", TextVariables.LABEL, getTopLabel()); - if (getIslands().hasIsland(getWorld(), invitedPlayer.getUniqueId())) { + if (getIWM().getWorldSettings(getWorld()).isTeamMembersDropIsland() + && getIslands().hasIsland(getWorld(), invitedPlayer.getUniqueId())) { invitedPlayer.sendMessage("commands.island.team.invite.you-will-lose-your-island"); } return true; 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 465956d4b..7c0d9a5d0 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 @@ -63,7 +63,7 @@ public class IslandTeamKickCommand extends ConfirmableCommand { user.sendMessage("commands.island.team.kick.cannot-kick"); return false; } - if (!getIslands().getPrimaryIsland(getWorld(), user.getUniqueId()).getMemberSet().contains(targetUUID)) { + if (!getIslands().getPrimaryIsland(getWorld(), user.getUniqueId()).inTeam(targetUUID)) { user.sendMessage("general.errors.not-in-team"); return false; } 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 e35e8aeea..a13c050e3 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 @@ -69,7 +69,7 @@ public class IslandTeamPromoteCommand extends CompositeCommand { return false; } // Check that target is a member of this island - if (!island.getMemberSet().contains(target.getUniqueId())) { + if (!island.inTeam(target.getUniqueId())) { user.sendMessage("commands.island.team.promote.errors.must-be-member"); return false; } diff --git a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamSetownerCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamSetownerCommand.java index bd1d2a1a0..e80580aa9 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamSetownerCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamSetownerCommand.java @@ -42,7 +42,7 @@ public class IslandTeamSetownerCommand extends CompositeCommand { } // Can use if in a team Island is = getIslands().getPrimaryIsland(getWorld(), user.getUniqueId()); - if (is == null || !is.getMemberSet().contains(user.getUniqueId())) { + if (is == null || !is.inTeam(user.getUniqueId())) { user.sendMessage("general.errors.no-team"); return false; } @@ -60,7 +60,7 @@ public class IslandTeamSetownerCommand extends CompositeCommand { user.sendMessage("commands.island.team.setowner.errors.cant-transfer-to-yourself"); return false; } - if (!is.getMemberSet().contains(targetUUID)) { + if (!is.inTeam(targetUUID)) { user.sendMessage("commands.island.team.setowner.errors.target-is-not-member"); return false; } 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 c79b3e219..b4929b122 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 @@ -74,7 +74,7 @@ public class IslandTeamUncoopCommand extends CompositeCommand { user.sendMessage("commands.island.team.uncoop.cannot-uncoop-yourself"); return false; } - if (getIslands().getPrimaryIsland(getWorld(), user.getUniqueId()).getMemberSet().contains(targetUUID)) { + if (getIslands().getPrimaryIsland(getWorld(), user.getUniqueId()).inTeam(targetUUID)) { user.sendMessage("commands.island.team.uncoop.cannot-uncoop-member"); return false; } 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 index 11bdb82aa..16dec5d03 100644 --- 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 @@ -74,7 +74,7 @@ public class IslandTeamUntrustCommand extends CompositeCommand { user.sendMessage("commands.island.team.untrust.cannot-untrust-yourself"); return false; } - if (getIslands().getPrimaryIsland(getWorld(), user.getUniqueId()).getMemberSet().contains(targetUUID)) { + if (getIslands().getPrimaryIsland(getWorld(), user.getUniqueId()).inTeam(targetUUID)) { user.sendMessage("commands.island.team.untrust.cannot-untrust-member"); return false; } diff --git a/src/main/java/world/bentobox/bentobox/api/configuration/WorldSettings.java b/src/main/java/world/bentobox/bentobox/api/configuration/WorldSettings.java index 3aa960bd1..4f964892c 100644 --- a/src/main/java/world/bentobox/bentobox/api/configuration/WorldSettings.java +++ b/src/main/java/world/bentobox/bentobox/api/configuration/WorldSettings.java @@ -644,4 +644,12 @@ public interface WorldSettings extends ConfigObject { default int getConcurrentIslands() { return BentoBox.getInstance().getSettings().getIslandNumber(); } + + /** + * Remove islands when players join a team and not allow players to have other islands if they are in a team. + * @return true or false + */ + default boolean isTeamMembersDropIsland() { + return true; + } } 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 92e1b1f33..60d95c89f 100644 --- a/src/main/java/world/bentobox/bentobox/database/objects/Island.java +++ b/src/main/java/world/bentobox/bentobox/database/objects/Island.java @@ -1950,6 +1950,25 @@ public class Island implements DataObject, MetaDataAble { setChanged(); } + /** + * Check if a player is in this island's team + * @param playerUUID player's UUID + * @return true if in team + * @since 2.3.0 + */ + public boolean inTeam(UUID playerUUID) { + return this.getMemberSet().contains(playerUUID); + } + + /** + * Check if this island has a team + * @return true if this island has a team + * @since 2.3.0 + */ + public boolean hasTeam() { + return this.getMemberSet().size() > 1; + } + /* * (non-Javadoc) * diff --git a/src/main/java/world/bentobox/bentobox/listeners/JoinLeaveListener.java b/src/main/java/world/bentobox/bentobox/listeners/JoinLeaveListener.java index 40317c15c..37f5f0464 100644 --- a/src/main/java/world/bentobox/bentobox/listeners/JoinLeaveListener.java +++ b/src/main/java/world/bentobox/bentobox/listeners/JoinLeaveListener.java @@ -219,7 +219,7 @@ public class JoinLeaveListener implements Listener { .filter(island -> island.getMembers().containsKey(event.getPlayer().getUniqueId())).forEach(island -> { // Are there any online players still for this island? if (Bukkit.getOnlinePlayers().stream().filter(p -> !event.getPlayer().equals(p)) - .noneMatch(p -> island.getMemberSet().contains(p.getUniqueId()))) { + .noneMatch(p -> island.inTeam(p.getUniqueId()))) { // No, there are no more players online on this island // Tell players they are being removed island.getMembers().entrySet().stream().filter(e -> e.getValue() == RanksManager.COOP_RANK) diff --git a/src/main/java/world/bentobox/bentobox/listeners/flags/worldsettings/EnterExitListener.java b/src/main/java/world/bentobox/bentobox/listeners/flags/worldsettings/EnterExitListener.java index 02404a7bc..bdcff20dd 100644 --- a/src/main/java/world/bentobox/bentobox/listeners/flags/worldsettings/EnterExitListener.java +++ b/src/main/java/world/bentobox/bentobox/listeners/flags/worldsettings/EnterExitListener.java @@ -107,7 +107,7 @@ public class EnterExitListener extends FlagListener { // Leave messages are always specific to this world String islandMessage = user.getTranslation(island.getWorld(), ISLAND_MESSAGE, TextVariables.NAME, getPlugin().getPlayers().getName(island.getOwner())); // Send specific message if the player is member of this island - if (island.getMemberSet().contains(user.getUniqueId())) { + if (island.inTeam(user.getUniqueId())) { user.notify(island.getWorld(), "protection.flags.ENTER_EXIT_MESSAGES.now-leaving-your-island", TextVariables.NAME, (island.getName() != null) ? island.getName() : islandMessage); } else { user.notify(island.getWorld(), "protection.flags.ENTER_EXIT_MESSAGES.now-leaving", TextVariables.NAME, (island.getName() != null) ? island.getName() : islandMessage); @@ -135,7 +135,7 @@ public class EnterExitListener extends FlagListener { // Enter messages are always specific to this world String islandMessage = user.getTranslation(island.getWorld(), ISLAND_MESSAGE, TextVariables.NAME, getPlugin().getPlayers().getName(island.getOwner())); // Send specific message if the player is member of this island - if (island.getMemberSet().contains(user.getUniqueId())) { + if (island.inTeam(user.getUniqueId())) { user.notify(island.getWorld(), "protection.flags.ENTER_EXIT_MESSAGES.now-entering-your-island", TextVariables.NAME, (island.getName() != null) ? island.getName() : islandMessage); } else { user.notify(island.getWorld(), "protection.flags.ENTER_EXIT_MESSAGES.now-entering", TextVariables.NAME, (island.getName() != null) ? island.getName() : islandMessage); diff --git a/src/main/java/world/bentobox/bentobox/listeners/flags/worldsettings/PetTeleportListener.java b/src/main/java/world/bentobox/bentobox/listeners/flags/worldsettings/PetTeleportListener.java index ced124ed8..fc10ed0e7 100644 --- a/src/main/java/world/bentobox/bentobox/listeners/flags/worldsettings/PetTeleportListener.java +++ b/src/main/java/world/bentobox/bentobox/listeners/flags/worldsettings/PetTeleportListener.java @@ -31,7 +31,7 @@ public class PetTeleportListener extends FlagListener { // Get where the pet is going e.setCancelled(getIslands().getProtectedIslandAt(e.getTo()) // Not home island - .map(i -> !i.getMemberSet().contains(t.getOwner().getUniqueId())) + .map(i -> !i.inTeam(t.getOwner().getUniqueId())) // Not any island .orElse(true)); } diff --git a/src/main/java/world/bentobox/bentobox/listeners/flags/worldsettings/VisitorsStartingRaidListener.java b/src/main/java/world/bentobox/bentobox/listeners/flags/worldsettings/VisitorsStartingRaidListener.java index bb8c2b03e..33dc06982 100644 --- a/src/main/java/world/bentobox/bentobox/listeners/flags/worldsettings/VisitorsStartingRaidListener.java +++ b/src/main/java/world/bentobox/bentobox/listeners/flags/worldsettings/VisitorsStartingRaidListener.java @@ -49,7 +49,7 @@ public class VisitorsStartingRaidListener extends FlagListener Optional island = this.getIslands().getProtectedIslandAt(event.getPlayer().getLocation()); - if (island.isPresent() && !island.get().getMemberSet().contains(event.getPlayer().getUniqueId())) + if (island.isPresent() && !island.get().inTeam(event.getPlayer().getUniqueId())) { event.setCancelled(true); this.report(User.getInstance(event.getPlayer()), diff --git a/src/main/java/world/bentobox/bentobox/managers/IslandsManager.java b/src/main/java/world/bentobox/bentobox/managers/IslandsManager.java index 5c78f52a8..486edb811 100644 --- a/src/main/java/world/bentobox/bentobox/managers/IslandsManager.java +++ b/src/main/java/world/bentobox/bentobox/managers/IslandsManager.java @@ -1319,7 +1319,7 @@ public class IslandsManager { return false; } // Get the player's island - return getIslandAt(loc).filter(i -> i.onIsland(loc)).map(i -> i.getMemberSet().contains(player.getUniqueId())) + return getIslandAt(loc).filter(i -> i.onIsland(loc)).map(i -> i.inTeam(player.getUniqueId())) .orElse(false); } @@ -1389,7 +1389,7 @@ public class IslandsManager { .filter(p -> p.getGameMode().equals(plugin.getIWM().getDefaultGameMode(island.getWorld()))) .filter(p -> island.onIsland(p.getLocation())).forEach(p -> { // Teleport island players to their island home - if (!island.getMemberSet().contains(p.getUniqueId()) + if (!island.inTeam(p.getUniqueId()) && (hasIsland(w, p.getUniqueId()) || inTeam(w, p.getUniqueId()))) { homeTeleportAsync(w, p); } else { @@ -1493,16 +1493,17 @@ public class IslandsManager { } /** - * Checks if a player is in a team in this world. Note that the player may have + * Checks if a player is in any team in this world. Note that the player may have * multiple islands in the world, any one of which may have a team. * * @param world - world * @param playerUUID - player's UUID * @return true if in team, false if not + * @see Consider checking the island itself {@link Island#inTeam(UUID)} */ public boolean inTeam(World world, @NonNull UUID playerUUID) { return this.islandCache.getIslands(world, playerUUID).stream() - .anyMatch(island -> island.getMemberSet().size() > 1 && island.getMemberSet().contains(playerUUID)); + .anyMatch(island -> island.getMemberSet().size() > 1 && island.inTeam(playerUUID)); } /** 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 24f6f0bdd..b678482fa 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 @@ -121,6 +121,7 @@ public class AdminDeleteCommandTest { // Island when(island.getOwner()).thenReturn(uuid); + when(island.hasTeam()).thenReturn(true); // Has team when(im.inTeam(any(), eq(uuid))).thenReturn(true); @@ -190,6 +191,7 @@ public class AdminDeleteCommandTest { public void testExecuteOwner() { when(im.inTeam(any(),any())).thenReturn(true); + when(island.inTeam(notUUID)).thenReturn(true); //when(im.getOwner(any(), any())).thenReturn(notUUID); String[] name = {"tastybento"}; when(pm.getUUID(any())).thenReturn(notUUID); @@ -203,6 +205,7 @@ public class AdminDeleteCommandTest { */ @Test public void testcanExecuteSuccessUUID() { + when(island.hasTeam()).thenReturn(false); when(im.inTeam(any(), any())).thenReturn(false); //when(im.getOwner(any(), any())).thenReturn(uuid); Island is = mock(Island.class); @@ -243,6 +246,7 @@ public class AdminDeleteCommandTest { */ @Test public void testCanExecuteSuccess() { + when(island.hasTeam()).thenReturn(false); when(im.inTeam(any(), any())).thenReturn(false); //when(im.getOwner(any(), any())).thenReturn(uuid); Island is = mock(Island.class); diff --git a/src/test/java/world/bentobox/bentobox/api/commands/admin/team/AdminTeamAddCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/admin/team/AdminTeamAddCommandTest.java index 4652490a5..8a559854d 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/admin/team/AdminTeamAddCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/admin/team/AdminTeamAddCommandTest.java @@ -19,6 +19,7 @@ import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.plugin.PluginManager; import org.bukkit.scheduler.BukkitScheduler; +import org.eclipse.jdt.annotation.NonNull; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -31,7 +32,9 @@ import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.reflect.Whitebox; import world.bentobox.bentobox.BentoBox; +import world.bentobox.bentobox.TestWorldSettings; import world.bentobox.bentobox.api.commands.CompositeCommand; +import world.bentobox.bentobox.api.configuration.WorldSettings; import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.database.objects.Island; @@ -123,6 +126,9 @@ public class AdminTeamAddCommandTest { // Island World Manager IslandWorldManager iwm = mock(IslandWorldManager.class); when(iwm.getFriendlyName(any())).thenReturn("BSkyBlock"); + @NonNull + WorldSettings ws = new TestWorldSettings(); + when(iwm.getWorldSettings(any())).thenReturn(ws); when(plugin.getIWM()).thenReturn(iwm); // Addon @@ -187,7 +193,7 @@ public class AdminTeamAddCommandTest { when(pm.getUUID(eq("poslovich"))).thenReturn(notUUID); when(im.inTeam(any(), eq(notUUID))).thenReturn(true); - + when(island.inTeam(notUUID)).thenReturn(true); assertFalse(itl.execute(user, itl.getLabel(), Arrays.asList(name))); verify(user).sendMessage(eq("commands.island.team.invite.errors.already-on-team")); } diff --git a/src/test/java/world/bentobox/bentobox/api/commands/admin/team/AdminTeamDisbandCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/admin/team/AdminTeamDisbandCommandTest.java index 38307ca88..5166a608f 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/admin/team/AdminTeamDisbandCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/admin/team/AdminTeamDisbandCommandTest.java @@ -205,7 +205,7 @@ public class AdminTeamDisbandCommandTest { public void testExecutePlayerNotInTeam() { when(Util.getUUID("tastybento")).thenReturn(notUUID); assertFalse(itl.canExecute(user, itl.getLabel(), List.of("tastybento"))); - verify(user).sendMessage("general.errors.player-is-not-owner", "[name]", "tastybento"); + verify(user).sendMessage("general.errors.player-has-no-island"); } /** 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 6e0718294..e7fe5e71c 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 @@ -228,6 +228,7 @@ public class IslandBanCommandTest extends RanksManagerBeforeClassTest { UUID teamMate = UUID.randomUUID(); when(pm.getUUID(anyString())).thenReturn(teamMate); when(island.getMemberSet()).thenReturn(ImmutableSet.of(uuid, teamMate)); + when(island.inTeam(teamMate)).thenReturn(true); assertFalse(ibc.canExecute(user, ibc.getLabel(), Collections.singletonList("bill"))); verify(user).sendMessage("commands.island.ban.cannot-ban-member"); } diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandExpelCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandExpelCommandTest.java index 941a19ae5..a21b03bd3 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandExpelCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandExpelCommandTest.java @@ -40,6 +40,7 @@ import com.google.common.collect.ImmutableSet; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.Settings; +import world.bentobox.bentobox.TestWorldSettings; import world.bentobox.bentobox.api.addons.Addon; import world.bentobox.bentobox.api.addons.GameModeAddon; import world.bentobox.bentobox.api.commands.CompositeCommand; @@ -140,6 +141,8 @@ public class IslandExpelCommandTest extends RanksManagerBeforeClassTest { // IWM friendly name when(iwm.getFriendlyName(any())).thenReturn("BSkyBlock"); + TestWorldSettings worldSettings = new TestWorldSettings(); + when(iwm.getWorldSettings(any())).thenReturn(worldSettings); when(plugin.getIWM()).thenReturn(iwm); // Server and Plugin Manager for events @@ -268,6 +271,7 @@ public class IslandExpelCommandTest extends RanksManagerBeforeClassTest { UUID target = UUID.randomUUID(); when(pm.getUUID(anyString())).thenReturn(target); when(island.getMemberSet()).thenReturn(ImmutableSet.of(target)); + when(island.inTeam(target)).thenReturn(true); assertFalse(iec.canExecute(user, "", Collections.singletonList("tasty"))); verify(user).sendMessage("commands.island.expel.cannot-expel-member"); } diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamInviteAcceptCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamInviteAcceptCommandTest.java index 4516362c6..ca056a9ac 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamInviteAcceptCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamInviteAcceptCommandTest.java @@ -31,6 +31,7 @@ import org.powermock.reflect.Whitebox; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.Settings; +import world.bentobox.bentobox.TestWorldSettings; import world.bentobox.bentobox.api.commands.island.team.Invite.Type; import world.bentobox.bentobox.api.events.IslandBaseEvent; import world.bentobox.bentobox.api.events.team.TeamEvent; @@ -142,6 +143,8 @@ public class IslandTeamInviteAcceptCommandTest { // IWM friendly name IslandWorldManager iwm = mock(IslandWorldManager.class); when(iwm.getFriendlyName(any())).thenReturn("BSkyBlock"); + TestWorldSettings worldSettings = new TestWorldSettings(); + when(iwm.getWorldSettings(any())).thenReturn(worldSettings); when(plugin.getIWM()).thenReturn(iwm); // Invite 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 6bf25e07b..3a50c1445 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 @@ -39,7 +39,9 @@ import com.google.common.collect.ImmutableSet; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.Settings; +import world.bentobox.bentobox.TestWorldSettings; import world.bentobox.bentobox.api.commands.island.team.Invite.Type; +import world.bentobox.bentobox.api.configuration.WorldSettings; import world.bentobox.bentobox.api.events.IslandBaseEvent; import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.user.User; @@ -170,6 +172,9 @@ public class IslandTeamInviteCommandTest extends RanksManagerBeforeClassTest { // IWM friendly name IslandWorldManager iwm = mock(IslandWorldManager.class); when(iwm.getFriendlyName(any())).thenReturn("BSkyBlock"); + @NonNull + WorldSettings ws = new TestWorldSettings(); + when(iwm.getWorldSettings(world)).thenReturn(ws); when(plugin.getIWM()).thenReturn(iwm); // Parent command 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 a1cc1b546..2a6001b28 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 @@ -211,7 +211,7 @@ public class IslandTeamKickCommandTest extends RanksManagerBeforeClassTest { when(pm.getName(notUUID)).thenReturn("poslovitch"); when(island.getMemberSet()).thenReturn(ImmutableSet.of(notUUID)); - + when(island.inTeam(notUUID)).thenReturn(true); IslandTeamKickCommand itl = new IslandTeamKickCommand(ic); assertFalse(itl.canExecute(user, itl.getLabel(), Collections.singletonList("poslovitch"))); verify(user).sendMessage(eq("commands.island.team.kick.cannot-kick-rank"), eq(TextVariables.NAME), eq("poslovitch")); @@ -229,7 +229,7 @@ public class IslandTeamKickCommandTest extends RanksManagerBeforeClassTest { when(pm.getName(notUUID)).thenReturn("poslovitch"); when(island.getMemberSet()).thenReturn(ImmutableSet.of(notUUID)); - + when(island.inTeam(notUUID)).thenReturn(true); IslandTeamKickCommand itl = new IslandTeamKickCommand(ic); assertFalse(itl.canExecute(user, itl.getLabel(), Collections.singletonList("poslovitch"))); verify(user).sendMessage(eq("commands.island.team.kick.cannot-kick-rank"), eq(TextVariables.NAME), eq("poslovitch")); diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamPromoteCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamPromoteCommandTest.java index c3dcb2f2e..2dfb203b7 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamPromoteCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamPromoteCommandTest.java @@ -33,6 +33,7 @@ import com.google.common.collect.ImmutableSet; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.Settings; +import world.bentobox.bentobox.TestWorldSettings; import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.database.objects.Island; @@ -119,6 +120,11 @@ public class IslandTeamPromoteCommandTest extends RanksManagerBeforeClassTest { // In team when(im.inTeam(world, uuid)).thenReturn(true); + when(island.inTeam(uuid)).thenReturn(true); + + // IWM + TestWorldSettings worldSettings = new TestWorldSettings(); + when(iwm.getWorldSettings(any())).thenReturn(worldSettings); // Ranks when(island.getRankCommand(anyString())).thenReturn(RanksManager.SUB_OWNER_RANK); // Allow sub owners diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamSetownerCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamSetownerCommandTest.java index 6525563db..63d6f1bd8 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamSetownerCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamSetownerCommandTest.java @@ -38,6 +38,7 @@ import com.google.common.collect.ImmutableSet; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.Settings; +import world.bentobox.bentobox.TestWorldSettings; import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.configuration.WorldSettings; import world.bentobox.bentobox.api.localization.TextVariables; @@ -119,7 +120,8 @@ public class IslandTeamSetownerCommandTest { when(plugin.getIslands()).thenReturn(im); // Has team - when(im.inTeam(any(), eq(uuid))).thenReturn(true); + when(im.inTeam(world, uuid)).thenReturn(true); + when(island.inTeam(uuid)).thenReturn(true); when(plugin.getPlayers()).thenReturn(pm); // Server & Scheduler @@ -128,6 +130,8 @@ public class IslandTeamSetownerCommandTest { when(Bukkit.getScheduler()).thenReturn(sch); // Island World Manager + TestWorldSettings worldSettings = new TestWorldSettings(); + when(iwm.getWorldSettings(any())).thenReturn(worldSettings); when(plugin.getIWM()).thenReturn(iwm); @NonNull WorldSettings ws = mock(WorldSettings.class); @@ -194,7 +198,7 @@ public class IslandTeamSetownerCommandTest { */ @Test public void testCanExecuteUserStringListOfStringNotInTeam() { - when(island.getMemberSet()).thenReturn(ImmutableSet.of()); + when(island.inTeam(uuid)).thenReturn(false); assertFalse(its.canExecute(user, "", List.of("gibby"))); verify(user).sendMessage("general.errors.no-team"); } @@ -267,6 +271,7 @@ public class IslandTeamSetownerCommandTest { UUID target = UUID.randomUUID(); when(pm.getUUID(anyString())).thenReturn(target); when(island.getMemberSet()).thenReturn(ImmutableSet.of(uuid, target)); + when(island.inTeam(any())).thenReturn(true); when(im.getIsland(any(), any(User.class))).thenReturn(island); assertTrue(its.canExecute(user, "", List.of("tastybento"))); assertTrue(its.execute(user, "", List.of("tastybento"))); @@ -282,7 +287,7 @@ public class IslandTeamSetownerCommandTest { when(im.inTeam(any(), any())).thenReturn(true); UUID target = UUID.randomUUID(); when(pm.getUUID(anyString())).thenReturn(target); - when(island.getMemberSet()).thenReturn(ImmutableSet.of(uuid, target)); + when(island.inTeam(any())).thenReturn(true); when(im.getIsland(any(), any(User.class))).thenReturn(island); assertTrue(its.canExecute(user, "", List.of("tastybento"))); assertTrue(its.execute(user, "", List.of("tastybento"))); 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 index 0e941c2e9..c5bb7976f 100644 --- 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 @@ -211,6 +211,7 @@ public class IslandTeamUncoopCommandTest extends RanksManagerBeforeClassTest { when(pm.getUUID(any())).thenReturn(notUUID); when(im.inTeam(any(), any())).thenReturn(true); when(island.getMemberSet()).thenReturn(ImmutableSet.of(notUUID)); + when(island.inTeam(notUUID)).thenReturn(true); assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("bento"))); verify(user).sendMessage(eq("commands.island.team.uncoop.cannot-uncoop-member")); } 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 index 0f0a34f94..4022b8c40 100644 --- 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 @@ -211,6 +211,7 @@ public class IslandTeamUntrustCommandTest extends RanksManagerBeforeClassTest { when(pm.getUUID(any())).thenReturn(notUUID); when(im.inTeam(any(), any())).thenReturn(true); when(island.getMemberSet()).thenReturn(ImmutableSet.of(notUUID)); + when(island.inTeam(notUUID)).thenReturn(true); assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("bento"))); verify(user).sendMessage(eq("commands.island.team.untrust.cannot-untrust-member")); } diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/PetTeleportListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/PetTeleportListenerTest.java index 55ed897a4..a44122b8c 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/PetTeleportListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/PetTeleportListenerTest.java @@ -47,6 +47,8 @@ public class PetTeleportListenerTest extends AbstractCommonSetup { @Before public void setUp() throws Exception { super.setUp(); + // Island + when(this.island.inTeam(uuid)).thenReturn(true); when(tamed.isTamed()).thenReturn(true); when(tamed.getOwner()).thenReturn(tamer); when(tamer.getUniqueId()).thenReturn(uuid); diff --git a/src/test/java/world/bentobox/bentobox/managers/IslandsManagerTest.java b/src/test/java/world/bentobox/bentobox/managers/IslandsManagerTest.java index 153f528cf..7931f9df7 100644 --- a/src/test/java/world/bentobox/bentobox/managers/IslandsManagerTest.java +++ b/src/test/java/world/bentobox/bentobox/managers/IslandsManagerTest.java @@ -830,6 +830,7 @@ public class IslandsManagerTest extends AbstractCommonSetup { Builder members = new ImmutableSet.Builder<>(); members.add(uuid); when(is.getMemberSet()).thenReturn(members.build()); + when(is.inTeam(uuid)).thenReturn(true); when(player.getUniqueId()).thenReturn(uuid); @@ -842,10 +843,12 @@ public class IslandsManagerTest extends AbstractCommonSetup { // No members Builder mem = new ImmutableSet.Builder<>(); when(is.getMemberSet()).thenReturn(mem.build()); + when(is.inTeam(uuid)).thenReturn(false); assertFalse(im.locationIsOnIsland(player, location)); // Not on island when(is.getMemberSet()).thenReturn(members.build()); + when(is.inTeam(uuid)).thenReturn(true); when(is.onIsland(any())).thenReturn(false); assertFalse(im.locationIsOnIsland(player, location)); }