Revert "Rewrote IslandGrid to use Table<> from Guava"

This reverts commit b54c7ad662.
This commit is contained in:
Florian CUNY 2018-08-15 09:53:25 +02:00
parent 82ebc8b018
commit bce73a551d

View File

@ -1,5 +1,6 @@
package world.bentobox.bentobox.managers.island;
<<<<<<< HEAD
<<<<<<< HEAD
import org.apache.commons.lang.Validate;
@ -10,20 +11,20 @@ import com.google.common.collect.TreeBasedTable;
import com.google.common.collect.Table;
import com.google.common.collect.TreeBasedTable;
>>>>>>> parent of bfff61e... Rewrote IslandGrid.Cell
=======
import java.util.Map.Entry;
import java.util.TreeMap;
>>>>>>> parent of b54c7ad... Rewrote IslandGrid to use Table<> from Guava
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 {
/**
* Row : x location
* Column : z location
* Value : Island
*/
private Table<Integer, Integer, Cell> grid = TreeBasedTable.create();
private TreeMap<Integer, TreeMap<Integer, Island>> grid = new TreeMap<>();
/**
* Adds island to grid
@ -31,13 +32,21 @@ 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.contains(island.getMinX(), island.getMinZ())) {
// It is either occupied or reserved, so return false
return false;
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);
}
// All clear, add the island to the grid.
grid.put(island.getMinX(), island.getMinZ(), new Cell(CellState.OCCUPIED, island));
return true;
}
@ -49,10 +58,16 @@ public class IslandGrid {
public boolean removeFromGrid(Island island) {
// Remove from grid
if (island != null) {
if (grid.contains(island.getMinX(), island.getMinZ())) {
// TODO add support for RESERVED cells
grid.remove(island.getMinX(), island.getMinZ());
return true;
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;
}
}
}
return false;
@ -67,41 +82,17 @@ public class IslandGrid {
* @return Island or null
*/
public Island getIslandAt(int x, int z) {
if (grid.contains(x, z)) {
Cell cell = grid.get(x, z);
if (cell.getState().equals(CellState.OCCUPIED)) {
return (Island) cell.getObject();
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;
}
}
}
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
}
}