Skip to content

Commit

Permalink
Abstract out getting and setting islands by ID
Browse files Browse the repository at this point in the history
This is preparation for potentially making the cache smaller and pulling
from the database instead when required. However, there are issues with
this because some calls can result in loading the whole database anyway.
  • Loading branch information
tastybento committed May 19, 2024
1 parent d701b7e commit 50276cb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 109 deletions.
101 changes: 0 additions & 101 deletions src/main/java/world/bentobox/bentobox/managers/IslandsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -1692,107 +1692,6 @@ public boolean nameExists(@NonNull World world, @NonNull String name) {
.anyMatch(n -> ChatColor.stripColor(n).equals(ChatColor.stripColor(name)));
}

/**
* Called by the admin team fix command. Attempts to fix the database for teams.
* It will identify and correct situations where a player is listed in multiple
* teams, or is the owner of multiple teams. It will also try to fix the current
* cache. It is recommended to restart the server after this command is run.
*
* @param user - admin calling
* @param world - game world to check
* @return CompletableFuture boolean - true when done
* @deprecated Not compatible with multi-islands. Will be removed.
*/
@Deprecated
public CompletableFuture<Boolean> checkTeams(User user, World world) {
CompletableFuture<Boolean> r = new CompletableFuture<>();
user.sendMessage("commands.admin.team.fix.scanning");
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
Map<UUID, Island> owners = new HashMap<>();
Map<UUID, Integer> freq = new HashMap<>();
Map<UUID, List<Island>> memberships = new HashMap<>();
handler.loadObjects().stream().filter(i -> i.getOwner() != null).filter(i -> i.getWorld() != null)
.filter(i -> i.getWorld().equals(world)).filter(i -> !i.isDoNotLoad()).forEach(i -> {
int count = freq.getOrDefault(i.getOwner(), 0);
freq.put(i.getOwner(), count + 1);
if (owners.containsKey(i.getOwner())) {
// Player already has an island in the database
user.sendMessage("commands.admin.team.fix.duplicate-owner", TextVariables.NAME,
plugin.getPlayers().getName(i.getOwner()));
Island prev = owners.get(i.getOwner());
// Find out if this island is in the cache
Island cachedIsland = this.getIsland(i.getWorld(), i.getOwner());
if (cachedIsland != null && !cachedIsland.getUniqueId().equals(i.getUniqueId())) {
islandCache.deleteIslandFromCache(i.getUniqueId());
handler.deleteID(i.getUniqueId());
}
if (cachedIsland != null && !cachedIsland.getUniqueId().equals(prev.getUniqueId())) {
islandCache.deleteIslandFromCache(prev.getUniqueId());
handler.deleteID(prev.getUniqueId());
}
} 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())));
// 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 = RanksManager.getInstance().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), TextVariables.XYZ, xyz);
user.sendRawMessage(i.getUniqueId());
}
// Fix island ownership in cache
// Correct island cache
if (highestRank == RanksManager.OWNER_RANK && highestIsland != null
&& islandCache.getIslandById(highestIsland.getUniqueId()) != null) {
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());
if (i != null) {
// Remove membership of this island
i.removeMember(en.getKey());
}
// Remove from database island
island.removeMember(en.getKey());
// Save to database
handler.saveObjectAsync(island)
.thenRun(() -> user.sendMessage("commands.admin.team.fix.fixed"));
} else {
// Special check for when a player is an owner and member
}
}

});
user.sendMessage("commands.admin.team.fix.done");
r.complete(true);
});

return r;
}

/**
* Is user mid home teleport?
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void updateIsland(@NonNull Island newIsland) {
return;
}
// Get the old island
Island oldIsland = islandsById.get(newIsland.getUniqueId());
Island oldIsland = getIslandById(newIsland.getUniqueId());
Set<UUID> newMembers = newIsland.getMembers().keySet();
if (oldIsland != null) {
Set<UUID> oldMembers = oldIsland.getMembers().keySet();
Expand All @@ -84,7 +84,7 @@ public void updateIsland(@NonNull Island newIsland) {
islandsByUUID.put(newMember, set);
}

if (islandsById.put(newIsland.getUniqueId(), newIsland) == null) {
if (setIslandById(newIsland) == null) {
BentoBox.getInstance().logError("islandsById failed to update");
}

Expand All @@ -101,7 +101,7 @@ public boolean addIsland(@NonNull Island island) {
return false;
}
if (addToGrid(island)) {
islandsById.put(island.getUniqueId(), island);
setIslandById(island);
// Only add islands to this map if they are owned
if (island.isOwned()) {
islandsByUUID.computeIfAbsent(island.getOwner(), k -> new HashSet<>()).add(island.getUniqueId());
Expand All @@ -120,7 +120,7 @@ public boolean addIsland(@NonNull Island island) {
* associated per world.
*/
public void addPlayer(@NonNull UUID uuid, @NonNull Island island) {
this.islandsById.put(island.getUniqueId(), island);
this.setIslandById(island);
this.islandsByUUID.computeIfAbsent(uuid, k -> new HashSet<>()).add(island.getUniqueId());
}

Expand Down Expand Up @@ -166,7 +166,7 @@ private void removeFromIslandsByUUID(Island island) {
*/
public void deleteIslandFromCache(@NonNull String uniqueId) {
if (islandsById.containsKey(uniqueId)) {
deleteIslandFromCache(islandsById.get(uniqueId));
deleteIslandFromCache(getIslandById(uniqueId));
}
}

Expand All @@ -182,7 +182,6 @@ public void deleteIslandFromCache(@NonNull String uniqueId) {
public Island get(@NonNull World world, @NonNull UUID uuid) {
List<Island> islands = getIslands(world, uuid);
if (islands.isEmpty()) {
System.out.println("empty");
return null;
}
for (Island island : islands) {
Expand All @@ -208,7 +207,7 @@ public List<Island> getIslands(@NonNull World world, @NonNull UUID uuid) {
if (w == null) {
return new ArrayList<>();
}
return islandsByUUID.computeIfAbsent(uuid, k -> new HashSet<>()).stream().map(islandsById::get)
return islandsByUUID.computeIfAbsent(uuid, k -> new HashSet<>()).stream().map(this::getIslandById)
.filter(Objects::nonNull).filter(island -> w.equals(island.getWorld()))
.sorted(Comparator.comparingLong(Island::getCreatedDate))
.collect(Collectors.toList());
Expand Down Expand Up @@ -384,7 +383,7 @@ public void setOwner(@NonNull Island island, @Nullable UUID newOwnerUUID) {
islandsByUUID.computeIfAbsent(newOwnerUUID, k -> new HashSet<>()).add(island.getUniqueId());
}
island.setRank(newOwnerUUID, RanksManager.OWNER_RANK);
islandsById.put(island.getUniqueId(), island);
setIslandById(island);
}

/**
Expand All @@ -399,6 +398,10 @@ public Island getIslandById(@NonNull String uniqueId) {
return islandsById.get(uniqueId);
}

private Island setIslandById(Island island) {
return islandsById.put(island.getUniqueId(), island);
}

/**
* Resets all islands in this game mode to default flag settings
*
Expand Down

0 comments on commit 50276cb

Please sign in to comment.