From f3472815c91da19326b7b5447ab555a2afbb76b7 Mon Sep 17 00:00:00 2001 From: tastybento Date: Sun, 3 Jun 2018 15:33:45 -0700 Subject: [PATCH] Fixed schems The admin command "schem" now works very like WorldEdit. You set the pos1 and pos2, copy to clipboard and paste You can save and load. Schems go into the schems folder. --- locales/en-US.yml | 1 + .../commands/admin/AdminSchemCommand.java | 41 ++----- .../bskyblock/island/builders/Clipboard.java | 107 +++++++++++++----- 3 files changed, 88 insertions(+), 61 deletions(-) diff --git a/locales/en-US.yml b/locales/en-US.yml index 502667c5f..1729cf49a 100644 --- a/locales/en-US.yml +++ b/locales/en-US.yml @@ -130,6 +130,7 @@ commands: copy-first: "&cCopy a schem first!" no-such-file: "&cNo such file!" could-not-load: "&cCould not load that file!" + could-not-save: "&Hmm, something went wrong saving that file: [message]" set-pos1: "&aPosition 1 set at [vector]" set-pos2: "&aPosition 2 set at [vector]" need-pos1-pos2: "&cSet pos1 and pos2 first!" diff --git a/src/main/java/us/tastybento/bskyblock/commands/admin/AdminSchemCommand.java b/src/main/java/us/tastybento/bskyblock/commands/admin/AdminSchemCommand.java index 74be92a46..8f4c63925 100644 --- a/src/main/java/us/tastybento/bskyblock/commands/admin/AdminSchemCommand.java +++ b/src/main/java/us/tastybento/bskyblock/commands/admin/AdminSchemCommand.java @@ -1,7 +1,5 @@ package us.tastybento.bskyblock.commands.admin; -import java.io.File; -import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -10,10 +8,10 @@ import java.util.UUID; import us.tastybento.bskyblock.api.commands.CompositeCommand; import us.tastybento.bskyblock.api.user.User; import us.tastybento.bskyblock.island.builders.Clipboard; +import us.tastybento.bskyblock.util.Util; public class AdminSchemCommand extends CompositeCommand { private Map clipboards; - private File schemFolder; public AdminSchemCommand(CompositeCommand parent) { super(parent, "schem"); @@ -25,10 +23,6 @@ public class AdminSchemCommand extends CompositeCommand { setDescription("commands.admin.schem.description"); setOnlyPlayer(true); clipboards = new HashMap<>(); - schemFolder = new File(getPlugin().getDataFolder(), "schems"); - if (!schemFolder.exists()) { - schemFolder.mkdirs(); - } } public boolean execute(User user, List args) { @@ -51,26 +45,15 @@ public class AdminSchemCommand extends CompositeCommand { if (args.get(0).equalsIgnoreCase("load")) { if (args.size() == 2) { - File file = new File(schemFolder, args.get(1)); - if (file.exists()) { - try { - cb.load(file); - user.sendMessage("general.success"); - clipboards.put(user.getUniqueId(), cb); - return true; - } catch (Exception e) { - user.sendMessage("commands.admin.schem.could-not-load"); - e.printStackTrace(); - return false; - } - } else { - user.sendMessage("commands.admin.schem.no-such-file"); - return false; + if (cb.load(user, args.get(1))) { + clipboards.put(user.getUniqueId(), cb); + return true; } } else { showHelp(this, user); return false; } + return false; } if (args.get(0).equalsIgnoreCase("copy")) { @@ -80,15 +63,7 @@ public class AdminSchemCommand extends CompositeCommand { if (args.get(0).equalsIgnoreCase("save")) { if (cb.isFull()) { if (args.size() == 2) { - File file = new File(schemFolder, args.get(1)); - user.sendMessage("general.success"); - try { - cb.save(file); - } catch (IOException e) { - user.sendRawMessage("Could not save!"); - return false; - } - return true; + return cb.save(user, args.get(1)); } else { showHelp(this, user); return false; @@ -101,14 +76,14 @@ public class AdminSchemCommand extends CompositeCommand { if (args.get(0).equalsIgnoreCase("pos1")) { cb.setPos1(user.getLocation()); - user.sendMessage("commands.admin.schem.set-pos1", "[vector]", user.getLocation().toVector().toString()); + user.sendMessage("commands.admin.schem.set-pos1", "[vector]", Util.xyz(user.getLocation().toVector())); clipboards.put(user.getUniqueId(), cb); return true; } if (args.get(0).equalsIgnoreCase("pos2")) { cb.setPos2(user.getLocation()); - user.sendMessage("commands.admin.schem.set-pos2", "[vector]", user.getLocation().toVector().toString()); + user.sendMessage("commands.admin.schem.set-pos2", "[vector]", Util.xyz(user.getLocation().toVector())); clipboards.put(user.getUniqueId(), cb); return true; } diff --git a/src/main/java/us/tastybento/bskyblock/island/builders/Clipboard.java b/src/main/java/us/tastybento/bskyblock/island/builders/Clipboard.java index b071d9e2c..87b647710 100644 --- a/src/main/java/us/tastybento/bskyblock/island/builders/Clipboard.java +++ b/src/main/java/us/tastybento/bskyblock/island/builders/Clipboard.java @@ -63,9 +63,15 @@ public class Clipboard { private BSkyBlock plugin; private boolean copied; + private File schemFolder; + public Clipboard(BSkyBlock plugin) { super(); this.plugin = plugin; + schemFolder = new File(plugin.getDataFolder(), "schems"); + if (!schemFolder.exists()) { + schemFolder.mkdirs(); + } } /** * @return the pos1 @@ -225,7 +231,7 @@ public class Clipboard { } bs.update(true, false); - + if (bs instanceof InventoryHolder) { Bukkit.getLogger().info("Inventory holder " + s.getCurrentPath()); Inventory ih = ((InventoryHolder)bs).getInventory(); @@ -316,7 +322,7 @@ public class Clipboard { } } - + /** * @return the blockConfig */ @@ -324,24 +330,10 @@ public class Clipboard { return blockConfig; } - /** - * Load a file to clipboard - * @param file - * @throws IOException - * @throws InvalidConfigurationException - */ - public void load(File file) throws IOException, InvalidConfigurationException { - unzip(file.getAbsolutePath()); - blockConfig = new YamlConfiguration(); - blockConfig.load(file); - copied = true; - Files.delete(file.toPath()); - } - private void unzip(final String zipFilePath) throws IOException { Path path = Paths.get(zipFilePath); if (!(path.toFile().exists())) { - return; + throw new IOException("No file exists!"); } try (ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipFilePath))) { ZipEntry entry = zipInputStream.getNextEntry(); @@ -371,18 +363,8 @@ public class Clipboard { } - /** - * Save the clipboard to a file - * @param file - * @throws IOException - */ - public void save(File file) throws IOException { - getBlockConfig().save(file); - zip(file); - } - private void zip(File targetFile) throws IOException { - try (ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(targetFile.getAbsolutePath() + ".zip"))) { + try (ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(targetFile.getAbsolutePath() + ".schem"))) { zipOutputStream.putNextEntry(new ZipEntry(targetFile.getName())); try (FileInputStream inputStream = new FileInputStream(targetFile)) { final byte[] buffer = new byte[1024]; @@ -402,4 +384,73 @@ public class Clipboard { public boolean isFull() { return copied; } + + /** + * Load a file to clipboard + * @param file + * @throws IOException + * @throws InvalidConfigurationException + */ + public boolean load(User user, String string) { + File zipFile = new File(schemFolder, string + ".schem"); + if (!zipFile.exists()) { + user.sendMessage("commands.admin.schem.no-such-file"); + return false; + } + try { + unzip(zipFile.getAbsolutePath()); + } catch (IOException e) { + user.sendMessage("commands.admin.schem.could-not-load"); + plugin.logError("Could not load schems file - could not unzip : " + zipFile.getName()); + return false; + } + File file = new File(schemFolder, string); + if (!file.exists()) { + user.sendMessage("commands.admin.schem.could-not-load"); + plugin.logError("Could not load schems file - does not exist : " + file.getName()); + return false; + } + blockConfig = new YamlConfiguration(); + try { + blockConfig.load(file); + } catch (Exception e) { + user.sendMessage("commands.admin.schem.could-not-load"); + plugin.logError("Could not load schems file - YAML error : " + file.getName()); + return false; + } + copied = true; + try { + Files.delete(file.toPath()); + } catch (IOException e) { + plugin.logError("Could not delete temporary schems file: " + file.getName()); + } + user.sendMessage("general.success"); + return true; + } + + /** + * Save the clipboard to a file + * @param user + * @param string + * @return + */ + public boolean save(User user, String string) { + File file = new File(schemFolder, string); + try { + getBlockConfig().save(file); + } catch (IOException e) { + user.sendMessage("commands.admin.schem.could-not-save", "[message]", "Could not save temp schems file."); + plugin.logError("Could not save temporary schems file: " + file.getName()); + return false; + } + try { + zip(file); + } catch (IOException e) { + user.sendMessage("commands.admin.schem.could-not-save", "[message]", "Could not zip temp schems file."); + plugin.logError("Could not zip temporary schems file: " + file.getName()); + return false; + } + user.sendMessage("general.success"); + return true; + } }