Fix RelativePath and build command
This commit is contained in:
parent
382ad0079c
commit
5ee1e738f4
@ -3,6 +3,7 @@ package baritone.api.utils.command.datatypes;
|
||||
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.FileSystems;
|
||||
import java.nio.file.InvalidPathException;
|
||||
import java.nio.file.Path;
|
||||
@ -33,22 +34,45 @@ public class RelativeFile implements IDatatypePost<File, File> {
|
||||
return Stream.empty();
|
||||
}
|
||||
|
||||
public static Stream<String> tabComplete(ArgConsumer consumer, File base) {
|
||||
/**
|
||||
* Seriously
|
||||
*
|
||||
* @param file File
|
||||
* @return Canonical file of file
|
||||
* @author LoganDark and his hate of checked exceptions
|
||||
*/
|
||||
private static File SHUT_THE_FUCK_UP_IOEXCEPTION_NOBODY_LIKES_YOU(File file) {
|
||||
try {
|
||||
return file.getCanonicalFile();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Fuck you", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static Stream<String> tabComplete(ArgConsumer consumer, File base0) {
|
||||
// I will not make the caller deal with this, seriously
|
||||
// Tab complete code is beautiful and I'm not going to bloat it with dumb ass checked exception bullshit
|
||||
File base = SHUT_THE_FUCK_UP_IOEXCEPTION_NOBODY_LIKES_YOU(base0);
|
||||
String currentPathStringThing = consumer.getString();
|
||||
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 Arrays.stream(Objects.requireNonNull((useParent ? currentFile.getParentFile() : currentFile).listFiles()))
|
||||
return Arrays.stream(Objects.requireNonNull(SHUT_THE_FUCK_UP_IOEXCEPTION_NOBODY_LIKES_YOU(
|
||||
useParent
|
||||
? currentFile.getParentFile()
|
||||
: currentFile
|
||||
).listFiles()))
|
||||
.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.toLowerCase(Locale.US).startsWith(currentPathStringThing.toLowerCase(Locale.US)))
|
||||
.filter(s -> !s.contains(" "));
|
||||
}
|
||||
|
||||
@Override
|
||||
public File apply(File original) {
|
||||
return original.toPath().resolve(path).toFile();
|
||||
return SHUT_THE_FUCK_UP_IOEXCEPTION_NOBODY_LIKES_YOU(original.toPath().resolve(path).toFile());
|
||||
}
|
||||
|
||||
public static File gameDir() {
|
||||
|
@ -21,22 +21,33 @@ import baritone.api.Settings;
|
||||
import baritone.api.utils.BetterBlockPos;
|
||||
import baritone.api.utils.command.Command;
|
||||
import baritone.api.utils.command.datatypes.RelativeBlockPos;
|
||||
import baritone.api.utils.command.datatypes.RelativeFile;
|
||||
import baritone.api.utils.command.exception.CommandInvalidStateException;
|
||||
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
|
||||
import net.minecraft.client.Minecraft;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
|
||||
public class BuildCommand extends Command {
|
||||
private static final File schematicsDir = new File(Minecraft.getMinecraft().gameDir, "schematics");
|
||||
|
||||
public BuildCommand() {
|
||||
super("build", "Build a schematic");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void executed(String label, ArgConsumer args, Settings settings) {
|
||||
String filename = String.format("%s.schematic", args.getString());
|
||||
File file = args.getDatatypePost(RelativeFile.class, schematicsDir).getAbsoluteFile();
|
||||
|
||||
if (!file.getName().toLowerCase(Locale.US).endsWith(".schematic")) {
|
||||
file = new File(file.getAbsolutePath() + ".schematic");
|
||||
}
|
||||
|
||||
BetterBlockPos origin = ctx.playerFeet();
|
||||
BetterBlockPos buildOrigin;
|
||||
|
||||
@ -48,18 +59,20 @@ public class BuildCommand extends Command {
|
||||
buildOrigin = origin;
|
||||
}
|
||||
|
||||
boolean success = baritone.getBuilderProcess().build(filename, buildOrigin);
|
||||
boolean success = baritone.getBuilderProcess().build(file.getName(), file, buildOrigin);
|
||||
|
||||
if (!success) {
|
||||
throw new CommandInvalidStateException("Couldn't load the schematic");
|
||||
}
|
||||
|
||||
logDirect(String.format("Successfully loaded schematic '%s' for building\nOrigin: %s", filename, buildOrigin));
|
||||
logDirect(String.format("Successfully loaded schematic for building\nOrigin: %s", buildOrigin));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Stream<String> tabCompleted(String label, ArgConsumer args, Settings settings) {
|
||||
if (args.has(2)) {
|
||||
if (args.hasExactlyOne()) {
|
||||
return RelativeFile.tabComplete(args, schematicsDir);
|
||||
} else if (args.has(2)) {
|
||||
args.get();
|
||||
|
||||
return args.tabCompleteDatatype(RelativeBlockPos.class);
|
||||
|
Loading…
Reference in New Issue
Block a user