From 04e87c981096c35aa494c67c06766c931edcd21f Mon Sep 17 00:00:00 2001 From: Leijurv Date: Thu, 15 Nov 2018 16:03:20 -0800 Subject: [PATCH 01/15] cleaner --- src/main/java/baritone/utils/InputOverrideHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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); } From 737c632227b7ab435d74ce2ca266c3327d9ad42a Mon Sep 17 00:00:00 2001 From: Leijurv Date: Fri, 16 Nov 2018 16:28:20 -0800 Subject: [PATCH 02/15] path creation no longer throws this --- .../java/baritone/pathing/calc/AbstractNodeCostSearch.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java b/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java index fa55a998..c2ce36e2 100644 --- a/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java +++ b/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java @@ -149,12 +149,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() { From ce0e8b4cd1caf77dada7a69197992983c5bfabfe Mon Sep 17 00:00:00 2001 From: Brady Date: Sat, 17 Nov 2018 11:18:55 -0600 Subject: [PATCH 03/15] Clean up some bad Optional practices --- .../api/pathing/goals/GoalRunAway.java | 17 ++++++++--------- .../api/utils/PathCalculationResult.java | 18 +++++++++++++++--- .../baritone/behavior/PathingBehavior.java | 4 ++-- .../pathing/calc/AbstractNodeCostSearch.java | 11 +++++------ .../java/baritone/process/MineProcess.java | 2 +- 5 files changed, 31 insertions(+), 21 deletions(-) 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/main/java/baritone/behavior/PathingBehavior.java b/src/main/java/baritone/behavior/PathingBehavior.java index 78cd0913..31f6721f 100644 --- a/src/main/java/baritone/behavior/PathingBehavior.java +++ b/src/main/java/baritone/behavior/PathingBehavior.java @@ -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); } diff --git a/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java b/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java index c2ce36e2..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; 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; From 6caae889b7c578ae359daf9ff2d6db5a886ac293 Mon Sep 17 00:00:00 2001 From: Brady Date: Sat, 17 Nov 2018 11:26:46 -0600 Subject: [PATCH 04/15] Fix AStarPathFinder Optional usage that's icky --- src/main/java/baritone/behavior/PathingBehavior.java | 10 +++++----- .../java/baritone/pathing/calc/AStarPathFinder.java | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/java/baritone/behavior/PathingBehavior.java b/src/main/java/baritone/behavior/PathingBehavior.java index 31f6721f..884fe75b 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) { @@ -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,11 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, transformed = new GoalXZ(pos.getX(), pos.getZ()); } } - Optional> favoredPositions; + HashSet favoredPositions; if (Baritone.settings().backtrackCostFavoringCoefficient.get() == 1D) { - favoredPositions = Optional.empty(); + favoredPositions = null; } 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 + 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(); From 689dc743301a97b3130adfc7249efc8248ca5a05 Mon Sep 17 00:00:00 2001 From: Brady Date: Sat, 17 Nov 2018 11:41:16 -0600 Subject: [PATCH 05/15] Whoops --- src/main/java/baritone/behavior/PathingBehavior.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/baritone/behavior/PathingBehavior.java b/src/main/java/baritone/behavior/PathingBehavior.java index 884fe75b..1943b215 100644 --- a/src/main/java/baritone/behavior/PathingBehavior.java +++ b/src/main/java/baritone/behavior/PathingBehavior.java @@ -483,10 +483,8 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, transformed = new GoalXZ(pos.getX(), pos.getZ()); } } - HashSet favoredPositions; - if (Baritone.settings().backtrackCostFavoringCoefficient.get() == 1D) { - favoredPositions = null; - } else { + 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); From 46de72e28c503330e5d7145ac9c99ee2dafc3c8d Mon Sep 17 00:00:00 2001 From: Brady Date: Sat, 17 Nov 2018 18:07:16 -0600 Subject: [PATCH 06/15] Comms Sourceset --- build.gradle | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/build.gradle b/build.gradle index e0b556a2..1c6f5b9e 100755 --- a/build.gradle +++ b/build.gradle @@ -54,6 +54,10 @@ sourceSets { launch { compileClasspath += main.compileClasspath + main.runtimeClasspath + main.output } + comms {} + main { + compileClasspath += comms.compileClasspath + } } minecraft { From 12bbd57a247d2323f7d3c8cb7b2ba6b363f4470c Mon Sep 17 00:00:00 2001 From: Leijurv Date: Sat, 17 Nov 2018 16:09:03 -0800 Subject: [PATCH 07/15] standalone build shouldn't keep ExampleBaritoneControl --- scripts/proguard.pro | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 { From f014e42aa4b6c4b47680803180ff4fe81b1e8f9e Mon Sep 17 00:00:00 2001 From: Leijurv Date: Sun, 18 Nov 2018 11:01:39 -0800 Subject: [PATCH 08/15] initial comms --- src/comms/java/comms/BufferedConnection.java | 86 ++++++++++++++++ .../java/comms/ConstructingDeserializer.java | 52 ++++++++++ src/comms/java/comms/FilteredConnection.java | 55 +++++++++++ src/comms/java/comms/HandlableMessage.java | 22 +++++ src/comms/java/comms/IConnection.java | 28 ++++++ src/comms/java/comms/IMessageListener.java | 36 +++++++ src/comms/java/comms/MessageDeserializer.java | 25 +++++ src/comms/java/comms/Pipe.java | 99 +++++++++++++++++++ src/comms/java/comms/SerializableMessage.java | 33 +++++++ .../java/comms/SerializedConnection.java | 59 +++++++++++ src/comms/java/comms/SocketConnection.java | 27 +++++ .../java/comms/downward/MessageChat.java | 49 +++++++++ src/comms/java/comms/iMessage.java | 30 ++++++ .../java/comms/upward/MessageStatus.java | 57 +++++++++++ src/main/java/baritone/Baritone.java | 59 +++++------ .../baritone/behavior/ControllerBehavior.java | 99 +++++++++++++++++++ .../utils/ExampleBaritoneControl.java | 20 ++++ 17 files changed, 808 insertions(+), 28 deletions(-) create mode 100644 src/comms/java/comms/BufferedConnection.java create mode 100644 src/comms/java/comms/ConstructingDeserializer.java create mode 100644 src/comms/java/comms/FilteredConnection.java create mode 100644 src/comms/java/comms/HandlableMessage.java create mode 100644 src/comms/java/comms/IConnection.java create mode 100644 src/comms/java/comms/IMessageListener.java create mode 100644 src/comms/java/comms/MessageDeserializer.java create mode 100644 src/comms/java/comms/Pipe.java create mode 100644 src/comms/java/comms/SerializableMessage.java create mode 100644 src/comms/java/comms/SerializedConnection.java create mode 100644 src/comms/java/comms/SocketConnection.java create mode 100644 src/comms/java/comms/downward/MessageChat.java create mode 100644 src/comms/java/comms/iMessage.java create mode 100644 src/comms/java/comms/upward/MessageStatus.java create mode 100644 src/main/java/baritone/behavior/ControllerBehavior.java diff --git a/src/comms/java/comms/BufferedConnection.java b/src/comms/java/comms/BufferedConnection.java new file mode 100644 index 00000000..13fef7f5 --- /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(T message) throws IOException { + wrapped.sendMessage(message); + } + + @Override + public T 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..4048a7be --- /dev/null +++ b/src/comms/java/comms/ConstructingDeserializer.java @@ -0,0 +1,52 @@ +/* + * 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()) { + MSGS.add((Class) m.getParameterTypes()[0]); + } + } + + @Override + public SerializableMessage 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/FilteredConnection.java b/src/comms/java/comms/FilteredConnection.java new file mode 100644 index 00000000..2d740fcd --- /dev/null +++ b/src/comms/java/comms/FilteredConnection.java @@ -0,0 +1,55 @@ +/* + * 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; + +/** + * This is just a meme idk if this is actually useful + * + * @param + */ +public class FilteredConnection implements IConnection { + private final Class klass; + private final IConnection wrapped; + + public FilteredConnection(IConnection wrapped, Class klass) { + this.klass = klass; + this.wrapped = wrapped; + } + + @Override + public void sendMessage(T message) throws IOException { + wrapped.sendMessage(message); + } + + @Override + public T receiveMessage() throws IOException { + while (true) { + iMessage msg = wrapped.receiveMessage(); + if (klass.isInstance(msg)) { + return klass.cast(msg); + } + } + } + + @Override + public void close() { + wrapped.close(); + } +} diff --git a/src/comms/java/comms/HandlableMessage.java b/src/comms/java/comms/HandlableMessage.java new file mode 100644 index 00000000..dce1d3de --- /dev/null +++ b/src/comms/java/comms/HandlableMessage.java @@ -0,0 +1,22 @@ +/* + * 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; + +public interface HandlableMessage { + void handle(IMessageListener listener); +} diff --git a/src/comms/java/comms/IConnection.java b/src/comms/java/comms/IConnection.java new file mode 100644 index 00000000..05cef73d --- /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(T message) throws IOException; + + T 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..ed18bf1a --- /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 { + SerializableMessage 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..a8cad44f --- /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(T 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 T 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/SerializableMessage.java b/src/comms/java/comms/SerializableMessage.java new file mode 100644 index 00000000..90187446 --- /dev/null +++ b/src/comms/java/comms/SerializableMessage.java @@ -0,0 +1,33 @@ +/* + * 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; + +public interface SerializableMessage extends 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()); + } +} diff --git a/src/comms/java/comms/SerializedConnection.java b/src/comms/java/comms/SerializedConnection.java new file mode 100644 index 00000000..9e8af84c --- /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(SerializableMessage message) throws IOException { + message.writeHeader(out); + message.write(out); + } + + @Override + public SerializableMessage 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..35b63ee5 --- /dev/null +++ b/src/comms/java/comms/downward/MessageChat.java @@ -0,0 +1,49 @@ +/* + * 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.HandlableMessage; +import comms.IMessageListener; +import comms.SerializableMessage; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +public class MessageChat implements SerializableMessage, HandlableMessage { + + 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..5a4ebd29 --- /dev/null +++ b/src/comms/java/comms/iMessage.java @@ -0,0 +1,30 @@ +/* + * 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; + +/** + * hell yeah + *

+ *

+ * dumb android users cant read this file + *

+ * + * @author leijurv + */ +public interface iMessage { +} diff --git a/src/comms/java/comms/upward/MessageStatus.java b/src/comms/java/comms/upward/MessageStatus.java new file mode 100644 index 00000000..fe0a6489 --- /dev/null +++ b/src/comms/java/comms/upward/MessageStatus.java @@ -0,0 +1,57 @@ +/* + * 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.HandlableMessage; +import comms.IMessageListener; +import comms.SerializableMessage; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +public class MessageStatus implements SerializableMessage, HandlableMessage { + + 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 42403e84..0b42e112 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; @@ -77,6 +74,7 @@ public class Baritone implements IBaritone { private GameEventHandler gameEventHandler; private List behaviors; + private ControllerBehavior controllerBehavior; private PathingBehavior pathingBehavior; private LookBehavior lookBehavior; private MemoryBehavior memoryBehavior; @@ -107,6 +105,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); @@ -131,10 +130,6 @@ public class Baritone implements IBaritone { this.initialized = true; } - public PathingControlManager getPathingControlManager() { - return this.pathingControlManager; - } - public List getBehaviors() { return this.behaviors; } @@ -144,11 +139,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; @@ -160,18 +163,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 @@ -180,13 +173,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 @@ -199,6 +192,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(); } @@ -206,8 +213,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..25a128a1 --- /dev/null +++ b/src/main/java/baritone/behavior/ControllerBehavior.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 baritone.behavior; + +import baritone.Baritone; +import baritone.api.event.events.ChatEvent; +import baritone.api.event.events.TickEvent; +import baritone.utils.Helper; +import comms.*; +import comms.downward.MessageChat; +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)); + } + + private void readAndHandle() { + if (conn == null) { + return; + } + try { + List msgs = conn.receiveMessagesNonBlocking(); + msgs.forEach(msg -> ((HandlableMessage) msg).handle(this)); + } catch (IOException e) { + e.printStackTrace(); + disconnect(); + } + } + + public boolean trySend(SerializableMessage 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/utils/ExampleBaritoneControl.java b/src/main/java/baritone/utils/ExampleBaritoneControl.java index 841349e4..03f1079f 100644 --- a/src/main/java/baritone/utils/ExampleBaritoneControl.java +++ b/src/main/java/baritone/utils/ExampleBaritoneControl.java @@ -31,6 +31,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; @@ -38,6 +39,8 @@ import net.minecraft.entity.player.EntityPlayer; 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; @@ -462,6 +465,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"); } From 2d87033f491c53f6cb9f5443e24aac304025a78e Mon Sep 17 00:00:00 2001 From: Leijurv Date: Sun, 18 Nov 2018 11:06:11 -0800 Subject: [PATCH 09/15] f --- build.gradle | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/build.gradle b/build.gradle index 1c6f5b9e..f38b6c85 100755 --- a/build.gradle +++ b/build.gradle @@ -51,13 +51,13 @@ compileJava { } sourceSets { - launch { - compileClasspath += main.compileClasspath + main.runtimeClasspath + main.output - } comms {} main { compileClasspath += comms.compileClasspath } + launch { + compileClasspath += main.compileClasspath + main.runtimeClasspath + main.output + } } minecraft { From f01cf669e80d921e168bf4e6f53e5c7aa0501a59 Mon Sep 17 00:00:00 2001 From: Leijurv Date: Sun, 18 Nov 2018 11:06:40 -0800 Subject: [PATCH 10/15] wtf --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index f38b6c85..86638f2f 100755 --- a/build.gradle +++ b/build.gradle @@ -53,7 +53,7 @@ compileJava { sourceSets { comms {} main { - compileClasspath += comms.compileClasspath + compileClasspath += comms.compileClasspath + comms.runtimeClasspath + comms.output } launch { compileClasspath += main.compileClasspath + main.runtimeClasspath + main.output From 3c913a7b85dc9a37b3c976cc52af42d2b3e1c116 Mon Sep 17 00:00:00 2001 From: Brady Date: Sun, 18 Nov 2018 13:27:25 -0600 Subject: [PATCH 11/15] Fix jar export --- build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index 86638f2f..6120aa47 100755 --- a/build.gradle +++ b/build.gradle @@ -53,7 +53,7 @@ compileJava { sourceSets { comms {} main { - compileClasspath += comms.compileClasspath + comms.runtimeClasspath + comms.output + compileClasspath += comms.compileClasspath + comms.output } launch { compileClasspath += main.compileClasspath + main.runtimeClasspath + main.output @@ -103,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 } From dfb49179c5fd7bedaf6e25a438ee506affe978cf Mon Sep 17 00:00:00 2001 From: Leijurv Date: Sun, 18 Nov 2018 11:27:38 -0800 Subject: [PATCH 12/15] not a handler lol --- src/comms/java/comms/ConstructingDeserializer.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/comms/java/comms/ConstructingDeserializer.java b/src/comms/java/comms/ConstructingDeserializer.java index 4048a7be..3879204a 100644 --- a/src/comms/java/comms/ConstructingDeserializer.java +++ b/src/comms/java/comms/ConstructingDeserializer.java @@ -32,7 +32,9 @@ public enum ConstructingDeserializer implements MessageDeserializer { 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()) { - MSGS.add((Class) m.getParameterTypes()[0]); + if (m.getName().equals("handle")) { + MSGS.add((Class) m.getParameterTypes()[0]); + } } } From 9ad35dbf28f1a5c6f3ae4c74263fec4ec888e8b6 Mon Sep 17 00:00:00 2001 From: Leijurv Date: Sun, 18 Nov 2018 11:35:54 -0800 Subject: [PATCH 13/15] remove the useless stuff --- src/comms/java/comms/BufferedConnection.java | 16 +++--- .../java/comms/ConstructingDeserializer.java | 8 +-- src/comms/java/comms/FilteredConnection.java | 55 ------------------- src/comms/java/comms/HandlableMessage.java | 22 -------- src/comms/java/comms/IConnection.java | 6 +- src/comms/java/comms/MessageDeserializer.java | 2 +- src/comms/java/comms/Pipe.java | 18 +++--- src/comms/java/comms/SerializableMessage.java | 33 ----------- .../java/comms/SerializedConnection.java | 6 +- .../java/comms/downward/MessageChat.java | 5 +- src/comms/java/comms/iMessage.java | 14 +++++ .../java/comms/upward/MessageStatus.java | 5 +- .../baritone/behavior/ControllerBehavior.java | 13 +++-- 13 files changed, 54 insertions(+), 149 deletions(-) delete mode 100644 src/comms/java/comms/FilteredConnection.java delete mode 100644 src/comms/java/comms/HandlableMessage.java delete mode 100644 src/comms/java/comms/SerializableMessage.java diff --git a/src/comms/java/comms/BufferedConnection.java b/src/comms/java/comms/BufferedConnection.java index 13fef7f5..450010e7 100644 --- a/src/comms/java/comms/BufferedConnection.java +++ b/src/comms/java/comms/BufferedConnection.java @@ -32,16 +32,16 @@ import java.util.concurrent.LinkedBlockingQueue; * * @author leijurv */ -public class BufferedConnection implements IConnection { +public class BufferedConnection implements IConnection { private final IConnection wrapped; - private final LinkedBlockingQueue queue; + private final LinkedBlockingQueue queue; private volatile transient IOException thrownOnRead; - public BufferedConnection(IConnection wrapped) { + public BufferedConnection(IConnection wrapped) { this(wrapped, Integer.MAX_VALUE); // LinkedBlockingQueue accepts this as "no limit" } - public BufferedConnection(IConnection wrapped, int maxInternalQueueSize) { + public BufferedConnection(IConnection wrapped, int maxInternalQueueSize) { this.wrapped = wrapped; this.queue = new LinkedBlockingQueue<>(); this.thrownOnRead = null; @@ -59,12 +59,12 @@ public class BufferedConnection implements IConnection { } @Override - public void sendMessage(T message) throws IOException { + public void sendMessage(iMessage message) throws IOException { wrapped.sendMessage(message); } @Override - public T receiveMessage() { + public iMessage receiveMessage() { throw new UnsupportedOperationException("BufferedConnection can only be read from non-blockingly"); } @@ -74,8 +74,8 @@ public class BufferedConnection implements IConnection { thrownOnRead = new EOFException("Closed"); } - public List receiveMessagesNonBlocking() throws IOException { - ArrayList msgs = new ArrayList<>(); + 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); diff --git a/src/comms/java/comms/ConstructingDeserializer.java b/src/comms/java/comms/ConstructingDeserializer.java index 3879204a..c6407976 100644 --- a/src/comms/java/comms/ConstructingDeserializer.java +++ b/src/comms/java/comms/ConstructingDeserializer.java @@ -26,20 +26,20 @@ import java.util.List; public enum ConstructingDeserializer implements MessageDeserializer { INSTANCE; - private final List> MSGS; + 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]); + MSGS.add((Class) m.getParameterTypes()[0]); } } } @Override - public SerializableMessage deserialize(DataInputStream in) throws IOException { + public iMessage deserialize(DataInputStream in) throws IOException { int type = ((int) in.readByte()) & 0xff; try { return MSGS.get(type).getConstructor(DataInputStream.class).newInstance(in); @@ -48,7 +48,7 @@ public enum ConstructingDeserializer implements MessageDeserializer { } } - public byte getHeader(Class klass) { + public byte getHeader(Class klass) { return (byte) MSGS.indexOf(klass); } } diff --git a/src/comms/java/comms/FilteredConnection.java b/src/comms/java/comms/FilteredConnection.java deleted file mode 100644 index 2d740fcd..00000000 --- a/src/comms/java/comms/FilteredConnection.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * 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; - -/** - * This is just a meme idk if this is actually useful - * - * @param - */ -public class FilteredConnection implements IConnection { - private final Class klass; - private final IConnection wrapped; - - public FilteredConnection(IConnection wrapped, Class klass) { - this.klass = klass; - this.wrapped = wrapped; - } - - @Override - public void sendMessage(T message) throws IOException { - wrapped.sendMessage(message); - } - - @Override - public T receiveMessage() throws IOException { - while (true) { - iMessage msg = wrapped.receiveMessage(); - if (klass.isInstance(msg)) { - return klass.cast(msg); - } - } - } - - @Override - public void close() { - wrapped.close(); - } -} diff --git a/src/comms/java/comms/HandlableMessage.java b/src/comms/java/comms/HandlableMessage.java deleted file mode 100644 index dce1d3de..00000000 --- a/src/comms/java/comms/HandlableMessage.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * 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; - -public interface HandlableMessage { - void handle(IMessageListener listener); -} diff --git a/src/comms/java/comms/IConnection.java b/src/comms/java/comms/IConnection.java index 05cef73d..dc54beb0 100644 --- a/src/comms/java/comms/IConnection.java +++ b/src/comms/java/comms/IConnection.java @@ -19,10 +19,10 @@ package comms; import java.io.IOException; -public interface IConnection { - void sendMessage(T message) throws IOException; +public interface IConnection { + void sendMessage(iMessage message) throws IOException; - T receiveMessage() throws IOException; + iMessage receiveMessage() throws IOException; void close(); } diff --git a/src/comms/java/comms/MessageDeserializer.java b/src/comms/java/comms/MessageDeserializer.java index ed18bf1a..aece828d 100644 --- a/src/comms/java/comms/MessageDeserializer.java +++ b/src/comms/java/comms/MessageDeserializer.java @@ -21,5 +21,5 @@ import java.io.DataInputStream; import java.io.IOException; public interface MessageDeserializer { - SerializableMessage deserialize(DataInputStream in) throws IOException; + iMessage deserialize(DataInputStream in) throws IOException; } diff --git a/src/comms/java/comms/Pipe.java b/src/comms/java/comms/Pipe.java index a8cad44f..c189af60 100644 --- a/src/comms/java/comms/Pipe.java +++ b/src/comms/java/comms/Pipe.java @@ -26,8 +26,8 @@ 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 LinkedBlockingQueue> AtoB; + private final LinkedBlockingQueue> BtoA; private final PipedConnection A; private final PipedConnection B; private volatile boolean closed; @@ -47,17 +47,17 @@ public class Pipe { return B; } - public class PipedConnection implements IConnection { - private final LinkedBlockingQueue> in; - private final LinkedBlockingQueue> out; + public class PipedConnection implements IConnection { + private final LinkedBlockingQueue> in; + private final LinkedBlockingQueue> out; - private PipedConnection(LinkedBlockingQueue> in, LinkedBlockingQueue> out) { + private PipedConnection(LinkedBlockingQueue> in, LinkedBlockingQueue> out) { this.in = in; this.out = out; } @Override - public void sendMessage(T message) throws IOException { + public void sendMessage(iMessage message) throws IOException { if (closed) { throw new EOFException("Closed"); } @@ -69,12 +69,12 @@ public class Pipe { } @Override - public T receiveMessage() throws IOException { + public iMessage receiveMessage() throws IOException { if (closed) { throw new EOFException("Closed"); } try { - Optional t = in.take(); + Optional t = in.take(); if (!t.isPresent()) { throw new EOFException("Closed"); } diff --git a/src/comms/java/comms/SerializableMessage.java b/src/comms/java/comms/SerializableMessage.java deleted file mode 100644 index 90187446..00000000 --- a/src/comms/java/comms/SerializableMessage.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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; - -public interface SerializableMessage extends 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()); - } -} diff --git a/src/comms/java/comms/SerializedConnection.java b/src/comms/java/comms/SerializedConnection.java index 9e8af84c..0952854d 100644 --- a/src/comms/java/comms/SerializedConnection.java +++ b/src/comms/java/comms/SerializedConnection.java @@ -19,7 +19,7 @@ package comms; import java.io.*; -public class SerializedConnection implements IConnection { +public class SerializedConnection implements IConnection { private final DataInputStream in; private final DataOutputStream out; private final MessageDeserializer deserializer; @@ -35,13 +35,13 @@ public class SerializedConnection implements IConnection { } @Override - public void sendMessage(SerializableMessage message) throws IOException { + public void sendMessage(iMessage message) throws IOException { message.writeHeader(out); message.write(out); } @Override - public SerializableMessage receiveMessage() throws IOException { + public iMessage receiveMessage() throws IOException { return deserializer.deserialize(in); } diff --git a/src/comms/java/comms/downward/MessageChat.java b/src/comms/java/comms/downward/MessageChat.java index 35b63ee5..b04d2a31 100644 --- a/src/comms/java/comms/downward/MessageChat.java +++ b/src/comms/java/comms/downward/MessageChat.java @@ -17,15 +17,14 @@ package comms.downward; -import comms.HandlableMessage; import comms.IMessageListener; -import comms.SerializableMessage; +import comms.iMessage; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; -public class MessageChat implements SerializableMessage, HandlableMessage { +public class MessageChat implements iMessage { public final String msg; diff --git a/src/comms/java/comms/iMessage.java b/src/comms/java/comms/iMessage.java index 5a4ebd29..67ec2965 100644 --- a/src/comms/java/comms/iMessage.java +++ b/src/comms/java/comms/iMessage.java @@ -17,6 +17,9 @@ package comms; +import java.io.DataOutputStream; +import java.io.IOException; + /** * hell yeah *

@@ -27,4 +30,15 @@ package comms; * @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 index fe0a6489..55a29828 100644 --- a/src/comms/java/comms/upward/MessageStatus.java +++ b/src/comms/java/comms/upward/MessageStatus.java @@ -17,15 +17,14 @@ package comms.upward; -import comms.HandlableMessage; import comms.IMessageListener; -import comms.SerializableMessage; +import comms.iMessage; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; -public class MessageStatus implements SerializableMessage, HandlableMessage { +public class MessageStatus implements iMessage { public final double x; public final double y; diff --git a/src/main/java/baritone/behavior/ControllerBehavior.java b/src/main/java/baritone/behavior/ControllerBehavior.java index 25a128a1..dd8594df 100644 --- a/src/main/java/baritone/behavior/ControllerBehavior.java +++ b/src/main/java/baritone/behavior/ControllerBehavior.java @@ -21,8 +21,11 @@ import baritone.Baritone; import baritone.api.event.events.ChatEvent; import baritone.api.event.events.TickEvent; import baritone.utils.Helper; -import comms.*; +import comms.BufferedConnection; +import comms.IConnection; +import comms.IMessageListener; import comms.downward.MessageChat; +import comms.iMessage; import comms.upward.MessageStatus; import java.io.IOException; @@ -33,7 +36,7 @@ public class ControllerBehavior extends Behavior implements IMessageListener { super(baritone); } - private BufferedConnection conn; + private BufferedConnection conn; @Override public void onTick(TickEvent event) { @@ -48,15 +51,15 @@ public class ControllerBehavior extends Behavior implements IMessageListener { return; } try { - List msgs = conn.receiveMessagesNonBlocking(); - msgs.forEach(msg -> ((HandlableMessage) msg).handle(this)); + List msgs = conn.receiveMessagesNonBlocking(); + msgs.forEach(msg -> msg.handle(this)); } catch (IOException e) { e.printStackTrace(); disconnect(); } } - public boolean trySend(SerializableMessage msg) { + public boolean trySend(iMessage msg) { if (conn == null) { return false; } From f99befd30703872a0e74983a7738ce25c7618f21 Mon Sep 17 00:00:00 2001 From: Leijurv Date: Sun, 18 Nov 2018 12:04:37 -0800 Subject: [PATCH 14/15] oh thats important --- src/main/java/baritone/behavior/ControllerBehavior.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/baritone/behavior/ControllerBehavior.java b/src/main/java/baritone/behavior/ControllerBehavior.java index dd8594df..f1deb618 100644 --- a/src/main/java/baritone/behavior/ControllerBehavior.java +++ b/src/main/java/baritone/behavior/ControllerBehavior.java @@ -44,6 +44,7 @@ public class ControllerBehavior extends Behavior implements IMessageListener { return; } trySend(new MessageStatus(ctx.player().posX, ctx.player().posY, ctx.player().posZ)); + readAndHandle(); } private void readAndHandle() { From 4fb6c71174e49211d35c54df793819a04aa70d1c Mon Sep 17 00:00:00 2001 From: Leijurv Date: Sun, 18 Nov 2018 17:32:16 -0800 Subject: [PATCH 15/15] lol --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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