From 166c47a697b4eea9eca6712b76e8d8f8af8b6c5b Mon Sep 17 00:00:00 2001 From: boy0001 Date: Wed, 25 Feb 2015 19:25:22 +1100 Subject: [PATCH] Add export implementation for HybridPlotWorld --- .../plot/commands/Template.java | 54 +++++++------------ .../plot/generator/HybridPlotManager.java | 30 +++++++++-- .../plot/object/FileBytes.java | 11 ++++ .../plot/object/PlotManager.java | 6 ++- 4 files changed, 58 insertions(+), 43 deletions(-) create mode 100644 PlotSquared/src/main/java/com/intellectualcrafters/plot/object/FileBytes.java diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Template.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Template.java index f2203e3d2..561e42aed 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Template.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Template.java @@ -24,9 +24,13 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Enumeration; import java.util.Set; import java.util.zip.GZIPOutputStream; import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; import java.util.zip.ZipOutputStream; import org.bukkit.configuration.ConfigurationSection; @@ -35,6 +39,7 @@ import org.bukkit.configuration.file.YamlConfiguration; import com.intellectualcrafters.plot.PlotSquared; import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.config.Configuration; +import com.intellectualcrafters.plot.object.FileBytes; import com.intellectualcrafters.plot.object.PlotManager; import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.object.PlotWorld; @@ -53,12 +58,6 @@ public class Template extends SubCommand { return false; } final String world = args[1]; - final PlotWorld plotworld = PlotSquared.getPlotWorld(world); - if (!BlockManager.manager.isWorld(world) || (plotworld == null)) { - MainUtil.sendMessage(plr, C.NOT_VALID_PLOT_WORLD); - return false; - } - PlotManager manager = PlotSquared.getPlotManager(world); switch (args[0].toLowerCase()) { case "import": { // TODO import template @@ -66,6 +65,12 @@ public class Template extends SubCommand { return true; } case "export": { + final PlotWorld plotworld = PlotSquared.getPlotWorld(world); + if (!BlockManager.manager.isWorld(world) || (plotworld == null)) { + MainUtil.sendMessage(plr, C.NOT_VALID_PLOT_WORLD); + return false; + } + PlotManager manager = PlotSquared.getPlotManager(world); manager.export(plotworld); MainUtil.sendMessage(plr, "Done!"); } @@ -81,42 +86,19 @@ public class Template extends SubCommand { plotworld.saveConfiguration(section); return config.saveToString().getBytes(); } - - public static boolean zip(final String world, final byte[] bytes, String location, File output) { - try { - output.mkdirs(); - FileOutputStream fos = new FileOutputStream(output + File.separator + world + ".template"); - ZipOutputStream zos = new ZipOutputStream(fos); - ZipEntry ze = new ZipEntry(location); - zos.putNextEntry(ze); - zos.write(bytes); - zos.closeEntry(); - zos.close(); - return true; - } catch (final IOException ex) { - ex.printStackTrace(); - return false; - } - } - public static boolean zip(final String world, final File file, File output) { - if (!file.exists()) { - System.out.print("NOT EXIST: " + file); - return false; - } - byte[] buffer = new byte[1024]; + public static boolean zipAll(final String world, Set files) { try { + File output = new File(PlotSquared.IMP.getDirectory() + File.separator + "templates"); output.mkdirs(); FileOutputStream fos = new FileOutputStream(output + File.separator + world + ".template"); ZipOutputStream zos = new ZipOutputStream(fos); - ZipEntry ze= new ZipEntry(file.getPath()); - zos.putNextEntry(ze); - FileInputStream in = new FileInputStream(file.getPath()); - int len; - while ((len = in.read(buffer)) > 0) { - zos.write(buffer, 0, len); + + for (FileBytes file : files) { + ZipEntry ze = new ZipEntry(file.path); + zos.putNextEntry(ze); + zos.write(file.data); } - in.close(); zos.closeEntry(); zos.close(); return true; diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/HybridPlotManager.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/HybridPlotManager.java index 4b18a8b58..f7f4192c1 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/HybridPlotManager.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/HybridPlotManager.java @@ -21,9 +21,13 @@ package com.intellectualcrafters.plot.generator; import java.io.File; +import java.nio.file.Files; +import java.util.Arrays; +import java.util.HashSet; import com.intellectualcrafters.plot.PlotSquared; import com.intellectualcrafters.plot.commands.Template; +import com.intellectualcrafters.plot.object.FileBytes; import com.intellectualcrafters.plot.object.Location; import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.PlotBlock; @@ -37,11 +41,27 @@ public class HybridPlotManager extends ClassicPlotManager { @Override public void export(PlotWorld plotworld) { - super.export(plotworld); - String directory = PlotSquared.IMP.getDirectory() + File.separator + "schematics" + File.separator + "GEN_ROAD_SCHEMATIC" + File.separator + plotworld.worldname + File.separator; - Template.zip(plotworld.worldname, new File(directory + "sideroad.schematic"), new File(PlotSquared.IMP.getDirectory() + File.separator + "templates")); - Template.zip(plotworld.worldname, new File(directory + "intersection.schematic"), new File(PlotSquared.IMP.getDirectory() + File.separator + "templates")); - Template.zip(plotworld.worldname, new File(directory + "plot.schematic"), new File(PlotSquared.IMP.getDirectory() + File.separator + "templates")); + HashSet files = new HashSet<>(Arrays.asList(new FileBytes("settings.yml", Template.getBytes(plotworld)))); + String psRoot = PlotSquared.IMP.getDirectory() + File.separator; + String dir = "schematics" + File.separator + "GEN_ROAD_SCHEMATIC" + File.separator + plotworld.worldname + File.separator; + try { + File sideroad = new File(psRoot + dir + "sideroad.schematic"); + if (sideroad.exists()) { + files.add(new FileBytes(dir + "sideroad.schematic", Files.readAllBytes(sideroad.toPath()))); + } + File intersection = new File(psRoot + dir + "intersection.schematic"); + if (sideroad.exists()) { + files.add(new FileBytes(dir + "intersection.schematic", Files.readAllBytes(intersection.toPath()))); + } + File plot = new File(psRoot + dir + "plot.schematic"); + if (sideroad.exists()) { + files.add(new FileBytes(dir + "plot.schematic", Files.readAllBytes(plot.toPath()))); + } + } + catch (Exception e) { + e.printStackTrace(); + } + Template.zipAll(plotworld.worldname, files); } /** diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/object/FileBytes.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/object/FileBytes.java new file mode 100644 index 000000000..c79bd9798 --- /dev/null +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/object/FileBytes.java @@ -0,0 +1,11 @@ +package com.intellectualcrafters.plot.object; + +public class FileBytes { + public String path; + public byte[] data; + + public FileBytes(String path, byte[] data) { + this.path = path; + this.data = data; + } +} diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/object/PlotManager.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/object/PlotManager.java index 3753b6d4a..cb5bae259 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/object/PlotManager.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/object/PlotManager.java @@ -22,6 +22,8 @@ package com.intellectualcrafters.plot.object; import java.io.File; import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; import com.intellectualcrafters.plot.PlotSquared; import com.intellectualcrafters.plot.commands.Template; @@ -87,8 +89,8 @@ public abstract class PlotManager { public abstract boolean finishPlotUnlink(final PlotWorld plotworld, final ArrayList plotIds); public void export(PlotWorld plotworld) { - byte[] bytes = Template.getBytes(plotworld); - Template.zip(plotworld.worldname, bytes, "settings.yml", new File(PlotSquared.IMP.getDirectory() + File.separator + "templates")); + HashSet files = new HashSet<>(Arrays.asList(new FileBytes("settings.yml", Template.getBytes(plotworld)))); + Template.zipAll(plotworld.worldname, files); } }