From 4fe0c586d9f62777f9690493820bb91e6ddc04f3 Mon Sep 17 00:00:00 2001 From: BlockyTheDev <86119630+BlockyTheDev@users.noreply.github.com> Date: Sun, 1 Oct 2023 11:50:12 +0200 Subject: [PATCH] Address deprecated URL instantiation (#4178) Fixes https://github.com/IntellectualSites/PlotSquared/issues/4166 --- .../com/plotsquared/bukkit/util/UpdateUtility.java | 5 +++-- .../main/java/com/plotsquared/core/PlotSquared.java | 9 +++++---- .../main/java/com/plotsquared/core/command/Load.java | 3 ++- .../com/plotsquared/core/command/SchematicCmd.java | 4 ++-- .../com/plotsquared/core/util/SchematicHandler.java | 12 +++++++----- 5 files changed, 19 insertions(+), 14 deletions(-) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/util/UpdateUtility.java b/Bukkit/src/main/java/com/plotsquared/bukkit/util/UpdateUtility.java index 923ba65ab..2cffa291b 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/util/UpdateUtility.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/util/UpdateUtility.java @@ -35,7 +35,7 @@ import org.bukkit.scheduler.BukkitTask; import javax.net.ssl.HttpsURLConnection; import java.io.IOException; import java.io.InputStreamReader; -import java.net.URL; +import java.net.URI; public class UpdateUtility implements Listener { @@ -59,8 +59,9 @@ public class UpdateUtility implements Listener { public void updateChecker() { task = Bukkit.getScheduler().runTaskTimerAsynchronously(this.javaPlugin, () -> { try { - HttpsURLConnection connection = (HttpsURLConnection) new URL( + HttpsURLConnection connection = (HttpsURLConnection) URI.create( "https://api.spigotmc.org/simple/0.2/index.php?action=getResource&id=77506") + .toURL() .openConnection(); connection.setRequestMethod("GET"); JsonObject result = new JsonParser() diff --git a/Core/src/main/java/com/plotsquared/core/PlotSquared.java b/Core/src/main/java/com/plotsquared/core/PlotSquared.java index 580fe35b1..702de9e68 100644 --- a/Core/src/main/java/com/plotsquared/core/PlotSquared.java +++ b/Core/src/main/java/com/plotsquared/core/PlotSquared.java @@ -84,7 +84,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; -import java.net.MalformedURLException; +import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import java.nio.file.Files; @@ -210,9 +210,10 @@ public class PlotSquared { try { URL logurl = PlotSquared.class.getProtectionDomain().getCodeSource().getLocation(); this.jarFile = new File( - new URL(logurl.toURI().toString().split("\\!")[0].replaceAll("jar:file", "file")) - .toURI().getPath()); - } catch (MalformedURLException | URISyntaxException | SecurityException e) { + URI.create( + logurl.toURI().toString().split("\\!")[0].replaceAll("jar:file", "file")) + .getPath()); + } catch (URISyntaxException | SecurityException e) { e.printStackTrace(); this.jarFile = new File(this.platform.getDirectory().getParentFile(), "PlotSquared.jar"); if (!this.jarFile.exists()) { diff --git a/Core/src/main/java/com/plotsquared/core/command/Load.java b/Core/src/main/java/com/plotsquared/core/command/Load.java index 4ead54146..14c700d4c 100644 --- a/Core/src/main/java/com/plotsquared/core/command/Load.java +++ b/Core/src/main/java/com/plotsquared/core/command/Load.java @@ -41,6 +41,7 @@ import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import org.checkerframework.checker.nullness.qual.NonNull; import java.net.MalformedURLException; +import java.net.URI; import java.net.URL; import java.util.Collections; import java.util.List; @@ -116,7 +117,7 @@ public class Load extends SubCommand { } final URL url; try { - url = new URL(Settings.Web.URL + "saves/" + player.getUUID() + '/' + schematic); + url = URI.create(Settings.Web.URL + "saves/" + player.getUUID() + '/' + schematic).toURL(); } catch (MalformedURLException e) { e.printStackTrace(); player.sendMessage(TranslatableCaption.of("web.load_failed")); diff --git a/Core/src/main/java/com/plotsquared/core/command/SchematicCmd.java b/Core/src/main/java/com/plotsquared/core/command/SchematicCmd.java index 613f2eec7..a92f06c5a 100644 --- a/Core/src/main/java/com/plotsquared/core/command/SchematicCmd.java +++ b/Core/src/main/java/com/plotsquared/core/command/SchematicCmd.java @@ -40,6 +40,7 @@ import net.kyori.adventure.text.minimessage.tag.Tag; import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import org.checkerframework.checker.nullness.qual.NonNull; +import java.net.URI; import java.net.URL; import java.util.ArrayList; import java.util.Collection; @@ -130,8 +131,7 @@ public class SchematicCmd extends SubCommand { if (location.startsWith("url:")) { try { UUID uuid = UUID.fromString(location.substring(4)); - URL base = new URL(Settings.Web.URL); - URL url = new URL(base, "uploads/" + uuid + ".schematic"); + URL url = URI.create(Settings.Web.URL + "uploads/" + uuid + ".schematic").toURL(); schematic = this.schematicHandler.getSchematic(url); } catch (Exception e) { e.printStackTrace(); diff --git a/Core/src/main/java/com/plotsquared/core/util/SchematicHandler.java b/Core/src/main/java/com/plotsquared/core/util/SchematicHandler.java index c3f75e649..93e885608 100644 --- a/Core/src/main/java/com/plotsquared/core/util/SchematicHandler.java +++ b/Core/src/main/java/com/plotsquared/core/util/SchematicHandler.java @@ -83,6 +83,7 @@ import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.HttpURLConnection; import java.net.MalformedURLException; +import java.net.URI; import java.net.URL; import java.net.URLConnection; import java.nio.channels.Channels; @@ -144,7 +145,7 @@ public abstract class SchematicHandler { } final URL url; try { - url = new URL(Settings.Web.URL + "?key=" + uuid + "&type=" + extension); + url = URI.create(Settings.Web.URL + "?key=" + uuid + "&type=" + extension).toURL(); } catch (MalformedURLException e) { e.printStackTrace(); whenDone.run(); @@ -153,7 +154,7 @@ public abstract class SchematicHandler { TaskManager.runTaskAsync(() -> { try { String boundary = Long.toHexString(System.currentTimeMillis()); - URLConnection con = new URL(website).openConnection(); + URLConnection con = URI.create(website).toURL().openConnection(); con.setDoOutput(true); con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); try (OutputStream output = con.getOutputStream(); @@ -498,9 +499,10 @@ public abstract class SchematicHandler { public List getSaves(UUID uuid) { String rawJSON; try { - String website = Settings.Web.URL + "list.php?" + uuid.toString(); - URL url = new URL(website); - URLConnection connection = new URL(url.toString()).openConnection(); + URLConnection connection = URI.create( + Settings.Web.URL + "list.php?" + uuid.toString()) + .toURL() + .openConnection(); connection.setRequestProperty("User-Agent", "Mozilla/5.0"); try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { rawJSON = reader.lines().collect(Collectors.joining());