From 64a87a59d85c738be1defe0ec09bf377d5dd7f2f Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+chomens@users.noreply.github.com> Date: Sun, 5 Jan 2025 14:42:25 +0700 Subject: [PATCH] refactor: some stuff to the argument types (also fixes URL class deprecation) --- .../arguments/LocationArgumentType.java | 18 ++++++++++-------- .../arguments/TimestampArgumentType.java | 2 +- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/main/java/land/chipmunk/chipmunkmod/command/arguments/LocationArgumentType.java b/src/main/java/land/chipmunk/chipmunkmod/command/arguments/LocationArgumentType.java index d6aeffb..c1b8b80 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/command/arguments/LocationArgumentType.java +++ b/src/main/java/land/chipmunk/chipmunkmod/command/arguments/LocationArgumentType.java @@ -8,19 +8,21 @@ import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; import net.minecraft.text.Text; import java.net.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; import java.net.URL; import java.nio.file.Path; import java.util.Arrays; import java.util.Collection; public class LocationArgumentType implements ArgumentType { - private static final Collection EXAMPLES = Arrays.asList("songs/amogus.mid", "images/cat.jpg", "videos/badapple.mp4"); + private static final Collection EXAMPLES = Arrays.asList("songs/amogus.mid", "images/cat.jpg", "videos/badapple.mp4"); private static final SimpleCommandExceptionType OOB_FILEPATH = new SimpleCommandExceptionType(Text.translatable("The specified file path is outside of the allowed directory")); - private boolean allowsUrls = false; - private boolean allowsPaths = false; - private Path root; + private final boolean allowsUrls; + private final boolean allowsPaths; + private final Path root; private LocationArgumentType (boolean allowsUrls, boolean allowsPaths, Path root) { this.allowsUrls = allowsUrls; @@ -49,8 +51,8 @@ public class LocationArgumentType implements ArgumentType { } try { - return new URL(sb.toString()); - } catch (MalformedURLException exception) { + return new URI(sb.toString()).toURL(); + } catch (MalformedURLException | URISyntaxException exception) { throw new SimpleCommandExceptionType(Text.literal(exception.getMessage())).create(); } } @@ -70,8 +72,8 @@ public class LocationArgumentType implements ArgumentType { final Object location = getLocation(context, name); if (location instanceof URL) return (URL) location; try { - if (location instanceof Path) return new URL("file", "", -1, location.toString()); - } catch (MalformedURLException ignored) { + if (location instanceof Path) return new URI("file", "", "", -1, location.toString(), "", "").toURL(); + } catch (MalformedURLException | URISyntaxException ignored) { return null; // The real question is whether this will actually ever get called } return null; diff --git a/src/main/java/land/chipmunk/chipmunkmod/command/arguments/TimestampArgumentType.java b/src/main/java/land/chipmunk/chipmunkmod/command/arguments/TimestampArgumentType.java index c956f71..6c6d71c 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/command/arguments/TimestampArgumentType.java +++ b/src/main/java/land/chipmunk/chipmunkmod/command/arguments/TimestampArgumentType.java @@ -7,7 +7,7 @@ import java.util.Collection; import java.util.Arrays; public class TimestampArgumentType implements ArgumentType { - private static final Collection EXAMPLES = Arrays.asList("0:01", "1:23", "6:09"); + private static final Collection EXAMPLES = Arrays.asList("0:01", "1:23", "6:09"); private TimestampArgumentType () { }