From 28be7121e3e712473c08538f19c9b8a0a4727438 Mon Sep 17 00:00:00 2001 From: Leijurv Date: Thu, 23 Aug 2018 15:39:02 -0700 Subject: [PATCH] path event --- .../behavior/impl/PathingBehavior.java | 43 +++++++++++++++---- .../java/baritone/event/GameEventHandler.java | 32 +++----------- .../java/baritone/event/events/PathEvent.java | 22 ++++++++++ .../listener/AbstractGameEventListener.java | 6 ++- .../event/listener/IGameEventListener.java | 7 +++ 5 files changed, 73 insertions(+), 37 deletions(-) create mode 100644 src/main/java/baritone/event/events/PathEvent.java diff --git a/src/main/java/baritone/behavior/impl/PathingBehavior.java b/src/main/java/baritone/behavior/impl/PathingBehavior.java index b35baca1..de322b98 100644 --- a/src/main/java/baritone/behavior/impl/PathingBehavior.java +++ b/src/main/java/baritone/behavior/impl/PathingBehavior.java @@ -19,6 +19,7 @@ package baritone.behavior.impl; import baritone.Baritone; import baritone.behavior.Behavior; +import baritone.event.events.PathEvent; import baritone.event.events.PlayerUpdateEvent; import baritone.event.events.RenderEvent; import baritone.event.events.TickEvent; @@ -59,6 +60,14 @@ public class PathingBehavior extends Behavior { private boolean lastAutoJump; + private void dispatchPathEvent(PathEvent event) { + new Thread() { + public void run() { + Baritone.INSTANCE.getGameEventHandler().onPathEvent(event); + } + }.start(); + } + @Override public void onTick(TickEvent event) { if (event.getType() == TickEvent.Type.OUT) { @@ -74,6 +83,7 @@ public class PathingBehavior extends Behavior { current = null; if (goal == null || goal.isInGoal(playerFeet())) { displayChatMessageRaw("All done. At " + goal); + dispatchPathEvent(PathEvent.AT_GOAL); next = null; return; } @@ -85,10 +95,12 @@ public class PathingBehavior extends Behavior { // but if we fail in the middle of current // we're nowhere close to our planned ahead path // so need to discard it sadly. + dispatchPathEvent(PathEvent.DISCARD_NEXT); next = null; } if (next != null) { displayChatMessageRaw("Continuing on to planned next path"); + dispatchPathEvent(PathEvent.CONTINUING_ONTO_PLANNED_NEXT); current = next; next = null; return; @@ -96,9 +108,11 @@ public class PathingBehavior extends Behavior { // at this point, current just ended, but we aren't in the goal and have no plan for the future synchronized (pathCalcLock) { if (isPathCalcInProgress) { + dispatchPathEvent(PathEvent.PATH_FINISHED_NEXT_STILL_CALCULATING); // if we aren't calculating right now return; } + dispatchPathEvent(PathEvent.CALC_STARTED); findPathInNewThread(pathStart(), true, Optional.empty()); } return; @@ -110,6 +124,7 @@ public class PathingBehavior extends Behavior { if (next.getPath().positions().contains(playerFeet())) { // jump directly onto the next path displayChatMessageRaw("Splicing into planned next path early..."); + dispatchPathEvent(PathEvent.SPLICING_ONTO_NEXT_EARLY); current = next; next = null; return; @@ -132,6 +147,7 @@ public class PathingBehavior extends Behavior { if (ticksRemainingInSegment().get() < Baritone.settings().planningTickLookAhead.get()) { // and this path has 5 seconds or less left displayChatMessageRaw("Path almost over. Planning ahead..."); + dispatchPathEvent(PathEvent.NEXT_SEGMENT_CALC_STARTED); findPathInNewThread(current.getPath().getDest(), false, Optional.of(current.getPath())); } } @@ -192,6 +208,7 @@ public class PathingBehavior extends Behavior { if (isPathCalcInProgress) { return; } + dispatchPathEvent(PathEvent.CALC_STARTED); findPathInNewThread(pathStart(), true, Optional.empty()); } } @@ -227,19 +244,29 @@ public class PathingBehavior extends Behavior { if (Baritone.settings().cutoffAtLoadBoundary.get()) { path = path.map(IPath::cutoffAtLoadedChunks); } - path.map(p -> p.staticCutoff(goal)).map(PathExecutor::new).ifPresent(p -> { - synchronized (pathPlanLock) { - if (current == null) { - current = p; + Optional executor = path.map(p -> p.staticCutoff(goal)).map(PathExecutor::new); + synchronized (pathPlanLock) { + if (current == null) { + if (executor.isPresent()) { + dispatchPathEvent(PathEvent.CALC_FINISHED_NOW_EXECUTING); + current = executor.get(); } else { - if (next == null) { - next = p; + dispatchPathEvent(PathEvent.CALC_FAILED); + } + } else { + if (next == null) { + if (executor.isPresent()) { + dispatchPathEvent(PathEvent.NEXT_SEGMENT_CALC_FINISHED); + next = executor.get(); } else { - throw new IllegalStateException("I have no idea what to do with this path"); + dispatchPathEvent(PathEvent.NEXT_CALC_FAILED); } + } else { + throw new IllegalStateException("I have no idea what to do with this path"); } } - }); + } + if (talkAboutIt && current != null && current.getPath() != null) { if (goal == null || goal.isInGoal(current.getPath().getDest())) { displayChatMessageRaw("Finished finding a path from " + start + " to " + goal + ". " + current.getPath().getNumNodesConsidered() + " nodes considered"); diff --git a/src/main/java/baritone/event/GameEventHandler.java b/src/main/java/baritone/event/GameEventHandler.java index 7185b632..02c7f393 100644 --- a/src/main/java/baritone/event/GameEventHandler.java +++ b/src/main/java/baritone/event/GameEventHandler.java @@ -42,10 +42,6 @@ import baritone.event.listener.IGameEventListener; import baritone.utils.Helper; import baritone.utils.InputOverrideHandler; import baritone.utils.interfaces.Toggleable; -import net.minecraft.client.renderer.BufferBuilder; -import net.minecraft.client.renderer.GlStateManager; -import net.minecraft.client.renderer.Tessellator; -import net.minecraft.client.renderer.vertex.DefaultVertexFormats; import net.minecraft.client.settings.KeyBinding; import net.minecraft.world.chunk.Chunk; import org.lwjgl.input.Keyboard; @@ -181,6 +177,11 @@ public final class GameEventHandler implements IGameEventListener, Helper { dispatch(IGameEventListener::onPlayerDeath); } + @Override + public void onPathEvent(PathEvent event) { + dispatch(listener -> listener.onPathEvent(event)); + } + public final void registerEventListener(IGameEventListener listener) { this.listeners.add(listener); } @@ -192,27 +193,4 @@ public final class GameEventHandler implements IGameEventListener, Helper { private boolean canDispatch(IGameEventListener listener) { return !(listener instanceof Toggleable) || ((Toggleable) listener).isEnabled(); } - - private void drawChunkLine(int posX, int posZ, float partialTicks) { - GlStateManager.enableBlend(); - GlStateManager.tryBlendFuncSeparate(770, 771, 1, 0); - GlStateManager.color(1.0F, 1.0F, 0.0F, 0.4F); - GlStateManager.glLineWidth(2.0F); - GlStateManager.disableTexture2D(); - - Tessellator tessellator = Tessellator.getInstance(); - BufferBuilder buffer = tessellator.getBuffer(); - double d0 = mc.getRenderManager().viewerPosX; - double d1 = mc.getRenderManager().viewerPosY; - double d2 = mc.getRenderManager().viewerPosZ; - buffer.begin(3, DefaultVertexFormats.POSITION); - buffer.pos(posX - d0, 0 - d1, posZ - d2).endVertex(); - buffer.pos(posX - d0, 256 - d1, posZ - d2).endVertex(); - tessellator.draw(); - - GlStateManager.enableDepth(); - GlStateManager.depthMask(true); - GlStateManager.enableTexture2D(); - GlStateManager.disableBlend(); - } } diff --git a/src/main/java/baritone/event/events/PathEvent.java b/src/main/java/baritone/event/events/PathEvent.java new file mode 100644 index 00000000..0b110899 --- /dev/null +++ b/src/main/java/baritone/event/events/PathEvent.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 General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Baritone is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Baritone. If not, see . + */ + +package baritone.event.events; + +public enum PathEvent { + CALC_STARTED, CALC_FINISHED_NOW_EXECUTING, CALC_FAILED, NEXT_SEGMENT_CALC_STARTED, NEXT_SEGMENT_CALC_FINISHED, CONTINUING_ONTO_PLANNED_NEXT, SPLICING_ONTO_NEXT_EARLY, AT_GOAL, PATH_FINISHED_NEXT_STILL_CALCULATING, NEXT_CALC_FAILED, DISCARD_NEXT; +} diff --git a/src/main/java/baritone/event/listener/AbstractGameEventListener.java b/src/main/java/baritone/event/listener/AbstractGameEventListener.java index 0f113119..4b8de23a 100644 --- a/src/main/java/baritone/event/listener/AbstractGameEventListener.java +++ b/src/main/java/baritone/event/listener/AbstractGameEventListener.java @@ -41,9 +41,8 @@ import baritone.event.events.*; * overridden with empty bodies, allowing inheritors of this class to choose * which events they would like to listen in on. * - * @see IGameEventListener - * * @author Brady + * @see IGameEventListener * @since 8/1/2018 6:29 PM */ public interface AbstractGameEventListener extends IGameEventListener { @@ -86,4 +85,7 @@ public interface AbstractGameEventListener extends IGameEventListener { @Override default void onPlayerDeath() {} + + @Override + default void onPathEvent(PathEvent event) {} } diff --git a/src/main/java/baritone/event/listener/IGameEventListener.java b/src/main/java/baritone/event/listener/IGameEventListener.java index bb52a66d..4900f1eb 100644 --- a/src/main/java/baritone/event/listener/IGameEventListener.java +++ b/src/main/java/baritone/event/listener/IGameEventListener.java @@ -149,4 +149,11 @@ public interface IGameEventListener { * @see GuiGameOver(ITextComponent) */ void onPlayerDeath(); + + /** + * When the pathfinder's state changes + * + * @param event + */ + void onPathEvent(PathEvent event); }