diff --git a/src/main/java/baritone/bot/event/IGameEventListener.java b/src/main/java/baritone/bot/event/IGameEventListener.java index 1e3c03ed..e7694689 100755 --- a/src/main/java/baritone/bot/event/IGameEventListener.java +++ b/src/main/java/baritone/bot/event/IGameEventListener.java @@ -35,7 +35,7 @@ public interface IGameEventListener { /** - * Runs before and after whenever a chunk is either loaded or unloaded. + * Runs before and after whenever a chunk is either loaded, unloaded, or populated. * * @see WorldClient#doPreChunk(int, int, boolean) */ diff --git a/src/main/java/baritone/bot/event/events/ChunkEvent.java b/src/main/java/baritone/bot/event/events/ChunkEvent.java index d3e713a6..16e46bf2 100644 --- a/src/main/java/baritone/bot/event/events/ChunkEvent.java +++ b/src/main/java/baritone/bot/event/events/ChunkEvent.java @@ -24,15 +24,15 @@ public final class ChunkEvent { private final int x; /** - * The Chunk Y position. + * The Chunk Z position. */ - private final int y; + private final int z; - public ChunkEvent(EventState state, Type type, int x, int y) { + public ChunkEvent(EventState state, Type type, int x, int z) { this.state = state; this.type = type; this.x = x; - this.y = y; + this.z = z; } /** @@ -57,14 +57,27 @@ public final class ChunkEvent { } /** - * @return The Chunk Y position. + * @return The Chunk Z position. */ - public final int getY() { - return this.y; + public final int getZ() { + return this.z; } public enum Type { + + /** + * When the chunk is constructed. + */ LOAD, - UNLOAD + + /** + * When the chunk is deconstructed. + */ + UNLOAD, + + /** + * When the chunk is being populated with blocks, tile entities, etc. + */ + POPULATE } } diff --git a/src/main/java/baritone/launch/mixins/MixinNetHandlerPlayClient.java b/src/main/java/baritone/launch/mixins/MixinNetHandlerPlayClient.java new file mode 100644 index 00000000..47e2db3a --- /dev/null +++ b/src/main/java/baritone/launch/mixins/MixinNetHandlerPlayClient.java @@ -0,0 +1,52 @@ +package baritone.launch.mixins; + +import baritone.bot.Baritone; +import baritone.bot.event.events.ChunkEvent; +import baritone.bot.event.events.type.EventState; +import net.minecraft.client.network.NetHandlerPlayClient; +import net.minecraft.network.play.server.SPacketChunkData; +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/3/2018 12:54 AM + */ +@Mixin(NetHandlerPlayClient.class) +public class MixinNetHandlerPlayClient { + + @Inject( + method = "handleChunkData", + at = @At( + value = "INVOKE", + target = "net/minecraft/world/chunk/Chunk.read(Lnet/minecraft/network/PacketBuffer;IZ)V" + ) + ) + private void preRead(SPacketChunkData packetIn, CallbackInfo ci) { + Baritone.INSTANCE.getGameEventHandler().onChunkEvent( + new ChunkEvent( + EventState.PRE, + ChunkEvent.Type.POPULATE, + packetIn.getChunkX(), + packetIn.getChunkZ() + ) + ); + } + + @Inject( + method = "handleChunkData", + at = @At("RETURN") + ) + private void postHandleChunkData(SPacketChunkData packetIn, CallbackInfo ci) { + Baritone.INSTANCE.getGameEventHandler().onChunkEvent( + new ChunkEvent( + EventState.POST, + ChunkEvent.Type.POPULATE, + packetIn.getChunkX(), + packetIn.getChunkZ() + ) + ); + } +} diff --git a/src/main/java/baritone/launch/mixins/MixinWorldClient.java b/src/main/java/baritone/launch/mixins/MixinWorldClient.java index ac6f925e..599823cd 100644 --- a/src/main/java/baritone/launch/mixins/MixinWorldClient.java +++ b/src/main/java/baritone/launch/mixins/MixinWorldClient.java @@ -20,13 +20,13 @@ public class MixinWorldClient { method = "doPreChunk", at = @At("HEAD") ) - private void preDoPreChunk(int chunkX, int chunkY, boolean loadChunk, CallbackInfo ci) { + private void preDoPreChunk(int chunkX, int chunkZ, boolean loadChunk, CallbackInfo ci) { Baritone.INSTANCE.getGameEventHandler().onChunkEvent( new ChunkEvent( EventState.PRE, loadChunk ? ChunkEvent.Type.LOAD : ChunkEvent.Type.UNLOAD, chunkX, - chunkY + chunkZ ) ); } @@ -35,13 +35,13 @@ public class MixinWorldClient { method = "doPreChunk", at = @At("RETURN") ) - private void postDoPreChunk(int chunkX, int chunkY, boolean loadChunk, CallbackInfo ci) { + private void postDoPreChunk(int chunkX, int chunkZ, boolean loadChunk, CallbackInfo ci) { Baritone.INSTANCE.getGameEventHandler().onChunkEvent( new ChunkEvent( EventState.POST, loadChunk ? ChunkEvent.Type.LOAD : ChunkEvent.Type.UNLOAD, chunkX, - chunkY + chunkZ ) ); }