From a63bcbba1ae5d1ca7b6dc084fc39980c0190ebc0 Mon Sep 17 00:00:00 2001 From: boy0001 Date: Wed, 25 Feb 2015 18:50:17 +1100 Subject: [PATCH] World template exporting --- PlotSquared/pom.xml | 4 -- .../plot/commands/MainCommand.java | 2 +- .../plot/commands/Template.java | 71 +++++++++++++++---- .../plot/generator/HybridGen.java | 4 -- .../plot/generator/HybridPlotManager.java | 13 ++++ .../plot/listeners/PlayerEvents.java | 4 +- .../plot/listeners/WorldEditListener.java | 3 + .../plot/object/BukkitPlayer.java | 4 +- .../plot/object/PlotManager.java | 10 +++ 9 files changed, 89 insertions(+), 26 deletions(-) diff --git a/PlotSquared/pom.xml b/PlotSquared/pom.xml index 053a83c19..a7c895e7a 100644 --- a/PlotSquared/pom.xml +++ b/PlotSquared/pom.xml @@ -61,10 +61,6 @@ sk80q http://maven.sk89q.com/artifactory/repo/ - - vault-repo - http://nexus.theyeticave.net/content/repositories/pub_releases - diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java index 6106562e1..4f6b38354 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/MainCommand.java @@ -40,7 +40,7 @@ public class MainCommand { /** * Main Permission Node */ - private final static SubCommand[] _subCommands = new SubCommand[] { new Setup(), new DebugSaveTest(), new DebugLoadTest(), new CreateRoadSchematic(), new RegenAllRoads(), new DebugClear(), new Claim(), new Auto(), new Home(), new Visit(), new TP(), new Set(), new Clear(), new Delete(), new SetOwner(), new Denied(), new Helpers(), new Trusted(), new Info(), new list(), new Help(), new Debug(), new Schematic(), new plugin(), new Inventory(), new Purge(), new Reload(), new Merge(), new Unlink(), new Kick(), new Rate(), new DebugClaimTest(), new Inbox(), new Comment(), new Database(), new Unclaim(), new Swap(), new MusicSubcommand(), new DebugRoadRegen(), new Trim(), new DebugExec(), new FlagCmd(), new Target(), new DebugFixFlags(), new Move(), new Condense() }; + private final static SubCommand[] _subCommands = new SubCommand[] { new Template(), new Setup(), new DebugSaveTest(), new DebugLoadTest(), new CreateRoadSchematic(), new RegenAllRoads(), new DebugClear(), new Claim(), new Auto(), new Home(), new Visit(), new TP(), new Set(), new Clear(), new Delete(), new SetOwner(), new Denied(), new Helpers(), new Trusted(), new Info(), new list(), new Help(), new Debug(), new Schematic(), new plugin(), new Inventory(), new Purge(), new Reload(), new Merge(), new Unlink(), new Kick(), new Rate(), new DebugClaimTest(), new Inbox(), new Comment(), new Database(), new Unclaim(), new Swap(), new MusicSubcommand(), new DebugRoadRegen(), new Trim(), new DebugExec(), new FlagCmd(), new Target(), new DebugFixFlags(), new Move(), new Condense() }; public final static ArrayList subCommands = new ArrayList() { { addAll(Arrays.asList(_subCommands)); 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 60e8401d8..f2203e3d2 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Template.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Template.java @@ -20,13 +20,22 @@ //////////////////////////////////////////////////////////////////////////////////////////////////// package com.intellectualcrafters.plot.commands; +import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; +import java.util.Set; import java.util.zip.GZIPOutputStream; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + +import org.bukkit.configuration.ConfigurationSection; +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.PlotManager; import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.object.PlotWorld; import com.intellectualcrafters.plot.util.BlockManager; @@ -34,7 +43,7 @@ import com.intellectualcrafters.plot.util.MainUtil; public class Template extends SubCommand { public Template() { - super("template", "plots.admin", "Create or use a world template", "template", "", CommandCategory.DEBUG, true); + super("template", "plots.admin", "Create or use a world template", "template", "", CommandCategory.DEBUG, false); } @Override @@ -49,6 +58,7 @@ public class Template extends SubCommand { MainUtil.sendMessage(plr, C.NOT_VALID_PLOT_WORLD); return false; } + PlotManager manager = PlotSquared.getPlotManager(world); switch (args[0].toLowerCase()) { case "import": { // TODO import template @@ -56,28 +66,63 @@ public class Template extends SubCommand { return true; } case "export": { - MainUtil.sendMessage(plr, "TODO"); + manager.export(plotworld); + MainUtil.sendMessage(plr, "Done!"); } } // TODO allow world settings (including schematics to be packed into a single file) // TODO allow world created based on these packaged files return true; } + + public static byte[] getBytes(PlotWorld plotworld) { + YamlConfiguration config = new YamlConfiguration(); + ConfigurationSection section = config.getConfigurationSection(""); + plotworld.saveConfiguration(section); + return config.saveToString().getBytes(); + } - public void gzipIt(final String output, final String input) { - final byte[] buffer = new byte[1024]; + public static boolean zip(final String world, final byte[] bytes, String location, File output) { try { - final GZIPOutputStream gzos = new GZIPOutputStream(new FileOutputStream(output)); - final FileInputStream in = new FileInputStream(input); - int len; - while ((len = in.read(buffer)) > 0) { - gzos.write(buffer, 0, len); - } - in.close(); - gzos.finish(); - gzos.close(); + 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]; + 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); + FileInputStream in = new FileInputStream(file.getPath()); + int len; + while ((len = in.read(buffer)) > 0) { + zos.write(buffer, 0, len); + } + in.close(); + zos.closeEntry(); + zos.close(); + return true; + } catch (final IOException ex) { + ex.printStackTrace(); + return false; } } } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/HybridGen.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/HybridGen.java index fc60221ee..3d60a00c0 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/HybridGen.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/HybridGen.java @@ -90,9 +90,6 @@ public class HybridGen extends PlotGenerator { if (this.plotworld == null) { this.plotworld = (HybridPlotWorld) plotworld; } - - System.out.print("INITIALIZING ==================================="); - this.plotsize = this.plotworld.PLOT_WIDTH; this.pathsize = this.plotworld.ROAD_WIDTH; this.roadblock = this.plotworld.ROAD_BLOCK.id; @@ -238,7 +235,6 @@ public class HybridGen extends PlotGenerator { h = (prime * h) + cz; this.state = h; } - System.out.print(this.maxY); this.result = new short[this.maxY / 16][]; if (this.plotworld.PLOT_BEDROCK) { for (short x = 0; x < 16; x++) { 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 a644e658f..4b18a8b58 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/HybridPlotManager.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/generator/HybridPlotManager.java @@ -20,6 +20,10 @@ //////////////////////////////////////////////////////////////////////////////////////////////////// package com.intellectualcrafters.plot.generator; +import java.io.File; + +import com.intellectualcrafters.plot.PlotSquared; +import com.intellectualcrafters.plot.commands.Template; import com.intellectualcrafters.plot.object.Location; import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.PlotBlock; @@ -31,6 +35,15 @@ import com.intellectualcrafters.plot.util.bukkit.BukkitUtil; @SuppressWarnings("deprecation") 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")); + } + /** * Clearing the plot needs to only consider removing the blocks - This implementation has used the SetCuboid * function, as it is fast, and uses NMS code - It also makes use of the fact that deleting chunks is a lot faster diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlayerEvents.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlayerEvents.java index 28813447b..dd9568c4b 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlayerEvents.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlayerEvents.java @@ -345,8 +345,8 @@ public class PlayerEvents extends com.intellectualcrafters.plot.listeners.PlotLi @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) public static void onWorldChanged(final PlayerChangedWorldEvent event) { final PlotPlayer player = BukkitUtil.getPlayer(event.getPlayer()); - ((BukkitPlayer) player).hasPerm = null; - ((BukkitPlayer) player).noPerm = null; + ((BukkitPlayer) player).hasPerm = new HashSet<>(); + ((BukkitPlayer) player).noPerm = new HashSet<>(); } @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/WorldEditListener.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/WorldEditListener.java index 83bbfa0c6..a44c432a9 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/WorldEditListener.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/WorldEditListener.java @@ -192,6 +192,9 @@ public class WorldEditListener implements Listener { return; } final Location f = e.getFrom(); + if ((f.getX() == t.getX()) && (f.getZ() == t.getZ())) { + return; + } final Player p = e.getPlayer(); final PlotPlayer pp = BukkitUtil.getPlayer(p); if (Permissions.hasPermission(pp, "plots.worldedit.bypass")) { diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/object/BukkitPlayer.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/object/BukkitPlayer.java index 2d32295c2..2c2d86bd2 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/object/BukkitPlayer.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/object/BukkitPlayer.java @@ -13,8 +13,8 @@ public class BukkitPlayer implements PlotPlayer { public final Player player; UUID uuid; String name; - public HashSet hasPerm; - public HashSet noPerm; + public HashSet hasPerm = new HashSet<>(); + public HashSet noPerm = new HashSet<>(); private int op = 0; /** 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 2a1c4a5e5..3753b6d4a 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/object/PlotManager.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/object/PlotManager.java @@ -20,8 +20,12 @@ //////////////////////////////////////////////////////////////////////////////////////////////////// package com.intellectualcrafters.plot.object; +import java.io.File; import java.util.ArrayList; +import com.intellectualcrafters.plot.PlotSquared; +import com.intellectualcrafters.plot.commands.Template; + public abstract class PlotManager { /* * Plot locations (methods with Abs in them will not need to consider mega @@ -81,4 +85,10 @@ public abstract class PlotManager { public abstract boolean finishPlotMerge(final PlotWorld plotworld, final ArrayList plotIds); 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")); + } + }