Fix RelativePath and build command

This commit is contained in:
Logan Darklock 2019-09-04 02:30:24 -07:00
parent 382ad0079c
commit 5ee1e738f4
No known key found for this signature in database
GPG Key ID: B8C37CEDE1AC60EA
2 changed files with 45 additions and 8 deletions

View File

@ -3,6 +3,7 @@ package baritone.api.utils.command.datatypes;
import baritone.api.utils.command.helpers.arguments.ArgConsumer; import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems; import java.nio.file.FileSystems;
import java.nio.file.InvalidPathException; import java.nio.file.InvalidPathException;
import java.nio.file.Path; import java.nio.file.Path;
@ -33,22 +34,45 @@ public class RelativeFile implements IDatatypePost<File, File> {
return Stream.empty(); 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(); String currentPathStringThing = consumer.getString();
Path currentPath = FileSystems.getDefault().getPath(currentPathStringThing); Path currentPath = FileSystems.getDefault().getPath(currentPathStringThing);
Path basePath = currentPath.isAbsolute() ? currentPath.getRoot() : base.toPath(); Path basePath = currentPath.isAbsolute() ? currentPath.getRoot() : base.toPath();
boolean useParent = !currentPathStringThing.isEmpty() && !currentPathStringThing.endsWith(File.separator); boolean useParent = !currentPathStringThing.isEmpty() && !currentPathStringThing.endsWith(File.separator);
File currentFile = currentPath.isAbsolute() ? currentPath.toFile() : new File(base, currentPathStringThing); 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()) + .map(f -> (currentPath.isAbsolute() ? f : basePath.relativize(f.toPath()).toString()) +
(f.isDirectory() ? File.separator : "")) (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 @Override
public File apply(File original) { 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() { public static File gameDir() {

View File

@ -21,22 +21,33 @@ import baritone.api.Settings;
import baritone.api.utils.BetterBlockPos; import baritone.api.utils.BetterBlockPos;
import baritone.api.utils.command.Command; import baritone.api.utils.command.Command;
import baritone.api.utils.command.datatypes.RelativeBlockPos; 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.exception.CommandInvalidStateException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer; import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import net.minecraft.client.Minecraft;
import java.io.File;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.stream.Stream; import java.util.stream.Stream;
import static java.util.Arrays.asList; import static java.util.Arrays.asList;
public class BuildCommand extends Command { public class BuildCommand extends Command {
private static final File schematicsDir = new File(Minecraft.getMinecraft().gameDir, "schematics");
public BuildCommand() { public BuildCommand() {
super("build", "Build a schematic"); super("build", "Build a schematic");
} }
@Override @Override
protected void executed(String label, ArgConsumer args, Settings settings) { 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 origin = ctx.playerFeet();
BetterBlockPos buildOrigin; BetterBlockPos buildOrigin;
@ -48,18 +59,20 @@ public class BuildCommand extends Command {
buildOrigin = origin; buildOrigin = origin;
} }
boolean success = baritone.getBuilderProcess().build(filename, buildOrigin); boolean success = baritone.getBuilderProcess().build(file.getName(), file, buildOrigin);
if (!success) { if (!success) {
throw new CommandInvalidStateException("Couldn't load the schematic"); 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 @Override
protected Stream<String> tabCompleted(String label, ArgConsumer args, Settings settings) { 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(); args.get();
return args.tabCompleteDatatype(RelativeBlockPos.class); return args.tabCompleteDatatype(RelativeBlockPos.class);