diff --git a/src/main/java/pw/yumc/YumCore/bukkit/compatible/C.java b/src/main/java/pw/yumc/YumCore/bukkit/compatible/C.java index 5cac378..9713a53 100644 --- a/src/main/java/pw/yumc/YumCore/bukkit/compatible/C.java +++ b/src/main/java/pw/yumc/YumCore/bukkit/compatible/C.java @@ -52,9 +52,9 @@ public class C { getHandle = typeCraftPlayer.getMethod("getHandle"); playerConnection = typeNMSPlayer.getField("playerConnection"); sendPacket = typePlayerConnection.getMethod("sendPacket", Class.forName(a("Packet"))); - } catch (final Throwable e) { + } catch (final Exception e) { Log.warning(C.class.getSimpleName() + " 兼容性工具初始化失败 可能造成部分功能不可用!"); - e.printStackTrace(); + Log.debug(e); } } @@ -225,7 +225,7 @@ public class C { } } // getOnlinePlayers end - } catch (NoSuchMethodException | SecurityException e) { + } catch (final Exception e) { Log.warning(Player.class.getSimpleName() + "兼容性工具初始化失败 可能造成部分功能不可用!"); } try { @@ -246,6 +246,7 @@ public class C { craftOfflinePlayerConstructor.setAccessible(true); // getOfflinePlayer end } catch (final Exception e) { + Log.debug(e); } } @@ -261,7 +262,7 @@ public class C { final Object gameProfile = gameProfileConstructor.newInstance(new Object[] { UUID.nameUUIDFromBytes(("OfflinePlayer:" + playerName).getBytes(Charsets.UTF_8)), playerName }); final Object offlinePlayer = craftOfflinePlayerConstructor.newInstance(new Object[] { Bukkit.getServer(), gameProfile }); return (OfflinePlayer) offlinePlayer; - } catch (final Throwable e) { + } catch (final Exception e) { return Bukkit.getOfflinePlayer(playerName); } } @@ -274,7 +275,7 @@ public class C { public static Collection getOnlinePlayers() { try { return Arrays.asList((org.bukkit.entity.Player[]) getOnlinePlayers.invoke(null)); - } catch (final Throwable e) { + } catch (final Exception e) { return Bukkit.getOnlinePlayers(); } } @@ -399,13 +400,13 @@ public class C { Object serialized = nmsChatSerializer.getMethod("a", String.class).invoke(null, "{\"text\":\"" + ChatColor.translateAlternateColorCodes('&', title) + "\"}"); packet = packetTitle.getConstructor(packetActions, nmsIChatBaseComponent).newInstance(actions[0], serialized); sendPacket.invoke(connection, packet); - if (subtitle != "") { + if (!"".equals(subtitle)) { // Send subtitle if present serialized = nmsChatSerializer.getMethod("a", String.class).invoke(null, "{\"text\":\"" + ChatColor.translateAlternateColorCodes('&', subtitle) + "\"}"); packet = packetTitle.getConstructor(packetActions, nmsIChatBaseComponent).newInstance(actions[1], serialized); sendPacket.invoke(connection, packet); } - } catch (final Throwable e) { + } catch (final Exception e) { e.printStackTrace(); } } diff --git a/src/main/java/pw/yumc/YumCore/statistic/Statistics.java b/src/main/java/pw/yumc/YumCore/statistic/Statistics.java index 606d398..93c0ae7 100644 --- a/src/main/java/pw/yumc/YumCore/statistic/Statistics.java +++ b/src/main/java/pw/yumc/YumCore/statistic/Statistics.java @@ -130,40 +130,37 @@ public class Statistics { * @param param * 请求参数 * @return 所代表远程资源的响应结果 + * @throws IOException */ - public static String postData(final String url, final String param) { + public static String postData(final String url, final String param) throws IOException { PrintWriter out = null; String result = ""; - try { - final URL realUrl = new URL(url); - // 打开和URL之间的连接 - final URLConnection conn = realUrl.openConnection(); - // 设置通用的请求属性 - conn.setRequestProperty("Accept", "*/*"); - conn.setRequestProperty("Connection", "Keep-Alive"); - conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36"); - // 设置超时时间 10秒 - conn.setReadTimeout(10000); - // 发送POST请求必须设置如下两行 - conn.setDoOutput(true); - conn.setDoInput(true); - // 获取URLConnection对象对应的输出流 - out = new PrintWriter(conn.getOutputStream()); - // 发送请求参数 - out.write(param); - // flush输出流的缓冲 - out.flush(); - String response = ""; - final BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), UTF_8)); - while ((response = reader.readLine()) != null) { - result += response; - } - reader.close(); - } catch (final Exception e) { - } finally { - if (out != null) { - out.close(); - } + final URL realUrl = new URL(url); + // 打开和URL之间的连接 + final URLConnection conn = realUrl.openConnection(); + // 设置通用的请求属性 + conn.setRequestProperty("Accept", "*/*"); + conn.setRequestProperty("Connection", "Keep-Alive"); + conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36"); + // 设置超时时间 10秒 + conn.setReadTimeout(10000); + // 发送POST请求必须设置如下两行 + conn.setDoOutput(true); + conn.setDoInput(true); + // 获取URLConnection对象对应的输出流 + out = new PrintWriter(conn.getOutputStream()); + // 发送请求参数 + out.write(param); + // flush输出流的缓冲 + out.flush(); + String response = ""; + final BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), UTF_8)); + while ((response = reader.readLine()) != null) { + result += response; + } + reader.close(); + if (out != null) { + out.close(); } return result; }