From 916ba5d9993eb8de2a21a721a0939aeb9c7b8168 Mon Sep 17 00:00:00 2001 From: tastybento Date: Sun, 1 Nov 2020 17:29:18 -0800 Subject: [PATCH] Uses a different method to fix the database. https://github.com/BentoBoxWorld/BentoBox/issues/1556 --- .../bentobox/managers/IslandsManager.java | 55 +++++++++++++------ src/main/resources/locales/en-US.yml | 8 +-- 2 files changed, 43 insertions(+), 20 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/managers/IslandsManager.java b/src/main/java/world/bentobox/bentobox/managers/IslandsManager.java index c52c4d9fa..b97b0326c 100644 --- a/src/main/java/world/bentobox/bentobox/managers/IslandsManager.java +++ b/src/main/java/world/bentobox/bentobox/managers/IslandsManager.java @@ -1517,6 +1517,7 @@ public class IslandsManager { Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { Map owners = new HashMap<>(); Map freq = new HashMap<>(); + Map> memberships = new HashMap<>(); handler.loadObjects() .stream().filter(i -> i.getOwner() != null) .filter(i -> i.getWorld().equals(world)) @@ -1527,28 +1528,50 @@ public class IslandsManager { user.sendMessage("commands.admin.team.fix.duplicate-owner" , TextVariables.NAME, plugin.getPlayers().getName(i.getOwner())); } else { owners.put(i.getOwner(), i); + i.getMemberSet().forEach(u -> + // Place into membership + memberships.computeIfAbsent(u, k -> new ArrayList<>()).add(i)); } }); freq.entrySet().stream().filter(en -> en.getValue() > 1).forEach(en -> { user.sendMessage("commands.admin.team.fix.player-has", TextVariables.NAME, plugin.getPlayers().getName(en.getKey()), TextVariables.NUMBER, String.valueOf(en.getValue())); }); - owners.entrySet().stream().forEach(en -> { - en.getValue().getMemberSet().stream() - // Filter out owners - .filter(u-> owners.containsKey(u) && !owners.get(u).equals(en.getValue())) - .forEach(u -> { - user.sendMessage("commands.admin.team.fix.member-and-owner", TextVariables.NAME, plugin.getPlayers().getName(u)); - user.sendMessage("commands.admin.team.fix.member-of", "[xyz]", Util.xyz(en.getValue().getCenter().toVector())); - user.sendMessage("commands.admin.team.fix.owner-of", "[xyz]", Util.xyz(owners.get(u).getCenter().toVector())); - // Remove membership of this island - Island i = islandCache.getIslandById(en.getValue().getUniqueId()); - i.removeMember(u); - // Correct island cache - islandCache.setOwner(islandCache.getIslandById(owners.get(u).getUniqueId()), u); - // Save to database - handler.saveObjectAsync(i).thenRun(() -> user.sendMessage("commands.admin.team.fix.fixed")); + // Check for players in multiple teams + memberships.entrySet().stream() + .filter(en -> en.getValue().size() > 1) + .forEach(en -> { + // Get the islands + String ownerName = plugin.getPlayers().getName(en.getKey()); + user.sendMessage("commands.admin.team.fix.duplicate-member", TextVariables.NAME, ownerName); + int highestRank = 0; + Island highestIsland = null; + for (Island i : en.getValue()) { + int rankValue = i.getRank(en.getKey()); + String rank = plugin.getRanksManager().getRank(rankValue); + if (rankValue > highestRank || highestIsland == null) { + highestRank = rankValue; + highestIsland = i; + } + String xyz = Util.xyz(i.getCenter().toVector()); + user.sendMessage("commands.admin.team.fix.rank-on-island", TextVariables.RANK, user.getTranslation(rank), "[xyz]", xyz); + } + // Fix island ownership in cache + // Correct island cache + if (highestRank == RanksManager.OWNER_RANK) { + islandCache.setOwner(islandCache.getIslandById(highestIsland.getUniqueId()), en.getKey()); + } + // Fix all the entries that are not the highest + for (Island island : en.getValue()) { + if (!island.equals(highestIsland)) { + // Get the actual island being used in the cache + Island i = islandCache.getIslandById(island.getUniqueId()); + // Remove membership of this island + i.removeMember(en.getKey()); + // Save to database + handler.saveObjectAsync(i).thenRun(() -> user.sendMessage("commands.admin.team.fix.fixed")); + } + } - }); }); user.sendMessage("commands.admin.team.fix.done"); r.complete(true); diff --git a/src/main/resources/locales/en-US.yml b/src/main/resources/locales/en-US.yml index 42e0d658a..073e2fa67 100644 --- a/src/main/resources/locales/en-US.yml +++ b/src/main/resources/locales/en-US.yml @@ -118,11 +118,11 @@ commands: scanning: "Scanning database..." duplicate-owner: "&c Player owns more than one island in database: [name]" player-has: "&c Player [name] has [number] islands" - member-and-owner: "&c Cross-membership found: Player [name] is an island owner and member" - owner-of: "&c Owner of island at [xyz]" - member-of: "&c Member of island at [xyz]" + duplicate-member: "&c Player [name] is a member of more than one island in the database" + player-has: "&c Player [name] has [number] islands" + rank-on-island: "&c [rank] on island at [xyz]" fixed: "&a Fixed" - done: "&a Scan completed" + done: "&a Scan" kick: parameters: "" description: "kick a player from a team"