Store the island sizes in the grid to avoid database loadings #2534

This commit is contained in:
tastybento 2024-10-20 17:10:10 -07:00
parent e0a3f48aed
commit 6766c0f906
4 changed files with 74 additions and 83 deletions

View File

@ -442,6 +442,10 @@ public class IslandsManager {
: Optional.empty();
}
public boolean isIslandAd(@NonNull Location location) {
return plugin.getIWM().inWorld(location) ? islandCache.isIslandAt(location) : false;
}
/**
* Returns an <strong>unmodifiable collection</strong> of all existing islands
* (even those who may be unowned).

View File

@ -257,6 +257,23 @@ public class IslandCache {
}
}
/**
* Returns if there is island at the location. This includes
* the full island space, not just the protected area.
* Does not cause a database load of the island.
*
* @param location the location
* @return true if there is an island there
*/
@Nullable
public boolean isIslandAt(@NonNull Location location) {
World w = Util.getWorld(location.getWorld());
if (w == null || !grids.containsKey(w)) {
return false;
}
return grids.get(w).isIslandAt(location.getBlockX(), location.getBlockZ());
}
/**
* Returns the island at the location or null if there is none. This includes
* the full island space, not just the protected area

View File

@ -3,6 +3,8 @@ package world.bentobox.bentobox.managers.island;
import java.util.Map.Entry;
import java.util.TreeMap;
import org.eclipse.jdt.annotation.Nullable;
import world.bentobox.bentobox.database.objects.Island;
/**
@ -11,7 +13,11 @@ import world.bentobox.bentobox.database.objects.Island;
*
*/
class IslandGrid {
private final TreeMap<Integer, TreeMap<Integer, String>> grid = new TreeMap<>();
private record IslandData(String id, int minX, int minZ, int range) {
}
private final TreeMap<Integer, TreeMap<Integer, IslandData>> grid = new TreeMap<>();
private final IslandCache im;
/**
@ -29,10 +35,13 @@ class IslandGrid {
*/
public boolean addToGrid(Island island) {
// Check if we know about this island already
if (grid.containsKey(island.getMinX())) {
TreeMap<Integer, String> zEntry = grid.get(island.getMinX());
if (zEntry.containsKey(island.getMinZ())) {
if (island.getUniqueId().equals(zEntry.get(island.getMinZ()))) {
int minX = island.getMinX();
int minZ = island.getMinZ();
IslandData islandData = new IslandData(island.getUniqueId(), minZ, minZ, island.getRange());
if (grid.containsKey(minX)) {
TreeMap<Integer, IslandData> zEntry = grid.get(minX);
if (zEntry.containsKey(minZ)) {
if (island.getUniqueId().equals(zEntry.get(minZ).id())) {
// If it is the same island then it's okay
return true;
}
@ -40,14 +49,14 @@ class IslandGrid {
return false;
} else {
// Add island
zEntry.put(island.getMinZ(), island.getUniqueId());
grid.put(island.getMinX(), zEntry);
zEntry.put(minZ, islandData);
grid.put(minX, zEntry);
}
} else {
// Add island
TreeMap<Integer, String> zEntry = new TreeMap<>();
zEntry.put(island.getMinZ(), island.getUniqueId());
grid.put(island.getMinX(), zEntry);
TreeMap<Integer, IslandData> zEntry = new TreeMap<>();
zEntry.put(minZ, islandData);
grid.put(minX, zEntry);
}
return true;
}
@ -60,7 +69,7 @@ class IslandGrid {
public boolean removeFromGrid(Island island) {
String id = island.getUniqueId();
boolean removed = grid.values().stream()
.anyMatch(innerMap -> innerMap.values().removeIf(innerValue -> innerValue.equals(id)));
.anyMatch(innerMap -> innerMap.values().removeIf(innerValue -> innerValue.id().equals(id)));
grid.values().removeIf(TreeMap::isEmpty);
@ -70,35 +79,55 @@ class IslandGrid {
/**
* Retrieves the island located at the specified x and z coordinates, covering both the protected area
* and the full island space. Returns null if no island exists at the given location.
* This will load the island from the database if it is not in the cache.
*
* @param x the x coordinate of the location
* @param z the z coordinate of the location
* @return the Island at the specified location, or null if no island is found
*/
public Island getIslandAt(int x, int z) {
String id = getIslandStringAt(x, z);
if (id == null) {
return null;
}
// Retrieve the island using the id found - loading from database if required
return im.getIslandById(id);
}
/**
* Checks if an island is at this coordinate or not
* @param x coord
* @param z coord
* @return true if there is an island registered in the grid
*/
public boolean isIslandAt(int x, int z) {
return getIslandStringAt(x, z) != null;
}
/**
* Get the island ID string for an island at this coordinates, or null if none.
* @param x coord
* @param z coord
* @return Unique Island ID string, or null if there is no island here.
*/
public @Nullable String getIslandStringAt(int x, int z) {
// Attempt to find the closest x-coordinate entry that does not exceed 'x'
Entry<Integer, TreeMap<Integer, String>> xEntry = grid.floorEntry(x);
Entry<Integer, TreeMap<Integer, IslandData>> xEntry = grid.floorEntry(x);
if (xEntry == null) {
return null; // No x-coordinate entry found, return null
}
// Attempt to find the closest z-coordinate entry that does not exceed 'z' within the found x-coordinate
Entry<Integer, String> zEntry = xEntry.getValue().floorEntry(z);
Entry<Integer, IslandData> zEntry = xEntry.getValue().floorEntry(z);
if (zEntry == null) {
return null; // No z-coordinate entry found, return null
}
// Retrieve the island using the id found in the z-coordinate entry
Island island = im.getIslandById(zEntry.getValue());
if (island == null) {
return null; // No island found by the id, return null
}
// Check if the specified coordinates are within the island space
if (island.inIslandSpace(x, z)) {
return island; // Coordinates are within island space, return the island
if (x >= zEntry.getValue().minX() && x < zEntry.getValue().minX() + zEntry.getValue().range() * 2
&& z >= zEntry.getValue().minZ() && z < zEntry.getValue().minZ() + zEntry.getValue().range() * 2) {
return zEntry.getValue().id();
}
// Coordinates are outside the island space, return null
return null;
}
@ -107,7 +136,7 @@ class IslandGrid {
*/
public long getSize() {
long count = 0;
for (TreeMap<Integer, String> innerMap : grid.values()) {
for (TreeMap<Integer, IslandData> innerMap : grid.values()) {
count += innerMap.size();
}
return count;

View File

@ -1,59 +0,0 @@
/**
*
*/
package world.bentobox.bentobox.panels.customizable;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.bukkit.World;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.flags.Flag;
import world.bentobox.bentobox.api.panels.PanelItem;
import world.bentobox.bentobox.api.panels.TabbedPanel;
import world.bentobox.bentobox.api.panels.TemplatedPanel.ItemSlot;
import world.bentobox.bentobox.api.panels.reader.ItemTemplateRecord;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
/**
*
*/
public class SettingsPanel extends AbstractPanel {
protected static final String PROTECTION_PANEL = "protection.panel.";
private static final String CLICK_TO_SWITCH = PROTECTION_PANEL + "mode.click-to-switch";
protected Flag.Type type;
protected World world;
protected Island island;
protected TabbedPanel parent;
private Map<UUID, Flag.Mode> currentMode = new HashMap<>();
public SettingsPanel(CompositeCommand command, User user) {
super(command, user);
// TODO Auto-generated constructor stub
}
@Override
protected void build() {
// TODO Auto-generated method stub
}
@Override
protected PanelItem createNextButton(ItemTemplateRecord arg0, ItemSlot arg1) {
// TODO Auto-generated method stub
return null;
}
@Override
protected PanelItem createPreviousButton(ItemTemplateRecord arg0, ItemSlot arg1) {
// TODO Auto-generated method stub
return null;
}
}