diff --git a/src/main/java/fr/xephi/authme/AuthMe.java b/src/main/java/fr/xephi/authme/AuthMe.java index 3b8a53dab..537cd1c32 100644 --- a/src/main/java/fr/xephi/authme/AuthMe.java +++ b/src/main/java/fr/xephi/authme/AuthMe.java @@ -25,6 +25,7 @@ import fr.xephi.authme.settings.OtherAccounts; import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.Spawn; import fr.xephi.authme.util.GeoLiteAPI; +import fr.xephi.authme.util.StringUtils; import fr.xephi.authme.util.Utils; import net.minelink.ctplus.CombatTagPlus; import org.apache.logging.log4j.LogManager; @@ -42,6 +43,7 @@ import org.mcstats.Metrics; import org.mcstats.Metrics.Graph; import java.io.BufferedReader; +import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; @@ -905,16 +907,23 @@ public class AuthMe extends JavaPlugin { public String getVeryGamesIp(Player player) { String realIP = player.getAddress().getAddress().getHostAddress(); String sUrl = "http://monitor-1.verygames.net/api/?action=ipclean-real-ip&out=raw&ip=%IP%&port=%PORT%"; - sUrl = sUrl.replace("%IP%", player.getAddress().getAddress().getHostAddress()).replace("%PORT%", "" + player.getAddress().getPort()); + sUrl = sUrl.replace("%IP%", player.getAddress().getAddress().getHostAddress()) + .replace("%PORT%", "" + player.getAddress().getPort()); try { URL url = new URL(sUrl); URLConnection urlCon = url.openConnection(); - BufferedReader in = new BufferedReader(new InputStreamReader(urlCon.getInputStream())); - String inputLine = in.readLine(); - if (inputLine != null && !inputLine.isEmpty() && !inputLine.equalsIgnoreCase("error") && !inputLine.contains("error")) { - realIP = inputLine; + try (BufferedReader in = new BufferedReader(new InputStreamReader(urlCon.getInputStream()))) { + String inputLine = in.readLine(); + if (!StringUtils.isEmpty(inputLine) && !inputLine.equalsIgnoreCase("error") + && !inputLine.contains("error")) { + realIP = inputLine; + } + } catch (IOException e) { + ConsoleLogger.showError("Could not read from Very Games API - " + StringUtils.formatException(e)); } - } catch (Exception ignored) { + } catch (IOException e) { + ConsoleLogger.showError("Could not fetch Very Games API with URL '" + sUrl + "' - " + + StringUtils.formatException(e)); } return realIP; } diff --git a/src/main/java/fr/xephi/authme/converter/vAuthFileReader.java b/src/main/java/fr/xephi/authme/converter/vAuthFileReader.java index 1ab969ea5..3546a547c 100644 --- a/src/main/java/fr/xephi/authme/converter/vAuthFileReader.java +++ b/src/main/java/fr/xephi/authme/converter/vAuthFileReader.java @@ -33,11 +33,6 @@ public class vAuthFileReader { this.sender = sender; } - /** - * Method convert. - * - * @throws IOException - */ public void convert() { final File file = new File(plugin.getDataFolder().getParent() + "" + File.separator + "vAuth" + File.separator + "passwords.yml"); Scanner scanner; @@ -63,6 +58,7 @@ public class vAuthFileReader { } database.saveAuth(auth); } + scanner.close(); } catch (Exception e) { ConsoleLogger.writeStackTrace(e); } diff --git a/src/main/java/fr/xephi/authme/util/StringUtils.java b/src/main/java/fr/xephi/authme/util/StringUtils.java index b0328a45c..395e4db1b 100644 --- a/src/main/java/fr/xephi/authme/util/StringUtils.java +++ b/src/main/java/fr/xephi/authme/util/StringUtils.java @@ -107,4 +107,15 @@ public class StringUtils { // Return the result as a string return stringWriter.toString(); } + /** + * Format the information from a Throwable as string, retaining the type and its message. + * + * @param th The throwable to process + * + * @return String with the type of the Throwable and its message, e.g. "[IOException]: Could not open stream" + */ + public static String formatException(Throwable th) { + return "[" + th.getClass().getSimpleName() + "]: " + th.getMessage(); + } + } diff --git a/src/test/java/fr/xephi/authme/util/StringUtilsTest.java b/src/test/java/fr/xephi/authme/util/StringUtilsTest.java index 51cd96776..47d183979 100644 --- a/src/test/java/fr/xephi/authme/util/StringUtilsTest.java +++ b/src/test/java/fr/xephi/authme/util/StringUtilsTest.java @@ -2,6 +2,7 @@ package fr.xephi.authme.util; import org.junit.Test; +import java.net.MalformedURLException; import java.util.Arrays; import java.util.List; @@ -84,4 +85,16 @@ public class StringUtilsTest { // then assertThat(result, equalTo("hello")); } + + @Test + public void shouldFormatException() { + // given + MalformedURLException ex = new MalformedURLException("Unrecognized URL format"); + + // when + String result = StringUtils.formatException(ex); + + // then + assertThat(result, equalTo("[MalformedURLException]: Unrecognized URL format")); + } }