diff --git a/README.md b/README.md index 445d34bb..e458484a 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/build.gradle b/build.gradle index e0b556a2..6120aa47 100755 --- a/build.gradle +++ b/build.gradle @@ -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 } diff --git a/scripts/proguard.pro b/scripts/proguard.pro index a13a0e3f..270a0631 100644 --- a/scripts/proguard.pro +++ b/scripts/proguard.pro @@ -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 { diff --git a/src/api/java/baritone/api/pathing/goals/GoalRunAway.java b/src/api/java/baritone/api/pathing/goals/GoalRunAway.java index 44c5fa80..3f4a02de 100644 --- a/src/api/java/baritone/api/pathing/goals/GoalRunAway.java +++ b/src/api/java/baritone/api/pathing/goals/GoalRunAway.java @@ -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 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 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); } diff --git a/src/api/java/baritone/api/utils/PathCalculationResult.java b/src/api/java/baritone/api/utils/PathCalculationResult.java index 69d2a9b3..df009952 100644 --- a/src/api/java/baritone/api/utils/PathCalculationResult.java +++ b/src/api/java/baritone/api/utils/PathCalculationResult.java @@ -23,14 +23,26 @@ import java.util.Optional; public class PathCalculationResult { - public final Optional path; - public final Type type; + private final IPath path; + private final Type type; - public PathCalculationResult(Type type, Optional path) { + public PathCalculationResult(Type type) { + this(type, null); + } + + public PathCalculationResult(Type type, IPath path) { this.path = path; this.type = type; } + public final Optional getPath() { + return Optional.ofNullable(this.path); + } + + public final Type getType() { + return this.type; + } + public enum Type { SUCCESS_TO_GOAL, SUCCESS_SEGMENT, diff --git a/src/comms/java/comms/BufferedConnection.java b/src/comms/java/comms/BufferedConnection.java new file mode 100644 index 00000000..450010e7 --- /dev/null +++ b/src/comms/java/comms/BufferedConnection.java @@ -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 . + */ + +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? + *

+ * Do you prefer just being able to get a list of all messages received since the last tick? + *

+ * If so, this class is for you! + * + * @author leijurv + */ +public class BufferedConnection implements IConnection { + private final IConnection wrapped; + private final LinkedBlockingQueue 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 receiveMessagesNonBlocking() throws IOException { + ArrayList 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; + } +} diff --git a/src/comms/java/comms/ConstructingDeserializer.java b/src/comms/java/comms/ConstructingDeserializer.java new file mode 100644 index 00000000..c6407976 --- /dev/null +++ b/src/comms/java/comms/ConstructingDeserializer.java @@ -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 . + */ + +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> 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) 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 klass) { + return (byte) MSGS.indexOf(klass); + } +} diff --git a/src/comms/java/comms/IConnection.java b/src/comms/java/comms/IConnection.java new file mode 100644 index 00000000..dc54beb0 --- /dev/null +++ b/src/comms/java/comms/IConnection.java @@ -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 . + */ + +package comms; + +import java.io.IOException; + +public interface IConnection { + void sendMessage(iMessage message) throws IOException; + + iMessage receiveMessage() throws IOException; + + void close(); +} diff --git a/src/comms/java/comms/IMessageListener.java b/src/comms/java/comms/IMessageListener.java new file mode 100644 index 00000000..afed4cfe --- /dev/null +++ b/src/comms/java/comms/IMessageListener.java @@ -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 . + */ + +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 + } +} \ No newline at end of file diff --git a/src/comms/java/comms/MessageDeserializer.java b/src/comms/java/comms/MessageDeserializer.java new file mode 100644 index 00000000..aece828d --- /dev/null +++ b/src/comms/java/comms/MessageDeserializer.java @@ -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 . + */ + +package comms; + +import java.io.DataInputStream; +import java.io.IOException; + +public interface MessageDeserializer { + iMessage deserialize(DataInputStream in) throws IOException; +} diff --git a/src/comms/java/comms/Pipe.java b/src/comms/java/comms/Pipe.java new file mode 100644 index 00000000..c189af60 --- /dev/null +++ b/src/comms/java/comms/Pipe.java @@ -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 . + */ + +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 { + private final LinkedBlockingQueue> AtoB; + private final LinkedBlockingQueue> 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> in; + private final LinkedBlockingQueue> out; + + private PipedConnection(LinkedBlockingQueue> in, LinkedBlockingQueue> 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 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) { + } + } + } +} diff --git a/src/comms/java/comms/SerializedConnection.java b/src/comms/java/comms/SerializedConnection.java new file mode 100644 index 00000000..0952854d --- /dev/null +++ b/src/comms/java/comms/SerializedConnection.java @@ -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 . + */ + +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) { + } + } +} \ No newline at end of file diff --git a/src/comms/java/comms/SocketConnection.java b/src/comms/java/comms/SocketConnection.java new file mode 100644 index 00000000..e40d5a1a --- /dev/null +++ b/src/comms/java/comms/SocketConnection.java @@ -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 . + */ + +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()); + } +} diff --git a/src/comms/java/comms/downward/MessageChat.java b/src/comms/java/comms/downward/MessageChat.java new file mode 100644 index 00000000..b04d2a31 --- /dev/null +++ b/src/comms/java/comms/downward/MessageChat.java @@ -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 . + */ + +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); + } +} diff --git a/src/comms/java/comms/iMessage.java b/src/comms/java/comms/iMessage.java new file mode 100644 index 00000000..67ec2965 --- /dev/null +++ b/src/comms/java/comms/iMessage.java @@ -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 . + */ + +package comms; + +import java.io.DataOutputStream; +import java.io.IOException; + +/** + * hell yeah + *

+ *

+ * dumb android users cant read this file + *

+ * + * @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); +} diff --git a/src/comms/java/comms/upward/MessageStatus.java b/src/comms/java/comms/upward/MessageStatus.java new file mode 100644 index 00000000..55a29828 --- /dev/null +++ b/src/comms/java/comms/upward/MessageStatus.java @@ -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 . + */ + +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); + } +} diff --git a/src/main/java/baritone/Baritone.java b/src/main/java/baritone/Baritone.java index 60707aac..951bcaa6 100755 --- a/src/main/java/baritone/Baritone.java +++ b/src/main/java/baritone/Baritone.java @@ -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 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 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; - } } diff --git a/src/main/java/baritone/behavior/ControllerBehavior.java b/src/main/java/baritone/behavior/ControllerBehavior.java new file mode 100644 index 00000000..f1deb618 --- /dev/null +++ b/src/main/java/baritone/behavior/ControllerBehavior.java @@ -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 . + */ + +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 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); + } +} diff --git a/src/main/java/baritone/behavior/PathingBehavior.java b/src/main/java/baritone/behavior/PathingBehavior.java index 78cd0913..1943b215 100644 --- a/src/main/java/baritone/behavior/PathingBehavior.java +++ b/src/main/java/baritone/behavior/PathingBehavior.java @@ -403,7 +403,7 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, timeout = Baritone.settings().planAheadTimeoutMS.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 path = calcResult.path; + Optional 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 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> 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 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); } diff --git a/src/main/java/baritone/pathing/calc/AStarPathFinder.java b/src/main/java/baritone/pathing/calc/AStarPathFinder.java index c9b7a11e..f6592094 100644 --- a/src/main/java/baritone/pathing/calc/AStarPathFinder.java +++ b/src/main/java/baritone/pathing/calc/AStarPathFinder.java @@ -39,10 +39,10 @@ import java.util.Optional; */ public final class AStarPathFinder extends AbstractNodeCostSearch implements Helper { - private final Optional> favoredPositions; + private final HashSet favoredPositions; private final CalculationContext calcContext; - public AStarPathFinder(int startX, int startY, int startZ, Goal goal, Optional> favoredPositions, CalculationContext context) { + public AStarPathFinder(int startX, int startY, int startZ, Goal goal, HashSet 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 favored = favoredPositions.orElse(null); + HashSet 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(); diff --git a/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java b/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java index fa55a998..ac4efa4d 100644 --- a/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java +++ b/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java @@ -89,16 +89,15 @@ public abstract class AbstractNodeCostSearch implements IPathFinder { } this.cancelRequested = false; try { - Optional 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 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() { diff --git a/src/main/java/baritone/process/MineProcess.java b/src/main/java/baritone/process/MineProcess.java index 60b36ea1..204e11c5 100644 --- a/src/main/java/baritone/process/MineProcess.java +++ b/src/main/java/baritone/process/MineProcess.java @@ -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; diff --git a/src/main/java/baritone/utils/ExampleBaritoneControl.java b/src/main/java/baritone/utils/ExampleBaritoneControl.java index 55e35fdc..cc79e13e 100644 --- a/src/main/java/baritone/utils/ExampleBaritoneControl.java +++ b/src/main/java/baritone/utils/ExampleBaritoneControl.java @@ -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"); } diff --git a/src/main/java/baritone/utils/InputOverrideHandler.java b/src/main/java/baritone/utils/InputOverrideHandler.java index 191d145a..c6e19c15 100755 --- a/src/main/java/baritone/utils/InputOverrideHandler.java +++ b/src/main/java/baritone/utils/InputOverrideHandler.java @@ -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); }