chunk manager error catching

This commit is contained in:
boy0001 2015-05-21 18:42:00 +10:00
parent e7733e749d
commit df5167813a

View File

@ -153,7 +153,7 @@ public class BukkitChunkManager extends ChunkManager {
*/ */
@Override @Override
public boolean copyRegion(final Location pos1, final Location pos2, final Location newPos, final Runnable whenDone) { public boolean copyRegion(final Location pos1, final Location pos2, final Location newPos, final Runnable whenDone) {
TaskManager.index.increment(); TaskManager.index.increment();
final int relX = newPos.getX() - pos1.getX(); final int relX = newPos.getX() - pos1.getX();
final int relZ = newPos.getZ() - pos1.getZ(); final int relZ = newPos.getZ() - pos1.getZ();
@ -196,7 +196,7 @@ public class BukkitChunkManager extends ChunkManager {
TaskManager.runTask(new Runnable() { TaskManager.runTask(new Runnable() {
@Override @Override
public void run() { public void run() {
TaskManager.index.increment(); TaskManager.index.increment();
// Copy entities // Copy entities
initMaps(); initMaps();
for (int x = c1x; x <= c2x; x++) { for (int x = c1x; x <= c2x; x++) {
@ -264,7 +264,7 @@ public class BukkitChunkManager extends ChunkManager {
@Override @Override
public boolean regenerateRegion(final Location pos1, final Location pos2, final Runnable whenDone) { public boolean regenerateRegion(final Location pos1, final Location pos2, final Runnable whenDone) {
TaskManager.index.increment(); TaskManager.index.increment();
final Plugin plugin = BukkitMain.THIS; final Plugin plugin = BukkitMain.THIS;
final World world = Bukkit.getWorld(pos1.getWorld()); final World world = Bukkit.getWorld(pos1.getWorld());
final Chunk c1 = world.getChunkAt(pos1.getX() >> 4, pos1.getZ() >> 4); final Chunk c1 = world.getChunkAt(pos1.getX() >> 4, pos1.getZ() >> 4);
@ -290,8 +290,8 @@ public class BukkitChunkManager extends ChunkManager {
final Integer task = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() { final Integer task = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
@Override @Override
public void run() { public void run() {
long start = System.currentTimeMillis(); final long start = System.currentTimeMillis();
while (System.currentTimeMillis() - start < 20) { while ((System.currentTimeMillis() - start) < 20) {
if (chunks.size() == 0) { if (chunks.size() == 0) {
TaskManager.runTaskLater(whenDone, 1); TaskManager.runTaskLater(whenDone, 1);
Bukkit.getScheduler().cancelTask(TaskManager.tasks.get(currentIndex)); Bukkit.getScheduler().cancelTask(TaskManager.tasks.get(currentIndex));
@ -403,7 +403,7 @@ public class BukkitChunkManager extends ChunkManager {
saveEntitiesIn(chunk, region, 0, 0, false); saveEntitiesIn(chunk, region, 0, 0, false);
} }
public static void saveEntitiesIn(final Chunk chunk, final RegionWrapper region, int offset_x, int offset_z, boolean delete) { public static void saveEntitiesIn(final Chunk chunk, final RegionWrapper region, final int offset_x, final int offset_z, final boolean delete) {
for (final Entity entity : chunk.getEntities()) { for (final Entity entity : chunk.getEntities()) {
final Location loc = BukkitUtil.getLocation(entity); final Location loc = BukkitUtil.getLocation(entity);
final int x = loc.getX(); final int x = loc.getX();
@ -439,183 +439,247 @@ public class BukkitChunkManager extends ChunkManager {
public static void restoreBlocks(final World world, final int x_offset, final int z_offset) { public static void restoreBlocks(final World world, final int x_offset, final int z_offset) {
for (final BlockLoc loc : chestContents.keySet()) { for (final BlockLoc loc : chestContents.keySet()) {
final Block block = world.getBlockAt(loc.x + x_offset, loc.y, loc.z + z_offset); try {
final BlockState state = block.getState(); final Block block = world.getBlockAt(loc.x + x_offset, loc.y, loc.z + z_offset);
if (state instanceof Chest) { final BlockState state = block.getState();
final Chest chest = (Chest) state; if (state instanceof Chest) {
chest.getInventory().setContents(chestContents.get(loc)); final Chest chest = (Chest) state;
state.update(true); chest.getInventory().setContents(chestContents.get(loc));
} else { state.update(true);
} else {
PlotSquared.log("&c[WARN] Plot clear failed to regenerate chest: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset);
}
} catch (final Exception e) {
PlotSquared.log("&c[WARN] Plot clear failed to regenerate chest: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset); PlotSquared.log("&c[WARN] Plot clear failed to regenerate chest: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset);
} }
} }
for (final BlockLoc loc : signContents.keySet()) { for (final BlockLoc loc : signContents.keySet()) {
final Block block = world.getBlockAt(loc.x + x_offset, loc.y, loc.z + z_offset); try {
final BlockState state = block.getState(); final Block block = world.getBlockAt(loc.x + x_offset, loc.y, loc.z + z_offset);
if (state instanceof Sign) { final BlockState state = block.getState();
final Sign sign = (Sign) state; if (state instanceof Sign) {
int i = 0; final Sign sign = (Sign) state;
for (final String line : signContents.get(loc)) { int i = 0;
sign.setLine(i, line); for (final String line : signContents.get(loc)) {
i++; sign.setLine(i, line);
i++;
}
state.update(true);
} else {
PlotSquared.log("&c[WARN] Plot clear failed to regenerate sign: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset);
} }
state.update(true); } catch (final Exception e) {
} else {
PlotSquared.log("&c[WARN] Plot clear failed to regenerate sign: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset); PlotSquared.log("&c[WARN] Plot clear failed to regenerate sign: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset);
} }
} }
for (final BlockLoc loc : dispenserContents.keySet()) { for (final BlockLoc loc : dispenserContents.keySet()) {
final Block block = world.getBlockAt(loc.x + x_offset, loc.y, loc.z + z_offset); try {
final BlockState state = block.getState(); final Block block = world.getBlockAt(loc.x + x_offset, loc.y, loc.z + z_offset);
if (state instanceof Dispenser) { final BlockState state = block.getState();
((Dispenser) (state)).getInventory().setContents(dispenserContents.get(loc)); if (state instanceof Dispenser) {
state.update(true); ((Dispenser) (state)).getInventory().setContents(dispenserContents.get(loc));
} else { state.update(true);
} else {
PlotSquared.log("&c[WARN] Plot clear failed to regenerate dispenser: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset);
}
} catch (final Exception e) {
PlotSquared.log("&c[WARN] Plot clear failed to regenerate dispenser: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset); PlotSquared.log("&c[WARN] Plot clear failed to regenerate dispenser: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset);
} }
} }
for (final BlockLoc loc : dropperContents.keySet()) { for (final BlockLoc loc : dropperContents.keySet()) {
final Block block = world.getBlockAt(loc.x + x_offset, loc.y, loc.z + z_offset); try {
final BlockState state = block.getState(); final Block block = world.getBlockAt(loc.x + x_offset, loc.y, loc.z + z_offset);
if (state instanceof Dropper) { final BlockState state = block.getState();
((Dropper) (state)).getInventory().setContents(dropperContents.get(loc)); if (state instanceof Dropper) {
state.update(true); ((Dropper) (state)).getInventory().setContents(dropperContents.get(loc));
} else { state.update(true);
} else {
PlotSquared.log("&c[WARN] Plot clear failed to regenerate dispenser: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset);
}
} catch (final Exception e) {
PlotSquared.log("&c[WARN] Plot clear failed to regenerate dispenser: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset); PlotSquared.log("&c[WARN] Plot clear failed to regenerate dispenser: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset);
} }
} }
for (final BlockLoc loc : beaconContents.keySet()) { for (final BlockLoc loc : beaconContents.keySet()) {
final Block block = world.getBlockAt(loc.x + x_offset, loc.y, loc.z + z_offset); try {
final BlockState state = block.getState(); final Block block = world.getBlockAt(loc.x + x_offset, loc.y, loc.z + z_offset);
if (state instanceof Beacon) { final BlockState state = block.getState();
((Beacon) (state)).getInventory().setContents(beaconContents.get(loc)); if (state instanceof Beacon) {
state.update(true); ((Beacon) (state)).getInventory().setContents(beaconContents.get(loc));
} else { state.update(true);
} else {
PlotSquared.log("&c[WARN] Plot clear failed to regenerate beacon: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset);
}
} catch (final Exception e) {
PlotSquared.log("&c[WARN] Plot clear failed to regenerate beacon: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset); PlotSquared.log("&c[WARN] Plot clear failed to regenerate beacon: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset);
} }
} }
for (final BlockLoc loc : jukeDisc.keySet()) { for (final BlockLoc loc : jukeDisc.keySet()) {
final Block block = world.getBlockAt(loc.x + x_offset, loc.y, loc.z + z_offset); try {
final BlockState state = block.getState(); final Block block = world.getBlockAt(loc.x + x_offset, loc.y, loc.z + z_offset);
if (state instanceof Jukebox) { final BlockState state = block.getState();
((Jukebox) (state)).setPlaying(Material.getMaterial(jukeDisc.get(loc))); if (state instanceof Jukebox) {
state.update(true); ((Jukebox) (state)).setPlaying(Material.getMaterial(jukeDisc.get(loc)));
} else { state.update(true);
PlotSquared.log("&c[WARN] Plot clear failed to restore jukebox: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset); } else {
PlotSquared.log("&c[WARN] Plot clear failed to restore jukebox: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset);
}
} catch (final Exception e) {
PlotSquared.log("&c[WARN] Plot clear failed to regenerate jukebox: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset);
} }
} }
for (final BlockLoc loc : skullData.keySet()) { for (final BlockLoc loc : skullData.keySet()) {
final Block block = world.getBlockAt(loc.x + x_offset, loc.y, loc.z + z_offset); try {
final BlockState state = block.getState(); final Block block = world.getBlockAt(loc.x + x_offset, loc.y, loc.z + z_offset);
if (state instanceof Skull) { final BlockState state = block.getState();
final Object[] data = skullData.get(loc); if (state instanceof Skull) {
if (data[0] != null) { final Object[] data = skullData.get(loc);
((Skull) (state)).setOwner((String) data[0]); if (data[0] != null) {
((Skull) (state)).setOwner((String) data[0]);
}
if (((Integer) data[1]) != 0) {
((Skull) (state)).setRotation(BlockFace.values()[(int) data[1]]);
}
if (((Integer) data[2]) != 0) {
((Skull) (state)).setSkullType(SkullType.values()[(int) data[2]]);
}
state.update(true);
} else {
PlotSquared.log("&c[WARN] Plot clear failed to restore skull: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset);
} }
if (((Integer) data[1]) != 0) { } catch (final Exception e) {
((Skull) (state)).setRotation(BlockFace.values()[(int) data[1]]); PlotSquared.log("&c[WARN] Plot clear failed to regenerate skull: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset);
}
if (((Integer) data[2]) != 0) {
((Skull) (state)).setSkullType(SkullType.values()[(int) data[2]]);
}
state.update(true);
} else {
PlotSquared.log("&c[WARN] Plot clear failed to restore jukebox: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset);
} }
} }
for (final BlockLoc loc : hopperContents.keySet()) { for (final BlockLoc loc : hopperContents.keySet()) {
final Block block = world.getBlockAt(loc.x + x_offset, loc.y, loc.z + z_offset); try {
final BlockState state = block.getState(); final Block block = world.getBlockAt(loc.x + x_offset, loc.y, loc.z + z_offset);
if (state instanceof Hopper) { final BlockState state = block.getState();
((Hopper) (state)).getInventory().setContents(hopperContents.get(loc)); if (state instanceof Hopper) {
state.update(true); ((Hopper) (state)).getInventory().setContents(hopperContents.get(loc));
} else { state.update(true);
} else {
PlotSquared.log("&c[WARN] Plot clear failed to regenerate hopper: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset);
}
} catch (final Exception e) {
PlotSquared.log("&c[WARN] Plot clear failed to regenerate hopper: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset); PlotSquared.log("&c[WARN] Plot clear failed to regenerate hopper: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset);
} }
} }
for (final BlockLoc loc : noteBlockContents.keySet()) { for (final BlockLoc loc : noteBlockContents.keySet()) {
final Block block = world.getBlockAt(loc.x + x_offset, loc.y, loc.z + z_offset); try {
final BlockState state = block.getState(); final Block block = world.getBlockAt(loc.x + x_offset, loc.y, loc.z + z_offset);
if (state instanceof NoteBlock) { final BlockState state = block.getState();
((NoteBlock) (state)).setNote(noteBlockContents.get(loc)); if (state instanceof NoteBlock) {
state.update(true); ((NoteBlock) (state)).setNote(noteBlockContents.get(loc));
} else { state.update(true);
} else {
PlotSquared.log("&c[WARN] Plot clear failed to regenerate note block: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset);
}
} catch (final Exception e) {
PlotSquared.log("&c[WARN] Plot clear failed to regenerate note block: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset); PlotSquared.log("&c[WARN] Plot clear failed to regenerate note block: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset);
} }
} }
for (final BlockLoc loc : brewTime.keySet()) { for (final BlockLoc loc : brewTime.keySet()) {
final Block block = world.getBlockAt(loc.x + x_offset, loc.y, loc.z + z_offset); try {
final BlockState state = block.getState(); final Block block = world.getBlockAt(loc.x + x_offset, loc.y, loc.z + z_offset);
if (state instanceof BrewingStand) { final BlockState state = block.getState();
((BrewingStand) (state)).setBrewingTime(brewTime.get(loc)); if (state instanceof BrewingStand) {
} else { ((BrewingStand) (state)).setBrewingTime(brewTime.get(loc));
} else {
PlotSquared.log("&c[WARN] Plot clear failed to restore brewing stand cooking: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset);
}
} catch (final Exception e) {
PlotSquared.log("&c[WARN] Plot clear failed to restore brewing stand cooking: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset); PlotSquared.log("&c[WARN] Plot clear failed to restore brewing stand cooking: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset);
} }
} }
for (final BlockLoc loc : spawnerData.keySet()) { for (final BlockLoc loc : spawnerData.keySet()) {
final Block block = world.getBlockAt(loc.x + x_offset, loc.y, loc.z + z_offset); try {
final BlockState state = block.getState(); final Block block = world.getBlockAt(loc.x + x_offset, loc.y, loc.z + z_offset);
if (state instanceof CreatureSpawner) { final BlockState state = block.getState();
((CreatureSpawner) (state)).setCreatureTypeId(spawnerData.get(loc)); if (state instanceof CreatureSpawner) {
state.update(true); ((CreatureSpawner) (state)).setCreatureTypeId(spawnerData.get(loc));
} else { state.update(true);
} else {
PlotSquared.log("&c[WARN] Plot clear failed to restore spawner type: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset);
}
} catch (final Exception e) {
PlotSquared.log("&c[WARN] Plot clear failed to restore spawner type: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset); PlotSquared.log("&c[WARN] Plot clear failed to restore spawner type: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset);
} }
} }
for (final BlockLoc loc : cmdData.keySet()) { for (final BlockLoc loc : cmdData.keySet()) {
final Block block = world.getBlockAt(loc.x + x_offset, loc.y, loc.z + z_offset); try {
final BlockState state = block.getState(); final Block block = world.getBlockAt(loc.x + x_offset, loc.y, loc.z + z_offset);
if (state instanceof CommandBlock) { final BlockState state = block.getState();
((CommandBlock) (state)).setCommand(cmdData.get(loc)); if (state instanceof CommandBlock) {
state.update(true); ((CommandBlock) (state)).setCommand(cmdData.get(loc));
} else { state.update(true);
} else {
PlotSquared.log("&c[WARN] Plot clear failed to restore command block: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset);
}
} catch (final Exception e) {
PlotSquared.log("&c[WARN] Plot clear failed to restore command block: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset); PlotSquared.log("&c[WARN] Plot clear failed to restore command block: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset);
} }
} }
for (final BlockLoc loc : brewingStandContents.keySet()) { for (final BlockLoc loc : brewingStandContents.keySet()) {
final Block block = world.getBlockAt(loc.x + x_offset, loc.y, loc.z + z_offset); try {
final BlockState state = block.getState(); final Block block = world.getBlockAt(loc.x + x_offset, loc.y, loc.z + z_offset);
if (state instanceof BrewingStand) { final BlockState state = block.getState();
((BrewingStand) (state)).getInventory().setContents(brewingStandContents.get(loc)); if (state instanceof BrewingStand) {
state.update(true); ((BrewingStand) (state)).getInventory().setContents(brewingStandContents.get(loc));
} else { state.update(true);
} else {
PlotSquared.log("&c[WARN] Plot clear failed to regenerate brewing stand: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset);
}
} catch (final Exception e) {
PlotSquared.log("&c[WARN] Plot clear failed to regenerate brewing stand: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset); PlotSquared.log("&c[WARN] Plot clear failed to regenerate brewing stand: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset);
} }
} }
for (final BlockLoc loc : furnaceTime.keySet()) { for (final BlockLoc loc : furnaceTime.keySet()) {
final Block block = world.getBlockAt(loc.x + x_offset, loc.y, loc.z + z_offset); try {
final BlockState state = block.getState(); final Block block = world.getBlockAt(loc.x + x_offset, loc.y, loc.z + z_offset);
if (state instanceof Furnace) { final BlockState state = block.getState();
final Short[] time = furnaceTime.get(loc); if (state instanceof Furnace) {
((Furnace) (state)).setBurnTime(time[0]); final Short[] time = furnaceTime.get(loc);
((Furnace) (state)).setCookTime(time[1]); ((Furnace) (state)).setBurnTime(time[0]);
} else { ((Furnace) (state)).setCookTime(time[1]);
} else {
PlotSquared.log("&c[WARN] Plot clear failed to restore furnace cooking: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset);
}
} catch (final Exception e) {
PlotSquared.log("&c[WARN] Plot clear failed to restore furnace cooking: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset); PlotSquared.log("&c[WARN] Plot clear failed to restore furnace cooking: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset);
} }
} }
for (final BlockLoc loc : furnaceContents.keySet()) { for (final BlockLoc loc : furnaceContents.keySet()) {
final Block block = world.getBlockAt(loc.x + x_offset, loc.y, loc.z + z_offset); try {
final BlockState state = block.getState(); final Block block = world.getBlockAt(loc.x + x_offset, loc.y, loc.z + z_offset);
if (state instanceof Furnace) { final BlockState state = block.getState();
((Furnace) (state)).getInventory().setContents(furnaceContents.get(loc)); if (state instanceof Furnace) {
state.update(true); ((Furnace) (state)).getInventory().setContents(furnaceContents.get(loc));
} else { state.update(true);
} else {
PlotSquared.log("&c[WARN] Plot clear failed to regenerate furnace: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset);
}
} catch (final Exception e) {
PlotSquared.log("&c[WARN] Plot clear failed to regenerate furnace: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset); PlotSquared.log("&c[WARN] Plot clear failed to regenerate furnace: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset);
} }
} }
for (final BlockLoc loc : bannerBase.keySet()) { for (final BlockLoc loc : bannerBase.keySet()) {
final Block block = world.getBlockAt(loc.x + x_offset, loc.y, loc.z + z_offset); try {
final BlockState state = block.getState(); final Block block = world.getBlockAt(loc.x + x_offset, loc.y, loc.z + z_offset);
if (state instanceof Banner) { final BlockState state = block.getState();
final Banner banner = (Banner) state; if (state instanceof Banner) {
final byte base = bannerBase.get(loc); final Banner banner = (Banner) state;
final ArrayList<Byte[]> colors = bannerColors.get(loc); final byte base = bannerBase.get(loc);
banner.setBaseColor(DyeColor.values()[base]); final ArrayList<Byte[]> colors = bannerColors.get(loc);
for (final Byte[] color : colors) { banner.setBaseColor(DyeColor.values()[base]);
banner.addPattern(new Pattern(DyeColor.getByDyeData(color[1]), PatternType.values()[color[0]])); for (final Byte[] color : colors) {
banner.addPattern(new Pattern(DyeColor.getByDyeData(color[1]), PatternType.values()[color[0]]));
}
state.update(true);
} else {
PlotSquared.log("&c[WARN] Plot clear failed to regenerate banner: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset);
} }
state.update(true); } catch (final Exception e) {
} else {
PlotSquared.log("&c[WARN] Plot clear failed to regenerate banner: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset); PlotSquared.log("&c[WARN] Plot clear failed to regenerate banner: " + loc.x + x_offset + "," + loc.y + "," + loc.z + z_offset);
} }
} }
@ -625,7 +689,7 @@ public class BukkitChunkManager extends ChunkManager {
saveBlocks(world, maxY, x, z, 0, 0); saveBlocks(world, maxY, x, z, 0, 0);
} }
public static void saveBlocks(final World world, final int maxY, int x, int z, int offset_x, int offset_z) { public static void saveBlocks(final World world, final int maxY, final int x, final int z, final int offset_x, final int offset_z) {
final HashMap<Short, Short> ids = new HashMap<>(); final HashMap<Short, Short> ids = new HashMap<>();
final HashMap<Short, Byte> datas = new HashMap<>(); final HashMap<Short, Byte> datas = new HashMap<>();
for (short y = 0; y < maxY; y++) { for (short y = 0; y < maxY; y++) {
@ -775,7 +839,7 @@ public class BukkitChunkManager extends ChunkManager {
if (plot.id.equals(id)) { if (plot.id.equals(id)) {
if (entity instanceof Player) { if (entity instanceof Player) {
final Player player = (Player) entity; final Player player = (Player) entity;
PlotPlayer pp = BukkitUtil.getPlayer(player); final PlotPlayer pp = BukkitUtil.getPlayer(player);
pp.teleport(MainUtil.getDefaultHome(plot)); pp.teleport(MainUtil.getDefaultHome(plot));
APlotListener.manager.plotExit(pp, plot); APlotListener.manager.plotExit(pp, plot);
} else { } else {
@ -795,50 +859,47 @@ public class BukkitChunkManager extends ChunkManager {
return BukkitUtil.getWorld(world).getChunkAt(loc.x, loc.z).unload(true, true); return BukkitUtil.getWorld(world).getChunkAt(loc.x, loc.z).unload(true, true);
} }
public static void swapChunk(World world, Chunk pos1, Chunk pos2, RegionWrapper r1, RegionWrapper r2) { public static void swapChunk(final World world, final Chunk pos1, final Chunk pos2, final RegionWrapper r1, final RegionWrapper r2) {
initMaps(); initMaps();
int relX = (r2.minX - r1.minX); final int relX = (r2.minX - r1.minX);
int relZ = (r2.minZ - r1.minZ); final int relZ = (r2.minZ - r1.minZ);
saveEntitiesIn(pos1, r1, relX, relZ, true); saveEntitiesIn(pos1, r1, relX, relZ, true);
saveEntitiesIn(pos2, r2, -relX, -relZ, true); saveEntitiesIn(pos2, r2, -relX, -relZ, true);
int sx = pos1.getX() << 4; final int sx = pos1.getX() << 4;
int sz = pos1.getZ() << 4; final int sz = pos1.getZ() << 4;
int maxY = world.getMaxHeight(); final int maxY = world.getMaxHeight();
for (int x = Math.max(r1.minX, sx); x <= Math.min(r1.maxX, sx + 15); x++) { for (int x = Math.max(r1.minX, sx); x <= Math.min(r1.maxX, sx + 15); x++) {
for (int z = Math.max(r1.minZ, sz); z <= Math.min(r1.maxZ, sz + 15); z++) { for (int z = Math.max(r1.minZ, sz); z <= Math.min(r1.maxZ, sz + 15); z++) {
saveBlocks(world, maxY, sx, sz, relX, relZ); saveBlocks(world, maxY, sx, sz, relX, relZ);
for (int y = 0; y < maxY; y++) { for (int y = 0; y < maxY; y++) {
Block block1 = world.getBlockAt(x, y, z); final Block block1 = world.getBlockAt(x, y, z);
int id1 = block1.getTypeId(); final int id1 = block1.getTypeId();
byte data1 = block1.getData(); final byte data1 = block1.getData();
int xx = x + relX; final int xx = x + relX;
int zz = z + relZ; final int zz = z + relZ;
Block block2 = world.getBlockAt(xx, y, zz); final Block block2 = world.getBlockAt(xx, y, zz);
int id2 = block2.getTypeId(); final int id2 = block2.getTypeId();
byte data2 = block2.getData(); final byte data2 = block2.getData();
if (id1 == 0) { if (id1 == 0) {
if (id2 != 0) { if (id2 != 0) {
BukkitSetBlockManager.setBlockManager.set(world, x, y, z, id2, data2); BukkitSetBlockManager.setBlockManager.set(world, x, y, z, id2, data2);
BukkitSetBlockManager.setBlockManager.set(world, xx, y, zz, 0, (byte) 0); BukkitSetBlockManager.setBlockManager.set(world, xx, y, zz, 0, (byte) 0);
} }
} } else if (id2 == 0) {
else if (id2 == 0) {
if (id1 != 0) { if (id1 != 0) {
BukkitSetBlockManager.setBlockManager.set(world, xx, y, zz, id1, data1); BukkitSetBlockManager.setBlockManager.set(world, xx, y, zz, id1, data1);
BukkitSetBlockManager.setBlockManager.set(world, x, y, z, 0, (byte) 0); BukkitSetBlockManager.setBlockManager.set(world, x, y, z, 0, (byte) 0);
} }
} } else if (id1 == id2) {
else if (id1 == id2) {
if (data1 != data2) { if (data1 != data2) {
block1.setData(data2); block1.setData(data2);
block2.setData(data1); block2.setData(data1);
} }
} } else {
else {
BukkitSetBlockManager.setBlockManager.set(world, x, y, z, id2, data2); BukkitSetBlockManager.setBlockManager.set(world, x, y, z, id2, data2);
BukkitSetBlockManager.setBlockManager.set(world, xx, y, zz, id1, data1); BukkitSetBlockManager.setBlockManager.set(world, xx, y, zz, id1, data1);
} }
@ -852,11 +913,11 @@ public class BukkitChunkManager extends ChunkManager {
@Override @Override
public void swap(final String worldname, final PlotId pos1, final PlotId pos2) { public void swap(final String worldname, final PlotId pos1, final PlotId pos2) {
Location bot1 = MainUtil.getPlotBottomLoc(worldname, pos1).add(1, 0, 1); final Location bot1 = MainUtil.getPlotBottomLoc(worldname, pos1).add(1, 0, 1);
Location top1 = MainUtil.getPlotTopLoc(worldname, pos1); final Location top1 = MainUtil.getPlotTopLoc(worldname, pos1);
Location bot2 = MainUtil.getPlotBottomLoc(worldname, pos2).add(1, 0, 1); final Location bot2 = MainUtil.getPlotBottomLoc(worldname, pos2).add(1, 0, 1);
Location top2 = MainUtil.getPlotTopLoc(worldname, pos2); final Location top2 = MainUtil.getPlotTopLoc(worldname, pos2);
swap(worldname, bot1, top1, bot2, top2); swap(worldname, bot1, top1, bot2, top2);
clearAllEntities(MainUtil.getPlot(worldname, pos1)); clearAllEntities(MainUtil.getPlot(worldname, pos1));
@ -864,7 +925,7 @@ public class BukkitChunkManager extends ChunkManager {
} }
@Override @Override
public void swap(final String worldname, final Location bot1, final Location top1, Location bot2, Location top2) { public void swap(final String worldname, final Location bot1, final Location top1, final Location bot2, final Location top2) {
final RegionWrapper region1 = new RegionWrapper(bot1.getX(), top1.getX(), bot1.getZ(), top1.getZ()); final RegionWrapper region1 = new RegionWrapper(bot1.getX(), top1.getX(), bot1.getZ(), top1.getZ());
final RegionWrapper region2 = new RegionWrapper(bot2.getX(), top2.getX(), bot2.getZ(), top2.getZ()); final RegionWrapper region2 = new RegionWrapper(bot2.getX(), top2.getX(), bot2.getZ(), top2.getZ());
final World world = Bukkit.getWorld(bot1.getWorld()); final World world = Bukkit.getWorld(bot1.getWorld());
@ -872,10 +933,10 @@ public class BukkitChunkManager extends ChunkManager {
final int relX = bot2.getX() - bot1.getX(); final int relX = bot2.getX() - bot1.getX();
final int relZ = bot2.getZ() - bot1.getZ(); final int relZ = bot2.getZ() - bot1.getZ();
for (int x = bot1.getX() >> 4; x <= top1.getX() >> 4; x++) { for (int x = bot1.getX() >> 4; x <= (top1.getX() >> 4); x++) {
for (int z = bot1.getZ() >> 4; z <= top1.getZ() >> 4; z++) { for (int z = bot1.getZ() >> 4; z <= (top1.getZ() >> 4); z++) {
Chunk chunk1 = world.getChunkAt(x, z); final Chunk chunk1 = world.getChunkAt(x, z);
Chunk chunk2 = world.getChunkAt(x + (relX >> 4), z + (relZ >> 4)); final Chunk chunk2 = world.getChunkAt(x + (relX >> 4), z + (relZ >> 4));
swapChunk(world, chunk1, chunk2, region1, region2); swapChunk(world, chunk1, chunk2, region1, region2);
} }
} }
@ -883,103 +944,98 @@ public class BukkitChunkManager extends ChunkManager {
} }
@Override @Override
public int[] countEntities(Plot plot) { public int[] countEntities(final Plot plot) {
int[] count = new int[5]; final int[] count = new int[5];
World world = BukkitUtil.getWorld(plot.world); final World world = BukkitUtil.getWorld(plot.world);
Location bot = MainUtil.getPlotBottomLoc(plot.world, plot.id).add(1, 0, 1); final Location bot = MainUtil.getPlotBottomLoc(plot.world, plot.id).add(1, 0, 1);
Location top = MainUtil.getPlotTopLoc(plot.world, plot.id); final Location top = MainUtil.getPlotTopLoc(plot.world, plot.id);
int bx = bot.getX() >> 4; final int bx = bot.getX() >> 4;
int bz = bot.getZ() >> 4; final int bz = bot.getZ() >> 4;
int tx = top.getX() >> 4; final int tx = top.getX() >> 4;
int tz = top.getZ() >> 4; final int tz = top.getZ() >> 4;
int size = (tx-bx) << 4; final int size = (tx - bx) << 4;
HashSet<Chunk> chunks = new HashSet<>(); final HashSet<Chunk> chunks = new HashSet<>();
for (int X = bx; X <= tx; X++) { for (int X = bx; X <= tx; X++) {
for (int Z = bz; Z <= tz; Z++) { for (int Z = bz; Z <= tz; Z++) {
chunks.add(world.getChunkAt(X,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) {
if (!(entity instanceof Creature || entity instanceof Vehicle)) {
continue;
}
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(count, entity);
}
else {
final PlotId id = MainUtil.getPlotId(BukkitUtil.getLocation(loc));
if (plot.id.equals(id)) {
count(count, entity);
}
}
} }
} }
}
else { boolean doWhole = false;
for (Chunk chunk : chunks) { List<Entity> entities = null;
int X = chunk.getX(); if (size > 200) {
int Z = chunk.getX(); entities = world.getEntities();
Entity[] ents = chunk.getEntities(); if (entities.size() < (16 + ((size * size) / 64))) {
for (final Entity entity : ents) { doWhole = true;
if (!(entity instanceof Creature || entity instanceof Vehicle)) { }
}
if (doWhole) {
for (final Entity entity : entities) {
if (!((entity instanceof Creature) || (entity instanceof Vehicle))) {
continue; continue;
} }
if (X == bx || X == tx || Z == bz || Z == tz) { final org.bukkit.Location loc = entity.getLocation();
final PlotId id = MainUtil.getPlotId(BukkitUtil.getLocation(entity)); final Chunk chunk = loc.getChunk();
if (plot.id.equals(id)) { if (chunks.contains(chunk)) {
final int X = chunk.getX();
final int Z = chunk.getX();
if ((X > bx) && (X < tx) && (Z > bz) && (Z < tz)) {
count(count, entity);
} else {
final PlotId id = MainUtil.getPlotId(BukkitUtil.getLocation(loc));
if (plot.id.equals(id)) {
count(count, entity);
}
}
}
}
} else {
for (final Chunk chunk : chunks) {
final int X = chunk.getX();
final int Z = chunk.getX();
final Entity[] ents = chunk.getEntities();
for (final Entity entity : ents) {
if (!((entity instanceof Creature) || (entity instanceof Vehicle))) {
continue;
}
if ((X == bx) || (X == tx) || (Z == bz) || (Z == tz)) {
final PlotId id = MainUtil.getPlotId(BukkitUtil.getLocation(entity));
if (plot.id.equals(id)) {
count(count, entity);
}
} else {
count(count, entity); count(count, entity);
} }
} }
else {
count(count, entity);
}
} }
} }
} return count;
return count;
} }
private void count(int[] count, Entity entity) { private void count(final int[] count, final Entity entity) {
count[0]++; count[0]++;
if (entity instanceof Creature) { if (entity instanceof Creature) {
count[3]++; count[3]++;
if (entity instanceof Animals) { if (entity instanceof Animals) {
count[1]++; count[1]++;
} } else {
else {
count[2]++; count[2]++;
} }
} } else {
else {
count[4]++; count[4]++;
} }
} }
@Override @Override
public void setChunk(ChunkWrapper loc, PlotBlock[][] blocks) { public void setChunk(final ChunkWrapper loc, final PlotBlock[][] blocks) {
CURRENT_PLOT_CLEAR = new RegionWrapper(0,0,0,0); CURRENT_PLOT_CLEAR = new RegionWrapper(0, 0, 0, 0);
World world = Bukkit.getWorld(loc.world); final World world = Bukkit.getWorld(loc.world);
Chunk chunk = world.getChunkAt(loc.x, loc.z); final Chunk chunk = world.getChunkAt(loc.x, loc.z);
final int cx = chunk.getX(); final int cx = chunk.getX();
final int cz = chunk.getZ(); final int cz = chunk.getZ();
if (!chunk.isLoaded()) { if (!chunk.isLoaded()) {
@ -988,20 +1044,20 @@ public class BukkitChunkManager extends ChunkManager {
initMaps(); initMaps();
final int absX = cx << 4; final int absX = cx << 4;
final int absZ = cz << 4; final int absZ = cz << 4;
boolean save = false; final boolean save = false;
for (int x = 0; x < 16; x++) { for (int x = 0; x < 16; x++) {
for (int z = 0; z < 16; z++) { for (int z = 0; z < 16; z++) {
saveBlocks(world, 255, absX + x, absZ + z); saveBlocks(world, 255, absX + x, absZ + z);
PlotLoc pl = new PlotLoc(absX + x, absZ + z); final PlotLoc pl = new PlotLoc(absX + x, absZ + z);
HashMap<Short, Short> ids = GENERATE_BLOCKS.get(pl); final HashMap<Short, Short> ids = GENERATE_BLOCKS.get(pl);
HashMap<Short, Short> datas = GENERATE_BLOCKS.get(pl); final HashMap<Short, Short> datas = GENERATE_BLOCKS.get(pl);
for (int i = 0; i < blocks.length; i++) { for (int i = 0; i < blocks.length; i++) {
if (blocks[i] != null) { if (blocks[i] != null) {
short y0 = (short) (i << 4); final short y0 = (short) (i << 4);
for (short y = y0; y < y0 + 16; y++) { for (short y = y0; y < (y0 + 16); y++) {
int j = ((y & 0xF) << 8) | (z << 4) | x; final int j = ((y & 0xF) << 8) | (z << 4) | x;
PlotBlock block = blocks[i][j]; final PlotBlock block = blocks[i][j];
if (block != null) { if (block != null) {
ids.put(y, block.id); ids.put(y, block.id);
if (block.data != 0) { if (block.data != 0) {