mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2024-12-28 03:57:39 +01:00
Rewrote IslandGrid to use Table<> from Guava
It also prepares further implementation for reserved "locations"
This commit is contained in:
parent
3ff387121a
commit
b54c7ad662
@ -1,17 +1,21 @@
|
|||||||
package world.bentobox.bentobox.managers.island;
|
package world.bentobox.bentobox.managers.island;
|
||||||
|
|
||||||
import java.util.Map.Entry;
|
import com.google.common.collect.Table;
|
||||||
import java.util.TreeMap;
|
import com.google.common.collect.TreeBasedTable;
|
||||||
|
|
||||||
import world.bentobox.bentobox.database.objects.Island;
|
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
|
* @author tastybento
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class IslandGrid {
|
public class IslandGrid {
|
||||||
private TreeMap<Integer, TreeMap<Integer, Island>> grid = new TreeMap<>();
|
|
||||||
|
/**
|
||||||
|
* Row : x location
|
||||||
|
* Column : z location
|
||||||
|
* Value : Island
|
||||||
|
*/
|
||||||
|
private Table<Integer, Integer, Cell> grid = TreeBasedTable.create();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds island to grid
|
* 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
|
* @return true if successfully added, false if island already exists, or there is an overlap
|
||||||
*/
|
*/
|
||||||
public boolean addToGrid(Island island) {
|
public boolean addToGrid(Island island) {
|
||||||
if (grid.containsKey(island.getMinX())) {
|
if (grid.contains(island.getMinX(), island.getMinZ())) {
|
||||||
TreeMap<Integer, Island> zEntry = grid.get(island.getMinX());
|
// It is either occupied or reserved, so return false
|
||||||
if (zEntry.containsKey(island.getMinZ())) {
|
return false;
|
||||||
return false;
|
|
||||||
} else {
|
|
||||||
// Add island
|
|
||||||
zEntry.put(island.getMinZ(), island);
|
|
||||||
grid.put(island.getMinX(), zEntry);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Add island
|
|
||||||
TreeMap<Integer, Island> zEntry = new TreeMap<>();
|
|
||||||
zEntry.put(island.getMinZ(), island);
|
|
||||||
grid.put(island.getMinX(), zEntry);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// All clear, add the island to the grid.
|
||||||
|
grid.put(island.getMinX(), island.getMinZ(), new Cell(CellState.OCCUPIED, island));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,16 +41,10 @@ public class IslandGrid {
|
|||||||
public boolean removeFromGrid(Island island) {
|
public boolean removeFromGrid(Island island) {
|
||||||
// Remove from grid
|
// Remove from grid
|
||||||
if (island != null) {
|
if (island != null) {
|
||||||
int x = island.getMinX();
|
if (grid.contains(island.getMinX(), island.getMinZ())) {
|
||||||
int z = island.getMinZ();
|
// TODO add support for RESERVED cells
|
||||||
if (grid.containsKey(x)) {
|
grid.remove(island.getMinX(), island.getMinZ());
|
||||||
TreeMap<Integer, Island> zEntry = grid.get(x);
|
return true;
|
||||||
if (zEntry.containsKey(z)) {
|
|
||||||
// Island exists - delete it
|
|
||||||
zEntry.remove(z);
|
|
||||||
grid.put(x, zEntry);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@ -69,17 +59,41 @@ public class IslandGrid {
|
|||||||
* @return Island or null
|
* @return Island or null
|
||||||
*/
|
*/
|
||||||
public Island getIslandAt(int x, int z) {
|
public Island getIslandAt(int x, int z) {
|
||||||
Entry<Integer, TreeMap<Integer, Island>> en = grid.floorEntry(x);
|
if (grid.contains(x, z)) {
|
||||||
if (en != null) {
|
Cell cell = grid.get(x, z);
|
||||||
Entry<Integer, Island> ent = en.getValue().floorEntry(z);
|
if (cell.getState().equals(CellState.OCCUPIED)) {
|
||||||
if (ent != null) {
|
return (Island) cell.getObject();
|
||||||
// Check if in the island range
|
|
||||||
Island island = ent.getValue();
|
|
||||||
if (island.inIslandSpace(x, z)) {
|
|
||||||
return island;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user