Add export implementation for HybridPlotWorld

This commit is contained in:
boy0001 2015-02-25 19:25:22 +11:00
parent a63bcbba1a
commit 166c47a697
4 changed files with 58 additions and 43 deletions

View File

@ -24,9 +24,13 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Set; import java.util.Set;
import java.util.zip.GZIPOutputStream; import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
@ -35,6 +39,7 @@ import org.bukkit.configuration.file.YamlConfiguration;
import com.intellectualcrafters.plot.PlotSquared; import com.intellectualcrafters.plot.PlotSquared;
import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.config.C;
import com.intellectualcrafters.plot.config.Configuration; import com.intellectualcrafters.plot.config.Configuration;
import com.intellectualcrafters.plot.object.FileBytes;
import com.intellectualcrafters.plot.object.PlotManager; import com.intellectualcrafters.plot.object.PlotManager;
import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.object.PlotWorld; import com.intellectualcrafters.plot.object.PlotWorld;
@ -53,12 +58,6 @@ public class Template extends SubCommand {
return false; return false;
} }
final String world = args[1]; 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()) { switch (args[0].toLowerCase()) {
case "import": { case "import": {
// TODO import template // TODO import template
@ -66,6 +65,12 @@ public class Template extends SubCommand {
return true; return true;
} }
case "export": { 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); manager.export(plotworld);
MainUtil.sendMessage(plr, "Done!"); MainUtil.sendMessage(plr, "Done!");
} }
@ -82,41 +87,18 @@ public class Template extends SubCommand {
return config.saveToString().getBytes(); return config.saveToString().getBytes();
} }
public static boolean zip(final String world, final byte[] bytes, String location, File output) { public static boolean zipAll(final String world, Set<FileBytes> files) {
try { try {
File output = new File(PlotSquared.IMP.getDirectory() + File.separator + "templates");
output.mkdirs(); output.mkdirs();
FileOutputStream fos = new FileOutputStream(output + File.separator + world + ".template"); FileOutputStream fos = new FileOutputStream(output + File.separator + world + ".template");
ZipOutputStream zos = new ZipOutputStream(fos); 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) { for (FileBytes file : files) {
if (!file.exists()) { ZipEntry ze = new ZipEntry(file.path);
System.out.print("NOT EXIST: " + file);
return false;
}
byte[] buffer = new byte[1024];
try {
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); zos.putNextEntry(ze);
FileInputStream in = new FileInputStream(file.getPath()); zos.write(file.data);
int len;
while ((len = in.read(buffer)) > 0) {
zos.write(buffer, 0, len);
} }
in.close();
zos.closeEntry(); zos.closeEntry();
zos.close(); zos.close();
return true; return true;

View File

@ -21,9 +21,13 @@
package com.intellectualcrafters.plot.generator; package com.intellectualcrafters.plot.generator;
import java.io.File; 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.PlotSquared;
import com.intellectualcrafters.plot.commands.Template; import com.intellectualcrafters.plot.commands.Template;
import com.intellectualcrafters.plot.object.FileBytes;
import com.intellectualcrafters.plot.object.Location; import com.intellectualcrafters.plot.object.Location;
import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.object.PlotBlock; import com.intellectualcrafters.plot.object.PlotBlock;
@ -37,11 +41,27 @@ public class HybridPlotManager extends ClassicPlotManager {
@Override @Override
public void export(PlotWorld plotworld) { public void export(PlotWorld plotworld) {
super.export(plotworld); HashSet<FileBytes> files = new HashSet<>(Arrays.asList(new FileBytes("settings.yml", Template.getBytes(plotworld))));
String directory = PlotSquared.IMP.getDirectory() + File.separator + "schematics" + File.separator + "GEN_ROAD_SCHEMATIC" + File.separator + plotworld.worldname + File.separator; String psRoot = PlotSquared.IMP.getDirectory() + File.separator;
Template.zip(plotworld.worldname, new File(directory + "sideroad.schematic"), new File(PlotSquared.IMP.getDirectory() + File.separator + "templates")); String dir = "schematics" + File.separator + "GEN_ROAD_SCHEMATIC" + File.separator + plotworld.worldname + File.separator;
Template.zip(plotworld.worldname, new File(directory + "intersection.schematic"), new File(PlotSquared.IMP.getDirectory() + File.separator + "templates")); try {
Template.zip(plotworld.worldname, new File(directory + "plot.schematic"), new File(PlotSquared.IMP.getDirectory() + File.separator + "templates")); 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);
} }
/** /**

View File

@ -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;
}
}

View File

@ -22,6 +22,8 @@ package com.intellectualcrafters.plot.object;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import com.intellectualcrafters.plot.PlotSquared; import com.intellectualcrafters.plot.PlotSquared;
import com.intellectualcrafters.plot.commands.Template; import com.intellectualcrafters.plot.commands.Template;
@ -87,8 +89,8 @@ public abstract class PlotManager {
public abstract boolean finishPlotUnlink(final PlotWorld plotworld, final ArrayList<PlotId> plotIds); public abstract boolean finishPlotUnlink(final PlotWorld plotworld, final ArrayList<PlotId> plotIds);
public void export(PlotWorld plotworld) { public void export(PlotWorld plotworld) {
byte[] bytes = Template.getBytes(plotworld); HashSet<FileBytes> files = new HashSet<>(Arrays.asList(new FileBytes("settings.yml", Template.getBytes(plotworld))));
Template.zip(plotworld.worldname, bytes, "settings.yml", new File(PlotSquared.IMP.getDirectory() + File.separator + "templates")); Template.zipAll(plotworld.worldname, files);
} }
} }