From 9e3a3e35f1d668c70919c858ebc2425746c55a34 Mon Sep 17 00:00:00 2001 From: NichtStudioCode <51272202+NichtStudioCode@users.noreply.github.com> Date: Sat, 18 Jun 2022 12:46:19 +0200 Subject: [PATCH] Check http response code in ForceResourcePack --- .../de/studiocode/invui/util/DataUtils.java | 22 ++++++++++------- .../invui/resourcepack/ForceResourcePack.java | 24 +++++++++++-------- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/InvUI/src/main/java/de/studiocode/invui/util/DataUtils.java b/InvUI/src/main/java/de/studiocode/invui/util/DataUtils.java index d63c347..1a2d247 100644 --- a/InvUI/src/main/java/de/studiocode/invui/util/DataUtils.java +++ b/InvUI/src/main/java/de/studiocode/invui/util/DataUtils.java @@ -77,15 +77,21 @@ public class DataUtils { return array2d; } - public static byte[] createSha1Hash(InputStream in) throws NoSuchAlgorithmException, IOException { - MessageDigest md = MessageDigest.getInstance("SHA1"); - int len; - byte[] buffer = new byte[4096]; - while ((len = in.read(buffer)) != -1) { - md.update(buffer, 0, len); + public static byte[] createSha1Hash(InputStream in) throws IOException { + try { + MessageDigest md = MessageDigest.getInstance("SHA1"); + int len; + byte[] buffer = new byte[4096]; + while ((len = in.read(buffer)) != -1) { + md.update(buffer, 0, len); + } + in.close(); + return md.digest(); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); } - in.close(); - return md.digest(); + + return null; } } diff --git a/ResourcePack/src/main/java/de/studiocode/invui/resourcepack/ForceResourcePack.java b/ResourcePack/src/main/java/de/studiocode/invui/resourcepack/ForceResourcePack.java index 05aaa2e..57e3282 100644 --- a/ResourcePack/src/main/java/de/studiocode/invui/resourcepack/ForceResourcePack.java +++ b/ResourcePack/src/main/java/de/studiocode/invui/resourcepack/ForceResourcePack.java @@ -18,8 +18,8 @@ import org.bukkit.scheduler.BukkitTask; import org.jetbrains.annotations.Nullable; import java.io.IOException; +import java.net.HttpURLConnection; import java.net.URL; -import java.security.NoSuchAlgorithmException; import java.util.HashMap; import static org.bukkit.event.player.PlayerResourcePackStatusEvent.Status.DECLINED; @@ -64,8 +64,9 @@ public class ForceResourcePack implements Listener { * * @param resourcePackUrl The ResourcePack URL String * @param prompt The prompt to be displayed (since 1.17) + * @throws IOException If the connection was not successful */ - public void setResourcePack(@Nullable String resourcePackUrl, @Nullable ComponentWrapper prompt) { + public void setResourcePack(@Nullable String resourcePackUrl, @Nullable ComponentWrapper prompt) throws IOException { setResourcePack(resourcePackUrl, prompt, true); } @@ -75,8 +76,9 @@ public class ForceResourcePack implements Listener { * * @param resourcePackUrl The ResourcePack URL String * @param prompt The prompt to be displayed (since 1.17) + * @throws IOException If the connection was not successful */ - public void setResourcePack(@Nullable String resourcePackUrl, @Nullable BaseComponent[] prompt) { + public void setResourcePack(@Nullable String resourcePackUrl, @Nullable BaseComponent[] prompt) throws IOException { setResourcePack(resourcePackUrl, new BaseComponentWrapper(prompt), true); } @@ -87,18 +89,19 @@ public class ForceResourcePack implements Listener { * @param resourcePackUrl The ResourcePack URL String * @param prompt The prompt to be displayed (since 1.17) * @param sendToOnlinePlayers If the resource pack should also be sent to all currently online players + * @throws IOException If the connection was not successful */ - public void setResourcePack(@Nullable String resourcePackUrl, @Nullable ComponentWrapper prompt, boolean sendToOnlinePlayers) { + public void setResourcePack(@Nullable String resourcePackUrl, @Nullable ComponentWrapper prompt, boolean sendToOnlinePlayers) throws IOException { this.prompt = prompt; if (resourcePackUrl != null) { - try { - URL url = new URL(resourcePackUrl); + URL url = new URL(resourcePackUrl); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + int response = connection.getResponseCode(); + if (response >= 200 && response < 300) { hash = DataUtils.createSha1Hash(url.openStream()); this.resourcePackUrl = resourcePackUrl; - } catch (IOException | NoSuchAlgorithmException e) { - e.printStackTrace(); - } + } else throw new IOException("Service returned response code " + response); if (sendToOnlinePlayers) Bukkit.getOnlinePlayers().forEach(this::sendResourcePack); } else { @@ -113,8 +116,9 @@ public class ForceResourcePack implements Listener { * @param resourcePackUrl The ResourcePack URL String * @param prompt The prompt to be displayed (since 1.17) * @param sendToOnlinePlayers If the resource pack should also be sent to all currently online players + * @throws IOException If the connection was not successful */ - public void setResourcePack(@Nullable String resourcePackUrl, @Nullable BaseComponent[] prompt, boolean sendToOnlinePlayers) { + public void setResourcePack(@Nullable String resourcePackUrl, @Nullable BaseComponent[] prompt, boolean sendToOnlinePlayers) throws IOException { setResourcePack(resourcePackUrl, new BaseComponentWrapper(prompt), sendToOnlinePlayers); }