From 3f814536221e0faa3c261f726c5789d270f1d0b1 Mon Sep 17 00:00:00 2001 From: Brady Date: Mon, 6 Aug 2018 21:41:17 -0500 Subject: [PATCH] Added packet send and receive listeners --- .../java/baritone/bot/GameEventHandler.java | 10 +++++ .../bot/event/AbstractGameEventListener.java | 9 +++-- .../bot/event/IGameEventListener.java | 18 ++++++++- .../bot/event/events/PacketEvent.java | 20 ++++++++++ .../launch/mixins/MixinNetworkManager.java | 40 +++++++++++++++++++ src/main/resources/mixins.baritone.json | 1 + 6 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 src/main/java/baritone/bot/event/events/PacketEvent.java create mode 100644 src/main/java/baritone/launch/mixins/MixinNetworkManager.java diff --git a/src/main/java/baritone/bot/GameEventHandler.java b/src/main/java/baritone/bot/GameEventHandler.java index 9ecd8803..504f570f 100755 --- a/src/main/java/baritone/bot/GameEventHandler.java +++ b/src/main/java/baritone/bot/GameEventHandler.java @@ -102,6 +102,16 @@ public final class GameEventHandler implements IGameEventListener { dispatch(behavior -> behavior.onWorldEvent(event)); } + @Override + public void onSendPacket(PacketEvent event) { + dispatch(behavior -> behavior.onSendPacket(event)); + } + + @Override + public void onReceivePacket(PacketEvent event) { + dispatch(behavior -> behavior.onReceivePacket(event)); + } + private void dispatch(Consumer dispatchFunction) { Baritone.INSTANCE.getBehaviors().stream().filter(Behavior::isEnabled).forEach(dispatchFunction); } diff --git a/src/main/java/baritone/bot/event/AbstractGameEventListener.java b/src/main/java/baritone/bot/event/AbstractGameEventListener.java index df23110f..60eee90a 100644 --- a/src/main/java/baritone/bot/event/AbstractGameEventListener.java +++ b/src/main/java/baritone/bot/event/AbstractGameEventListener.java @@ -7,9 +7,6 @@ import baritone.bot.event.events.*; * 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 - * manager/behavior system is ported over to the new system. - * * @see IGameEventListener * * @author Brady @@ -37,4 +34,10 @@ public interface AbstractGameEventListener extends IGameEventListener { @Override default void onWorldEvent(WorldEvent event) {} + + @Override + default void onSendPacket(PacketEvent event) {} + + @Override + default void onReceivePacket(PacketEvent event) {} } diff --git a/src/main/java/baritone/bot/event/IGameEventListener.java b/src/main/java/baritone/bot/event/IGameEventListener.java index 6a381beb..2b2c925a 100755 --- a/src/main/java/baritone/bot/event/IGameEventListener.java +++ b/src/main/java/baritone/bot/event/IGameEventListener.java @@ -1,11 +1,14 @@ package baritone.bot.event; import baritone.bot.event.events.*; +import io.netty.util.concurrent.GenericFutureListener; import net.minecraft.client.Minecraft; import net.minecraft.client.entity.EntityPlayerSP; import net.minecraft.client.multiplayer.WorldClient; import net.minecraft.client.renderer.EntityRenderer; import net.minecraft.client.settings.GameSettings; +import net.minecraft.network.NetworkManager; +import net.minecraft.network.Packet; /** * @author Brady @@ -40,7 +43,6 @@ public interface IGameEventListener { */ void onSendChatMessage(ChatEvent event); - /** * Runs before and after whenever a chunk is either loaded, unloaded, or populated. * @@ -63,4 +65,18 @@ public interface IGameEventListener { * @see Minecraft#loadWorld(WorldClient, String) */ void onWorldEvent(WorldEvent event); + + /** + * Runs before a outbound packet is sent + * + * @see NetworkManager#dispatchPacket(Packet, GenericFutureListener[]) + */ + void onSendPacket(PacketEvent event); + + /** + * Runs before an inbound packet is processed + * + * @see NetworkManager#dispatchPacket(Packet, GenericFutureListener[]) + */ + void onReceivePacket(PacketEvent event); } diff --git a/src/main/java/baritone/bot/event/events/PacketEvent.java b/src/main/java/baritone/bot/event/events/PacketEvent.java new file mode 100644 index 00000000..8d29ff46 --- /dev/null +++ b/src/main/java/baritone/bot/event/events/PacketEvent.java @@ -0,0 +1,20 @@ +package baritone.bot.event.events; + +import net.minecraft.network.Packet; + +/** + * @author Brady + * @since 8/6/2018 9:31 PM + */ +public final class PacketEvent { + + private final Packet packet; + + public PacketEvent(Packet packet) { + this.packet = packet; + } + + public final Packet getPacket() { + return this.packet; + } +} diff --git a/src/main/java/baritone/launch/mixins/MixinNetworkManager.java b/src/main/java/baritone/launch/mixins/MixinNetworkManager.java new file mode 100644 index 00000000..4e4aa0eb --- /dev/null +++ b/src/main/java/baritone/launch/mixins/MixinNetworkManager.java @@ -0,0 +1,40 @@ +package baritone.launch.mixins; + +import baritone.bot.Baritone; +import baritone.bot.event.events.PacketEvent; +import io.netty.channel.ChannelHandlerContext; +import io.netty.util.concurrent.Future; +import io.netty.util.concurrent.GenericFutureListener; +import net.minecraft.network.NetworkManager; +import net.minecraft.network.Packet; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +/** + * @author Brady + * @since 8/6/2018 9:30 PM + */ +@Mixin(NetworkManager.class) +public class MixinNetworkManager { + + @Inject( + method = "dispatchPacket", + at = @At("HEAD") + ) + private void dispatchPacket(Packet inPacket, final GenericFutureListener>[] futureListeners, CallbackInfo ci) { + Baritone.INSTANCE.getGameEventHandler().onSendPacket(new PacketEvent(inPacket)); + } + + @Inject( + method = "channelRead0", + at = @At( + value = "INVOKE", + target = "net/minecraft/network/Packet.processPacket(Lnet/minecraft/network/INetHandler;)V" + ) + ) + private void preProcessPacket(ChannelHandlerContext context, Packet packet, CallbackInfo ci) { + Baritone.INSTANCE.getGameEventHandler().onReceivePacket(new PacketEvent(packet)); + } +} diff --git a/src/main/resources/mixins.baritone.json b/src/main/resources/mixins.baritone.json index 0e4a36ab..5ffe4f65 100755 --- a/src/main/resources/mixins.baritone.json +++ b/src/main/resources/mixins.baritone.json @@ -15,6 +15,7 @@ "MixinMain", "MixinMinecraft", "MixinNetHandlerPlayClient", + "MixinNetworkManager", "MixinWorldClient", "accessor.IAnvilChunkLoader",