From 2eb46b0c2c5f92376ec6468fb15edd172967f39b Mon Sep 17 00:00:00 2001 From: Florian CUNY Date: Sat, 15 Sep 2018 11:37:56 +0200 Subject: [PATCH] Fixed some code smells --- .../bentobox/database/AbstractDatabaseHandler.java | 10 ++-------- .../bentobox/database/flatfile/ConfigHandler.java | 2 +- .../database/flatfile/FlatFileDatabaseHandler.java | 6 +++--- .../bentobox/database/mysql/MySQLDatabaseHandler.java | 3 +-- .../bentobox/listeners/flags/BreakBlocksListener.java | 11 ++++++----- .../bentobox/bentobox/managers/AddonsManager.java | 5 ++--- 6 files changed, 15 insertions(+), 22 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/database/AbstractDatabaseHandler.java b/src/main/java/world/bentobox/bentobox/database/AbstractDatabaseHandler.java index eeab806d5..4951dd73d 100644 --- a/src/main/java/world/bentobox/bentobox/database/AbstractDatabaseHandler.java +++ b/src/main/java/world/bentobox/bentobox/database/AbstractDatabaseHandler.java @@ -48,22 +48,16 @@ public abstract class AbstractDatabaseHandler { /** * Loads all the records in this table and returns a list of them * @return list of - * @throws SecurityException - * @throws NoSuchMethodException - * @throws IllegalArgumentException */ - public abstract List loadObjects() throws InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException, IntrospectionException, IllegalArgumentException, NoSuchMethodException, SecurityException; + public abstract List loadObjects() throws InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException, IntrospectionException, NoSuchMethodException; /** * Creates a filled with values from the corresponding * database file * @param uniqueId - unique ID * @return - * @throws SecurityException - * @throws NoSuchMethodException - * @throws IllegalArgumentException */ - public abstract T loadObject(String uniqueId) throws InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException, IntrospectionException, IllegalArgumentException, NoSuchMethodException, SecurityException; + public abstract T loadObject(String uniqueId) throws InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException, IntrospectionException, NoSuchMethodException; /** * Save T into the corresponding database diff --git a/src/main/java/world/bentobox/bentobox/database/flatfile/ConfigHandler.java b/src/main/java/world/bentobox/bentobox/database/flatfile/ConfigHandler.java index 7fe13201e..f3ca2ab2c 100644 --- a/src/main/java/world/bentobox/bentobox/database/flatfile/ConfigHandler.java +++ b/src/main/java/world/bentobox/bentobox/database/flatfile/ConfigHandler.java @@ -25,7 +25,7 @@ public class ConfigHandler extends FlatFileDatabaseHandler { saveObject(instance); } - public T loadSettings(String uniqueId, T dbConfig) throws InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException, IntrospectionException, IllegalArgumentException, NoSuchMethodException, SecurityException { + public T loadSettings(String uniqueId, T dbConfig) throws InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException, IntrospectionException, NoSuchMethodException { if (dbConfig == null) { return loadObject(uniqueId); } diff --git a/src/main/java/world/bentobox/bentobox/database/flatfile/FlatFileDatabaseHandler.java b/src/main/java/world/bentobox/bentobox/database/flatfile/FlatFileDatabaseHandler.java index 1bf91cb29..620a77726 100644 --- a/src/main/java/world/bentobox/bentobox/database/flatfile/FlatFileDatabaseHandler.java +++ b/src/main/java/world/bentobox/bentobox/database/flatfile/FlatFileDatabaseHandler.java @@ -72,7 +72,7 @@ public class FlatFileDatabaseHandler extends AbstractDatabaseHandler { * @see world.bentobox.bentobox.database.AbstractDatabaseHandler#loadObject(java.lang.String) */ @Override - public T loadObject(String key) throws InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException, IntrospectionException, IllegalArgumentException, NoSuchMethodException, SecurityException { + public T loadObject(String key) throws InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException, IntrospectionException, NoSuchMethodException { // Objects are loaded from a folder named after the simple name of the class being stored String path = DATABASE_FOLDER_NAME + File.separator + dataObject.getSimpleName(); // This path and key can be overridden by the StoreAt annotation in the code @@ -97,7 +97,7 @@ public class FlatFileDatabaseHandler extends AbstractDatabaseHandler { * @see world.bentobox.bentobox.database.AbstractDatabaseHandler#loadObjects() */ @Override - public List loadObjects() throws InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException, IntrospectionException, IllegalArgumentException, NoSuchMethodException, SecurityException { + public List loadObjects() throws InstantiationException, IllegalAccessException, InvocationTargetException, ClassNotFoundException, IntrospectionException, NoSuchMethodException { // In this case, all the objects of a specific type are being loaded. List list = new ArrayList<>(); // Look for any files that end in .yml in the folder @@ -139,7 +139,7 @@ public class FlatFileDatabaseHandler extends AbstractDatabaseHandler { * @throws NoSuchMethodException * @throws IllegalArgumentException */ - private T createObject(YamlConfiguration config) throws InstantiationException, IllegalAccessException, IntrospectionException, InvocationTargetException, ClassNotFoundException, IllegalArgumentException, NoSuchMethodException, SecurityException { + private T createObject(YamlConfiguration config) throws InstantiationException, IllegalAccessException, IntrospectionException, InvocationTargetException, ClassNotFoundException, NoSuchMethodException { // Create a new instance of the dataObject of type T (which can be any class) T instance = dataObject.getDeclaredConstructor().newInstance(); diff --git a/src/main/java/world/bentobox/bentobox/database/mysql/MySQLDatabaseHandler.java b/src/main/java/world/bentobox/bentobox/database/mysql/MySQLDatabaseHandler.java index c9bec3332..72627fd7e 100644 --- a/src/main/java/world/bentobox/bentobox/database/mysql/MySQLDatabaseHandler.java +++ b/src/main/java/world/bentobox/bentobox/database/mysql/MySQLDatabaseHandler.java @@ -185,8 +185,7 @@ public class MySQLDatabaseHandler extends AbstractDatabaseHandler { preparedStatement.setString(1, "\"" + uniqueId + "\""); try (ResultSet resultSet = preparedStatement.executeQuery()) { if (resultSet.next()) { - boolean result = resultSet.getBoolean(1); - return result; + return resultSet.getBoolean(1); } } } catch (SQLException e) { diff --git a/src/main/java/world/bentobox/bentobox/listeners/flags/BreakBlocksListener.java b/src/main/java/world/bentobox/bentobox/listeners/flags/BreakBlocksListener.java index 87626467c..48f8cfb93 100644 --- a/src/main/java/world/bentobox/bentobox/listeners/flags/BreakBlocksListener.java +++ b/src/main/java/world/bentobox/bentobox/listeners/flags/BreakBlocksListener.java @@ -58,16 +58,17 @@ public class BreakBlocksListener extends AbstractFlagListener { // Look along player's sight line to see if any blocks are skulls try { - BlockIterator iter = new BlockIterator(e.getPlayer(), 10); - Block lastBlock = iter.next(); - while (iter.hasNext()) { - lastBlock = iter.next(); + BlockIterator iterator = new BlockIterator(e.getPlayer(), 10); + while (iterator.hasNext()) { + Block lastBlock = iterator.next(); if (lastBlock.getType().toString().endsWith("_SKULL") || (lastBlock.getType().toString().endsWith("_HEAD") && !lastBlock.getType().equals(Material.PISTON_HEAD))) { checkIsland(e, lastBlock.getLocation(), Flags.BREAK_BLOCKS); return; } } - } catch (Exception ignored) {} + } catch (Exception ignored) { + // We can ignore this exception + } switch (e.getClickedBlock().getType()) { case CAKE: diff --git a/src/main/java/world/bentobox/bentobox/managers/AddonsManager.java b/src/main/java/world/bentobox/bentobox/managers/AddonsManager.java index 1af1a2036..87274e1cf 100644 --- a/src/main/java/world/bentobox/bentobox/managers/AddonsManager.java +++ b/src/main/java/world/bentobox/bentobox/managers/AddonsManager.java @@ -233,12 +233,11 @@ public class AddonsManager { } // Load dependencies or soft dependencies Map sortedAddons = new LinkedHashMap<>(); - List remaining = new ArrayList<>(); // Start with nodes with no dependencies addons.stream().filter(a -> a.getDescription().getDependencies().isEmpty() && a.getDescription().getSoftDependencies().isEmpty()) .forEach(a -> sortedAddons.put(a.getDescription().getName(), a)); // Fill remaining - remaining = addons.stream().filter(a -> !sortedAddons.containsKey(a.getDescription().getName())).collect(Collectors.toList()); + List remaining = addons.stream().filter(a -> !sortedAddons.containsKey(a.getDescription().getName())).collect(Collectors.toList()); // Run through remaining addons int index = 0; @@ -257,7 +256,7 @@ public class AddonsManager { depIt.remove(); } } - if (deps.stream().allMatch(s -> !a.getDescription().getDependencies().contains(s))) { + if (deps.stream().noneMatch(s -> a.getDescription().getDependencies().contains(s))) { // Add addons loaded sortedAddons.put(a.getDescription().getName(), a); it.remove();