From b54c7ad6624d430a0bd27077411fa75a2b54d24d Mon Sep 17 00:00:00 2001 From: Florian CUNY Date: Mon, 13 Aug 2018 21:45:43 +0200 Subject: [PATCH] Rewrote IslandGrid to use Table<> from Guava It also prepares further implementation for reserved "locations" --- .../bentobox/managers/island/IslandGrid.java | 92 +++++++++++-------- 1 file changed, 53 insertions(+), 39 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/managers/island/IslandGrid.java b/src/main/java/world/bentobox/bentobox/managers/island/IslandGrid.java index 3a002c191..08cbbc247 100644 --- a/src/main/java/world/bentobox/bentobox/managers/island/IslandGrid.java +++ b/src/main/java/world/bentobox/bentobox/managers/island/IslandGrid.java @@ -1,17 +1,21 @@ package world.bentobox.bentobox.managers.island; -import java.util.Map.Entry; -import java.util.TreeMap; - +import com.google.common.collect.Table; +import com.google.common.collect.TreeBasedTable; import world.bentobox.bentobox.database.objects.Island; /** - * Handles the island location grid for each world + * Handles the island location grid for each world. * @author tastybento - * */ public class IslandGrid { - private TreeMap> grid = new TreeMap<>(); + + /** + * Row : x location + * Column : z location + * Value : Island + */ + private Table grid = TreeBasedTable.create(); /** * Adds island to grid @@ -19,21 +23,13 @@ public class IslandGrid { * @return true if successfully added, false if island already exists, or there is an overlap */ public boolean addToGrid(Island island) { - if (grid.containsKey(island.getMinX())) { - TreeMap zEntry = grid.get(island.getMinX()); - if (zEntry.containsKey(island.getMinZ())) { - return false; - } else { - // Add island - zEntry.put(island.getMinZ(), island); - grid.put(island.getMinX(), zEntry); - } - } else { - // Add island - TreeMap zEntry = new TreeMap<>(); - zEntry.put(island.getMinZ(), island); - grid.put(island.getMinX(), zEntry); + if (grid.contains(island.getMinX(), island.getMinZ())) { + // It is either occupied or reserved, so return false + return false; } + + // All clear, add the island to the grid. + grid.put(island.getMinX(), island.getMinZ(), new Cell(CellState.OCCUPIED, island)); return true; } @@ -45,16 +41,10 @@ public class IslandGrid { public boolean removeFromGrid(Island island) { // Remove from grid if (island != null) { - int x = island.getMinX(); - int z = island.getMinZ(); - if (grid.containsKey(x)) { - TreeMap zEntry = grid.get(x); - if (zEntry.containsKey(z)) { - // Island exists - delete it - zEntry.remove(z); - grid.put(x, zEntry); - return true; - } + if (grid.contains(island.getMinX(), island.getMinZ())) { + // TODO add support for RESERVED cells + grid.remove(island.getMinX(), island.getMinZ()); + return true; } } return false; @@ -69,17 +59,41 @@ public class IslandGrid { * @return Island or null */ public Island getIslandAt(int x, int z) { - Entry> en = grid.floorEntry(x); - if (en != null) { - Entry ent = en.getValue().floorEntry(z); - if (ent != null) { - // Check if in the island range - Island island = ent.getValue(); - if (island.inIslandSpace(x, z)) { - return island; - } + if (grid.contains(x, z)) { + Cell cell = grid.get(x, z); + if (cell.getState().equals(CellState.OCCUPIED)) { + return (Island) cell.getObject(); } } return null; } + + /** + * @author Poslovitch + */ + private class Cell { + private CellState state; + private Object object; + + private Cell(CellState state, Object object) { + this.state = state; + this.object = object; + } + + private CellState getState() { + return state; + } + + private Object getObject() { + return object; + } + } + + /** + * @author Poslovitch + */ + private enum CellState { + RESERVED, + OCCUPIED + } }