From b402450253ae19114f6bbf8e53c5287f3fe35c0b Mon Sep 17 00:00:00 2001 From: tastybento Date: Sun, 27 May 2018 21:37:00 -0700 Subject: [PATCH] Cleaned up some vulnerabilities and code smells. --- .../api/commands/CompositeCommand.java | 3 ++ .../bskyblock/api/panels/PanelItem.java | 6 +--- .../commands/admin/AdminSchemCommand.java | 9 ++++-- .../bskyblock/island/builders/Clipboard.java | 29 ++++++++----------- .../island/builders/IslandBuilder.java | 3 +- .../managers/IslandWorldManager.java | 12 ++++---- .../bskyblock/managers/island/IslandGrid.java | 18 ++++++------ .../bskyblock/managers/island/NewIsland.java | 6 ++-- .../us/tastybento/bskyblock/util/Util.java | 10 ++++--- 9 files changed, 48 insertions(+), 48 deletions(-) diff --git a/src/main/java/us/tastybento/bskyblock/api/commands/CompositeCommand.java b/src/main/java/us/tastybento/bskyblock/api/commands/CompositeCommand.java index b88823ae8..d4eed5d43 100644 --- a/src/main/java/us/tastybento/bskyblock/api/commands/CompositeCommand.java +++ b/src/main/java/us/tastybento/bskyblock/api/commands/CompositeCommand.java @@ -535,6 +535,9 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi * @return the world */ public World getWorld() { + if (world == null) { + plugin.logError(getLabel() + " did not setWorld in setup!"); + } return world; } diff --git a/src/main/java/us/tastybento/bskyblock/api/panels/PanelItem.java b/src/main/java/us/tastybento/bskyblock/api/panels/PanelItem.java index 114d42c02..6b3155074 100644 --- a/src/main/java/us/tastybento/bskyblock/api/panels/PanelItem.java +++ b/src/main/java/us/tastybento/bskyblock/api/panels/PanelItem.java @@ -82,11 +82,7 @@ public class PanelItem { public void setGlow(boolean glow) { this.glow = glow; - if (glow) { - meta.addEnchant(Enchantment.ARROW_DAMAGE, 0, true); - } else { - meta.addEnchant(Enchantment.ARROW_DAMAGE, 0, false); - } + meta.addEnchant(Enchantment.ARROW_DAMAGE, 0, glow); } /** 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 81b7edf27..38f06f76e 100644 --- a/src/main/java/us/tastybento/bskyblock/commands/admin/AdminSchemCommand.java +++ b/src/main/java/us/tastybento/bskyblock/commands/admin/AdminSchemCommand.java @@ -1,6 +1,7 @@ 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; @@ -57,7 +58,6 @@ public class AdminSchemCommand extends CompositeCommand { return true; } catch (Exception e) { user.sendMessage("commands.admin.schem.could-not-load"); - e.printStackTrace(); return false; } } else { @@ -79,7 +79,12 @@ public class AdminSchemCommand extends CompositeCommand { if (args.size() == 2) { File file = new File(schemFolder, args.get(1)); user.sendMessage("general.success"); - cb.save(file); + try { + cb.save(file); + } catch (IOException e) { + user.sendRawMessage("Could not save!"); + return false; + } return true; } else { showHelp(this, user); 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 ffe5062f1..622ba59c6 100644 --- a/src/main/java/us/tastybento/bskyblock/island/builders/Clipboard.java +++ b/src/main/java/us/tastybento/bskyblock/island/builders/Clipboard.java @@ -53,6 +53,8 @@ public class Clipboard { UP } + private static final String ATTACHED = "attached"; + private YamlConfiguration blockConfig = new YamlConfiguration(); private Location pos1; private Location pos2; @@ -127,8 +129,8 @@ public class Clipboard { int z = location.getBlockZ() + Integer.valueOf(pos[2]); Material m = Material.getMaterial(s.getString("type")); Block block = location.getWorld().getBlockAt(x, y, z); - if (s.getBoolean("attached")) { - plugin.getLogger().info("Setting 1 tick later for " + m.toString()); + if (s.getBoolean(ATTACHED)) { + plugin.log("Setting 1 tick later for " + m.toString()); plugin.getServer().getScheduler().runTask(plugin, () -> setBlock(block, s, m)); } else { setBlock(block, s, m); @@ -139,7 +141,7 @@ public class Clipboard { private void setBlock(Block block, ConfigurationSection s, Material m) { // Block state - if (s.getBoolean("attached") && m.toString().contains("TORCH")) { + if (s.getBoolean(ATTACHED) && m.toString().contains("TORCH")) { TorchDir d = TorchDir.valueOf(s.getString("facing")); Block rel = block.getRelative(BlockFace.DOWN); @@ -271,7 +273,7 @@ public class Clipboard { Attachable facing = (Attachable)md; s.set("facing", facing.getFacing().name()); s.set("attached-face", facing.getAttachedFace().name()); - s.set("attached", true); + s.set(ATTACHED, true); } if (md instanceof Colorable) { Bukkit.getLogger().info("Colorable"); @@ -366,20 +368,14 @@ public class Clipboard { /** * Save the clipboard to a file * @param file + * @throws IOException */ - public void save(File file) { - try { + public void save(File file) throws IOException { getBlockConfig().save(file); zip(file); - - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - private void zip(File targetFile) { + private void zip(File targetFile) throws IOException { try (ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(targetFile.getAbsolutePath() + ".schem"))) { zipOutputStream.putNextEntry(new ZipEntry(targetFile.getName())); @@ -392,11 +388,10 @@ public class Clipboard { } inputStream.close(); zipOutputStream.close(); - targetFile.delete(); + if (!targetFile.delete()) { + throw new IOException("Could not delete temp file"); + } } - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); } } diff --git a/src/main/java/us/tastybento/bskyblock/island/builders/IslandBuilder.java b/src/main/java/us/tastybento/bskyblock/island/builders/IslandBuilder.java index 9fd4cd778..6d9fca47c 100644 --- a/src/main/java/us/tastybento/bskyblock/island/builders/IslandBuilder.java +++ b/src/main/java/us/tastybento/bskyblock/island/builders/IslandBuilder.java @@ -18,7 +18,6 @@ import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.ItemStack; import org.bukkit.material.Chest; -import us.tastybento.bskyblock.BSkyBlock; import us.tastybento.bskyblock.Constants; import us.tastybento.bskyblock.Constants.GameType; import us.tastybento.bskyblock.api.user.User; @@ -49,7 +48,7 @@ public class IslandBuilder { //TODO support companions? - public IslandBuilder(BSkyBlock plugin, Island island) { + public IslandBuilder(Island island) { this.island = island; world = island.getWorld(); } diff --git a/src/main/java/us/tastybento/bskyblock/managers/IslandWorldManager.java b/src/main/java/us/tastybento/bskyblock/managers/IslandWorldManager.java index da64f25c2..5b37f2b5a 100644 --- a/src/main/java/us/tastybento/bskyblock/managers/IslandWorldManager.java +++ b/src/main/java/us/tastybento/bskyblock/managers/IslandWorldManager.java @@ -148,7 +148,7 @@ public class IslandWorldManager { * @return Set of world names */ public Set getOverWorldNames() { - return worlds.entrySet().stream().filter(e -> e.getKey().getEnvironment().equals(Environment.NORMAL)).map(w -> w.getValue()).collect(Collectors.toSet()); + return worlds.entrySet().stream().filter(e -> e.getKey().getEnvironment().equals(Environment.NORMAL)).map(Map.Entry::getValue).collect(Collectors.toSet()); } /** @@ -159,7 +159,7 @@ public class IslandWorldManager { public Set getFreeOverWorldNames(User user) { return worlds.entrySet().stream().filter(e -> e.getKey().getEnvironment().equals(Environment.NORMAL)) .filter(w -> !plugin.getIslands().hasIsland(w.getKey(), user)) - .map(w -> w.getValue()).collect(Collectors.toSet()); + .map(Map.Entry::getValue).collect(Collectors.toSet()); } /** @@ -198,7 +198,7 @@ public class IslandWorldManager { * @return world, or null if it does not exist */ public World getWorld(String friendlyName) { - return worlds.entrySet().stream().filter(n -> n.getValue().equalsIgnoreCase(friendlyName)).map(e -> e.getKey()).findFirst().orElse(null); + return worlds.entrySet().stream().filter(n -> n.getValue().equalsIgnoreCase(friendlyName)).map(Map.Entry::getKey).findFirst().orElse(null); } /** @@ -366,7 +366,7 @@ public class IslandWorldManager { * @return nether world, or null if it does not exist */ public World getNetherWorld(World overWorld) { - return Bukkit.getWorld(overWorld.getName() + "_nether"); + return Bukkit.getWorld(overWorld.getName() + NETHER); } /** @@ -375,7 +375,7 @@ public class IslandWorldManager { * @return end world, or null if it does not exist */ public World getEndWorld(World overWorld) { - return Bukkit.getWorld(overWorld.getName() + "_the_end"); + return Bukkit.getWorld(overWorld.getName() + THE_END); } /** @@ -416,7 +416,7 @@ public class IslandWorldManager { * @return world, or null if not known */ public World getIslandWorld(String friendlyWorldName) { - return worlds.entrySet().stream().filter(e -> e.getValue().equalsIgnoreCase(friendlyWorldName)).findFirst().map(en -> en.getKey()).orElse(null); + return worlds.entrySet().stream().filter(e -> e.getValue().equalsIgnoreCase(friendlyWorldName)).findFirst().map(Map.Entry::getKey).orElse(null); } /** diff --git a/src/main/java/us/tastybento/bskyblock/managers/island/IslandGrid.java b/src/main/java/us/tastybento/bskyblock/managers/island/IslandGrid.java index a08c05eaf..f2124ff76 100644 --- a/src/main/java/us/tastybento/bskyblock/managers/island/IslandGrid.java +++ b/src/main/java/us/tastybento/bskyblock/managers/island/IslandGrid.java @@ -11,7 +11,7 @@ import us.tastybento.bskyblock.database.objects.Island; * */ public class IslandGrid { - private TreeMap> islandGrid = new TreeMap<>(); + private TreeMap> grid = new TreeMap<>(); /** * Adds island to grid @@ -19,20 +19,20 @@ public class IslandGrid { * @return true if successfully added, false if island already exists, or there is an overlap */ public boolean addToGrid(Island island) { - if (islandGrid.containsKey(island.getMinX())) { - TreeMap zEntry = islandGrid.get(island.getMinX()); + if (grid.containsKey(island.getMinX())) { + TreeMap zEntry = grid.get(island.getMinX()); if (zEntry.containsKey(island.getMinZ())) { return false; } else { // Add island zEntry.put(island.getMinZ(), island); - islandGrid.put(island.getMinX(), zEntry); + grid.put(island.getMinX(), zEntry); } } else { // Add island TreeMap zEntry = new TreeMap<>(); zEntry.put(island.getMinZ(), island); - islandGrid.put(island.getMinX(), zEntry); + grid.put(island.getMinX(), zEntry); } return true; } @@ -47,12 +47,12 @@ public class IslandGrid { if (island != null) { int x = island.getMinX(); int z = island.getMinZ(); - if (islandGrid.containsKey(x)) { - TreeMap zEntry = islandGrid.get(x); + if (grid.containsKey(x)) { + TreeMap zEntry = grid.get(x); if (zEntry.containsKey(z)) { // Island exists - delete it zEntry.remove(z); - islandGrid.put(x, zEntry); + grid.put(x, zEntry); return true; } } @@ -69,7 +69,7 @@ public class IslandGrid { * @return Island or null */ public Island getIslandAt(int x, int z) { - Entry> en = islandGrid.floorEntry(x); + Entry> en = grid.floorEntry(x); if (en != null) { Entry ent = en.getValue().floorEntry(z); if (ent != null) { diff --git a/src/main/java/us/tastybento/bskyblock/managers/island/NewIsland.java b/src/main/java/us/tastybento/bskyblock/managers/island/NewIsland.java index c4d6e049a..1183cf6d5 100644 --- a/src/main/java/us/tastybento/bskyblock/managers/island/NewIsland.java +++ b/src/main/java/us/tastybento/bskyblock/managers/island/NewIsland.java @@ -120,20 +120,20 @@ public class NewIsland { .build(); if (!event.isCancelled()) { // Create island - new IslandBuilder(plugin, island) + new IslandBuilder(island) .setPlayer(user.getPlayer()) .setChestItems(plugin.getSettings().getChestItems()) .setType(IslandType.ISLAND) .build(); if (plugin.getSettings().isNetherGenerate() && plugin.getSettings().isNetherIslands() && plugin.getIWM().getNetherWorld() != null) { - new IslandBuilder(plugin,island) + new IslandBuilder(island) .setPlayer(user.getPlayer()) .setChestItems(plugin.getSettings().getChestItems()) .setType(IslandType.NETHER) .build(); } if (plugin.getSettings().isEndGenerate() && plugin.getSettings().isEndIslands() && plugin.getIWM().getEndWorld() != null) { - new IslandBuilder(plugin,island) + new IslandBuilder(island) .setPlayer(user.getPlayer()) .setChestItems(plugin.getSettings().getChestItems()) .setType(IslandType.END) diff --git a/src/main/java/us/tastybento/bskyblock/util/Util.java b/src/main/java/us/tastybento/bskyblock/util/Util.java index b0d1b62c3..aef56aa23 100755 --- a/src/main/java/us/tastybento/bskyblock/util/Util.java +++ b/src/main/java/us/tastybento/bskyblock/util/Util.java @@ -23,12 +23,14 @@ import us.tastybento.bskyblock.api.user.User; /** * A set of utility methods * - * @author Tastybento + * @author tastybento * @author Poslovitch */ public class Util { private static final DecimalFormat df = new DecimalFormat("#.###"); + private static final String NETHER = "_nether"; + private static final String THE_END = "_the_end"; private static String serverVersion = null; private static BSkyBlock plugin = BSkyBlock.getInstance(); @@ -223,8 +225,8 @@ public class Util { * @return true if the same */ public static boolean sameWorld(World world, World world2) { - String worldName = world.getName().replaceAll("_nether", "").replaceAll("_the_end", ""); - String world2Name = world2.getName().replaceAll("_nether", "").replaceAll("_the_end", ""); + String worldName = world.getName().replaceAll(NETHER, "").replaceAll(THE_END, ""); + String world2Name = world2.getName().replaceAll(NETHER, "").replaceAll(THE_END, ""); return worldName.equalsIgnoreCase(world2Name); } @@ -234,7 +236,7 @@ public class Util { * @return over world */ public static World getWorld(World world) { - return world.getEnvironment().equals(Environment.NORMAL) ? world : Bukkit.getWorld(world.getName().replaceAll("_nether", "").replaceAll("_the_end", "")); + return world.getEnvironment().equals(Environment.NORMAL) ? world : Bukkit.getWorld(world.getName().replaceAll(NETHER, "").replaceAll(THE_END, "")); }