mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2025-01-27 02:21:42 +01:00
Removes coop on logout of cooped player or server shutdown.
https://github.com/BentoBoxWorld/bentobox/issues/335
This commit is contained in:
parent
49f9784b61
commit
ffd1af5867
@ -14,6 +14,7 @@ import world.bentobox.bentobox.api.user.User;
|
||||
import world.bentobox.bentobox.database.objects.Island;
|
||||
import world.bentobox.bentobox.lists.Flags;
|
||||
import world.bentobox.bentobox.managers.PlayersManager;
|
||||
import world.bentobox.bentobox.managers.RanksManager;
|
||||
import world.bentobox.bentobox.util.Util;
|
||||
|
||||
public class JoinLeaveListener implements Listener {
|
||||
@ -49,22 +50,22 @@ public class JoinLeaveListener implements Listener {
|
||||
|
||||
// Update the island range of the islands the player owns
|
||||
plugin.getIWM().getOverWorlds().stream()
|
||||
.filter(world -> plugin.getIslands().isOwner(world, user.getUniqueId()))
|
||||
.forEach(world -> {
|
||||
Island island = plugin.getIslands().getIsland(world, user);
|
||||
.filter(world -> plugin.getIslands().isOwner(world, user.getUniqueId()))
|
||||
.forEach(world -> {
|
||||
Island island = plugin.getIslands().getIsland(world, user);
|
||||
|
||||
// Check if new leader has a different range permission than the island size
|
||||
int range = user.getPermissionValue(plugin.getIWM().getAddon(island.getWorld()).get().getPermissionPrefix() + "island.range", plugin.getIWM().getIslandProtectionRange(Util.getWorld(island.getWorld())));
|
||||
// Check if new leader has a different range permission than the island size
|
||||
int range = user.getPermissionValue(plugin.getIWM().getAddon(island.getWorld()).get().getPermissionPrefix() + "island.range", plugin.getIWM().getIslandProtectionRange(Util.getWorld(island.getWorld())));
|
||||
|
||||
// Range can go up or down
|
||||
if (range != island.getProtectionRange()) {
|
||||
user.sendMessage("commands.admin.setrange.range-updated", TextVariables.NUMBER, String.valueOf(range));
|
||||
plugin.log("Makeleader: Island protection range changed from " + island.getProtectionRange() + " to "
|
||||
+ range + " for " + user.getName() + " due to permission.");
|
||||
}
|
||||
// Range can go up or down
|
||||
if (range != island.getProtectionRange()) {
|
||||
user.sendMessage("commands.admin.setrange.range-updated", TextVariables.NUMBER, String.valueOf(range));
|
||||
plugin.log("Makeleader: Island protection range changed from " + island.getProtectionRange() + " to "
|
||||
+ range + " for " + user.getName() + " due to permission.");
|
||||
}
|
||||
|
||||
island.setProtectionRange(range);
|
||||
});
|
||||
island.setProtectionRange(range);
|
||||
});
|
||||
|
||||
// Set the player's name (it may have changed), but only if it isn't empty
|
||||
if (!user.getName().isEmpty()) {
|
||||
@ -83,6 +84,8 @@ public class JoinLeaveListener implements Listener {
|
||||
|
||||
@EventHandler(priority = EventPriority.NORMAL)
|
||||
public void onPlayerQuit(final PlayerQuitEvent event) {
|
||||
// Remove any coop associations
|
||||
plugin.getIslands().clearRank(RanksManager.COOP_RANK, event.getPlayer().getUniqueId());
|
||||
players.save(event.getPlayer().getUniqueId());
|
||||
User.removePlayer(event.getPlayer());
|
||||
}
|
||||
|
@ -771,6 +771,8 @@ public class IslandsManager {
|
||||
}
|
||||
|
||||
public void shutdown(){
|
||||
// Remove all coop associations
|
||||
islandCache.getIslands().stream().forEach(i -> i.getMembers().values().removeIf(p -> p == RanksManager.COOP_RANK));
|
||||
save(false);
|
||||
islandCache.clear();
|
||||
handler.close();
|
||||
@ -835,4 +837,15 @@ public class IslandsManager {
|
||||
.forEach(Entity::remove);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a player from any island where they hold the indicated rank.
|
||||
* Typically this is to remove temporary ranks such as coop.
|
||||
* Removal is done in all worlds.
|
||||
* @param rank - rank to clear
|
||||
* @param uniqueId - UUID of player
|
||||
*/
|
||||
public void clearRank(int rank, UUID uniqueId) {
|
||||
islandCache.getIslands().stream().forEach(i -> i.getMembers().entrySet().removeIf(e -> e.getKey().equals(uniqueId) && e.getValue() == rank));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -967,7 +967,80 @@ public class IslandsManagerTest {
|
||||
*/
|
||||
@Test
|
||||
public void testShutdown() {
|
||||
//fail("Not yet implemented"); // TODO
|
||||
// Mock island cache
|
||||
Island is = mock(Island.class);
|
||||
|
||||
Collection<Island> collection = new ArrayList<>();
|
||||
collection.add(is);
|
||||
when(islandCache.getIslands()).thenReturn(collection);
|
||||
IslandsManager im = new IslandsManager(plugin);
|
||||
im.setIslandCache(islandCache);
|
||||
Map<UUID, Integer> members = new HashMap<>();
|
||||
when(is.getMembers()).thenReturn(members);
|
||||
// -- The user is the owner of the island --
|
||||
members.put(user.getUniqueId(), RanksManager.OWNER_RANK);
|
||||
// Add some members
|
||||
members.put(UUID.randomUUID(), RanksManager.MEMBER_RANK);
|
||||
members.put(UUID.randomUUID(), RanksManager.MEMBER_RANK);
|
||||
members.put(UUID.randomUUID(), RanksManager.MEMBER_RANK);
|
||||
members.put(UUID.randomUUID(), RanksManager.MEMBER_RANK);
|
||||
members.put(UUID.randomUUID(), RanksManager.MEMBER_RANK);
|
||||
members.put(UUID.randomUUID(), RanksManager.MEMBER_RANK);
|
||||
members.put(UUID.randomUUID(), RanksManager.MEMBER_RANK);
|
||||
// Add some coops
|
||||
members.put(UUID.randomUUID(), RanksManager.COOP_RANK);
|
||||
members.put(UUID.randomUUID(), RanksManager.COOP_RANK);
|
||||
members.put(UUID.randomUUID(), RanksManager.COOP_RANK);
|
||||
// Add some trusted
|
||||
members.put(UUID.randomUUID(), RanksManager.TRUSTED_RANK);
|
||||
members.put(UUID.randomUUID(), RanksManager.TRUSTED_RANK);
|
||||
|
||||
im.shutdown();
|
||||
|
||||
assertEquals(10, members.size());
|
||||
Mockito.verify(islandCache).clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.bentobox.managers.IslandsManager#clearRank(int, UUID)}.
|
||||
*/
|
||||
@Test
|
||||
public void testClearRank() {
|
||||
// Mock island cache
|
||||
Island is = mock(Island.class);
|
||||
|
||||
Collection<Island> collection = new ArrayList<>();
|
||||
collection.add(is);
|
||||
when(islandCache.getIslands()).thenReturn(collection);
|
||||
IslandsManager im = new IslandsManager(plugin);
|
||||
im.setIslandCache(islandCache);
|
||||
Map<UUID, Integer> members = new HashMap<>();
|
||||
when(is.getMembers()).thenReturn(members);
|
||||
// -- The user is the owner of the island --
|
||||
members.put(user.getUniqueId(), RanksManager.OWNER_RANK);
|
||||
// Add some members
|
||||
members.put(UUID.randomUUID(), RanksManager.MEMBER_RANK);
|
||||
members.put(UUID.randomUUID(), RanksManager.MEMBER_RANK);
|
||||
members.put(UUID.randomUUID(), RanksManager.MEMBER_RANK);
|
||||
members.put(UUID.randomUUID(), RanksManager.MEMBER_RANK);
|
||||
members.put(UUID.randomUUID(), RanksManager.MEMBER_RANK);
|
||||
members.put(UUID.randomUUID(), RanksManager.MEMBER_RANK);
|
||||
members.put(UUID.randomUUID(), RanksManager.MEMBER_RANK);
|
||||
// Add some coops
|
||||
members.put(UUID.randomUUID(), RanksManager.COOP_RANK);
|
||||
members.put(UUID.randomUUID(), RanksManager.COOP_RANK);
|
||||
members.put(UUID.randomUUID(), RanksManager.COOP_RANK);
|
||||
// Add some trusted
|
||||
members.put(UUID.randomUUID(), RanksManager.TRUSTED_RANK);
|
||||
members.put(UUID.randomUUID(), RanksManager.TRUSTED_RANK);
|
||||
// Add specific coop
|
||||
UUID coopUUID = UUID.randomUUID();
|
||||
members.put(coopUUID, RanksManager.COOP_RANK);
|
||||
// Clear a random user
|
||||
im.clearRank(RanksManager.COOP_RANK, UUID.randomUUID());
|
||||
assertEquals(14, members.size());
|
||||
im.clearRank(RanksManager.COOP_RANK, coopUUID);
|
||||
assertEquals(13, members.size());
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user