baritone/src/main/java/baritone/utils/ExampleBaritoneControl.java

384 lines
16 KiB
Java
Raw Normal View History

2018-08-14 03:57:29 +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-22 20:15:56 +00:00
package baritone.utils;
import baritone.Baritone;
import baritone.Settings;
2018-08-28 14:29:41 +00:00
import baritone.api.event.events.ChatEvent;
2018-08-22 20:15:56 +00:00
import baritone.behavior.Behavior;
2018-08-28 02:05:21 +00:00
import baritone.behavior.impl.FollowBehavior;
2018-08-28 18:43:28 +00:00
import baritone.behavior.impl.MineBehavior;
2018-08-22 20:15:56 +00:00
import baritone.behavior.impl.PathingBehavior;
2018-09-11 17:28:03 +00:00
import baritone.cache.ChunkPacker;
import baritone.cache.Waypoint;
import baritone.cache.WorldProvider;
2018-08-22 20:15:56 +00:00
import baritone.pathing.calc.AStarPathFinder;
2018-09-01 21:45:57 +00:00
import baritone.pathing.calc.AbstractNodeCostSearch;
import baritone.pathing.goals.*;
2018-08-22 20:15:56 +00:00
import baritone.pathing.movement.ActionCosts;
import baritone.pathing.movement.CalculationContext;
import baritone.pathing.movement.Movement;
2018-08-28 02:05:21 +00:00
import baritone.pathing.movement.MovementHelper;
2018-08-22 20:15:56 +00:00
import baritone.utils.pathing.BetterBlockPos;
import net.minecraft.block.Block;
2018-08-28 02:05:21 +00:00
import net.minecraft.entity.Entity;
2018-08-14 03:57:29 +00:00
import net.minecraft.util.math.BlockPos;
2018-08-23 16:54:29 +00:00
import java.util.*;
2018-08-14 22:04:41 +00:00
2018-08-14 03:57:29 +00:00
public class ExampleBaritoneControl extends Behavior {
public static ExampleBaritoneControl INSTANCE = new ExampleBaritoneControl();
private ExampleBaritoneControl() {
}
public void initAndRegister() {
Baritone.INSTANCE.registerBehavior(this);
}
@Override
public void onSendChatMessage(ChatEvent event) {
2018-08-14 22:04:41 +00:00
if (!Baritone.settings().chatControl.get()) {
2018-09-02 19:06:00 +00:00
if (!Baritone.settings().removePrefix.get()) {
return;
}
2018-08-14 03:57:29 +00:00
}
String msg = event.getMessage();
2018-08-24 03:12:36 +00:00
if (Baritone.settings().prefix.get()) {
if (!msg.startsWith("#")) {
return;
}
msg = msg.substring(1);
}
2018-08-14 03:57:29 +00:00
if (msg.toLowerCase().startsWith("goal")) {
event.cancel();
String[] params = msg.toLowerCase().substring(4).trim().split(" ");
if (params[0].equals("")) {
params = new String[]{};
}
Goal goal;
try {
switch (params.length) {
case 0:
goal = new GoalBlock(playerFeet());
break;
case 1:
2018-08-16 23:51:43 +00:00
if (params[0].equals("clear") || params[0].equals("none")) {
goal = null;
} else {
goal = new GoalYLevel(Integer.parseInt(params[0]));
}
2018-08-14 03:57:29 +00:00
break;
case 2:
goal = new GoalXZ(Integer.parseInt(params[0]), Integer.parseInt(params[1]));
break;
case 3:
goal = new GoalBlock(new BlockPos(Integer.parseInt(params[0]), Integer.parseInt(params[1]), Integer.parseInt(params[2])));
break;
default:
2018-09-11 20:45:43 +00:00
logDirect("unable to understand lol");
2018-08-14 03:57:29 +00:00
return;
}
} catch (NumberFormatException ex) {
2018-09-11 20:45:43 +00:00
logDirect("unable to parse integer " + ex);
2018-08-14 03:57:29 +00:00
return;
}
PathingBehavior.INSTANCE.setGoal(goal);
2018-09-11 20:45:43 +00:00
logDirect("Goal: " + goal);
2018-08-14 03:57:29 +00:00
return;
}
if (msg.equals("path")) {
2018-08-28 18:43:28 +00:00
if (!PathingBehavior.INSTANCE.path()) {
2018-09-04 22:53:11 +00:00
if (PathingBehavior.INSTANCE.getGoal() == null) {
2018-09-11 20:45:43 +00:00
logDirect("No goal.");
2018-09-04 22:53:11 +00:00
} else {
2018-09-09 20:09:56 +00:00
if (PathingBehavior.INSTANCE.getGoal().isInGoal(playerFeet())) {
2018-09-11 20:45:43 +00:00
logDirect("Already in goal");
2018-09-09 20:09:56 +00:00
} else {
2018-09-11 20:45:43 +00:00
logDirect("Currently executing a path. Please cancel it first.");
2018-09-09 20:09:56 +00:00
}
2018-09-04 22:53:11 +00:00
}
2018-08-28 18:43:28 +00:00
}
2018-08-14 03:57:29 +00:00
event.cancel();
return;
}
if (msg.toLowerCase().equals("cancel")) {
PathingBehavior.INSTANCE.cancel();
2018-08-28 14:29:41 +00:00
FollowBehavior.INSTANCE.cancel();
2018-08-28 18:43:28 +00:00
MineBehavior.INSTANCE.cancel();
2018-08-14 03:57:29 +00:00
event.cancel();
2018-09-11 20:45:43 +00:00
logDirect("ok canceled");
2018-08-14 03:57:29 +00:00
return;
2018-08-25 22:09:47 +00:00
}
2018-09-01 21:45:57 +00:00
if (msg.toLowerCase().equals("forcecancel")) {
AbstractNodeCostSearch.forceCancel();
event.cancel();
2018-09-11 20:45:43 +00:00
logDirect("ok force canceled");
2018-09-01 21:45:57 +00:00
return;
}
2018-08-25 22:09:47 +00:00
if (msg.toLowerCase().equals("invert")) {
Goal goal = PathingBehavior.INSTANCE.getGoal();
BlockPos runAwayFrom;
if (goal instanceof GoalXZ) {
runAwayFrom = new BlockPos(((GoalXZ) goal).getX(), 0, ((GoalXZ) goal).getZ());
} else if (goal instanceof GoalBlock) {
runAwayFrom = ((GoalBlock) goal).getGoalPos();
} else {
2018-09-11 20:45:43 +00:00
logDirect("Goal must be GoalXZ or GoalBlock to invert");
logDirect("Inverting goal of player feet");
2018-08-25 22:09:47 +00:00
runAwayFrom = playerFeet();
}
PathingBehavior.INSTANCE.setGoal(new GoalRunAway(1, runAwayFrom) {
@Override
public boolean isInGoal(BlockPos pos) {
return false;
}
});
2018-08-28 18:43:28 +00:00
if (!PathingBehavior.INSTANCE.path()) {
2018-09-11 20:45:43 +00:00
logDirect("Currently executing a path. Please cancel it first.");
2018-08-28 18:43:28 +00:00
}
2018-08-25 22:09:47 +00:00
event.cancel();
return;
2018-08-28 02:05:21 +00:00
}
if (msg.toLowerCase().equals("follow")) {
Optional<Entity> entity = MovementHelper.whatEntityAmILookingAt();
if (!entity.isPresent()) {
2018-09-11 20:45:43 +00:00
logDirect("You aren't looking at an entity bruh");
2018-08-28 02:05:21 +00:00
event.cancel();
return;
}
FollowBehavior.INSTANCE.follow(entity.get());
2018-09-11 20:45:43 +00:00
logDirect("Following " + entity.get());
2018-08-28 02:05:21 +00:00
event.cancel();
return;
2018-08-14 03:57:29 +00:00
}
if (msg.toLowerCase().equals("reloadall")) {
WorldProvider.INSTANCE.getCurrentWorld().cache.reloadAllFromDisk();
2018-09-11 20:45:43 +00:00
logDirect("ok");
event.cancel();
return;
}
if (msg.toLowerCase().equals("saveall")) {
WorldProvider.INSTANCE.getCurrentWorld().cache.save();
2018-09-11 20:45:43 +00:00
logDirect("ok");
event.cancel();
return;
}
if (msg.toLowerCase().startsWith("find")) {
String blockType = msg.toLowerCase().substring(4).trim();
2018-08-23 20:12:13 +00:00
LinkedList<BlockPos> locs = WorldProvider.INSTANCE.getCurrentWorld().cache.getLocationsOf(blockType, 1, 4);
2018-09-11 20:45:43 +00:00
logDirect("Have " + locs.size() + " locations");
for (BlockPos pos : locs) {
Block actually = BlockStateInterface.get(pos).getBlock();
if (!ChunkPacker.blockToString(actually).equalsIgnoreCase(blockType)) {
System.out.println("Was looking for " + blockType + " but actually found " + actually + " " + ChunkPacker.blockToString(actually));
}
}
event.cancel();
return;
}
if (msg.toLowerCase().startsWith("mine")) {
String[] blockTypes = msg.toLowerCase().substring(4).trim().split(" ");
for (String s : blockTypes) {
if (ChunkPacker.stringToBlock(s) == null) {
2018-09-11 20:45:43 +00:00
logDirect(s + " isn't a valid block name");
event.cancel();
return;
}
}
MineBehavior.INSTANCE.mine(blockTypes);
2018-09-11 20:45:43 +00:00
logDirect("Started mining blocks of type " + Arrays.toString(blockTypes));
event.cancel();
return;
}
2018-08-14 03:57:29 +00:00
if (msg.toLowerCase().startsWith("thisway")) {
Goal goal = GoalXZ.fromDirection(playerFeetAsVec(), player().rotationYaw, Double.parseDouble(msg.substring(7).trim()));
PathingBehavior.INSTANCE.setGoal(goal);
2018-09-11 20:45:43 +00:00
logDirect("Goal: " + goal);
2018-08-14 03:57:29 +00:00
event.cancel();
return;
}
2018-08-23 16:54:29 +00:00
if (msg.toLowerCase().startsWith("list") || msg.toLowerCase().startsWith("get ") || msg.toLowerCase().startsWith("show")) {
String waypointType = msg.toLowerCase().substring(4).trim();
if (waypointType.endsWith("s")) {
// for example, "show deaths"
waypointType = waypointType.substring(0, waypointType.length() - 1);
}
2018-08-28 01:06:38 +00:00
Waypoint.Tag tag = Waypoint.Tag.fromString(waypointType);
2018-08-23 16:54:29 +00:00
if (tag == null) {
2018-09-11 20:45:43 +00:00
logDirect("Not a valid tag. Tags are: " + Arrays.asList(Waypoint.Tag.values()).toString().toLowerCase());
2018-08-23 16:54:29 +00:00
event.cancel();
return;
}
Set<Waypoint> waypoints = WorldProvider.INSTANCE.getCurrentWorld().waypoints.getByTag(tag);
// might as well show them from oldest to newest
List<Waypoint> sorted = new ArrayList<>(waypoints);
2018-08-23 16:56:57 +00:00
sorted.sort(Comparator.comparingLong(Waypoint::creationTimestamp));
2018-09-11 20:45:43 +00:00
logDirect("Waypoints under tag " + tag + ":");
2018-08-23 16:54:29 +00:00
for (Waypoint waypoint : sorted) {
2018-09-11 20:45:43 +00:00
logDirect(waypoint.toString());
2018-08-23 16:54:29 +00:00
}
event.cancel();
return;
}
if (msg.toLowerCase().startsWith("goto")) {
String waypointType = msg.toLowerCase().substring(4).trim();
2018-09-01 18:31:42 +00:00
if (waypointType.endsWith("s") && Waypoint.Tag.fromString(waypointType.substring(0, waypointType.length() - 1)) != null) {
2018-08-23 16:54:29 +00:00
// for example, "show deaths"
waypointType = waypointType.substring(0, waypointType.length() - 1);
}
2018-08-28 01:06:38 +00:00
Waypoint.Tag tag = Waypoint.Tag.fromString(waypointType);
2018-08-23 16:54:29 +00:00
if (tag == null) {
String mining = waypointType;
Block block = ChunkPacker.stringToBlock(mining);
2018-09-11 20:45:43 +00:00
//logDirect("Not a valid tag. Tags are: " + Arrays.asList(Waypoint.Tag.values()).toString().toLowerCase());
2018-08-23 16:54:29 +00:00
event.cancel();
if (block == null) {
2018-09-11 20:45:43 +00:00
logDirect("No locations for " + mining + " known, cancelling");
return;
}
List<BlockPos> locs = MineBehavior.scanFor(Collections.singletonList(block), 64);
if (locs.isEmpty()) {
2018-09-11 20:45:43 +00:00
logDirect("No locations for " + mining + " known, cancelling");
return;
}
PathingBehavior.INSTANCE.setGoal(new GoalComposite(locs.stream().map(GoalGetToBlock::new).toArray(Goal[]::new)));
PathingBehavior.INSTANCE.path();
2018-08-23 16:54:29 +00:00
return;
}
Waypoint waypoint = WorldProvider.INSTANCE.getCurrentWorld().waypoints.getMostRecentByTag(tag);
if (waypoint == null) {
2018-09-11 20:45:43 +00:00
logDirect("None saved for tag " + tag);
2018-08-23 16:54:29 +00:00
event.cancel();
return;
}
Goal goal = new GoalBlock(waypoint.location);
PathingBehavior.INSTANCE.setGoal(goal);
2018-08-28 18:43:28 +00:00
if (!PathingBehavior.INSTANCE.path()) {
2018-09-09 20:09:56 +00:00
if (!goal.isInGoal(playerFeet())) {
2018-09-11 20:45:43 +00:00
logDirect("Currently executing a path. Please cancel it first.");
2018-09-09 20:09:56 +00:00
}
2018-08-28 18:43:28 +00:00
}
2018-08-23 16:54:29 +00:00
event.cancel();
return;
}
2018-08-23 00:56:08 +00:00
if (msg.toLowerCase().equals("spawn") || msg.toLowerCase().equals("bed")) {
Waypoint waypoint = WorldProvider.INSTANCE.getCurrentWorld().waypoints.getMostRecentByTag(Waypoint.Tag.BED);
if (waypoint == null) {
BlockPos spawnPoint = player().getBedLocation();
// for some reason the default spawnpoint is underground sometimes
Goal goal = new GoalXZ(spawnPoint.getX(), spawnPoint.getZ());
2018-09-11 20:45:43 +00:00
logDirect("spawn not saved, defaulting to world spawn. set goal to " + goal);
2018-08-23 00:56:08 +00:00
PathingBehavior.INSTANCE.setGoal(goal);
} else {
Goal goal = new GoalBlock(waypoint.location);
PathingBehavior.INSTANCE.setGoal(goal);
2018-09-11 20:45:43 +00:00
logDirect("Set goal to most recent bed " + goal);
2018-08-23 00:56:08 +00:00
}
2018-08-20 05:28:47 +00:00
event.cancel();
return;
}
2018-08-23 00:49:55 +00:00
if (msg.toLowerCase().equals("sethome")) {
WorldProvider.INSTANCE.getCurrentWorld().waypoints.addWaypoint(new Waypoint("", Waypoint.Tag.HOME, playerFeet()));
2018-09-11 20:45:43 +00:00
logDirect("Saved. Say home to set goal.");
2018-08-23 00:49:55 +00:00
event.cancel();
return;
}
if (msg.toLowerCase().equals("home")) {
Waypoint waypoint = WorldProvider.INSTANCE.getCurrentWorld().waypoints.getMostRecentByTag(Waypoint.Tag.HOME);
if (waypoint == null) {
2018-09-11 20:45:43 +00:00
logDirect("home not saved");
2018-08-23 00:49:55 +00:00
} else {
Goal goal = new GoalBlock(waypoint.location);
PathingBehavior.INSTANCE.setGoal(goal);
2018-09-11 20:45:43 +00:00
logDirect("Set goal to saved home " + goal);
2018-08-23 00:49:55 +00:00
}
event.cancel();
return;
}
if (msg.toLowerCase().equals("costs")) {
Movement[] movements = AStarPathFinder.getConnectedPositions(new BetterBlockPos(playerFeet()), new CalculationContext());
List<Movement> moves = new ArrayList<>(Arrays.asList(movements));
2018-09-09 20:19:48 +00:00
while (moves.contains(null)) {
moves.remove(null);
}
moves.sort(Comparator.comparingDouble(movement -> movement.getCost(new CalculationContext())));
for (Movement move : moves) {
String[] parts = move.getClass().toString().split("\\.");
double cost = move.getCost(new CalculationContext());
String strCost = cost + "";
if (cost >= ActionCosts.COST_INF) {
strCost = "IMPOSSIBLE";
}
2018-09-11 20:45:43 +00:00
logDirect(parts[parts.length - 1] + " " + move.getDest().getX() + "," + move.getDest().getY() + "," + move.getDest().getZ() + " " + strCost);
}
event.cancel();
return;
}
List<Settings.Setting<Boolean>> toggleable = Baritone.settings().getAllValuesByType(Boolean.class);
2018-08-14 22:04:41 +00:00
for (Settings.Setting<Boolean> setting : toggleable) {
if (msg.equalsIgnoreCase(setting.getName())) {
2018-08-14 22:04:41 +00:00
setting.value ^= true;
event.cancel();
2018-09-11 20:45:43 +00:00
logDirect("Toggled " + setting.getName() + " to " + setting.value);
2018-08-14 22:04:41 +00:00
return;
}
}
if (msg.toLowerCase().equals("baritone") || msg.toLowerCase().equals("settings")) {
for (Settings.Setting<?> setting : Baritone.settings().allSettings) {
2018-09-11 20:45:43 +00:00
logDirect(setting.toString());
}
event.cancel();
return;
}
if (msg.contains(" ")) {
String[] data = msg.split(" ");
if (data.length == 2) {
Settings.Setting setting = Baritone.settings().byLowerName.get(data[0].toLowerCase());
if (setting != null) {
try {
if (setting.value.getClass() == Long.class) {
setting.value = Long.parseLong(data[1]);
}
if (setting.value.getClass() == Integer.class) {
setting.value = Integer.parseInt(data[1]);
}
if (setting.value.getClass() == Double.class) {
setting.value = Double.parseDouble(data[1]);
}
} catch (NumberFormatException e) {
2018-09-11 20:45:43 +00:00
logDirect("Unable to parse " + data[1]);
event.cancel();
return;
}
2018-09-11 20:45:43 +00:00
logDirect(setting.toString());
event.cancel();
return;
}
}
}
if (Baritone.settings().byLowerName.containsKey(msg.toLowerCase())) {
Settings.Setting<?> setting = Baritone.settings().byLowerName.get(msg.toLowerCase());
2018-09-11 20:45:43 +00:00
logDirect(setting.toString());
event.cancel();
return;
}
2018-08-14 03:57:29 +00:00
}
}