baritone/src/main/java/baritone/command/defaults/FindCommand.java

118 lines
4.5 KiB
Java

/*
* 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/>.
*/
package baritone.command.defaults;
import baritone.api.IBaritone;
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.core.Registry;
import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.*;
import net.minecraft.network.chat.ClickEvent;
import net.minecraft.network.chat.HoverEvent;
import net.minecraft.world.level.block.Block;
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) {
super(baritone, "find");
}
@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();
BaseComponent[] components = toFind.stream()
.flatMap(block ->
ctx.worldData().getCachedWorld().getLocationsOf(
Registry.BLOCK.getKey(block).getPath(),
Integer.MAX_VALUE,
origin.x,
origin.y,
4
).stream()
)
.map(BetterBlockPos::new)
.map(this::positionToComponent)
.toArray(BaseComponent[]::new);
if (components.length > 0) {
Arrays.asList(components).forEach(this::logDirect);
} else {
logDirect("No positions known, are you sure the blocks are cached?");
}
}
private BaseComponent 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);
BaseComponent baseComponent = new TextComponent(pos.toString());
BaseComponent hoverComponent = new TextComponent("Click to set goal to this position");
baseComponent.setStyle(baseComponent.getStyle()
.withColor(ChatFormatting.GRAY)
.withInsertion(positionText)
.withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, command))
.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, hoverComponent)));
return baseComponent;
}
@Override
public Stream<String> tabComplete(String label, IArgConsumer args) throws CommandException {
return new TabCompleteHelper()
.append(
CachedChunk.BLOCKS_TO_KEEP_TRACK_OF.stream()
.map(Registry.BLOCK::getKey)
.map(Object::toString)
)
.filterPrefixNamespaced(args.getString())
.sortAlphabetically()
.stream();
}
@Override
public String getShortDesc() {
return "Find positions of a certain block";
}
@Override
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> [...] - Try finding the listed blocks"
);
}
}