Cleanup BukkitChunkManager

This commit is contained in:
Jesse Boyd 2016-03-29 18:43:57 +11:00
parent 75a70bcff4
commit 6f9af04274
2 changed files with 525 additions and 514 deletions

View File

@ -408,7 +408,7 @@ public class EntityWrapper {
Location loc = new Location(world, this.x + x_offset, this.y, this.z + z_offset);
loc.setYaw(this.yaw);
loc.setPitch(this.pitch);
if (this.type.isSpawnable()) {
if (!this.type.isSpawnable()) {
return null;
}
Entity entity;

View File

@ -20,6 +20,14 @@ import com.intellectualcrafters.plot.util.TaskManager;
import com.intellectualcrafters.plot.util.UUIDHandler;
import com.intellectualcrafters.plot.util.WorldUtil;
import com.plotsquared.bukkit.object.entity.EntityWrapper;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.DyeColor;
@ -33,7 +41,6 @@ import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState;
import org.bukkit.block.BrewingStand;
import org.bukkit.block.Chest;
import org.bukkit.block.CommandBlock;
import org.bukkit.block.CreatureSpawner;
import org.bukkit.block.Dispenser;
@ -53,37 +60,30 @@ import org.bukkit.entity.Player;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
public class BukkitChunkManager extends ChunkManager {
private static HashMap<BlockLoc, ItemStack[]> chestContents;
private static HashMap<BlockLoc, ItemStack[]> furnaceContents;
private static HashMap<BlockLoc, ItemStack[]> dispenserContents;
private static HashMap<BlockLoc, ItemStack[]> dropperContents;
private static HashMap<BlockLoc, ItemStack[]> brewingStandContents;
private static HashMap<BlockLoc, ItemStack[]> beaconContents;
private static HashMap<BlockLoc, ItemStack[]> hopperContents;
private static HashMap<BlockLoc, Short[]> furnaceTime;
private static HashMap<BlockLoc, Object[]> skullData;
private static HashMap<BlockLoc, Material> jukeboxDisc;
private static HashMap<BlockLoc, Short> brewTime;
private static HashMap<BlockLoc, EntityType> spawnerData;
private static HashMap<BlockLoc, String> cmdData;
private static HashMap<BlockLoc, String[]> signContents;
private static HashMap<BlockLoc, Note> noteBlockContents;
private static HashMap<BlockLoc, List<Pattern>> bannerPatterns;
private static HashMap<BlockLoc, DyeColor> bannerBase;
private static HashSet<EntityWrapper> entities;
private static HashMap<PlotLoc, PlotBlock[]> allBlocks;
public static class ContentMap {
public HashMap<BlockLoc, ItemStack[]> chestContents;
public HashMap<BlockLoc, ItemStack[]> furnaceContents;
public HashMap<BlockLoc, ItemStack[]> dispenserContents;
public HashMap<BlockLoc, ItemStack[]> dropperContents;
public HashMap<BlockLoc, ItemStack[]> brewingStandContents;
public HashMap<BlockLoc, ItemStack[]> beaconContents;
public HashMap<BlockLoc, ItemStack[]> hopperContents;
public HashMap<BlockLoc, Short[]> furnaceTime;
public HashMap<BlockLoc, Object[]> skullData;
public HashMap<BlockLoc, Material> jukeboxDisc;
public HashMap<BlockLoc, Short> brewTime;
public HashMap<BlockLoc, EntityType> spawnerData;
public HashMap<BlockLoc, String> cmdData;
public HashMap<BlockLoc, String[]> signContents;
public HashMap<BlockLoc, Note> noteBlockContents;
public HashMap<BlockLoc, List<Pattern>> bannerPatterns;
public HashMap<BlockLoc, DyeColor> bannerBase;
public HashSet<EntityWrapper> entities;
public HashMap<PlotLoc, PlotBlock[]> allBlocks;
public static void initMaps() {
public ContentMap() {
chestContents = new HashMap<>();
furnaceContents = new HashMap<>();
dispenserContents = new HashMap<>();
@ -105,11 +105,29 @@ public class BukkitChunkManager extends ChunkManager {
allBlocks = new HashMap<>();
}
public static boolean isIn(RegionWrapper region, int x, int z) {
return x >= region.minX && x <= region.maxX && z >= region.minZ && z <= region.maxZ;
public void saveRegion(World world, int x1, int x2, int z1, int z2) {
if (z1 > z2) {
int tmp = z1;
z1 = z2;
z2 = tmp;
}
if (x1 > x2) {
int tmp = x1;
x1 = x2;
x2 = tmp;
}
for (int x = x1; x <= x2; x++) {
for (int z = z1; z <= z2; z++) {
saveBlocks(world, 256, x, z, 0, 0, true);
}
}
}
public static void saveEntitiesOut(Chunk chunk, RegionWrapper region) {
public void saveEntitiesIn(Chunk chunk, RegionWrapper region) {
saveEntitiesIn(chunk, region, 0, 0, false);
}
public void saveEntitiesOut(Chunk chunk, RegionWrapper region) {
for (Entity entity : chunk.getEntities()) {
Location loc = BukkitUtil.getLocation(entity);
int x = loc.getX();
@ -125,11 +143,7 @@ public class BukkitChunkManager extends ChunkManager {
}
}
public static void saveEntitiesIn(Chunk chunk, RegionWrapper region) {
saveEntitiesIn(chunk, region, 0, 0, false);
}
public static void saveEntitiesIn(Chunk chunk, RegionWrapper region, int offsetX, int offsetZ, boolean delete) {
public void saveEntitiesIn(Chunk chunk, RegionWrapper region, int offsetX, int offsetZ, boolean delete) {
for (Entity entity : chunk.getEntities()) {
Location loc = BukkitUtil.getLocation(entity);
int x = loc.getX();
@ -152,7 +166,7 @@ public class BukkitChunkManager extends ChunkManager {
}
}
public static void restoreEntities(World world, int xOffset, int zOffset) {
public void restoreEntities(World world, int xOffset, int zOffset) {
for (EntityWrapper entity : entities) {
try {
entity.spawn(world, xOffset, zOffset);
@ -164,13 +178,13 @@ public class BukkitChunkManager extends ChunkManager {
entities.clear();
}
public static void restoreBlocks(World world, int xOffset, int zOffset) {
public void restoreBlocks(World world, int xOffset, int zOffset) {
for (Entry<BlockLoc, ItemStack[]> blockLocEntry : chestContents.entrySet()) {
try {
Block block =
world.getBlockAt(blockLocEntry.getKey().x + xOffset, blockLocEntry.getKey().y, blockLocEntry.getKey().z + zOffset);
BlockState state = block.getState();
if (state instanceof Chest) {
if (state instanceof InventoryHolder) {
InventoryHolder chest = (InventoryHolder) state;
chest.getInventory().setContents(blockLocEntry.getValue());
state.update(true);
@ -488,7 +502,7 @@ public class BukkitChunkManager extends ChunkManager {
}
}
public static void saveBlocks(World world, int maxY, int x, int z, int offsetX, int offsetZ, boolean storeNormal) {
public void saveBlocks(World world, int maxY, int x, int z, int offsetX, int offsetZ, boolean storeNormal) {
maxY = Math.min(255, maxY);
PlotBlock[] ids;
if (storeNormal) {
@ -582,6 +596,11 @@ public class BukkitChunkManager extends ChunkManager {
PlotLoc loc = new PlotLoc(x + offsetX, z + offsetZ);
allBlocks.put(loc, ids);
}
}
public static boolean isIn(RegionWrapper region, int x, int z) {
return x >= region.minX && x <= region.maxX && z >= region.minZ && z <= region.maxZ;
}
private static byte getOrdinal(Object[] list, Object value) {
for (byte i = 0; i < list.length; i++) {
@ -592,14 +611,13 @@ public class BukkitChunkManager extends ChunkManager {
return 0;
}
public static void swapChunk(World world1, World world2, Chunk pos1, Chunk pos2, RegionWrapper r1,
RegionWrapper r2) {
initMaps();
public static ContentMap swapChunk(World world1, World world2, Chunk pos1, Chunk pos2, RegionWrapper r1, RegionWrapper r2) {
ContentMap map = new ContentMap();
int relX = r2.minX - r1.minX;
int relZ = r2.minZ - r1.minZ;
saveEntitiesIn(pos1, r1, relX, relZ, true);
saveEntitiesIn(pos2, r2, -relX, -relZ, true);
map.saveEntitiesIn(pos1, r1, relX, relZ, true);
map.saveEntitiesIn(pos2, r2, -relX, -relZ, true);
int sx = pos1.getX() << 4;
int sz = pos1.getZ() << 4;
@ -609,7 +627,7 @@ public class BukkitChunkManager extends ChunkManager {
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++) {
saveBlocks(world1, 256, sx, sz, relX, relZ, false);
map.saveBlocks(world1, 256, sx, sz, relX, relZ, false);
for (int y = 0; y < 256; y++) {
Block block1 = world1.getBlockAt(x, y, z);
int id1 = block1.getTypeId();
@ -639,10 +657,7 @@ public class BukkitChunkManager extends ChunkManager {
}
}
}
while (SetQueue.IMP.forceChunkSet()) {
}
restoreBlocks(world1, 0, 0);
restoreEntities(world1, 0, 0);
return map;
}
@Override
@ -684,7 +699,7 @@ public class BukkitChunkManager extends ChunkManager {
final World newWorld = Bukkit.getWorld(newPos.getWorld());
final String newWorldName = newWorld.getName();
List<ChunkLoc> chunks = new ArrayList<>();
initMaps();
final ContentMap map = new ContentMap();
ChunkManager.chunkTask(pos1, pos2, new RunnableVal<int[]>() {
@Override
public void run(int[] value) {
@ -696,17 +711,17 @@ public class BukkitChunkManager extends ChunkManager {
int cxx = loc.x << 4;
int czz = loc.z << 4;
Chunk chunk = oldWorld.getChunkAt(loc.x, loc.z);
saveEntitiesIn(chunk, region);
map.saveEntitiesIn(chunk, region);
for (int x = bx & 15; x <= (tx & 15); x++) {
for (int z = bz & 15; z <= (tz & 15); z++) {
saveBlocks(oldWorld, 256, cxx + x, czz + z, relX, relZ, true);
map.saveBlocks(oldWorld, 256, cxx + x, czz + z, relX, relZ, true);
}
}
}
}, new Runnable() {
@Override
public void run() {
for (Entry<PlotLoc, PlotBlock[]> entry : allBlocks.entrySet()) {
for (Entry<PlotLoc, PlotBlock[]> entry : map.allBlocks.entrySet()) {
PlotLoc loc = entry.getKey();
PlotBlock[] blocks = entry.getValue();
for (int y = 0; y < blocks.length; y++) {
@ -716,34 +731,19 @@ public class BukkitChunkManager extends ChunkManager {
}
}
}
while (SetQueue.IMP.forceChunkSet()) {
}
restoreBlocks(newWorld, 0, 0);
restoreEntities(newWorld, relX, relZ);
SetQueue.IMP.addTask(new Runnable() {
@Override
public void run() {
map.restoreBlocks(newWorld, 0, 0);
map.restoreEntities(newWorld, relX, relZ);
TaskManager.runTask(whenDone);
}
});
}
}, 5);
return true;
}
public void saveRegion(World world, int x1, int x2, int z1, int z2) {
if (z1 > z2) {
int tmp = z1;
z1 = z2;
z2 = tmp;
}
if (x1 > x2) {
int tmp = x1;
x1 = x2;
x2 = tmp;
}
for (int x = x1; x <= x2; x++) {
for (int z = z1; z <= z2; z++) {
saveBlocks(world, 256, x, z, 0, 0, true);
}
}
}
@Override
public boolean regenerateRegion(final Location pos1, final Location pos2, final boolean ignoreAugment, final Runnable whenDone) {
final String world = pos1.getWorld();
@ -825,32 +825,32 @@ public class BukkitChunkManager extends ChunkManager {
} else {
zzt2 = zzt;
}
initMaps();
final ContentMap map = new ContentMap();
if (checkX1) {
saveRegion(worldObj, xxb, xxb2, zzb2, zzt2); //
map.saveRegion(worldObj, xxb, xxb2, zzb2, zzt2); //
}
if (checkX2) {
saveRegion(worldObj, xxt2, xxt, zzb2, zzt2); //
map.saveRegion(worldObj, xxt2, xxt, zzb2, zzt2); //
}
if (checkZ1) {
saveRegion(worldObj, xxb2, xxt2, zzb, zzb2); //
map.saveRegion(worldObj, xxb2, xxt2, zzb, zzb2); //
}
if (checkZ2) {
saveRegion(worldObj, xxb2, xxt2, zzt2, zzt); //
map.saveRegion(worldObj, xxb2, xxt2, zzt2, zzt); //
}
if (checkX1 && checkZ1) {
saveRegion(worldObj, xxb, xxb2, zzb, zzb2); //
map.saveRegion(worldObj, xxb, xxb2, zzb, zzb2); //
}
if (checkX2 && checkZ1) {
saveRegion(worldObj, xxt2, xxt, zzb, zzb2); // ?
map.saveRegion(worldObj, xxt2, xxt, zzb, zzb2); // ?
}
if (checkX1 && checkZ2) {
saveRegion(worldObj, xxb, xxb2, zzt2, zzt); // ?
map.saveRegion(worldObj, xxb, xxb2, zzt2, zzt); // ?
}
if (checkX2 && checkZ2) {
saveRegion(worldObj, xxt2, xxt, zzt2, zzt); //
map.saveRegion(worldObj, xxt2, xxt, zzt2, zzt); //
}
saveEntitiesOut(chunkObj, currentPlotClear);
map.saveEntitiesOut(chunkObj, currentPlotClear);
AugmentedUtils.bypass(ignoreAugment, new Runnable() {
@Override
public void run() {
@ -864,7 +864,7 @@ public class BukkitChunkManager extends ChunkManager {
for (int x = 0; x < 16; x++) {
for (int z = 0; z < 16; z++) {
PlotLoc loc = new PlotLoc(bx + x, bz + z);
PlotBlock[] ids = allBlocks.get(loc);
PlotBlock[] ids = map.allBlocks.get(loc);
if (ids != null) {
for (int y = 0; y < Math.min(128, ids.length); y++) {
PlotBlock id = ids[y];
@ -887,8 +887,8 @@ public class BukkitChunkManager extends ChunkManager {
}, world, chunk);
}
});
restoreBlocks(worldObj, 0, 0);
restoreEntities(worldObj, 0, 0);
map.restoreBlocks(worldObj, 0, 0);
map.restoreEntities(worldObj, 0, 0);
}
if (!chunks.isEmpty()) {
TaskManager.runTaskLater(this, 1);
@ -938,24 +938,35 @@ public class BukkitChunkManager extends ChunkManager {
}
@Override
public void swap(Location bot1, Location top1, Location bot2, Location top2, Runnable whenDone) {
public void swap(Location bot1, Location top1, Location bot2, Location top2, final Runnable whenDone) {
RegionWrapper region1 = new RegionWrapper(bot1.getX(), top1.getX(), bot1.getZ(), top1.getZ());
RegionWrapper region2 = new RegionWrapper(bot2.getX(), top2.getX(), bot2.getZ(), top2.getZ());
World world1 = Bukkit.getWorld(bot1.getWorld());
World world2 = Bukkit.getWorld(bot2.getWorld());
final World world1 = Bukkit.getWorld(bot1.getWorld());
final World world2 = Bukkit.getWorld(bot2.getWorld());
int relX = bot2.getX() - bot1.getX();
int relZ = bot2.getZ() - bot1.getZ();
final ArrayDeque<ContentMap> maps = new ArrayDeque<>();
for (int x = bot1.getX() >> 4; x <= top1.getX() >> 4; x++) {
for (int z = bot1.getZ() >> 4; z <= top1.getZ() >> 4; z++) {
Chunk chunk1 = world1.getChunkAt(x, z);
Chunk chunk2 = world2.getChunkAt(x + (relX >> 4), z + (relZ >> 4));
swapChunk(world1, world2, chunk1, chunk2, region1, region2);
maps.add(swapChunk(world1, world2, chunk1, chunk2, region1, region2));
}
}
SetQueue.IMP.addTask(new Runnable() {
@Override
public void run() {
for (ContentMap map : maps) {
map.restoreBlocks(world1, 0, 0);
map.restoreEntities(world1, 0, 0);
TaskManager.runTaskLater(whenDone, 1);
}
}
});
}
@Override
public int[] countEntities(Plot plot) {