diff --git a/src/main/java/us/tastybento/bskyblock/Metrics.java b/src/main/java/us/tastybento/bskyblock/Metrics.java index 555cf2e5d..25be8f7ed 100755 --- a/src/main/java/us/tastybento/bskyblock/Metrics.java +++ b/src/main/java/us/tastybento/bskyblock/Metrics.java @@ -157,7 +157,7 @@ public class Metrics { } }); } - }, 1000*60*5, 1000*60*30); + }, 1000*60*5L, 1000*60*30L); // Submit the data every 30 minutes, first time after 5 minutes to give other plugins enough time to start // WARNING: Changing the frequency has no effect but your plugin WILL be blocked/deleted! // WARNING: Just don't do it! @@ -283,7 +283,7 @@ public class Metrics { // Compress the data to save bandwidth byte[] compressedData = compress(data.toString()); - + if (compressedData == null) throw new Exception(); // Add headers connection.setRequestMethod("POST"); connection.addRequestProperty("Accept", "application/json"); diff --git a/src/main/java/us/tastybento/bskyblock/api/addons/Addon.java b/src/main/java/us/tastybento/bskyblock/api/addons/Addon.java index b44d2544a..ac774c9fc 100644 --- a/src/main/java/us/tastybento/bskyblock/api/addons/Addon.java +++ b/src/main/java/us/tastybento/bskyblock/api/addons/Addon.java @@ -157,36 +157,32 @@ public abstract class Addon implements AddonInterface { } jarResource = jarResource.replace('\\', '/'); - InputStream in = null; - try { - JarFile jar = new JarFile(file); + try (JarFile jar = new JarFile(file)) { JarEntry config = jar.getJarEntry(jarResource); if (config != null) { - in = jar.getInputStream(config); + try (InputStream in = jar.getInputStream(config)) { + if (in == null) { + jar.close(); + throw new IllegalArgumentException("The embedded resource '" + jarResource + "' cannot be found in " + jar.getName()); + } + // There are two options, use the path of the resource or not + File outFile = new File(destinationFolder, jarResource); + if (noPath) { + outFile = new File(destinationFolder, outFile.getName()); + } + // Make any dirs that need to be made + outFile.getParentFile().mkdirs(); + if (DEBUG) { + Bukkit.getLogger().info("DEBUG: outFile = " + outFile.getAbsolutePath()); + Bukkit.getLogger().info("DEBUG: outFile name = " + outFile.getName()); + } + if (!outFile.exists() || replace) { + java.nio.file.Files.copy(in, outFile.toPath()); + } + } } - if (in == null) { - jar.close(); - throw new IllegalArgumentException("The embedded resource '" + jarResource + "' cannot be found in " + jar.getName()); - } - - // There are two options, use the path of the resource or not - File outFile = new File(destinationFolder, jarResource); - if (noPath) { - outFile = new File(destinationFolder, outFile.getName()); - } - // Make any dirs that need to be made - outFile.getParentFile().mkdirs(); - if (DEBUG) { - Bukkit.getLogger().info("DEBUG: outFile = " + outFile.getAbsolutePath()); - Bukkit.getLogger().info("DEBUG: outFile name = " + outFile.getName()); - } - if (!outFile.exists() || replace) { - java.nio.file.Files.copy(in, outFile.toPath()); - } - in.close(); - jar.close(); - } catch (IOException ex) { - ex.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); } } diff --git a/src/main/java/us/tastybento/bskyblock/api/addons/AddonClassLoader.java b/src/main/java/us/tastybento/bskyblock/api/addons/AddonClassLoader.java index e03df0d89..37f77b0ef 100644 --- a/src/main/java/us/tastybento/bskyblock/api/addons/AddonClassLoader.java +++ b/src/main/java/us/tastybento/bskyblock/api/addons/AddonClassLoader.java @@ -25,13 +25,17 @@ public class AddonClassLoader extends URLClassLoader { public Addon addon; private AddonsManager loader; - public AddonClassLoader(AddonsManager addonsManager, Mapdata, File path, BufferedReader reader, ClassLoader parent) throws InvalidAddonInheritException, MalformedURLException, InvalidAddonFormatException, InvalidDescriptionException { + public AddonClassLoader(AddonsManager addonsManager, Mapdata, File path, BufferedReader reader, ClassLoader parent) + throws InvalidAddonInheritException, + MalformedURLException, + InvalidAddonFormatException, + InvalidDescriptionException, + InstantiationException, + IllegalAccessException { super(new URL[]{path.toURI().toURL()}, parent); this.loader = addonsManager; - - Addon addon = null; - + Class javaClass = null; try { //Bukkit.getLogger().info("data " + data.get("main")); @@ -51,15 +55,11 @@ public class AddonClassLoader extends URLClassLoader { Class addonClass; try{ addonClass = javaClass.asSubclass(Addon.class); - }catch(ClassCastException e){ + } catch(ClassCastException e){ throw new InvalidAddonInheritException("Main class doesn't not extends super class 'Addon'"); } - try { - addon = addonClass.newInstance(); - } catch (InstantiationException | IllegalAccessException e) { - e.printStackTrace(); - } + Addon addon = addonClass.newInstance(); addon.setDescription(this.asDescription(data)); 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 a165aa1ae..2e080d2ab 100644 --- a/src/main/java/us/tastybento/bskyblock/api/commands/CompositeCommand.java +++ b/src/main/java/us/tastybento/bskyblock/api/commands/CompositeCommand.java @@ -195,8 +195,8 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi // get the subcommand corresponding to the arg if (subCommand.hasSubCommmands()) { if (DEBUG) - Bukkit.getLogger().info("DEBUG: This command has subcommands"); - if (subCommand.hasSubCommand(args[i])) { + Bukkit.getLogger().info("DEBUG: This command has subcommands"); + if (subCommand.getSubCommand(args[i]).isPresent()) { // Step down one subCommand = subCommand.getSubCommand(args[i]).get(); if (DEBUG) @@ -337,7 +337,7 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi * @param subCommand * @return true if this command has this sub command */ - private boolean hasSubCommand(String subCommand) { + protected boolean hasSubCommand(String subCommand) { return subCommands.containsKey(subCommand) || subCommandAliases.containsKey(subCommand); } diff --git a/src/main/java/us/tastybento/bskyblock/api/commands/DefaultHelpCommand.java b/src/main/java/us/tastybento/bskyblock/api/commands/DefaultHelpCommand.java index 1fa00bdd5..f11883ceb 100644 --- a/src/main/java/us/tastybento/bskyblock/api/commands/DefaultHelpCommand.java +++ b/src/main/java/us/tastybento/bskyblock/api/commands/DefaultHelpCommand.java @@ -74,14 +74,11 @@ public class DefaultHelpCommand extends CompositeCommand { // Ignore the help command if (!subCommand.getLabel().equals("help")) { // Every command should have help because every command has a default help - if (subCommand.getSubCommand("help").isPresent()) { - // This sub-sub command has a help, so use it - subCommand.getSubCommand("help").get().execute(user, Arrays.asList(String.valueOf(newDepth))); - } + subCommand.getSubCommand("help").ifPresent(sub -> execute(user, Arrays.asList(String.valueOf(newDepth)))); } } } - + if (depth == 0) { user.sendMessage("commands.help.end"); } diff --git a/src/main/java/us/tastybento/bskyblock/commands/AdminCommand.java b/src/main/java/us/tastybento/bskyblock/commands/AdminCommand.java index c03f35d96..8e77600a8 100755 --- a/src/main/java/us/tastybento/bskyblock/commands/AdminCommand.java +++ b/src/main/java/us/tastybento/bskyblock/commands/AdminCommand.java @@ -27,7 +27,8 @@ public class AdminCommand extends CompositeCommand { @Override public boolean execute(User user, List args) { - return this.getSubCommand("help").get().execute(user, args); + // By default run the attached help command, if it exists (it should) + return this.getSubCommand("help").map(help -> execute(user, args)).orElse(false); } } diff --git a/src/main/java/us/tastybento/bskyblock/commands/IslandCommand.java b/src/main/java/us/tastybento/bskyblock/commands/IslandCommand.java index 45d0e19f9..612da5d0e 100755 --- a/src/main/java/us/tastybento/bskyblock/commands/IslandCommand.java +++ b/src/main/java/us/tastybento/bskyblock/commands/IslandCommand.java @@ -43,11 +43,12 @@ public class IslandCommand extends CompositeCommand { @Override public boolean execute(User user, List args) { + // If this player does not have an island, create one if (!getPlugin().getIslands().hasIsland(user.getUniqueId())) { - return this.getSubCommand("create").get().execute(user, args); + return this.getSubCommand("create").map(command -> execute(user, args)).orElse(false); } - // Currently, just go home - return this.getSubCommand("go").get().execute(user, args); + // Otherwise, currently, just go home + return this.getSubCommand("go").map(command -> execute(user, args)).orElse(false); } } diff --git a/src/main/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamInviteCommand.java b/src/main/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamInviteCommand.java index e75808dd7..6574e76a2 100644 --- a/src/main/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamInviteCommand.java +++ b/src/main/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamInviteCommand.java @@ -49,10 +49,10 @@ public class IslandTeamInviteCommand extends AbstractIslandTeamCommand { if (inviteList.containsKey(playerUUID)) { OfflinePlayer inviter = getPlugin().getServer().getOfflinePlayer(inviteList.get(playerUUID)); user.sendMessage("commands.island.team.invite.name-has-invited-you", "[name]", inviter.getName()); - } else { - this.getSubCommand("help").get().execute(user, args); + return true; } - return true; + // Show help + return this.getSubCommand("help").map(command -> execute(user, args)).orElse(false); } else { // Only online players can be invited UUID invitedPlayerUUID = getPlayers().getUUID(args.get(0)); diff --git a/src/main/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamKickCommand.java b/src/main/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamKickCommand.java index f3316846f..ce1e49c1e 100644 --- a/src/main/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamKickCommand.java +++ b/src/main/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamKickCommand.java @@ -40,8 +40,7 @@ public class IslandTeamKickCommand extends AbstractIslandTeamCommand { } // If args are not right, show help if (args.size() != 1) { - this.getSubCommand("help").get().execute(user, new ArrayList<>()); - return true; + return this.getSubCommand("help").map(command -> execute(user, new ArrayList<>())).orElse(false); } // Get target UUID targetUUID = getPlayers().getUUID(args.get(0)); diff --git a/src/main/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamSetownerCommand.java b/src/main/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamSetownerCommand.java index 3a5ebc9ce..47f866b2a 100644 --- a/src/main/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamSetownerCommand.java +++ b/src/main/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamSetownerCommand.java @@ -41,8 +41,7 @@ public class IslandTeamSetownerCommand extends AbstractIslandTeamCommand { } // If args are not right, show help if (args.size() != 1) { - this.getSubCommand("help").get().execute(user, new ArrayList<>()); - return true; + return this.getSubCommand("help").map(command -> execute(user, new ArrayList<>())).orElse(false); } //getPlugin().getLogger().info("DEBUG: arg[0] = " + args.get(0)); UUID targetUUID = getPlayers().getUUID(args.get(0)); diff --git a/src/main/java/us/tastybento/bskyblock/managers/AddonsManager.java b/src/main/java/us/tastybento/bskyblock/managers/AddonsManager.java index 898db2123..e93718315 100644 --- a/src/main/java/us/tastybento/bskyblock/managers/AddonsManager.java +++ b/src/main/java/us/tastybento/bskyblock/managers/AddonsManager.java @@ -148,7 +148,7 @@ public final class AddonsManager { // Close the jar jar.close(); - } catch (IOException e) { + } catch (Exception e) { if (DEBUG) { plugin.getLogger().info(f.getName() + "is not a jarfile, ignoring..."); }