From 427d9479b969eac33bf28faf7b9cdc7b9cd06803 Mon Sep 17 00:00:00 2001 From: boy0001 Date: Wed, 11 Feb 2015 16:44:18 +1100 Subject: [PATCH] plot debugclear now actually async --- .../plot/commands/DebugClear.java | 24 ++++++- .../plot/generator/HybridPlotWorld.java | 55 ++++++++-------- .../plot/util/ChunkManager.java | 63 ++++++++++++++----- .../plot/util/PlotHelper.java | 8 ++- 4 files changed, 102 insertions(+), 48 deletions(-) diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/DebugClear.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/DebugClear.java index cfee7711a..eaf248f34 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/DebugClear.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/DebugClear.java @@ -64,7 +64,17 @@ public class DebugClear extends SubCommand { World bukkitWorld = Bukkit.getWorld(world); Location pos1 = PlotHelper.getPlotBottomLoc(bukkitWorld, plot.id).add(1, 0, 1); Location pos2 = PlotHelper.getPlotTopLoc(bukkitWorld, plot.id); - ChunkManager.regenerateRegion(pos1, pos2); + if (PlotHelper.runners.containsKey(plot)) { + PlayerFunctions.sendMessage(null, C.WAIT_FOR_TIMER); + return false; + } + PlotHelper.runners.put(plot, 1); + ChunkManager.regenerateRegion(pos1, pos2, new Runnable() { + @Override + public void run() { + PlotHelper.runners.remove(plot); + } + }); PlotMain.sendConsoleSenderMessage("Plot " + plot.getId().toString() + " cleared."); PlotMain.sendConsoleSenderMessage("&aDone!"); } @@ -88,7 +98,17 @@ public class DebugClear extends SubCommand { World bukkitWorld = plr.getWorld(); Location pos1 = PlotHelper.getPlotBottomLoc(bukkitWorld, plot.id).add(1, 0, 1); Location pos2 = PlotHelper.getPlotTopLoc(bukkitWorld, plot.id); - ChunkManager.regenerateRegion(pos1, pos2); + if (PlotHelper.runners.containsKey(plot)) { + PlayerFunctions.sendMessage(null, C.WAIT_FOR_TIMER); + return false; + } + PlotHelper.runners.put(plot, 1); + ChunkManager.regenerateRegion(pos1, pos2, new Runnable() { + @Override + public void run() { + PlotHelper.runners.remove(plot); + } + }); PlayerFunctions.sendMessage(plr, "&aDone!"); // sign diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/HybridPlotWorld.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/HybridPlotWorld.java index b30a272b2..cac4e60ae 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/HybridPlotWorld.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/HybridPlotWorld.java @@ -221,6 +221,33 @@ public class HybridPlotWorld extends PlotWorld { Schematic schem2 = SchematicHandler.getSchematic(schem2Str); Schematic schem3 = SchematicHandler.getSchematic(schem3Str); + int shift = (int) Math.floor(this.ROAD_WIDTH / 2); + int oddshift = 0; + if (this.ROAD_WIDTH % 2 != 0) { + oddshift = 1; + } + + if (schem3 != null) { + PLOT_SCHEMATIC = true; + DataCollection[] blocks3 = schem3.getBlockCollection(); + Dimension d3 = schem3.getSchematicDimension(); + short w3 = (short) d3.getX(); + short l3 = (short) d3.getZ(); + short h3 = (short) d3.getY(); + for (short x = 0; x < w3; x++) { + for (short z = 0; z < l3; z++) { + for (short y = 0; y < h3; y++) { + int index = y * w3 * l3 + z * w3 + x; + short id = blocks3[index].getBlock(); + byte data = blocks3[index].getData(); + if (id != 0) { + addOverlayBlock((short) (x + shift + oddshift), (short) (y + this.OFFSET), (short) (z + shift + oddshift), id, data, false); + } + } + } + } + } + if (schem1 == null || schem2 == null || this.ROAD_WIDTH == 0) { PlotMain.sendConsoleSenderMessage(C.PREFIX.s() + "&3 - schematic: &7false"); return; @@ -242,12 +269,6 @@ public class HybridPlotWorld extends PlotWorld { short h2 = (short) d2.getY(); this.SCHEMATIC_HEIGHT = (short) Math.max(h2, h1); - int shift = (int) Math.floor(this.ROAD_WIDTH / 2); - int oddshift = 0; - if (this.ROAD_WIDTH % 2 != 0) { - oddshift = 1; - } - for (short x = 0; x < w1; x++) { for (short z = 0; z < l1; z++) { for (short y = 0; y < h1; y++) { @@ -276,28 +297,6 @@ public class HybridPlotWorld extends PlotWorld { } } } - - if (schem3 != null) { - PLOT_SCHEMATIC = true; - DataCollection[] blocks3 = schem3.getBlockCollection(); - Dimension d3 = schem3.getSchematicDimension(); - short w3 = (short) d3.getX(); - short l3 = (short) d3.getZ(); - short h3 = (short) d3.getY(); - for (short x = 0; x < w3; x++) { - for (short z = 0; z < l3; z++) { - for (short y = 0; y < h3; y++) { - int index = y * w3 * l3 + z * w3 + x; - short id = blocks3[index].getBlock(); - byte data = blocks3[index].getData(); - if (id != 0) { - addOverlayBlock((short) (x + shift + oddshift), (short) (y + this.OFFSET), (short) (z + shift + oddshift), id, data, false); - } - } - } - } - } - this.ROAD_SCHEMATIC_ENABLED = true; } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/ChunkManager.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/ChunkManager.java index 23e746d1c..6e9404fce 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/ChunkManager.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/ChunkManager.java @@ -5,6 +5,8 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; +import org.apache.commons.lang.mutable.MutableInt; +import org.bukkit.Bukkit; import org.bukkit.Chunk; import org.bukkit.Location; import org.bukkit.Material; @@ -29,8 +31,11 @@ import org.bukkit.block.Skull; import org.bukkit.entity.Entity; import org.bukkit.entity.EntityType; import org.bukkit.inventory.ItemStack; +import org.bukkit.plugin.Plugin; import com.intellectualcrafters.plot.PlotMain; +import com.intellectualcrafters.plot.config.C; +import com.intellectualcrafters.plot.generator.HybridPlotManager; import com.intellectualcrafters.plot.object.BlockLoc; import com.intellectualcrafters.plot.object.ChunkLoc; import com.intellectualcrafters.plot.object.Plot; @@ -42,6 +47,8 @@ public class ChunkManager { public static RegionWrapper CURRENT_PLOT_CLEAR = null; public static HashMap> GENERATE_BLOCKS = new HashMap<>(); public static HashMap> GENERATE_DATA = new HashMap<>(); + public static MutableInt index = new MutableInt(0); + public static HashMap tasks = new HashMap<>(); public static ArrayList getChunkChunks(World world) { File[] regionFiles = new File(new File(".").getAbsolutePath() + File.separator + world.getName() + File.separator + "region").listFiles(); @@ -110,27 +117,49 @@ public class ChunkManager { private static HashSet entities; - public static boolean regenerateRegion(Location pos1, Location pos2) { - World world = pos1.getWorld(); + public static boolean regenerateRegion(final Location pos1, final Location pos2, final Runnable whenDone) { + index.increment(); + final Plugin plugin = (Plugin) PlotMain.getMain(); + + final World world = pos1.getWorld(); Chunk c1 = world.getChunkAt(pos1); Chunk c2 = world.getChunkAt(pos2); + + final int sx = pos1.getBlockX(); + final int sz = pos1.getBlockZ(); + final int ex = pos2.getBlockX(); + final int ez = pos2.getBlockZ(); - CURRENT_PLOT_CLEAR = new RegionWrapper(pos1.getBlockX(), pos2.getBlockX(), pos1.getBlockZ(), pos2.getBlockZ()); + final int c1x = c1.getX(); + final int c1z = c1.getZ(); + final int c2x = c2.getX(); + final int c2z = c2.getZ(); - int sx = pos1.getBlockX(); - int sz = pos1.getBlockZ(); - int ex = pos2.getBlockX(); - int ez = pos2.getBlockZ(); - - int c1x = c1.getX(); - int c1z = c1.getZ(); - int c2x = c2.getX(); - int c2z = c2.getZ(); - - int maxY = world.getMaxHeight(); + final ArrayList chunks = new ArrayList(); for (int x = c1x; x <= c2x; x ++) { for (int z = c1z; z <= c2z; z ++) { Chunk chunk = world.getChunkAt(x, z); + chunk.load(false); + chunks.add(chunk); + } + } + final int maxY = world.getMaxHeight(); + final Integer currentIndex = index.toInteger(); + final Integer task = Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() { + @Override + public void run() { + if (chunks.size() == 0) { + System.out.print("DONE"); + TaskManager.runTaskLater(whenDone, 1); + Bukkit.getScheduler().cancelTask(tasks.get(currentIndex)); + return; + } + CURRENT_PLOT_CLEAR = new RegionWrapper(pos1.getBlockX(), pos2.getBlockX(), pos1.getBlockZ(), pos2.getBlockZ()); + Chunk chunk = chunks.get(0); + chunks.remove(0); + int x = chunk.getX(); + int z = chunk.getZ(); + boolean loaded = true; if (!chunk.isLoaded()) { boolean result = chunk.load(false); @@ -177,10 +206,10 @@ public class ChunkManager { chunk.unload(); chunk.load(); } + CURRENT_PLOT_CLEAR = null; } - } - CURRENT_PLOT_CLEAR = null; - initMaps(); + }, 1, 1); + tasks.put(currentIndex, task); return true; } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/PlotHelper.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/PlotHelper.java index 1c4455dc8..f7abb601a 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/PlotHelper.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/PlotHelper.java @@ -765,8 +765,14 @@ import com.intellectualcrafters.plot.object.PlotWorld; PlotWorld plotworld = PlotMain.getWorldSettings(world); if (plotworld.TERRAIN != 0) { + runners.put(plot, 1); final Location pos2 = PlotHelper.getPlotTopLoc(world, plot.id); - ChunkManager.regenerateRegion(pos1, pos2); + ChunkManager.regenerateRegion(pos1, pos2, new Runnable() { + @Override + public void run() { + runners.remove(plot); + } + }); return; }