mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2024-12-27 11:37:36 +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;
|
||||
|
||||
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<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
|
||||
@ -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<Integer, Island> 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<Integer, Island> 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<Integer, Island> 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<Integer, TreeMap<Integer, Island>> en = grid.floorEntry(x);
|
||||
if (en != null) {
|
||||
Entry<Integer, Island> 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
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user