baritone/src/main/java/baritone/bot/GameEventHandler.java

112 lines
3.6 KiB
Java
Raw Normal View History

2018-08-01 17:10:48 +00:00
package baritone.bot;
import baritone.bot.behavior.Behavior;
2018-08-01 17:10:48 +00:00
import baritone.bot.event.IGameEventListener;
2018-08-02 01:46:51 +00:00
import baritone.bot.event.events.ChatEvent;
2018-08-02 07:45:15 +00:00
import baritone.bot.event.events.ChunkEvent;
import baritone.bot.event.events.RenderEvent;
2018-08-05 04:44:42 +00:00
import baritone.bot.event.events.WorldEvent;
import net.minecraft.client.settings.KeyBinding;
import org.lwjgl.input.Keyboard;
2018-08-01 17:10:48 +00:00
import java.util.function.Consumer;
2018-08-01 17:10:48 +00:00
/**
* @author Brady
* @since 7/31/2018 11:04 PM
*/
2018-08-03 20:32:54 +00:00
public final class GameEventHandler implements IGameEventListener {
2018-08-01 17:10:48 +00:00
GameEventHandler() {}
2018-08-01 17:10:48 +00:00
@Override
public final void onTick() {
2018-08-04 19:31:52 +00:00
dispatch(Behavior::onTick);
}
2018-08-05 01:55:38 +00:00
@Override
2018-08-05 02:58:32 +00:00
public void onPlayerUpdate() {
dispatch(Behavior::onPlayerUpdate);
2018-08-05 01:55:38 +00:00
}
@Override
public void onProcessKeyBinds() {
InputOverrideHandler inputHandler = Baritone.INSTANCE.getInputOverrideHandler();
// Simulate the key being held down this tick
for (InputOverrideHandler.Input input : InputOverrideHandler.Input.values()) {
KeyBinding keyBinding = input.getKeyBinding();
if (inputHandler.isInputForcedDown(keyBinding) && !keyBinding.isKeyDown()) {
int keyCode = keyBinding.getKeyCode();
if (keyCode < Keyboard.KEYBOARD_SIZE)
KeyBinding.onTick(keyCode < 0 ? keyCode + 100 : keyCode);
}
}
2018-08-04 19:31:52 +00:00
dispatch(Behavior::onProcessKeyBinds);
2018-08-01 17:10:48 +00:00
}
2018-08-02 01:46:51 +00:00
@Override
public void onSendChatMessage(ChatEvent event) {
2018-08-04 19:31:52 +00:00
dispatch(behavior -> behavior.onSendChatMessage(event));
}
2018-08-02 07:45:15 +00:00
@Override
public void onChunkEvent(ChunkEvent event) {
2018-08-05 04:44:42 +00:00
/*
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()))));
}
*/
2018-08-04 19:31:52 +00:00
dispatch(behavior -> behavior.onChunkEvent(event));
2018-08-02 07:45:15 +00:00
}
2018-08-05 02:58:32 +00:00
@Override
public void onRenderPass(RenderEvent event) {
dispatch(behavior -> onRenderPass(event));
2018-08-05 02:58:32 +00:00
}
2018-08-05 04:44:42 +00:00
@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));
}
2018-08-02 07:45:15 +00:00
private void dispatch(Consumer<Behavior> dispatchFunction) {
Baritone.INSTANCE.getBehaviors().stream().filter(Behavior::isEnabled).forEach(dispatchFunction);
}
}