Add EventTick w/ state guarantees

This commit is contained in:
Howard Stark 2018-08-05 19:50:24 -04:00
parent 89189c532e
commit 379d358408
No known key found for this signature in database
GPG Key ID: 9FA4E350B33067F3
8 changed files with 49 additions and 21 deletions

View File

@ -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

View File

@ -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;

View File

@ -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.IPathFinder;
import baritone.bot.pathing.goals.Goal;
@ -34,7 +35,7 @@ public class PathingBehavior extends Behavior {
private Goal goal;
@Override
public void onTick() {
public void onTick(TickEvent event) {
//System.out.println("Ticking");
if (current == null) {
return;

View File

@ -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.*;
/**
* An implementation of {@link IGameEventListener} that has all methods
@ -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() {}

View File

@ -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.

View File

@ -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,
}
}

View File

@ -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)

View File

@ -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(