From e239a49be1e76806adb0ea9ead8a1eeb6fbd7d84 Mon Sep 17 00:00:00 2001 From: boy0001 Date: Thu, 16 Apr 2015 14:04:11 +1000 Subject: [PATCH] Simplified mass schematic export --- .../plot/commands/SchematicCmd.java | 110 +++++------------- .../plot/util/SchematicHandler.java | 72 ++++++++++++ 2 files changed, 104 insertions(+), 78 deletions(-) diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/SchematicCmd.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/SchematicCmd.java index ce57799a0..c36ef959f 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/SchematicCmd.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/SchematicCmd.java @@ -20,8 +20,10 @@ //////////////////////////////////////////////////////////////////////////////////////////////////// package com.intellectualcrafters.plot.commands; +import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.HashSet; import com.intellectualcrafters.jnbt.CompoundTag; import com.intellectualcrafters.plot.PlotSquared; @@ -45,7 +47,6 @@ import com.intellectualcrafters.plot.util.bukkit.UUIDHandler; public class SchematicCmd extends SubCommand { private int counter = 0; private boolean running = false; - private Plot[] plots; private int task; public SchematicCmd() { @@ -196,52 +197,24 @@ public class SchematicCmd extends SubCommand { } final HashMap plotmap = PlotSquared.getPlots(args[1]); if ((plotmap == null) || (plotmap.size() == 0)) { - MainUtil.sendMessage(null, "&cInvalid world. Use &7/plots sch exportall "); + MainUtil.sendMessage(plr, "&cInvalid world. Use &7/plots sch exportall "); return false; } - if (this.running) { - MainUtil.sendMessage(null, "&cTask is already running."); + Collection plots = plotmap.values(); + boolean result = SchematicHandler.manager.exportAll(plots, null, null, new Runnable() { + @Override + public void run() { + MainUtil.sendMessage(plr, "&aFinished mass export"); + } + }); + if (!result) { + MainUtil.sendMessage(plr, "&cTask is already running."); return false; } - PlotSquared.log("&3PlotSquared&8->&3Schemaitc&8: &7Mass export has started. This may take a while."); - PlotSquared.log("&3PlotSquared&8->&3Schemaitc&8: &7Found &c" + plotmap.size() + "&7 plots..."); - final String worldname = args[1]; - final Collection values = plotmap.values(); - this.plots = values.toArray(new Plot[values.size()]); - this.running = true; - this.counter = 0; - this.task = TaskManager.runTaskRepeat(new Runnable() { - @Override - public void run() { - if (SchematicCmd.this.counter >= SchematicCmd.this.plots.length) { - PlotSquared.log("&3PlotSquared&8->&3Schemaitc&8: &aFinished!"); - SchematicCmd.this.running = false; - PlotSquared.TASK.cancelTask(SchematicCmd.this.task); - return; - } - final Plot plot = SchematicCmd.this.plots[SchematicCmd.this.counter]; - final CompoundTag sch = SchematicHandler.manager.getCompoundTag(worldname, plot.id); - final String o = UUIDHandler.getName(plot.owner); - final String owner = o == null ? "unknown" : o; - if (sch == null) { - MainUtil.sendMessage(null, "&7 - Skipped plot &c" + plot.id); - } else { - TaskManager.runTaskAsync(new Runnable() { - @Override - public void run() { - MainUtil.sendMessage(null, "&6ID: " + plot.id); - final boolean result = SchematicHandler.manager.save(sch, Settings.SCHEMATIC_SAVE_PATH + "/" + plot.id.x + ";" + plot.id.y + "," + worldname + "," + owner + ".schematic"); - if (!result) { - MainUtil.sendMessage(null, "&7 - Failed to save &c" + plot.id); - } else { - MainUtil.sendMessage(null, "&7 - &a success: " + plot.id); - } - } - }); - } - SchematicCmd.this.counter++; - } - }, 20); + else { + PlotSquared.log("&3PlotSquared&8->&3Schemaitc&8: &7Mass export has started. This may take a while."); + PlotSquared.log("&3PlotSquared&8->&3Schemaitc&8: &7Found &c" + plotmap.size() + "&7 plots..."); + } break; } case "export": @@ -288,41 +261,22 @@ public class SchematicCmd extends SubCommand { return false; } } - this.plots = new Plot[] { p2 }; - this.running = true; - this.counter = 0; - this.task = TaskManager.runTaskRepeat(new Runnable() { - @Override - public void run() { - if (SchematicCmd.this.counter >= SchematicCmd.this.plots.length) { - PlotSquared.log("&3PlotSquared&8->&3Schemaitc&8: &aFinished!"); - SchematicCmd.this.running = false; - PlotSquared.TASK.cancelTask(SchematicCmd.this.task); - return; - } - final Plot plot = SchematicCmd.this.plots[SchematicCmd.this.counter]; - final CompoundTag sch = SchematicHandler.manager.getCompoundTag(world, plot.id); - final String o = UUIDHandler.getName(plot.owner); - final String owner = o == null ? "unknown" : o; - if (sch == null) { - MainUtil.sendMessage(plr, "&7 - Skipped plot &c" + plot.id); - } else { - TaskManager.runTaskAsync(new Runnable() { - @Override - public void run() { - MainUtil.sendMessage(plr, "&6ID: " + plot.id); - final boolean result = SchematicHandler.manager.save(sch, Settings.SCHEMATIC_SAVE_PATH + "/" + plot.id.x + ";" + plot.id.y + "," + world + "," + owner.trim() + ".schematic"); - if (!result) { - MainUtil.sendMessage(plr, "&7 - Failed to save &c" + plot.id); - } else { - MainUtil.sendMessage(plr, "&7 - &aExport success: " + plot.id); - } - } - }); - } - SchematicCmd.this.counter++; - } - }, 60); + + Collection plots = new ArrayList(); + plots.add(p2); + boolean result = SchematicHandler.manager.exportAll(plots, null, null, new Runnable() { + @Override + public void run() { + MainUtil.sendMessage(plr, "&aFinished export"); + } + }); + if (!result) { + MainUtil.sendMessage(plr, "&cTask is already running."); + return false; + } + else { + MainUtil.sendMessage(plr, "&7Starting export..."); + } break; } default: { diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/SchematicHandler.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/SchematicHandler.java index 9bc3b5c7f..16f2eced5 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/SchematicHandler.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/SchematicHandler.java @@ -6,12 +6,16 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.util.Collection; import java.util.HashSet; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; +import org.bukkit.Bukkit; + import com.intellectualcrafters.jnbt.ByteArrayTag; import com.intellectualcrafters.jnbt.CompoundTag; import com.intellectualcrafters.jnbt.IntTag; @@ -21,16 +25,84 @@ import com.intellectualcrafters.jnbt.NBTOutputStream; import com.intellectualcrafters.jnbt.ShortTag; import com.intellectualcrafters.jnbt.Tag; import com.intellectualcrafters.plot.PlotSquared; +import com.intellectualcrafters.plot.commands.SchematicCmd; +import com.intellectualcrafters.plot.config.Settings; import com.intellectualcrafters.plot.object.Location; import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.PlotId; import com.intellectualcrafters.plot.object.schematic.PlotItem; import com.intellectualcrafters.plot.object.schematic.StateWrapper; import com.intellectualcrafters.plot.util.bukkit.BukkitUtil; +import com.intellectualcrafters.plot.util.bukkit.UUIDHandler; public abstract class SchematicHandler { public static SchematicHandler manager = new BukkitSchematicHandler(); + private boolean exportAll = false; + + public boolean exportAll(final Collection plots, final File outputDir, final String namingScheme, final Runnable ifSuccess) { + if (exportAll) { + return false; + } + if (plots.size() == 0) { + return false; + } + exportAll = true; + TaskManager.index.increment(); + final Integer currentIndex = TaskManager.index.toInteger(); + final int task = TaskManager.runTaskRepeat(new Runnable() { + @Override + public void run() { + if (plots.size() == 0) { + Bukkit.getScheduler().cancelTask(TaskManager.tasks.get(currentIndex)); + TaskManager.tasks.remove(currentIndex); + TaskManager.runTask(ifSuccess); + return; + } + Iterator i = plots.iterator(); + final Plot plot = i.next(); + i.remove(); + final CompoundTag sch = SchematicHandler.manager.getCompoundTag(plot.world, plot.id); + String o = UUIDHandler.getName(plot.owner); + if (o == null) { + o = "unknown"; + } + final String name; + if (namingScheme == null) { + name = plot.id.x + ";" + plot.id.y + "," + plot.world + "," + o; + } + else { + name = namingScheme.replaceAll("%owner%", o).replaceAll("%id%", plot.id.toString()).replaceAll("%idx%", plot.id.x + "").replaceAll("%idy%", plot.id.y + "").replaceAll("%world%", plot.world); + } + final String directory; + if (outputDir == null) { + directory = Settings.SCHEMATIC_SAVE_PATH; + } + else { + directory = outputDir.getPath(); + } + if (sch == null) { + MainUtil.sendMessage(null, "&7 - Skipped plot &c" + plot.id); + } else { + TaskManager.runTaskAsync(new Runnable() { + @Override + public void run() { + MainUtil.sendMessage(null, "&6ID: " + plot.id); + final boolean result = SchematicHandler.manager.save(sch, directory + File.separator + name + ".schematic"); + if (!result) { + MainUtil.sendMessage(null, "&7 - Failed to save &c" + plot.id); + } else { + MainUtil.sendMessage(null, "&7 - &a success: " + plot.id); + } + } + }); + } + } + }, 20); + TaskManager.tasks.put(currentIndex, task); + return true; + } + /** * Paste a schematic *