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

170 lines
5.9 KiB
Java
Raw Normal View History

2019-08-30 18:55:25 +00:00
/*
* 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/>.
*/
2019-10-06 20:20:42 +00:00
package baritone.command.defaults;
2019-08-30 18:55:25 +00:00
2020-04-12 07:01:24 +00:00
import baritone.KeepName;
2019-09-19 20:30:40 +00:00
import baritone.api.IBaritone;
2019-10-06 20:20:42 +00:00
import baritone.api.command.Command;
2021-01-30 04:17:53 +00:00
import baritone.api.command.argument.IArgConsumer;
2019-10-06 20:20:42 +00:00
import baritone.api.command.datatypes.EntityClassById;
import baritone.api.command.datatypes.IDatatypeFor;
import baritone.api.command.datatypes.NearbyPlayer;
2021-10-08 20:58:17 +00:00
import baritone.api.command.exception.CommandErrorMessageException;
2019-10-06 20:20:42 +00:00
import baritone.api.command.exception.CommandException;
import baritone.api.command.helpers.TabCompleteHelper;
2019-08-30 18:55:25 +00:00
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityList;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.ResourceLocation;
2019-09-19 21:38:13 +00:00
import java.util.*;
2019-08-30 18:55:25 +00:00
import java.util.function.Predicate;
import java.util.stream.Stream;
public class FollowCommand extends Command {
2019-09-19 20:40:46 +00:00
2019-09-19 20:30:40 +00:00
public FollowCommand(IBaritone baritone) {
super(baritone, "follow");
2019-08-30 18:55:25 +00:00
}
@Override
2019-10-04 13:55:02 +00:00
public void execute(String label, IArgConsumer args) throws CommandException {
2019-08-30 18:55:25 +00:00
args.requireMin(1);
FollowGroup group;
FollowList list;
List<Entity> entities = new ArrayList<>();
List<Class<? extends Entity>> classes = new ArrayList<>();
if (args.hasExactlyOne()) {
baritone.getFollowProcess().follow((group = args.getEnum(FollowGroup.class)).filter);
2019-08-30 18:55:25 +00:00
} else {
args.requireMin(2);
group = null;
list = args.getEnum(FollowList.class);
2019-09-20 23:32:43 +00:00
while (args.hasAny()) {
2019-08-30 18:55:25 +00:00
Object gotten = args.getDatatypeFor(list.datatype);
if (gotten instanceof Class) {
//noinspection unchecked
classes.add((Class<? extends Entity>) gotten);
2021-10-08 20:58:17 +00:00
} else if (gotten != null) {
2019-08-30 18:55:25 +00:00
entities.add((Entity) gotten);
}
}
baritone.getFollowProcess().follow(
2019-09-19 19:53:15 +00:00
classes.isEmpty()
? entities::contains
: e -> classes.stream().anyMatch(c -> c.isInstance(e))
2019-08-30 18:55:25 +00:00
);
}
2019-09-19 21:09:09 +00:00
if (group != null) {
2019-08-30 18:55:25 +00:00
logDirect(String.format("Following all %s", group.name().toLowerCase(Locale.US)));
} else {
if (classes.isEmpty()) {
2021-10-08 20:58:17 +00:00
if (entities.isEmpty()) throw new NoEntitiesException();
logDirect("Following these entities:");
2019-08-30 18:55:25 +00:00
entities.stream()
2019-09-19 19:53:15 +00:00
.map(Entity::toString)
.forEach(this::logDirect);
2019-08-30 18:55:25 +00:00
} else {
2021-10-08 20:58:17 +00:00
logDirect("Following these types of entities:");
2019-08-30 18:55:25 +00:00
classes.stream()
2019-09-19 19:53:15 +00:00
.map(EntityList::getKey)
.map(Objects::requireNonNull)
.map(ResourceLocation::toString)
.forEach(this::logDirect);
2019-08-30 18:55:25 +00:00
}
}
}
@Override
2019-10-04 13:55:02 +00:00
public Stream<String> tabComplete(String label, IArgConsumer args) throws CommandException {
2019-08-30 18:55:25 +00:00
if (args.hasExactlyOne()) {
return new TabCompleteHelper()
2019-09-19 19:53:15 +00:00
.append(FollowGroup.class)
.append(FollowList.class)
.filterPrefix(args.getString())
.stream();
2019-08-30 18:55:25 +00:00
} else {
2019-09-26 23:49:26 +00:00
IDatatypeFor followType;
2019-08-30 18:55:25 +00:00
try {
followType = args.getEnum(FollowList.class).datatype;
2019-08-30 18:55:25 +00:00
} catch (NullPointerException e) {
return Stream.empty();
}
while (args.has(2)) {
2019-09-19 21:09:09 +00:00
if (args.peekDatatypeOrNull(followType) == null) {
2019-08-30 18:55:25 +00:00
return Stream.empty();
}
args.get();
}
return args.tabCompleteDatatype(followType);
}
}
2019-09-06 10:59:10 +00:00
@Override
public String getShortDesc() {
return "Follow entity things";
}
2019-08-30 18:55:25 +00:00
@Override
public List<String> getLongDesc() {
2019-09-19 21:38:13 +00:00
return Arrays.asList(
2019-09-19 19:53:15 +00:00
"The follow command tells Baritone to follow certain kinds of entities.",
"",
"Usage:",
"> follow entities - Follows all entities.",
"> follow entity <entity1> <entity2> <...> - Follow certain entities (for example 'skeleton', 'horse' etc.)",
"> follow players - Follow players",
"> follow player <username1> <username2> <...> - Follow certain players"
2019-08-30 18:55:25 +00:00
);
}
2020-04-12 07:01:24 +00:00
@KeepName
2019-08-30 18:55:25 +00:00
private enum FollowGroup {
ENTITIES(EntityLiving.class::isInstance),
PLAYERS(EntityPlayer.class::isInstance); /* ,
FRIENDLY(entity -> entity.getAttackTarget() != HELPER.mc.player),
HOSTILE(FRIENDLY.filter.negate()); */
final Predicate<Entity> filter;
FollowGroup(Predicate<Entity> filter) {
this.filter = filter;
}
}
2020-04-12 07:01:24 +00:00
@KeepName
2019-08-30 18:55:25 +00:00
private enum FollowList {
2019-09-26 23:49:26 +00:00
ENTITY(EntityClassById.INSTANCE),
PLAYER(NearbyPlayer.INSTANCE);
2019-08-30 18:55:25 +00:00
2019-09-26 23:49:26 +00:00
final IDatatypeFor datatype;
FollowList(IDatatypeFor datatype) {
2019-08-30 18:55:25 +00:00
this.datatype = datatype;
}
}
2021-10-08 20:58:17 +00:00
public static class NoEntitiesException extends CommandErrorMessageException {
protected NoEntitiesException() {
2021-10-08 21:49:22 +00:00
super("No valid entities in range!");
2021-10-08 20:58:17 +00:00
}
}
2019-08-30 18:55:25 +00:00
}