baritone/src/api/java/baritone/api/command/datatypes/RelativeFile.java

104 lines
3.8 KiB
Java
Raw Normal View History

2019-09-04 16:16:36 +00:00
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Baritone is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
2019-10-06 20:20:42 +00:00
package baritone.api.command.datatypes;
2019-08-30 18:55:25 +00:00
import baritone.api.command.argument.IArgConsumer;
2019-10-06 20:20:42 +00:00
import baritone.api.command.exception.CommandException;
2019-08-30 18:55:25 +00:00
import java.io.File;
2019-09-04 09:30:24 +00:00
import java.io.IOException;
import java.io.UncheckedIOException;
2019-08-30 18:55:25 +00:00
import java.nio.file.FileSystems;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.util.Locale;
import java.util.Objects;
import java.util.stream.Stream;
import static baritone.api.utils.Helper.HELPER;
2019-09-26 23:49:26 +00:00
public enum RelativeFile implements IDatatypePost<File, File> {
INSTANCE;
2019-09-19 20:43:40 +00:00
2019-09-26 23:49:26 +00:00
@Override
public File apply(IDatatypeContext ctx, File original) throws CommandException {
if (original == null) {
original = new File("./");
}
2019-08-30 18:55:25 +00:00
2019-09-26 23:49:26 +00:00
Path path;
2019-08-30 18:55:25 +00:00
try {
2019-09-26 23:49:26 +00:00
path = FileSystems.getDefault().getPath(ctx.getConsumer().getString());
2019-08-30 18:55:25 +00:00
} catch (InvalidPathException e) {
2019-09-06 10:59:10 +00:00
throw new IllegalArgumentException("invalid path");
2019-08-30 18:55:25 +00:00
}
2019-09-26 23:49:26 +00:00
return getCanonicalFileUnchecked(original.toPath().resolve(path).toFile());
2019-08-30 18:55:25 +00:00
}
@Override
2019-09-26 23:49:26 +00:00
public Stream<String> tabComplete(IDatatypeContext ctx) {
2019-08-30 18:55:25 +00:00
return Stream.empty();
}
2019-09-04 09:30:24 +00:00
/**
* Seriously
*
* @param file File
* @return Canonical file of file
* @author LoganDark
2019-09-04 09:30:24 +00:00
*/
private static File getCanonicalFileUnchecked(File file) {
2019-09-04 09:30:24 +00:00
try {
return file.getCanonicalFile();
} catch (IOException e) {
throw new UncheckedIOException(e);
2019-09-04 09:30:24 +00:00
}
}
2019-10-04 13:55:02 +00:00
public static Stream<String> tabComplete(IArgConsumer consumer, File base0) throws CommandException {
2019-09-04 09:30:24 +00:00
// I will not make the caller deal with this, seriously
2019-09-20 23:32:43 +00:00
// Tab complete code is beautiful and I'm not going to bloat it with dumb ass checked exception bullshit -LoganDark
// lol owned -Brady
File base = getCanonicalFileUnchecked(base0);
String currentPathStringThing = consumer.getString();
2019-08-30 18:55:25 +00:00
Path currentPath = FileSystems.getDefault().getPath(currentPathStringThing);
Path basePath = currentPath.isAbsolute() ? currentPath.getRoot() : base.toPath();
boolean useParent = !currentPathStringThing.isEmpty() && !currentPathStringThing.endsWith(File.separator);
File currentFile = currentPath.isAbsolute() ? currentPath.toFile() : new File(base, currentPathStringThing);
return Stream.of(Objects.requireNonNull(getCanonicalFileUnchecked(
2023-01-15 09:02:52 +00:00
useParent
? currentFile.getParentFile()
: currentFile
).listFiles()))
2019-09-07 00:41:27 +00:00
.map(f -> (currentPath.isAbsolute() ? f : basePath.relativize(f.toPath()).toString()) +
(f.isDirectory() ? File.separator : ""))
.filter(s -> s.toLowerCase(Locale.US).startsWith(currentPathStringThing.toLowerCase(Locale.US)))
.filter(s -> !s.contains(" "));
2019-08-30 18:55:25 +00:00
}
public static File gameDir() {
2021-06-23 17:24:32 +00:00
File gameDir = HELPER.mc.gameDirectory.getAbsoluteFile();
if (gameDir.getName().equals(".")) {
return gameDir.getParentFile();
}
return gameDir;
}
2019-08-30 18:55:25 +00:00
}