Merge pull request #3296 from ZacSharp/findImprovements

Find improvements
This commit is contained in:
Leijurv 2022-07-05 15:32:02 -07:00 committed by GitHub
commit 49828baae3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -22,14 +22,23 @@ import baritone.api.command.Command;
import baritone.api.command.argument.IArgConsumer;
import baritone.api.command.datatypes.BlockById;
import baritone.api.command.exception.CommandException;
import baritone.api.command.helpers.TabCompleteHelper;
import baritone.api.utils.BetterBlockPos;
import baritone.cache.CachedChunk;
import net.minecraft.block.Block;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.util.text.event.ClickEvent;
import net.minecraft.util.text.event.HoverEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
import static baritone.api.command.IBaritoneChatControl.FORCE_COMMAND_PREFIX;
public class FindCommand extends Command {
public FindCommand(IBaritone baritone) {
@ -38,12 +47,13 @@ public class FindCommand extends Command {
@Override
public void execute(String label, IArgConsumer args) throws CommandException {
args.requireMin(1);
List<Block> toFind = new ArrayList<>();
while (args.hasAny()) {
toFind.add(args.getDatatypeFor(BlockById.INSTANCE));
}
BetterBlockPos origin = ctx.playerFeet();
toFind.stream()
ITextComponent[] components = toFind.stream()
.flatMap(block ->
ctx.worldData().getCachedWorld().getLocationsOf(
Block.REGISTRY.getNameForObject(block).getPath(),
@ -54,13 +64,39 @@ public class FindCommand extends Command {
).stream()
)
.map(BetterBlockPos::new)
.map(BetterBlockPos::toString)
.forEach(this::logDirect);
.map(this::positionToComponent)
.toArray(ITextComponent[]::new);
if (components.length > 0) {
Arrays.asList(components).forEach(this::logDirect);
} else {
logDirect("No positions known, are you sure the blocks are cached?");
}
}
private ITextComponent positionToComponent(BetterBlockPos pos) {
String positionText = String.format("%s %s %s", pos.x, pos.y, pos.z);
String command = String.format("%sgoal %s", FORCE_COMMAND_PREFIX, positionText);
ITextComponent baseComponent = new TextComponentString(pos.toString());
ITextComponent hoverComponent = new TextComponentString("Click to set goal to this position");
baseComponent.getStyle()
.setColor(TextFormatting.GRAY)
.setInsertion(positionText)
.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, command))
.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, hoverComponent));
return baseComponent;
}
@Override
public Stream<String> tabComplete(String label, IArgConsumer args) {
return args.tabCompleteDatatype(BlockById.INSTANCE);
public Stream<String> tabComplete(String label, IArgConsumer args) throws CommandException {
return new TabCompleteHelper()
.append(
CachedChunk.BLOCKS_TO_KEEP_TRACK_OF.stream()
.map(Block.REGISTRY::getNameForObject)
.map(Object::toString)
)
.filterPrefixNamespaced(args.getString())
.sortAlphabetically()
.stream();
}
@Override
@ -72,9 +108,10 @@ public class FindCommand extends Command {
public List<String> getLongDesc() {
return Arrays.asList(
"The find command searches through Baritone's cache and attempts to find the location of the block.",
"Tab completion will suggest only cached blocks and uncached blocks can not be found.",
"",
"Usage:",
"> find <block> - Find positions of a certain block"
"> find <block> [...] - Try finding the listed blocks"
);
}
}