Skip to content

Commit f019b81

Browse files
authored
Merge pull request cabaletta#2527 from ZacSharp/itemFollowing
Item following
2 parents 50df7b2 + 209b51b commit f019b81

7 files changed

Lines changed: 170 additions & 15 deletions

File tree

src/api/java/baritone/api/command/datatypes/BlockById.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,11 @@
2323
import net.minecraft.resources.ResourceLocation;
2424
import net.minecraft.world.level.block.Block;
2525

26-
import java.util.regex.Pattern;
2726
import java.util.stream.Stream;
2827

2928
public enum BlockById implements IDatatypeFor<Block> {
3029
INSTANCE;
3130

32-
/**
33-
* Matches (domain:)?name? where domain and name are [a-z0-9_.-]+ and [a-z0-9/_.-]+ respectively.
34-
*/
35-
private static Pattern PATTERN = Pattern.compile("(?:[a-z0-9_.-]+:)?[a-z0-9/_.-]*");
36-
3731
@Override
3832
public Block get(IDatatypeContext ctx) throws CommandException {
3933
ResourceLocation id = new ResourceLocation(ctx.getConsumer().getString());
@@ -48,10 +42,6 @@ public Block get(IDatatypeContext ctx) throws CommandException {
4842
public Stream<String> tabComplete(IDatatypeContext ctx) throws CommandException {
4943
String arg = ctx.getConsumer().getString();
5044

51-
if (!PATTERN.matcher(arg).matches()) {
52-
return Stream.empty();
53-
}
54-
5545
return new TabCompleteHelper()
5646
.append(
5747
BuiltInRegistries.BLOCK.keySet()
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* This file is part of Baritone.
3+
*
4+
* Baritone is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* Baritone is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package baritone.api.command.datatypes;
19+
20+
import baritone.api.command.exception.CommandException;
21+
import baritone.api.command.helpers.TabCompleteHelper;
22+
import net.minecraft.core.registries.BuiltInRegistries;
23+
import net.minecraft.resources.ResourceLocation;
24+
import net.minecraft.world.item.Item;
25+
26+
import java.util.stream.Stream;
27+
28+
public enum ItemById implements IDatatypeFor<Item> {
29+
INSTANCE;
30+
31+
@Override
32+
public Item get(IDatatypeContext ctx) throws CommandException {
33+
ResourceLocation id = new ResourceLocation(ctx.getConsumer().getString());
34+
Item item;
35+
if ((item = BuiltInRegistries.ITEM.getOptional(id).orElse(null)) == null) {
36+
throw new IllegalArgumentException("No item found by that id");
37+
}
38+
return item;
39+
}
40+
41+
@Override
42+
public Stream<String> tabComplete(IDatatypeContext ctx) throws CommandException {
43+
return new TabCompleteHelper()
44+
.append(
45+
BuiltInRegistries.BLOCK.keySet()
46+
.stream()
47+
.map(ResourceLocation::toString)
48+
)
49+
.filterPrefixNamespaced(ctx.getConsumer().getString())
50+
.sortAlphabetically()
51+
.stream();
52+
}
53+
}

src/api/java/baritone/api/command/helpers/TabCompleteHelper.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,12 @@ public TabCompleteHelper filterPrefix(String prefix) {
212212
* @return This {@link TabCompleteHelper}
213213
*/
214214
public TabCompleteHelper filterPrefixNamespaced(String prefix) {
215-
return filterPrefix(new ResourceLocation(prefix).toString());
215+
ResourceLocation loc = ResourceLocation.tryParse(prefix);
216+
if (loc == null) {
217+
stream = Stream.empty();
218+
return this;
219+
}
220+
return filterPrefix(loc.toString());
216221
}
217222

218223
/**

src/api/java/baritone/api/process/IFollowProcess.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717

1818
package baritone.api.process;
1919

20+
import net.minecraft.world.entity.Entity;
21+
import net.minecraft.world.item.ItemStack;
22+
2023
import java.util.List;
2124
import java.util.function.Predicate;
22-
import net.minecraft.world.entity.Entity;
2325

2426
/**
2527
* @author Brady
@@ -34,6 +36,13 @@ public interface IFollowProcess extends IBaritoneProcess {
3436
*/
3537
void follow(Predicate<Entity> filter);
3638

39+
/**
40+
* Try to pick up any items matching this predicate
41+
*
42+
* @param filter the predicate
43+
*/
44+
void pickup(Predicate<ItemStack> filter);
45+
3746
/**
3847
* @return The entities that are currently being followed. null if not currently following, empty if nothing matches the predicate
3948
*/

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public static List<ICommand> createAll(IBaritone baritone) {
5353
new RenderCommand(baritone),
5454
new FarmCommand(baritone),
5555
new FollowCommand(baritone),
56+
new PickupCommand(baritone),
5657
new ExploreFilterCommand(baritone),
5758
new ReloadAllCommand(baritone),
5859
new SaveAllCommand(baritone),
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* This file is part of Baritone.
3+
*
4+
* Baritone is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* Baritone is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package baritone.command.defaults;
19+
20+
import baritone.api.IBaritone;
21+
import baritone.api.command.Command;
22+
import baritone.api.command.argument.IArgConsumer;
23+
import baritone.api.command.datatypes.ItemById;
24+
import baritone.api.command.exception.CommandException;
25+
import net.minecraft.core.registries.BuiltInRegistries;
26+
import net.minecraft.resources.ResourceLocation;
27+
import net.minecraft.world.item.Item;
28+
29+
import java.util.Arrays;
30+
import java.util.HashSet;
31+
import java.util.List;
32+
import java.util.Set;
33+
import java.util.stream.Stream;
34+
35+
public class PickupCommand extends Command {
36+
37+
public PickupCommand(IBaritone baritone) {
38+
super(baritone, "pickup");
39+
}
40+
41+
@Override
42+
public void execute(String label, IArgConsumer args) throws CommandException {
43+
Set<Item> collecting = new HashSet<>();
44+
while (args.hasAny()) {
45+
Item item = args.getDatatypeFor(ItemById.INSTANCE);
46+
collecting.add(item);
47+
}
48+
if (collecting.isEmpty()) {
49+
baritone.getFollowProcess().pickup(stack -> true);
50+
logDirect("Picking up all items");
51+
} else {
52+
baritone.getFollowProcess().pickup(stack -> collecting.contains(stack.getItem()));
53+
logDirect("Picking up these items:");
54+
collecting.stream().map(BuiltInRegistries.ITEM::getKey).map(ResourceLocation::toString).forEach(this::logDirect);
55+
}
56+
}
57+
58+
@Override
59+
public Stream<String> tabComplete(String label, IArgConsumer args) throws CommandException {
60+
while (args.has(2)) {
61+
if (args.peekDatatypeOrNull(ItemById.INSTANCE) == null) {
62+
return Stream.empty();
63+
}
64+
args.get();
65+
}
66+
return args.tabCompleteDatatype(ItemById.INSTANCE);
67+
}
68+
69+
@Override
70+
public String getShortDesc() {
71+
return "Pickup items";
72+
}
73+
74+
@Override
75+
public List<String> getLongDesc() {
76+
return Arrays.asList(
77+
"Usage:",
78+
"> pickup - Pickup anything",
79+
"> pickup <item1> <item2> <...> - Pickup certain items"
80+
);
81+
}
82+
}

src/main/java/baritone/process/FollowProcess.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import baritone.Baritone;
2121
import baritone.api.pathing.goals.Goal;
22+
import baritone.api.pathing.goals.GoalBlock;
2223
import baritone.api.pathing.goals.GoalComposite;
2324
import baritone.api.pathing.goals.GoalNear;
2425
import baritone.api.pathing.goals.GoalXZ;
@@ -27,11 +28,14 @@
2728
import baritone.api.process.PathingCommandType;
2829
import baritone.api.utils.BetterBlockPos;
2930
import baritone.utils.BaritoneProcessHelper;
31+
import net.minecraft.core.BlockPos;
32+
import net.minecraft.world.entity.Entity;
33+
import net.minecraft.world.entity.item.ItemEntity;
34+
import net.minecraft.world.item.ItemStack;
35+
3036
import java.util.List;
3137
import java.util.function.Predicate;
3238
import java.util.stream.Collectors;
33-
import net.minecraft.core.BlockPos;
34-
import net.minecraft.world.entity.Entity;
3539

3640
/**
3741
* Follow an entity
@@ -42,6 +46,7 @@ public final class FollowProcess extends BaritoneProcessHelper implements IFollo
4246

4347
private Predicate<Entity> filter;
4448
private List<Entity> cache;
49+
private boolean into; // walk straight into the target, regardless of settings
4550

4651
public FollowProcess(Baritone baritone) {
4752
super(baritone);
@@ -56,12 +61,15 @@ public PathingCommand onTick(boolean calcFailed, boolean isSafeToCancel) {
5661

5762
private Goal towards(Entity following) {
5863
BlockPos pos;
59-
if (Baritone.settings().followOffsetDistance.value == 0) {
64+
if (Baritone.settings().followOffsetDistance.value == 0 || into) {
6065
pos = following.blockPosition();
6166
} else {
6267
GoalXZ g = GoalXZ.fromDirection(following.position(), Baritone.settings().followOffsetDirection.value, Baritone.settings().followOffsetDistance.value);
6368
pos = new BetterBlockPos(g.getX(), following.position().y, g.getZ());
6469
}
70+
if (into) {
71+
return new GoalBlock(pos);
72+
}
6573
return new GoalNear(pos, Baritone.settings().followRadius.value);
6674
}
6775

@@ -114,6 +122,13 @@ public String displayName0() {
114122
@Override
115123
public void follow(Predicate<Entity> filter) {
116124
this.filter = filter;
125+
this.into = false;
126+
}
127+
128+
@Override
129+
public void pickup(Predicate<ItemStack> filter) {
130+
this.filter = e -> e instanceof ItemEntity && filter.test(((ItemEntity) e).getItem());
131+
this.into = true;
117132
}
118133

119134
@Override

0 commit comments

Comments
 (0)