Entity tracking and invalid world detection

This commit is contained in:
boy0001 2015-03-12 19:52:26 +11:00
parent 12f024664a
commit 1f8702f072
4 changed files with 79 additions and 3 deletions

View File

@ -136,8 +136,10 @@ public class PlotSquared {
public static Set<Plot> getPlots() {
final ArrayList<Plot> newplots = new ArrayList<>();
for (final HashMap<PlotId, Plot> world : plots.values()) {
newplots.addAll(world.values());
for (final Entry<String, HashMap<PlotId, Plot>> entry : plots.entrySet()) {
if (isPlotWorld(entry.getKey())) {
newplots.addAll(entry.getValue().values());
}
}
return new LinkedHashSet<>(newplots);
}

View File

@ -21,6 +21,8 @@ public abstract class ChunkManager {
final int z = loc.getZ() >> 9;
return new ChunkLoc(x, z);
}
public abstract int countEntities(Plot plot);
public abstract boolean loadChunk(String world, ChunkLoc loc);

View File

@ -54,7 +54,7 @@ public class MainUtil {
static long state = 1;
public static HashMap<String, PlotId> lastPlot = new HashMap<>();
public static HashMap<String, Integer> worldBorder = new HashMap<>();
public static ArrayList<PlotId> getMaxPlotSelectionIds(final String world, PlotId pos1, PlotId pos2) {
final Plot plot1 = PlotSquared.getPlots(world).get(pos1);

View File

@ -865,4 +865,76 @@ public class BukkitChunkManager extends ChunkManager {
clearAllEntities(MainUtil.getPlot(worldname, pos2));
// FIXME swap plots
}
@Override
public int countEntities(Plot plot) {
int count = 0;
World world = BukkitUtil.getWorld(plot.world);
Location bot = MainUtil.getPlotBottomLoc(plot.world, plot.id).add(1, 0, 1);
Location top = MainUtil.getPlotTopLoc(plot.world, plot.id);
int bx = bot.getX() >> 4;
int bz = bot.getZ() >> 4;
int tx = top.getX() >> 4;
int tz = top.getZ() >> 4;
int size = (tx-bx) << 4;
HashSet<Chunk> chunks = new HashSet<>();
for (int X = bx; X <= tx; X++) {
for (int Z = bz; Z <= tz; Z++) {
chunks.add(world.getChunkAt(X,Z));
}
}
boolean doWhole = false;
List<Entity> entities = null;
if (size > 200) {
entities = world.getEntities();
if (entities.size() < 16 + (size * size / 64)) {
doWhole = true;
}
}
if (doWhole) {
for (final Entity entity : entities) {
org.bukkit.Location loc = entity.getLocation();
Chunk chunk = loc.getChunk();
if (chunks.contains(chunk)) {
int X = chunk.getX();
int Z = chunk.getX();
if (X > bx && X < tx && Z > bz && Z < tz) {
count++;
}
else {
final PlotId id = MainUtil.getPlotId(BukkitUtil.getLocation(loc));
if (plot.id.equals(id)) {
count++;
}
}
}
}
}
else {
for (Chunk chunk : chunks) {
int X = chunk.getX();
int Z = chunk.getX();
Entity[] ents = chunk.getEntities();
if (X == bx || X == tx || Z == bz || Z == tz) {
for (final Entity entity : ents) {
final PlotId id = MainUtil.getPlotId(BukkitUtil.getLocation(entity));
if (plot.id.equals(id)) {
count++;
}
}
}
else {
count += ents.length;
}
}
return count;
}
return count;
}
}