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 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<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 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<Object> {
}
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<Object> {
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;

View file

@ -7,7 +7,7 @@ import java.util.Collection;
import java.util.Arrays;
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 () {
}