From b87b00d8879d51da500130b889360549bd28bd70 Mon Sep 17 00:00:00 2001 From: tastybento Date: Sat, 2 Jun 2018 19:54:41 -0700 Subject: [PATCH] Cleaned up some code smells. --- .../bskyblock/island/builders/Clipboard.java | 41 ++++++++++--------- .../bskyblock/managers/AddonsManager.java | 29 ++++--------- .../bskyblock/panels/SettingsPanel.java | 2 - 3 files changed, 28 insertions(+), 44 deletions(-) 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 ac845b5a6..de672ffd5 100644 --- a/src/main/java/us/tastybento/bskyblock/island/builders/Clipboard.java +++ b/src/main/java/us/tastybento/bskyblock/island/builders/Clipboard.java @@ -139,8 +139,8 @@ public class Clipboard { @SuppressWarnings("deprecation") private void setBlock(Block block, ConfigurationSection s, Material m) { - // Block state - + // Block state + if (s.getBoolean(ATTACHED) && m.toString().contains("TORCH")) { TorchDir d = TorchDir.valueOf(s.getString("facing")); @@ -163,7 +163,7 @@ public class Clipboard { } block.setType(m); - + BlockState bs = block.getState(); byte data = (byte)s.getInt("data"); @@ -219,11 +219,11 @@ public class Clipboard { } } } - + bs.update(true, false); if (bs instanceof InventoryHolder) { Bukkit.getLogger().info("Inventory holder " + s.getCurrentPath()); - + InventoryHolder ih = (InventoryHolder)bs; @SuppressWarnings("unchecked") List items = (List) s.get("inventory"); @@ -233,7 +233,7 @@ public class Clipboard { } } - + @SuppressWarnings("deprecation") private void copyBlock(Block block, Location origin) { if (block.getType().equals(Material.AIR)) { @@ -324,15 +324,15 @@ public class Clipboard { * @throws InvalidConfigurationException */ public void load(File file) throws IOException, InvalidConfigurationException { - unzip(file.getAbsolutePath()); - blockConfig.load(file); - copied = true; - Files.delete(file.toPath()); + unzip(file.getAbsolutePath()); + blockConfig.load(file); + copied = true; + Files.delete(file.toPath()); } - + private void unzip(final String zipFilePath) throws IOException { Path path = Paths.get(zipFilePath); - if (!(Files.exists(path))) { + if (!(path.toFile().exists())) { return; } try (ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipFilePath))) { @@ -362,19 +362,18 @@ 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); + getBlockConfig().save(file); + zip(file); } - - private void zip(File targetFile) throws IOException { + private void zip(File targetFile) throws IOException { try (ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(targetFile.getAbsolutePath() + ".schem"))) { zipOutputStream.putNextEntry(new ZipEntry(targetFile.getName())); try (FileInputStream inputStream = new FileInputStream(targetFile)) { @@ -386,13 +385,15 @@ public class Clipboard { } inputStream.close(); zipOutputStream.close(); - if (!targetFile.delete()) { - throw new IOException("Could not delete temp file"); + try { + Files.delete(targetFile.toPath()); + } catch (Exception e) { + plugin.logError(e.getMessage()); } } } } - + public boolean isFull() { return copied; } diff --git a/src/main/java/us/tastybento/bskyblock/managers/AddonsManager.java b/src/main/java/us/tastybento/bskyblock/managers/AddonsManager.java index a1343bb27..b95e84104 100644 --- a/src/main/java/us/tastybento/bskyblock/managers/AddonsManager.java +++ b/src/main/java/us/tastybento/bskyblock/managers/AddonsManager.java @@ -48,17 +48,11 @@ public class AddonsManager { public void loadAddons() { plugin.log("Loading addons..."); File f = new File(plugin.getDataFolder(), "addons"); - if (!f.exists()) { - f.mkdirs(); + if (!f.exists() && !f.mkdirs()) { + plugin.logError("Cannot make addons folder!"); + return; } - Arrays.stream(Objects.requireNonNull(f.listFiles())).filter(x -> !x.isDirectory() && x.getName().endsWith(".jar")).forEach(t -> { - plugin.log("Loading " + t.getName()); - try { - loadAddon(t); - } catch (Exception e) { - plugin.logError("Could not load addon " + t.getName() + " : " + e.getMessage()); - } - }); + Arrays.stream(Objects.requireNonNull(f.listFiles())).filter(x -> !x.isDirectory() && x.getName().endsWith(".jar")).forEach(this::loadAddon); addons.forEach(Addon::onLoad); } @@ -150,7 +144,9 @@ public class AddonsManager { loader.forEach(l -> { try { l.close(); - } catch (IOException ignore) {} + } catch (IOException ignore) { + // Ignore + } }); } @@ -168,7 +164,6 @@ public class AddonsManager { /** * Finds a class by name that has been loaded by this loader - * Code copied from Bukkit JavaPluginLoader * @param name - name of the class * @return Class - the class */ @@ -178,22 +173,12 @@ public class AddonsManager { /** * Sets a class that this loader should know about - * Code copied from Bukkit JavaPluginLoader * * @param name - name of the class * @param clazz - the class */ public void setClass(final String name, final Class clazz) { classes.putIfAbsent(name, clazz); - /* - if (!classes.containsKey(name)) { - classes.put(name, clazz); - - if (ConfigurationSerializable.class.isAssignableFrom(clazz)) { - Class serializable = clazz.asSubclass(ConfigurationSerializable.class); - ConfigurationSerialization.registerClass(serializable); - } - }*/ } /** diff --git a/src/main/java/us/tastybento/bskyblock/panels/SettingsPanel.java b/src/main/java/us/tastybento/bskyblock/panels/SettingsPanel.java index 900133e15..c9f95f72f 100644 --- a/src/main/java/us/tastybento/bskyblock/panels/SettingsPanel.java +++ b/src/main/java/us/tastybento/bskyblock/panels/SettingsPanel.java @@ -1,9 +1,7 @@ package us.tastybento.bskyblock.panels; import us.tastybento.bskyblock.BSkyBlock; -import us.tastybento.bskyblock.api.panels.PanelItem; import us.tastybento.bskyblock.api.panels.builders.PanelBuilder; -import us.tastybento.bskyblock.api.panels.builders.PanelItemBuilder; import us.tastybento.bskyblock.api.user.User; /**