diff --git a/src/main/java/baritone/bot/GameEventHandler.java b/src/main/java/baritone/bot/GameEventHandler.java index b3570110..9ecd8803 100755 --- a/src/main/java/baritone/bot/GameEventHandler.java +++ b/src/main/java/baritone/bot/GameEventHandler.java @@ -2,10 +2,7 @@ package baritone.bot; import baritone.bot.behavior.Behavior; import baritone.bot.event.IGameEventListener; -import baritone.bot.event.events.ChatEvent; -import baritone.bot.event.events.ChunkEvent; -import baritone.bot.event.events.RenderEvent; -import baritone.bot.event.events.WorldEvent; +import baritone.bot.event.events.*; import net.minecraft.client.settings.KeyBinding; import org.lwjgl.input.Keyboard; @@ -20,8 +17,8 @@ public final class GameEventHandler implements IGameEventListener { GameEventHandler() {} @Override - public final void onTick() { - dispatch(Behavior::onTick); + public final void onTick(TickEvent event) { + dispatch(behavior -> behavior.onTick(event)); } @Override diff --git a/src/main/java/baritone/bot/behavior/Behavior.java b/src/main/java/baritone/bot/behavior/Behavior.java index e61f5850..8fcdbf51 100644 --- a/src/main/java/baritone/bot/behavior/Behavior.java +++ b/src/main/java/baritone/bot/behavior/Behavior.java @@ -47,7 +47,7 @@ public class Behavior implements AbstractGameEventListener, Helper { /** * Function to determine what the new enabled state of this * {@link Behavior} should be given the old state, and the - * proposed state. Intended to be overriden by behaviors + * proposed state. Intended to be overridden by behaviors * that should always be active, given that the bot itself is * active. * diff --git a/src/main/java/baritone/bot/behavior/impl/LookBehavior.java b/src/main/java/baritone/bot/behavior/impl/LookBehavior.java index 175d01d5..e1897e92 100644 --- a/src/main/java/baritone/bot/behavior/impl/LookBehavior.java +++ b/src/main/java/baritone/bot/behavior/impl/LookBehavior.java @@ -1,9 +1,7 @@ package baritone.bot.behavior.impl; import baritone.bot.behavior.Behavior; -import baritone.bot.utils.Utils; import net.minecraft.util.Tuple; -import net.minecraft.util.math.BlockPos; import java.util.Optional; @@ -12,7 +10,7 @@ public class LookBehavior extends Behavior { public static final LookBehavior INSTANCE = new LookBehavior(); public LookBehavior() { - + target = Optional.empty(); } /** @@ -21,12 +19,7 @@ public class LookBehavior extends Behavior { * getFirst() -> yaw * getSecond() -> pitch */ - private Optional> target = Optional.empty(); - - public void updateTarget(BlockPos blockPos) { - Utils.calcRotationFromVec3d(player().getPositionEyes(1.0F), - Utils.calcCenterFromCoords(blockPos, world())); - } + private Optional> target; public void updateTarget(Tuple target) { this.target = Optional.of(target); diff --git a/src/main/java/baritone/bot/behavior/impl/PathingBehavior.java b/src/main/java/baritone/bot/behavior/impl/PathingBehavior.java index ed52dfd0..3d664de1 100644 --- a/src/main/java/baritone/bot/behavior/impl/PathingBehavior.java +++ b/src/main/java/baritone/bot/behavior/impl/PathingBehavior.java @@ -3,6 +3,7 @@ package baritone.bot.behavior.impl; import baritone.bot.behavior.Behavior; import baritone.bot.event.events.ChatEvent; import baritone.bot.event.events.RenderEvent; +import baritone.bot.event.events.TickEvent; import baritone.bot.pathing.calc.AStarPathFinder; import baritone.bot.pathing.calc.AbstractNodeCostSearch; import baritone.bot.pathing.calc.IPathFinder; @@ -23,7 +24,6 @@ import net.minecraft.entity.player.EntityPlayer; import net.minecraft.init.Blocks; import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.BlockPos; -import net.minecraft.util.text.TextComponentString; import org.lwjgl.opengl.GL11; import java.awt.*; @@ -40,12 +40,12 @@ public class PathingBehavior extends Behavior { private Goal goal; @Override - public void onTick() { - //System.out.println("Ticking"); + public void onTick(TickEvent event) { + // System.out.println("Ticking"); if (current == null) { return; } - //current.onTick(); + // current.onTick(); if (current.failed() || current.finished()) { current = null; } @@ -62,16 +62,12 @@ public class PathingBehavior extends Behavior { return current.getPath(); } - private static void chatRaw(String s) { - Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(new TextComponentString(s)); - } - @Override public void onSendChatMessage(ChatEvent event) { String msg = event.getMessage(); if (msg.equals("goal")) { goal = new GoalBlock(playerFeet()); - chatRaw("Goal: " + goal); + displayChatMessageRaw("Goal: " + goal); event.cancel(); return; } @@ -90,33 +86,27 @@ public class PathingBehavior extends Behavior { * @param talkAboutIt */ public void findPathInNewThread(final BlockPos start, final boolean talkAboutIt) { - - new Thread() { - @Override - public void run() { - if (talkAboutIt) { - - chatRaw("Starting to search for path from " + start + " to " + goal); - } - - try { - IPath path = findPath(start); - if (path != null) { - current = new PathExecutor(path); - } - } catch (Exception e) { - } - /*isThereAnythingInProgress = false; - if (!currentPath.goal.isInGoal(currentPath.end)) { - if (talkAboutIt) { - Out.gui("I couldn't get all the way to " + goal + ", but I'm going to get as close as I can. " + currentPath.numNodes + " nodes considered", Out.Mode.Standard); - } - planAhead(); - } else if (talkAboutIt) { - Out.gui("Finished finding a path from " + start + " to " + goal + ". " + currentPath.numNodes + " nodes considered", Out.Mode.Debug); - }*/ + new Thread(() -> { + if (talkAboutIt) { + displayChatMessageRaw("Starting to search for path from " + start + " to " + goal); } - }.start(); + + try { + IPath path = findPath(start); + if (path != null) { + current = new PathExecutor(path); + } + } catch (Exception ignored) {} + /*isThereAnythingInProgress = false; + if (!currentPath.goal.isInGoal(currentPath.end)) { + if (talkAboutIt) { + Out.gui("I couldn't get all the way to " + goal + ", but I'm going to get as close as I can. " + currentPath.numNodes + " nodes considered", Out.Mode.Standard); + } + planAhead(); + } else if (talkAboutIt) { + Out.gui("Finished finding a path from " + start + " to " + goal + ". " + currentPath.numNodes + " nodes considered", Out.Mode.Debug); + }*/ + }).start(); } /** @@ -127,7 +117,7 @@ public class PathingBehavior extends Behavior { */ private IPath findPath(BlockPos start) { if (goal == null) { - chatRaw("no goal"); + displayChatMessageRaw("no goal"); return null; } try { @@ -184,8 +174,6 @@ public class PathingBehavior extends Behavior { double z2 = b.getZ(); drawLine(player, x1, y1, z1, x2, y2, z2, partialTicks); } - - //GlStateManager.color(0.0f, 0.0f, 0.0f, 0.4f); GlStateManager.depthMask(true); GlStateManager.enableTexture2D(); @@ -256,5 +244,4 @@ public class PathingBehavior extends Behavior { GlStateManager.enableTexture2D(); GlStateManager.disableBlend(); } - } diff --git a/src/main/java/baritone/bot/event/AbstractGameEventListener.java b/src/main/java/baritone/bot/event/AbstractGameEventListener.java index 7b7b0d30..df23110f 100644 --- a/src/main/java/baritone/bot/event/AbstractGameEventListener.java +++ b/src/main/java/baritone/bot/event/AbstractGameEventListener.java @@ -1,13 +1,10 @@ package baritone.bot.event; -import baritone.bot.event.events.ChatEvent; -import baritone.bot.event.events.ChunkEvent; -import baritone.bot.event.events.RenderEvent; -import baritone.bot.event.events.WorldEvent; +import baritone.bot.event.events.*; /** * An implementation of {@link IGameEventListener} that has all methods - * overriden with empty bodies, allowing inheritors of this class to choose + * overridden with empty bodies, allowing inheritors of this class to choose * which events they would like to listen in on. * * Has no implementors at the moment, but will likely be used with the @@ -21,7 +18,7 @@ import baritone.bot.event.events.WorldEvent; public interface AbstractGameEventListener extends IGameEventListener { @Override - default void onTick() {} + default void onTick(TickEvent event) {} @Override default void onPlayerUpdate() {} diff --git a/src/main/java/baritone/bot/event/IGameEventListener.java b/src/main/java/baritone/bot/event/IGameEventListener.java index da31b9a8..6a381beb 100755 --- a/src/main/java/baritone/bot/event/IGameEventListener.java +++ b/src/main/java/baritone/bot/event/IGameEventListener.java @@ -1,9 +1,6 @@ package baritone.bot.event; -import baritone.bot.event.events.ChatEvent; -import baritone.bot.event.events.ChunkEvent; -import baritone.bot.event.events.RenderEvent; -import baritone.bot.event.events.WorldEvent; +import baritone.bot.event.events.*; import net.minecraft.client.Minecraft; import net.minecraft.client.entity.EntityPlayerSP; import net.minecraft.client.multiplayer.WorldClient; @@ -21,7 +18,7 @@ public interface IGameEventListener { * * @see Minecraft#runTick() */ - void onTick(); + void onTick(TickEvent event); /** * Run once per game tick from before the player rotation is sent to the server. diff --git a/src/main/java/baritone/bot/event/events/TickEvent.java b/src/main/java/baritone/bot/event/events/TickEvent.java new file mode 100644 index 00000000..4e6790ba --- /dev/null +++ b/src/main/java/baritone/bot/event/events/TickEvent.java @@ -0,0 +1,29 @@ +package baritone.bot.event.events; + +import baritone.bot.event.events.type.EventState; +import javafx.event.EventType; + +public final class TickEvent { + + private final EventState state; + private final Type type; + + public TickEvent(EventState state, Type type) { + this.state = state; + this.type = type; + } + + + public enum Type { + /** + * When guarantees can be made about + * the game state and in-game variables. + */ + IN, + /** + * No guarantees can be made about the game state. + * This probably means we are at the main menu. + */ + OUT, + } +} diff --git a/src/main/java/baritone/bot/pathing/path/PathExecutor.java b/src/main/java/baritone/bot/pathing/path/PathExecutor.java index 1184ef3b..9ef967de 100644 --- a/src/main/java/baritone/bot/pathing/path/PathExecutor.java +++ b/src/main/java/baritone/bot/pathing/path/PathExecutor.java @@ -1,6 +1,7 @@ package baritone.bot.pathing.path; import baritone.bot.behavior.Behavior; +import baritone.bot.event.events.TickEvent; import baritone.bot.pathing.movement.ActionCosts; import baritone.bot.pathing.movement.Movement; import baritone.bot.pathing.movement.MovementState; @@ -33,7 +34,8 @@ public class PathExecutor extends Behavior { this.pathPosition = 0; } - public void onTick() { + @Override + public void onTick(TickEvent event) { if (pathPosition >= path.length()) { //stop bugging me, I'm done //TODO Baritone.INSTANCE.behaviors.remove(this) diff --git a/src/main/java/baritone/bot/utils/Helper.java b/src/main/java/baritone/bot/utils/Helper.java index 516c7955..e3d002f7 100755 --- a/src/main/java/baritone/bot/utils/Helper.java +++ b/src/main/java/baritone/bot/utils/Helper.java @@ -4,7 +4,7 @@ import net.minecraft.client.Minecraft; import net.minecraft.client.entity.EntityPlayerSP; import net.minecraft.client.multiplayer.WorldClient; import net.minecraft.util.math.BlockPos; -import net.minecraft.world.World; +import net.minecraft.util.text.TextComponentString; /** * @author Brady @@ -18,7 +18,7 @@ public interface Helper { return mc.player; } - default World world() { + default WorldClient world() { return mc.world; } @@ -26,4 +26,7 @@ public interface Helper { return new BlockPos(mc.player.posX, mc.player.posY, mc.player.posZ); } + default void displayChatMessageRaw(String message) { + mc.ingameGUI.getChatGUI().printChatMessage(new TextComponentString(message)); + } } diff --git a/src/main/java/baritone/launch/mixins/MixinMinecraft.java b/src/main/java/baritone/launch/mixins/MixinMinecraft.java index 4dbc40bf..4fd13314 100755 --- a/src/main/java/baritone/launch/mixins/MixinMinecraft.java +++ b/src/main/java/baritone/launch/mixins/MixinMinecraft.java @@ -1,6 +1,7 @@ package baritone.launch.mixins; import baritone.bot.Baritone; +import baritone.bot.event.events.TickEvent; import baritone.bot.event.events.WorldEvent; import baritone.bot.event.events.type.EventState; import net.minecraft.client.Minecraft; @@ -47,7 +48,13 @@ public class MixinMinecraft { ) ) private void runTick(CallbackInfo ci) { - Baritone.INSTANCE.getGameEventHandler().onTick(); + Minecraft mc = (Minecraft) (Object) this; + Baritone.INSTANCE.getGameEventHandler().onTick(new TickEvent( + EventState.PRE, + (mc.player != null && mc.world != null) + ? TickEvent.Type.IN + : TickEvent.Type.OUT + )); } @Redirect(