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.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<FileBytes> 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;

View File

@ -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<FileBytes> 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);
}
/**

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.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<PlotId> 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<FileBytes> files = new HashSet<>(Arrays.asList(new FileBytes("settings.yml", Template.getBytes(plotworld))));
Template.zipAll(plotworld.worldname, files);
}
}