diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/DebugExec.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/DebugExec.java index b268b389a..c7c381a67 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/DebugExec.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/DebugExec.java @@ -59,7 +59,7 @@ public class DebugExec extends SubCommand { @Override public boolean execute(final Player player, final String... args) { - List allowed_params = Arrays.asList(new String[]{"stop-expire","start-expire", "show-expired", "update-expired", "seen", "trim-check-chunks", "trim-get-chunks"}); + List allowed_params = Arrays.asList(new String[]{"stop-expire","start-expire", "show-expired", "update-expired", "seen", "trim-check"}); if (args.length > 0) { String arg = args[0].toLowerCase(); switch (arg) { @@ -133,7 +133,7 @@ public class DebugExec extends SubCommand { } case "trim-check": { if (args.length != 2) { - PlayerFunctions.sendMessage(null, "Use /plot debugexec trim-get-chunks "); + PlayerFunctions.sendMessage(null, "Use /plot debugexec trim-check "); PlayerFunctions.sendMessage(null, "&7 - Generates a list of regions to trim"); return PlayerFunctions.sendMessage(null, "&7 - Run after plot expiry has run"); } @@ -142,12 +142,12 @@ public class DebugExec extends SubCommand { return PlayerFunctions.sendMessage(null, "Invalid world: "+args[1]); } final ArrayList empty = new ArrayList<>(); - Trim.getTrimRegions(empty, world, new Runnable() { + boolean result = Trim.getTrimRegions(empty, world, new Runnable() { @Override public void run() { Trim.sendMessage("Processing is complete! Here's how many chunks would be deleted:"); Trim.sendMessage(" - MCA #: " + empty.size()); - Trim.sendMessage(" - CHUNKS: " + (empty.size() * 256) + " (max)"); + Trim.sendMessage(" - CHUNKS: " + (empty.size() * 1024) + " (max)"); Trim.sendMessage("Exporting log for manual approval..."); final File file = new File(PlotMain.getMain().getDataFolder() + File.separator + "trim.txt"); PrintWriter writer; @@ -158,7 +158,7 @@ public class DebugExec extends SubCommand { writer.println(worldname +"/region/r." + loc.x + "." + loc.z +".mca" ); } writer.close(); - Trim.sendMessage("File saved"); + Trim.sendMessage("File saved to 'plugins/PlotSquared/trim.txt'"); } catch (FileNotFoundException e) { e.printStackTrace(); Trim.sendMessage("File failed to save! :("); @@ -169,7 +169,10 @@ public class DebugExec extends SubCommand { Trim.sendMessage(" - Add 31 to each number to get the end position"); } }); - return true; + if (!result) { + PlayerFunctions.sendMessage(null, "Trim task already started!"); + } + return result; } } } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/RegenAllRoads.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/RegenAllRoads.java index d751e9f56..48d7d7d3f 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/RegenAllRoads.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/RegenAllRoads.java @@ -67,7 +67,7 @@ public class RegenAllRoads extends SubCommand { PlotMain.sendConsoleSenderMessage("&cIf no schematic is set, the following will not do anything"); PlotMain.sendConsoleSenderMessage("&7 - To set a schematic, stand in a plot and use &c/plot createroadschematic"); - PlotMain.sendConsoleSenderMessage("&6Potential chunks to update: &7"+ (chunks.size() * 256)); + PlotMain.sendConsoleSenderMessage("&6Potential chunks to update: &7"+ (chunks.size() * 1024)); PlotMain.sendConsoleSenderMessage("&6Estimated time: &7"+ (chunks.size()) + " seconds"); boolean result = hpm.scheduleRoadUpdate(world); diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Trim.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Trim.java index e1794a161..2968a7273 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Trim.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Trim.java @@ -29,10 +29,12 @@ import java.nio.file.attribute.BasicFileAttributes; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.HashSet; import java.util.Set; import org.bukkit.Bukkit; import org.bukkit.Chunk; +import org.bukkit.Location; import org.bukkit.World; import org.bukkit.entity.Player; @@ -46,6 +48,7 @@ import com.intellectualcrafters.plot.object.PlotId; import com.intellectualcrafters.plot.util.ChunkManager; import com.intellectualcrafters.plot.util.ExpireManager; import com.intellectualcrafters.plot.util.PlayerFunctions; +import com.intellectualcrafters.plot.util.PlotHelper; import com.intellectualcrafters.plot.util.TaskManager; public class Trim extends SubCommand { @@ -180,40 +183,42 @@ public class Trim extends SubCommand { if (Trim.TASK) { return false; } + final long startOld = System.currentTimeMillis(); sendMessage("Collecting region data..."); - final ArrayList chunks = ChunkManager.getChunkChunks(world); + final ArrayList plots = new ArrayList<>(); + plots.addAll(PlotMain.getPlots(world).values()); + final HashSet chunks = new HashSet<>(ChunkManager.getChunkChunks(world)); sendMessage(" - MCA #: " + chunks.size()); - sendMessage(" - CHUNKS: " + (chunks.size() * 256) +" (max)"); + sendMessage(" - CHUNKS: " + (chunks.size() * 1024) +" (max)"); sendMessage(" - TIME ESTIMATE: " + (chunks.size()/1200) +" minutes"); Trim.TASK_ID = Bukkit.getScheduler().scheduleSyncRepeatingTask(PlotMain.getMain(), new Runnable() { @Override public void run() { - if (chunks.size() == 0) { - TaskManager.runTask(whenDone); - Bukkit.getScheduler().cancelTask(Trim.TASK_ID); - return; - } - ChunkLoc loc = chunks.get(0); - int sx = loc.x << 5; - int sz = loc.z << 5; - - boolean delete = true; - - loop: - for (int x = sx; x < sx + 32; x++) { - for (int z = sz; z < sz + 32; z++) { - Chunk chunk = world.getChunkAt(x, z); - if (ChunkManager.hasPlot(world, chunk) != null) { - delete = false; - break loop; - } + long start = System.currentTimeMillis(); + while (System.currentTimeMillis() - start < 50) { + if (plots.size() == 0) { + empty.addAll(chunks); + System.out.print("DONE!"); + Trim.TASK = false; + TaskManager.runTask(whenDone); + Bukkit.getScheduler().cancelTask(Trim.TASK_ID); + return; } - } - if (delete) { - empty.add(loc); + Plot plot = plots.get(0); + plots.remove(0); + Location pos1 = PlotHelper.getPlotBottomLoc(world, plot.id); + Location pos2 = PlotHelper.getPlotTopLoc(world, plot.id); + + Location pos3 = new Location(world, pos1.getBlockX(), 64, pos2.getBlockZ()); + Location pos4 = new Location(world, pos2.getBlockX(), 64, pos1.getBlockZ()); + + chunks.remove(ChunkManager.getChunkChunk(pos1)); + chunks.remove(ChunkManager.getChunkChunk(pos2)); + chunks.remove(ChunkManager.getChunkChunk(pos3)); + chunks.remove(ChunkManager.getChunkChunk(pos4)); } } - }, 1L, 1L); + }, 20L, 20L); Trim.TASK = true; return 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 a796ed909..7c572f8da 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/ChunkManager.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/ChunkManager.java @@ -34,15 +34,11 @@ import org.bukkit.block.Skull; import org.bukkit.block.banner.Pattern; import org.bukkit.block.banner.PatternType; import org.bukkit.entity.Entity; -import org.bukkit.entity.EntityType; -import org.bukkit.inventory.Inventory; import org.bukkit.inventory.InventoryHolder; 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; @@ -57,6 +53,12 @@ public class ChunkManager { public static MutableInt index = new MutableInt(0); public static HashMap tasks = new HashMap<>(); + public static ChunkLoc getChunkChunk(Location loc) { + int x = loc.getBlockX() >> 9; + int z = loc.getBlockZ() >> 9; + return new ChunkLoc(x, z); + } + public static ArrayList getChunkChunks(World world) { File[] regionFiles = new File(world.getName() + File.separator + "region").listFiles(); ArrayList chunks = new ArrayList<>();