Fixes for Island cache issues

Fix the size check and the new island creation.
This commit is contained in:
tastybento 2024-06-01 11:42:22 -07:00
parent 01dcd6ecc6
commit 250c7950f9
3 changed files with 27 additions and 3 deletions

View File

@ -1262,7 +1262,7 @@ public class IslandsManager {
} else { } else {
// Fix island center if it is off // Fix island center if it is off
fixIslandCenter(island); fixIslandCenter(island);
islandCache.addIsland(island); islandCache.addIsland(island, true);
if (island.isSpawn()) { if (island.isSpawn()) {
// Success, set spawn if this is the spawn island. // Success, set spawn if this is the spawn island.

View File

@ -97,17 +97,29 @@ public class IslandCache {
/** /**
* Adds an island to the grid, used for new islands * Adds an island to the grid, used for new islands
* Caches island.
* *
* @param island island to add, not null * @param island island to add, not null
* @return true if successfully added, false if not * @return true if successfully added, false if not
*/ */
public boolean addIsland(@NonNull Island island) { public boolean addIsland(@NonNull Island island) {
return addIsland(island, false);
}
/**
* Adds an island to the grid, used for new islands
*
* @param island island to add, not null
* @param noCache - if true, island will not be cached
* @return true if successfully added, false if not
*/
public boolean addIsland(@NonNull Island island, boolean noCache) {
if (island.getCenter() == null || island.getWorld() == null) { if (island.getCenter() == null || island.getWorld() == null) {
return false; return false;
} }
if (addToGrid(island)) { if (addToGrid(island)) {
// Insert a null into the map as a placeholder for cache // Insert a null into the map as a placeholder for cache
islandsById.put(island.getUniqueId().intern(), null); islandsById.put(island.getUniqueId().intern(), noCache ? null : island);
// Only add islands to this map if they are owned // Only add islands to this map if they are owned
if (island.isOwned()) { if (island.isOwned()) {
islandsByUUID.computeIfAbsent(island.getOwner(), k -> new HashSet<>()).add(island.getUniqueId()); islandsByUUID.computeIfAbsent(island.getOwner(), k -> new HashSet<>()).add(island.getUniqueId());
@ -402,7 +414,8 @@ public class IslandCache {
* @return the number of islands * @return the number of islands
*/ */
public long size(World world) { public long size(World world) {
return this.islandsById.values().stream().map(Island::getWorld).filter(world::equals).count(); // Get from grids because this is where we have islands by world
return this.grids.containsKey(world) ? this.grids.get(world).getSize() : 0L;
} }
/** /**

View File

@ -100,4 +100,15 @@ class IslandGrid {
return null; return null;
} }
/**
* @return number of islands stored in the grid
*/
public long getSize() {
long count = 0;
for (TreeMap<Integer, String> innerMap : grid.values()) {
count += innerMap.size();
}
return count;
}
} }