Skip to content

Commit

Permalink
Fix RelativePath and build command
Browse files Browse the repository at this point in the history
  • Loading branch information
LoganDark committed Sep 4, 2019
1 parent 382ad00 commit 5ee1e73
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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;
Expand Down Expand Up @@ -33,22 +34,45 @@ public Stream<String> tabComplete(ArgConsumer consumer) {
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() {
Expand Down
21 changes: 17 additions & 4 deletions src/api/java/baritone/api/utils/command/defaults/BuildCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,33 @@
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;

Expand All @@ -48,18 +59,20 @@ protected void executed(String label, ArgConsumer args, Settings settings) {
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);
Expand Down

0 comments on commit 5ee1e73

Please sign in to comment.