Merge branch 'comms' into bot-system

This commit is contained in:
Leijurv
2018-11-18 17:39:35 -08:00
24 changed files with 765 additions and 71 deletions
+1 -2
View File
@@ -58,8 +58,7 @@ Quick start example: `thisway 1000` or `goal 70` to set the goal, `path` to actu
BaritoneAPI.getSettings().allowSprint.value = true;
BaritoneAPI.getSettings().pathTimeoutMS.value = 2000L;
BaritoneAPI.getPathingBehavior().setGoal(new GoalXZ(10000, 20000));
BaritoneAPI.getPathingBehavior().path();
BaritoneAPI.getProvider().getPrimaryBaritone().getCustomGoalProcess().setGoalAndPath(new GoalXZ(10000, 20000));
```
# FAQ
+5 -1
View File
@@ -51,6 +51,10 @@ compileJava {
}
sourceSets {
comms {}
main {
compileClasspath += comms.compileClasspath + comms.output
}
launch {
compileClasspath += main.compileClasspath + main.runtimeClasspath + main.output
}
@@ -99,7 +103,7 @@ mixin {
}
jar {
from sourceSets.launch.output, sourceSets.api.output
from sourceSets.comms.output, sourceSets.launch.output, sourceSets.api.output
preserveFileTimestamps = false
reproducibleFileOrder = true
}
+1 -1
View File
@@ -19,7 +19,7 @@
-keep class baritone.api.IBaritoneProvider
# hack
-keep class baritone.utils.ExampleBaritoneControl { *; }
-keep class baritone.utils.ExampleBaritoneControl { *; } # have to include this string to remove this keep in the standalone build: # this is the keep api
# setting names are reflected from field names, so keep field names
-keepclassmembers class baritone.api.Settings {
@@ -20,7 +20,6 @@ package baritone.api.pathing.goals;
import net.minecraft.util.math.BlockPos;
import java.util.Arrays;
import java.util.Optional;
/**
* Useful for automated combat (retreating specifically)
@@ -33,13 +32,13 @@ public class GoalRunAway implements Goal {
private final double distanceSq;
private final Optional<Integer> maintainY;
private final Integer maintainY;
public GoalRunAway(double distance, BlockPos... from) {
this(distance, Optional.empty(), from);
this(distance, null, from);
}
public GoalRunAway(double distance, Optional<Integer> maintainY, BlockPos... from) {
public GoalRunAway(double distance, Integer maintainY, BlockPos... from) {
if (from.length == 0) {
throw new IllegalArgumentException();
}
@@ -50,7 +49,7 @@ public class GoalRunAway implements Goal {
@Override
public boolean isInGoal(int x, int y, int z) {
if (maintainY.isPresent() && maintainY.get() != y) {
if (maintainY != null && maintainY != y) {
return false;
}
for (BlockPos p : from) {
@@ -74,16 +73,16 @@ public class GoalRunAway implements Goal {
}
}
min = -min;
if (maintainY.isPresent()) {
min = min * 0.6 + GoalYLevel.calculate(maintainY.get(), y) * 1.5;
if (maintainY != null) {
min = min * 0.6 + GoalYLevel.calculate(maintainY, y) * 1.5;
}
return min;
}
@Override
public String toString() {
if (maintainY.isPresent()) {
return "GoalRunAwayFromMaintainY y=" + maintainY.get() + ", " + Arrays.asList(from);
if (maintainY != null) {
return "GoalRunAwayFromMaintainY y=" + maintainY + ", " + Arrays.asList(from);
} else {
return "GoalRunAwayFrom" + Arrays.asList(from);
}
@@ -23,14 +23,26 @@ import java.util.Optional;
public class PathCalculationResult {
public final Optional<IPath> path;
public final Type type;
private final IPath path;
private final Type type;
public PathCalculationResult(Type type, Optional<IPath> path) {
public PathCalculationResult(Type type) {
this(type, null);
}
public PathCalculationResult(Type type, IPath path) {
this.path = path;
this.type = type;
}
public final Optional<IPath> getPath() {
return Optional.ofNullable(this.path);
}
public final Type getType() {
return this.type;
}
public enum Type {
SUCCESS_TO_GOAL,
SUCCESS_SEGMENT,
@@ -0,0 +1,86 @@
/*
* 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 comms;
import java.io.EOFException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
/**
* Do you not like having a blocking "receiveMessage" thingy?
* <p>
* Do you prefer just being able to get a list of all messages received since the last tick?
* <p>
* If so, this class is for you!
*
* @author leijurv
*/
public class BufferedConnection implements IConnection {
private final IConnection wrapped;
private final LinkedBlockingQueue<iMessage> queue;
private volatile transient IOException thrownOnRead;
public BufferedConnection(IConnection wrapped) {
this(wrapped, Integer.MAX_VALUE); // LinkedBlockingQueue accepts this as "no limit"
}
public BufferedConnection(IConnection wrapped, int maxInternalQueueSize) {
this.wrapped = wrapped;
this.queue = new LinkedBlockingQueue<>();
this.thrownOnRead = null;
new Thread(() -> {
try {
while (thrownOnRead == null) {
queue.put(wrapped.receiveMessage());
}
} catch (IOException e) {
thrownOnRead = e;
} catch (InterruptedException e) {
thrownOnRead = new IOException("Interrupted while enqueueing", e);
}
}).start();
}
@Override
public void sendMessage(iMessage message) throws IOException {
wrapped.sendMessage(message);
}
@Override
public iMessage receiveMessage() {
throw new UnsupportedOperationException("BufferedConnection can only be read from non-blockingly");
}
@Override
public void close() {
wrapped.close();
thrownOnRead = new EOFException("Closed");
}
public List<iMessage> receiveMessagesNonBlocking() throws IOException {
ArrayList<iMessage> msgs = new ArrayList<>();
queue.drainTo(msgs); // preserves order -- first message received will be first in this arraylist
if (msgs.isEmpty() && thrownOnRead != null) {
IOException up = new IOException("BufferedConnection wrapped", thrownOnRead);
throw up;
}
return msgs;
}
}
@@ -0,0 +1,54 @@
/*
* 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 comms;
import java.io.DataInputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
public enum ConstructingDeserializer implements MessageDeserializer {
INSTANCE;
private final List<Class<? extends iMessage>> MSGS;
ConstructingDeserializer() {
MSGS = new ArrayList<>();
// imagine doing something in reflect but it's actually concise and you don't need to catch 42069 different exceptions. huh.
for (Method m : IMessageListener.class.getDeclaredMethods()) {
if (m.getName().equals("handle")) {
MSGS.add((Class<? extends iMessage>) m.getParameterTypes()[0]);
}
}
}
@Override
public iMessage deserialize(DataInputStream in) throws IOException {
int type = ((int) in.readByte()) & 0xff;
try {
return MSGS.get(type).getConstructor(DataInputStream.class).newInstance(in);
} catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException ex) {
throw new IOException("Unknown message type " + type, ex);
}
}
public byte getHeader(Class<? extends iMessage> klass) {
return (byte) MSGS.indexOf(klass);
}
}
+28
View File
@@ -0,0 +1,28 @@
/*
* 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 comms;
import java.io.IOException;
public interface IConnection {
void sendMessage(iMessage message) throws IOException;
iMessage receiveMessage() throws IOException;
void close();
}
@@ -0,0 +1,36 @@
/*
* 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 comms;
import comms.downward.MessageChat;
import comms.upward.MessageStatus;
public interface IMessageListener {
default void handle(MessageStatus message) {
unhandled(message);
}
default void handle(MessageChat message) {
unhandled(message);
}
default void unhandled(iMessage msg) {
// can override this to throw UnsupportedOperationException, if you want to make sure you're handling everything
// default is to silently ignore messages without handlers
}
}
@@ -0,0 +1,25 @@
/*
* 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 comms;
import java.io.DataInputStream;
import java.io.IOException;
public interface MessageDeserializer {
iMessage deserialize(DataInputStream in) throws IOException;
}
+99
View File
@@ -0,0 +1,99 @@
/*
* 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 comms;
import java.io.EOFException;
import java.io.IOException;
import java.util.Optional;
import java.util.concurrent.LinkedBlockingQueue;
/**
* Do you want a socket to localhost without actually making a gross real socket to localhost?
*/
public class Pipe<T extends iMessage> {
private final LinkedBlockingQueue<Optional<iMessage>> AtoB;
private final LinkedBlockingQueue<Optional<iMessage>> BtoA;
private final PipedConnection A;
private final PipedConnection B;
private volatile boolean closed;
public Pipe() {
this.AtoB = new LinkedBlockingQueue<>();
this.BtoA = new LinkedBlockingQueue<>();
this.A = new PipedConnection(BtoA, AtoB);
this.B = new PipedConnection(AtoB, BtoA);
}
public PipedConnection getA() {
return A;
}
public PipedConnection getB() {
return B;
}
public class PipedConnection implements IConnection {
private final LinkedBlockingQueue<Optional<iMessage>> in;
private final LinkedBlockingQueue<Optional<iMessage>> out;
private PipedConnection(LinkedBlockingQueue<Optional<iMessage>> in, LinkedBlockingQueue<Optional<iMessage>> out) {
this.in = in;
this.out = out;
}
@Override
public void sendMessage(iMessage message) throws IOException {
if (closed) {
throw new EOFException("Closed");
}
try {
out.put(Optional.of(message));
} catch (InterruptedException e) {
// this can never happen since the LinkedBlockingQueues are not constructed with a maximum capacity, see above
}
}
@Override
public iMessage receiveMessage() throws IOException {
if (closed) {
throw new EOFException("Closed");
}
try {
Optional<iMessage> t = in.take();
if (!t.isPresent()) {
throw new EOFException("Closed");
}
return t.get();
} catch (InterruptedException e) {
// again, cannot happen
// but we have to throw something
throw new IllegalStateException(e);
}
}
@Override
public void close() {
closed = true;
try {
AtoB.put(Optional.empty()); // unstick threads
BtoA.put(Optional.empty());
} catch (InterruptedException e) {
}
}
}
}
@@ -0,0 +1,59 @@
/*
* 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 comms;
import java.io.*;
public class SerializedConnection implements IConnection {
private final DataInputStream in;
private final DataOutputStream out;
private final MessageDeserializer deserializer;
public SerializedConnection(InputStream in, OutputStream out) {
this(ConstructingDeserializer.INSTANCE, in, out);
}
public SerializedConnection(MessageDeserializer d, InputStream in, OutputStream out) {
this.in = new DataInputStream(in);
this.out = new DataOutputStream(out);
this.deserializer = d;
}
@Override
public void sendMessage(iMessage message) throws IOException {
message.writeHeader(out);
message.write(out);
}
@Override
public iMessage receiveMessage() throws IOException {
return deserializer.deserialize(in);
}
@Override
public void close() {
try {
in.close();
} catch (IOException e) {
}
try {
out.close();
} catch (IOException e) {
}
}
}
@@ -0,0 +1,27 @@
/*
* 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 comms;
import java.io.IOException;
import java.net.Socket;
public class SocketConnection extends SerializedConnection {
public SocketConnection(Socket s) throws IOException {
super(s.getInputStream(), s.getOutputStream());
}
}
@@ -0,0 +1,48 @@
/*
* 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 comms.downward;
import comms.IMessageListener;
import comms.iMessage;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class MessageChat implements iMessage {
public final String msg;
public MessageChat(DataInputStream in) throws IOException {
this.msg = in.readUTF();
}
public MessageChat(String msg) {
this.msg = msg;
}
@Override
public void write(DataOutputStream out) throws IOException {
out.writeUTF(msg);
}
@Override
public void handle(IMessageListener listener) {
listener.handle(this);
}
}
+44
View File
@@ -0,0 +1,44 @@
/*
* 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 comms;
import java.io.DataOutputStream;
import java.io.IOException;
/**
* hell yeah
* <p>
* <p>
* dumb android users cant read this file
* <p>
*
* @author leijurv
*/
public interface iMessage {
void write(DataOutputStream out) throws IOException;
default void writeHeader(DataOutputStream out) throws IOException {
out.writeByte(getHeader());
}
default byte getHeader() {
return ConstructingDeserializer.INSTANCE.getHeader(getClass());
}
void handle(IMessageListener listener);
}
@@ -0,0 +1,56 @@
/*
* 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 comms.upward;
import comms.IMessageListener;
import comms.iMessage;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class MessageStatus implements iMessage {
public final double x;
public final double y;
public final double z;
public MessageStatus(DataInputStream in) throws IOException {
this.x = in.readDouble();
this.y = in.readDouble();
this.z = in.readDouble();
}
public MessageStatus(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
@Override
public void write(DataOutputStream out) throws IOException {
out.writeDouble(x);
out.writeDouble(y);
out.writeDouble(z);
}
@Override
public void handle(IMessageListener listener) {
listener.handle(this);
}
}
+31 -28
View File
@@ -22,10 +22,7 @@ import baritone.api.IBaritone;
import baritone.api.Settings;
import baritone.api.event.listener.IEventBus;
import baritone.api.utils.IPlayerContext;
import baritone.behavior.Behavior;
import baritone.behavior.LookBehavior;
import baritone.behavior.MemoryBehavior;
import baritone.behavior.PathingBehavior;
import baritone.behavior.*;
import baritone.cache.WorldProvider;
import baritone.event.GameEventHandler;
import baritone.process.CustomGoalProcess;
@@ -76,6 +73,7 @@ public class Baritone implements IBaritone {
private final GameEventHandler gameEventHandler;
private List<Behavior> behaviors;
private ControllerBehavior controllerBehavior;
private PathingBehavior pathingBehavior;
private LookBehavior lookBehavior;
private MemoryBehavior memoryBehavior;
@@ -104,6 +102,7 @@ public class Baritone implements IBaritone {
this.behaviors = new ArrayList<>();
{
// the Behavior constructor calls baritone.registerBehavior(this) so this populates the behaviors arraylist
controllerBehavior = new ControllerBehavior(this);
pathingBehavior = new PathingBehavior(this);
lookBehavior = new LookBehavior(this);
memoryBehavior = new MemoryBehavior(this);
@@ -128,10 +127,6 @@ public class Baritone implements IBaritone {
this.initialized = true;
}
public PathingControlManager getPathingControlManager() {
return this.pathingControlManager;
}
public List<Behavior> getBehaviors() {
return this.behaviors;
}
@@ -141,11 +136,19 @@ public class Baritone implements IBaritone {
this.gameEventHandler.registerEventListener(behavior);
}
public PathingControlManager getPathingControlManager() {
return this.pathingControlManager;
}
@Override
public InputOverrideHandler getInputOverrideHandler() {
return this.inputOverrideHandler;
}
public ControllerBehavior getControllerBehavior() {
return this.controllerBehavior;
}
@Override
public CustomGoalProcess getCustomGoalProcess() { // Iffy
return this.customGoalProcess;
@@ -157,18 +160,8 @@ public class Baritone implements IBaritone {
}
@Override
public IPlayerContext getPlayerContext() {
return this.playerContext;
}
@Override
public FollowProcess getFollowProcess() {
return this.followProcess;
}
@Override
public LookBehavior getLookBehavior() {
return this.lookBehavior;
public PathingBehavior getPathingBehavior() {
return this.pathingBehavior;
}
@Override
@@ -177,13 +170,13 @@ public class Baritone implements IBaritone {
}
@Override
public MineProcess getMineProcess() {
return this.mineProcess;
public IPlayerContext getPlayerContext() {
return this.playerContext;
}
@Override
public PathingBehavior getPathingBehavior() {
return this.pathingBehavior;
public FollowProcess getFollowProcess() {
return this.followProcess;
}
@Override
@@ -196,6 +189,20 @@ public class Baritone implements IBaritone {
return this.gameEventHandler;
}
@Override
public LookBehavior getLookBehavior() {
return this.lookBehavior;
}
@Override
public MineProcess getMineProcess() {
return this.mineProcess;
}
public static Executor getExecutor() {
return threadPool;
}
public static Settings settings() {
return BaritoneAPI.getSettings();
}
@@ -203,8 +210,4 @@ public class Baritone implements IBaritone {
public static File getDir() {
return dir;
}
public static Executor getExecutor() {
return threadPool;
}
}
@@ -0,0 +1,103 @@
/*
* 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.behavior;
import baritone.Baritone;
import baritone.api.event.events.ChatEvent;
import baritone.api.event.events.TickEvent;
import baritone.utils.Helper;
import comms.BufferedConnection;
import comms.IConnection;
import comms.IMessageListener;
import comms.downward.MessageChat;
import comms.iMessage;
import comms.upward.MessageStatus;
import java.io.IOException;
import java.util.List;
public class ControllerBehavior extends Behavior implements IMessageListener {
public ControllerBehavior(Baritone baritone) {
super(baritone);
}
private BufferedConnection conn;
@Override
public void onTick(TickEvent event) {
if (event.getType() == TickEvent.Type.OUT) {
return;
}
trySend(new MessageStatus(ctx.player().posX, ctx.player().posY, ctx.player().posZ));
readAndHandle();
}
private void readAndHandle() {
if (conn == null) {
return;
}
try {
List<iMessage> msgs = conn.receiveMessagesNonBlocking();
msgs.forEach(msg -> msg.handle(this));
} catch (IOException e) {
e.printStackTrace();
disconnect();
}
}
public boolean trySend(iMessage msg) {
if (conn == null) {
return false;
}
try {
conn.sendMessage(msg);
return true;
} catch (IOException e) {
e.printStackTrace();
disconnect();
return false;
}
}
public void connectTo(IConnection conn) {
disconnect();
if (conn instanceof BufferedConnection) {
this.conn = (BufferedConnection) conn;
} else {
this.conn = new BufferedConnection(conn);
}
}
public void disconnect() {
if (conn != null) {
conn.close();
}
conn = null;
}
@Override
public void handle(MessageChat msg) { // big brain
ChatEvent event = new ChatEvent(ctx.player(), msg.msg);
baritone.getGameEventHandler().onSendChatMessage(event);
}
@Override
public void unhandled(iMessage msg) {
Helper.HELPER.logDebug("Unhandled message received by ControllerBehavior " + msg);
}
}
@@ -403,7 +403,7 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior,
timeout = Baritone.settings().planAheadTimeoutMS.<Long>get();
}
CalculationContext context = new CalculationContext(baritone); // not safe to create on the other thread, it looks up a lot of stuff in minecraft
AbstractNodeCostSearch pathfinder = createPathfinder(start, goal, Optional.ofNullable(current).map(PathExecutor::getPath), context);
AbstractNodeCostSearch pathfinder = createPathfinder(start, goal, current == null ? null : current.getPath(), context);
inProgress = pathfinder;
Baritone.getExecutor().execute(() -> {
if (talkAboutIt) {
@@ -411,7 +411,7 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior,
}
PathCalculationResult calcResult = pathfinder.calculate(timeout);
Optional<IPath> path = calcResult.path;
Optional<IPath> path = calcResult.getPath();
if (Baritone.settings().cutoffAtLoadBoundary.get()) {
path = path.map(p -> {
IPath result = p.cutoffAtLoadedChunks(context.world());
@@ -443,7 +443,7 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior,
queuePathEvent(PathEvent.CALC_FINISHED_NOW_EXECUTING);
current = executor.get();
} else {
if (calcResult.type != PathCalculationResult.Type.CANCELLATION && calcResult.type != PathCalculationResult.Type.EXCEPTION) {
if (calcResult.getType() != PathCalculationResult.Type.CANCELLATION && calcResult.getType() != PathCalculationResult.Type.EXCEPTION) {
// don't dispatch CALC_FAILED on cancellation
queuePathEvent(PathEvent.CALC_FAILED);
}
@@ -474,7 +474,7 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior,
});
}
private AbstractNodeCostSearch createPathfinder(BlockPos start, Goal goal, Optional<IPath> previous, CalculationContext context) {
private AbstractNodeCostSearch createPathfinder(BlockPos start, Goal goal, IPath previous, CalculationContext context) {
Goal transformed = goal;
if (Baritone.settings().simplifyUnloadedYCoord.get() && goal instanceof IGoalRenderPos) {
BlockPos pos = ((IGoalRenderPos) goal).getGoalPos();
@@ -483,11 +483,9 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior,
transformed = new GoalXZ(pos.getX(), pos.getZ());
}
}
Optional<HashSet<Long>> favoredPositions;
if (Baritone.settings().backtrackCostFavoringCoefficient.get() == 1D) {
favoredPositions = Optional.empty();
} else {
favoredPositions = previous.map(IPath::positions).map(Collection::stream).map(x -> x.map(BetterBlockPos::longHash)).map(x -> x.collect(Collectors.toList())).map(HashSet::new); // <-- okay this is EPIC
HashSet<Long> favoredPositions = null;
if (Baritone.settings().backtrackCostFavoringCoefficient.get() != 1D && previous != null) {
favoredPositions = previous.positions().stream().map(BetterBlockPos::longHash).collect(Collectors.toCollection(HashSet::new));
}
return new AStarPathFinder(start.getX(), start.getY(), start.getZ(), transformed, favoredPositions, context);
}
@@ -39,10 +39,10 @@ import java.util.Optional;
*/
public final class AStarPathFinder extends AbstractNodeCostSearch implements Helper {
private final Optional<HashSet<Long>> favoredPositions;
private final HashSet<Long> favoredPositions;
private final CalculationContext calcContext;
public AStarPathFinder(int startX, int startY, int startZ, Goal goal, Optional<HashSet<Long>> favoredPositions, CalculationContext context) {
public AStarPathFinder(int startX, int startY, int startZ, Goal goal, HashSet<Long> favoredPositions, CalculationContext context) {
super(startX, startY, startZ, goal, context);
this.favoredPositions = favoredPositions;
this.calcContext = context;
@@ -63,7 +63,7 @@ public final class AStarPathFinder extends AbstractNodeCostSearch implements Hel
bestSoFar[i] = startNode;
}
MutableMoveResult res = new MutableMoveResult();
HashSet<Long> favored = favoredPositions.orElse(null);
HashSet<Long> favored = favoredPositions;
BetterWorldBorder worldBorder = new BetterWorldBorder(calcContext.world().getWorldBorder());
long startTime = System.nanoTime() / 1000000L;
boolean slowPath = Baritone.settings().slowPath.get();
@@ -75,7 +75,7 @@ public final class AStarPathFinder extends AbstractNodeCostSearch implements Hel
int numNodes = 0;
int numMovementsConsidered = 0;
int numEmptyChunk = 0;
boolean favoring = favoredPositions.isPresent();
boolean favoring = favored != null;
int pathingMaxChunkBorderFetch = Baritone.settings().pathingMaxChunkBorderFetch.get(); // grab all settings beforehand so that changing settings during pathing doesn't cause a crash or unpredictable behavior
double favorCoeff = Baritone.settings().backtrackCostFavoringCoefficient.get();
boolean minimumImprovementRepropagation = Baritone.settings().minimumImprovementRepropagation.get();
@@ -89,16 +89,15 @@ public abstract class AbstractNodeCostSearch implements IPathFinder {
}
this.cancelRequested = false;
try {
Optional<IPath> path = calculate0(timeout);
path = path.map(IPath::postProcess);
IPath path = calculate0(timeout).map(IPath::postProcess).orElse(null);
isFinished = true;
if (cancelRequested) {
return new PathCalculationResult(PathCalculationResult.Type.CANCELLATION, path);
}
if (!path.isPresent()) {
return new PathCalculationResult(PathCalculationResult.Type.FAILURE, path);
if (path == null) {
return new PathCalculationResult(PathCalculationResult.Type.FAILURE);
}
if (goal.isInGoal(path.get().getDest())) {
if (goal.isInGoal(path.getDest())) {
return new PathCalculationResult(PathCalculationResult.Type.SUCCESS_TO_GOAL, path);
} else {
return new PathCalculationResult(PathCalculationResult.Type.SUCCESS_SEGMENT, path);
@@ -106,7 +105,7 @@ public abstract class AbstractNodeCostSearch implements IPathFinder {
} catch (Exception e) {
Helper.HELPER.logDebug("Pathing exception: " + e);
e.printStackTrace();
return new PathCalculationResult(PathCalculationResult.Type.EXCEPTION, Optional.empty());
return new PathCalculationResult(PathCalculationResult.Type.EXCEPTION);
} finally {
// this is run regardless of what exception may or may not be raised by calculate0
isFinished = true;
@@ -149,12 +148,7 @@ public abstract class AbstractNodeCostSearch implements IPathFinder {
@Override
public Optional<IPath> pathToMostRecentNodeConsidered() {
try {
return Optional.ofNullable(mostRecentConsidered).map(node -> new Path(startNode, node, 0, goal, context));
} catch (IllegalStateException ex) {
System.out.println("Unable to construct path to render");
return Optional.empty();
}
return Optional.ofNullable(mostRecentConsidered).map(node -> new Path(startNode, node, 0, goal, context));
}
protected int mapSize() {
@@ -141,7 +141,7 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro
// TODO shaft mode, mine 1x1 shafts to either side
// TODO also, see if the GoalRunAway with maintain Y at 11 works even from the surface
if (branchPointRunaway == null) {
branchPointRunaway = new GoalRunAway(1, Optional.of(y), branchPoint) {
branchPointRunaway = new GoalRunAway(1, y, branchPoint) {
@Override
public boolean isInGoal(int x, int y, int z) {
return false;
@@ -33,6 +33,7 @@ import baritone.cache.Waypoint;
import baritone.pathing.movement.Movement;
import baritone.pathing.movement.Moves;
import baritone.process.CustomGoalProcess;
import comms.SocketConnection;
import net.minecraft.block.Block;
import net.minecraft.client.multiplayer.ChunkProviderClient;
import net.minecraft.entity.Entity;
@@ -41,6 +42,8 @@ import net.minecraft.util.Session;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.chunk.Chunk;
import java.io.IOException;
import java.net.Socket;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -465,6 +468,23 @@ public class ExampleBaritoneControl extends Behavior implements Helper {
}
return true;
}
if (msg.startsWith("connect")) {
String dest = msg.substring(7).trim();
String[] parts = dest.split(" ");
if (parts.length != 2) {
logDirect("Unable to parse");
return true;
}
try {
Socket s = new Socket(parts[0], Integer.parseInt(parts[1]));
SocketConnection conn = new SocketConnection(s);
baritone.getControllerBehavior().connectTo(conn);
logDirect("Created and attached socket connection");
} catch (IOException | NumberFormatException e) {
logDirect("Unable to connect " + e);
}
return true;
}
if (msg.equals("damn")) {
logDirect("daniel");
}
@@ -112,7 +112,7 @@ public final class InputOverrideHandler extends Behavior implements IInputOverri
if (event.getType() == TickEvent.Type.OUT) {
return;
}
boolean stillClick = blockBreakHelper.tick(isInputForcedDown(Input.CLICK_LEFT.getKeyBinding()));
boolean stillClick = blockBreakHelper.tick(isInputForcedDown(Input.CLICK_LEFT));
setInputForceState(Input.CLICK_LEFT, stillClick);
}