diff --git a/src/main/java/baritone/bot/GameEventHandler.java b/src/main/java/baritone/bot/GameEventHandler.java index 35cfe22a..69a22e50 100755 --- a/src/main/java/baritone/bot/GameEventHandler.java +++ b/src/main/java/baritone/bot/GameEventHandler.java @@ -4,6 +4,7 @@ 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.WorldEvent; import net.minecraft.client.settings.KeyBinding; import org.lwjgl.input.Keyboard; @@ -54,6 +55,28 @@ public final class GameEventHandler implements IGameEventListener { @Override public void onChunkEvent(ChunkEvent event) { + /* + + EventState state = event.getState(); + ChunkEvent.Type type = event.getType(); + + boolean isPostPopulate = state == EventState.POST + && type == ChunkEvent.Type.POPULATE; + + // Whenever the server sends us to another dimension, chunks are unloaded + // technically after the new world has been loaded, so we perform a check + // to make sure the chunk being unloaded is already loaded. + boolean isPreUnload = state == EventState.PRE + && type == ChunkEvent.Type.UNLOAD + && mc.world.getChunkProvider().isChunkGeneratedAt(event.getX(), event.getZ()); + + if (isPostPopulate || isPreUnload) { + CachedWorldProvider.INSTANCE.ifWorldLoaded(world -> + world.updateCachedChunk(event.getX(), event.getZ(), + ChunkPacker.createPackedChunk(mc.world.getChunk(event.getX(), event.getZ())))); + } + */ + dispatch(behavior -> behavior.onChunkEvent(event)); } @@ -62,6 +85,26 @@ public final class GameEventHandler implements IGameEventListener { dispatch(Behavior::onRenderPass); } + @Override + public void onWorldEvent(WorldEvent event) { + /* + CachedWorldProvider cache = CachedWorldProvider.INSTANCE; + + switch (event.getState()) { + case PRE: + cache.ifWorldLoaded(CachedWorld::save); + break; + case POST: + cache.closeWorld(); + if (event.getWorld() != null) + cache.initWorld(event.getWorld()); + break; + } + */ + + dispatch(behavior -> behavior.onWorldEvent(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 eda61a62..9a410478 100644 --- a/src/main/java/baritone/bot/event/AbstractGameEventListener.java +++ b/src/main/java/baritone/bot/event/AbstractGameEventListener.java @@ -2,6 +2,7 @@ package baritone.bot.event; import baritone.bot.event.events.ChatEvent; import baritone.bot.event.events.ChunkEvent; +import baritone.bot.event.events.WorldEvent; /** * An implementation of {@link IGameEventListener} that has all methods @@ -35,4 +36,7 @@ public interface AbstractGameEventListener extends IGameEventListener { @Override default void onRenderPass() {} + + @Override + default void onWorldEvent(WorldEvent event) {} } diff --git a/src/main/java/baritone/bot/event/IGameEventListener.java b/src/main/java/baritone/bot/event/IGameEventListener.java index 515ba6c4..10066b19 100755 --- a/src/main/java/baritone/bot/event/IGameEventListener.java +++ b/src/main/java/baritone/bot/event/IGameEventListener.java @@ -2,6 +2,7 @@ package baritone.bot.event; import baritone.bot.event.events.ChatEvent; import baritone.bot.event.events.ChunkEvent; +import baritone.bot.event.events.WorldEvent; import net.minecraft.client.Minecraft; import net.minecraft.client.entity.EntityPlayerSP; import net.minecraft.client.multiplayer.WorldClient; @@ -54,4 +55,11 @@ public interface IGameEventListener { * @see EntityRenderer#renderWorldPass(int, float, long) */ void onRenderPass(); + + /** + * Runs before and after whenever a new world is loaded + * + * @see Minecraft#loadWorld(WorldClient, String) + */ + void onWorldEvent(WorldEvent event); }