baritone/src/main/java/baritone/behavior/impl/MineBehavior.java

91 lines
3.0 KiB
Java
Raw Normal View History

2018-08-28 18:43:28 +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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
2018-08-28 18:28:36 +00:00
package baritone.behavior.impl;
2018-08-28 18:43:28 +00:00
import baritone.api.event.events.TickEvent;
import baritone.behavior.Behavior;
import baritone.chunk.ChunkPacker;
import baritone.chunk.WorldProvider;
import baritone.pathing.goals.Goal;
import baritone.pathing.goals.GoalComposite;
import baritone.pathing.goals.GoalTwoBlocks;
import baritone.utils.BlockStateInterface;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.chunk.EmptyChunk;
import java.util.ArrayList;
2018-09-04 15:50:42 +00:00
import java.util.Arrays;
2018-08-28 18:43:28 +00:00
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
/**
* Mine blocks of a certain type
*
* @author leijurv
*/
public class MineBehavior extends Behavior {
public static final MineBehavior INSTANCE = new MineBehavior();
private MineBehavior() {
}
2018-09-04 15:50:42 +00:00
List<String> mining;
2018-08-28 18:43:28 +00:00
@Override
public void onTick(TickEvent event) {
if (event.getType() == TickEvent.Type.OUT) {
return;
}
if (mining == null) {
return;
}
2018-09-04 15:50:42 +00:00
List<BlockPos> locs = new ArrayList<>();
for (String m : mining) {
locs.addAll(WorldProvider.INSTANCE.getCurrentWorld().cache.getLocationsOf(m, 1, 1));
}
2018-08-28 18:43:28 +00:00
BlockPos playerFeet = playerFeet();
locs.sort(Comparator.comparingDouble(playerFeet::distanceSq));
// remove any that are within loaded chunks that aren't actually what we want
locs.removeAll(locs.stream()
.filter(pos -> !(world().getChunk(pos) instanceof EmptyChunk))
2018-09-04 15:50:42 +00:00
.filter(pos -> !mining.contains(ChunkPacker.blockToString(BlockStateInterface.get(pos).getBlock()).toLowerCase()))
2018-08-28 18:43:28 +00:00
.collect(Collectors.toList()));
if (locs.size() > 30) {
locs = locs.subList(0, 30);
}
2018-09-01 19:16:55 +00:00
if (locs.isEmpty()) {
displayChatMessageRaw("No locations for " + mining + " known, cancelling");
cancel();
return;
}
2018-08-28 18:43:28 +00:00
PathingBehavior.INSTANCE.setGoal(new GoalComposite(locs.stream().map(GoalTwoBlocks::new).toArray(Goal[]::new)));
PathingBehavior.INSTANCE.path();
}
2018-09-04 15:50:42 +00:00
public void mine(String... mining) {
2018-09-04 20:20:49 +00:00
this.mining = mining == null || mining.length == 0 ? null : new ArrayList<>(Arrays.asList(mining));
2018-08-28 18:43:28 +00:00
}
public void cancel() {
PathingBehavior.INSTANCE.cancel();
2018-09-04 20:20:49 +00:00
mine();
2018-08-28 18:43:28 +00:00
}
2018-08-28 18:28:36 +00:00
}