refactor: some stuff to the argument types (also fixes URL class deprecation)

This commit is contained in:
Chayapak Supasakul 2025-01-05 14:42:25 +07:00
parent 3be587d815
commit 64a87a59d8
Signed by: ChomeNS
SSH key fingerprint: SHA256:0YoxhdyXsgbc0nfeB2N6FYE60mxMU7DS4uCUMaw2mvA
2 changed files with 11 additions and 9 deletions

View file

@ -8,19 +8,21 @@ import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
public class LocationArgumentType implements ArgumentType<Object> { public class LocationArgumentType implements ArgumentType<Object> {
private static final Collection<String> EXAMPLES = Arrays.<String>asList("songs/amogus.mid", "images/cat.jpg", "videos/badapple.mp4"); private static final Collection<String> 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 static final SimpleCommandExceptionType OOB_FILEPATH = new SimpleCommandExceptionType(Text.translatable("The specified file path is outside of the allowed directory"));
private boolean allowsUrls = false; private final boolean allowsUrls;
private boolean allowsPaths = false; private final boolean allowsPaths;
private Path root; private final Path root;
private LocationArgumentType (boolean allowsUrls, boolean allowsPaths, Path root) { private LocationArgumentType (boolean allowsUrls, boolean allowsPaths, Path root) {
this.allowsUrls = allowsUrls; this.allowsUrls = allowsUrls;
@ -49,8 +51,8 @@ public class LocationArgumentType implements ArgumentType<Object> {
} }
try { try {
return new URL(sb.toString()); return new URI(sb.toString()).toURL();
} catch (MalformedURLException exception) { } catch (MalformedURLException | URISyntaxException exception) {
throw new SimpleCommandExceptionType(Text.literal(exception.getMessage())).create(); throw new SimpleCommandExceptionType(Text.literal(exception.getMessage())).create();
} }
} }
@ -70,8 +72,8 @@ public class LocationArgumentType implements ArgumentType<Object> {
final Object location = getLocation(context, name); final Object location = getLocation(context, name);
if (location instanceof URL) return (URL) location; if (location instanceof URL) return (URL) location;
try { try {
if (location instanceof Path) return new URL("file", "", -1, location.toString()); if (location instanceof Path) return new URI("file", "", "", -1, location.toString(), "", "").toURL();
} catch (MalformedURLException ignored) { } catch (MalformedURLException | URISyntaxException ignored) {
return null; // The real question is whether this will actually ever get called return null; // The real question is whether this will actually ever get called
} }
return null; return null;

View file

@ -7,7 +7,7 @@ import java.util.Collection;
import java.util.Arrays; import java.util.Arrays;
public class TimestampArgumentType implements ArgumentType<Long> { public class TimestampArgumentType implements ArgumentType<Long> {
private static final Collection<String> EXAMPLES = Arrays.<String>asList("0:01", "1:23", "6:09"); private static final Collection<String> EXAMPLES = Arrays.asList("0:01", "1:23", "6:09");
private TimestampArgumentType () { private TimestampArgumentType () {
} }