baritone/src/main/java/baritone/event/GameEventHandler.java

215 lines
6.1 KiB
Java
Raw Normal View History

2018-08-11 05:03:16 +00:00
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
2018-08-11 05:03:16 +00:00
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Baritone is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
2018-08-11 05:03:16 +00:00
*
* You should have received a copy of the GNU Lesser General Public License
2018-08-11 05:03:16 +00:00
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.event;
2018-08-11 05:03:16 +00:00
2018-08-22 20:15:56 +00:00
import baritone.Baritone;
2018-08-28 00:37:21 +00:00
import baritone.api.event.events.*;
import baritone.api.event.events.type.EventState;
import baritone.api.event.listener.IGameEventListener;
2018-09-17 02:50:07 +00:00
import baritone.api.utils.interfaces.Toggleable;
2018-09-11 17:28:03 +00:00
import baritone.cache.WorldProvider;
import baritone.utils.BlockStateInterface;
2018-08-22 20:15:56 +00:00
import baritone.utils.Helper;
import baritone.utils.InputOverrideHandler;
2018-08-11 05:03:16 +00:00
import net.minecraft.client.settings.KeyBinding;
2018-08-21 22:18:35 +00:00
import net.minecraft.world.chunk.Chunk;
2018-08-11 05:03:16 +00:00
import org.lwjgl.input.Keyboard;
import java.util.ArrayList;
2018-08-11 05:03:16 +00:00
/**
* @author Brady
* @since 7/31/2018 11:04 PM
*/
public final class GameEventHandler implements IGameEventListener, Helper {
2018-09-09 18:00:57 +00:00
private final ArrayList<IGameEventListener> listeners = new ArrayList<>();
2018-08-11 05:03:16 +00:00
@Override
public final void onTick(TickEvent event) {
2018-09-09 17:27:47 +00:00
listeners.forEach(l -> {
2018-09-09 03:45:40 +00:00
if (canDispatch(l)) {
l.onTick(event);
}
2018-09-09 17:27:47 +00:00
});
2018-08-11 05:03:16 +00:00
}
@Override
public final void onPlayerUpdate(PlayerUpdateEvent event) {
2018-09-09 17:27:47 +00:00
listeners.forEach(l -> {
2018-09-09 03:45:40 +00:00
if (canDispatch(l)) {
l.onPlayerUpdate(event);
}
2018-09-09 17:27:47 +00:00
});
2018-08-11 05:03:16 +00:00
}
@Override
public final void onProcessKeyBinds() {
2018-08-11 05:03:16 +00:00
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();
2018-09-08 04:32:25 +00:00
if (keyCode < Keyboard.KEYBOARD_SIZE) {
2018-08-11 05:03:16 +00:00
KeyBinding.onTick(keyCode < 0 ? keyCode + 100 : keyCode);
2018-09-08 04:32:25 +00:00
}
2018-08-11 05:03:16 +00:00
}
}
2018-09-09 17:27:47 +00:00
listeners.forEach(l -> {
2018-09-09 03:45:40 +00:00
if (canDispatch(l)) {
l.onProcessKeyBinds();
}
2018-09-09 17:27:47 +00:00
});
2018-08-11 05:03:16 +00:00
}
@Override
public final void onSendChatMessage(ChatEvent event) {
2018-09-09 17:27:47 +00:00
listeners.forEach(l -> {
2018-09-09 03:45:40 +00:00
if (canDispatch(l)) {
l.onSendChatMessage(event);
}
2018-09-09 17:27:47 +00:00
});
2018-08-11 05:03:16 +00:00
}
@Override
public final void onChunkEvent(ChunkEvent event) {
2018-08-11 05:03:16 +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());
2018-08-23 20:39:13 +00:00
if (isPostPopulate || isPreUnload) {
WorldProvider.INSTANCE.ifWorldLoaded(world -> {
Chunk chunk = mc.world.getChunk(event.getX(), event.getZ());
world.cache.queueForPacking(chunk);
});
2018-08-11 05:03:16 +00:00
}
2018-08-23 20:39:13 +00:00
2018-09-09 17:27:47 +00:00
listeners.forEach(l -> {
2018-09-09 03:45:40 +00:00
if (canDispatch(l)) {
l.onChunkEvent(event);
}
2018-09-09 17:27:47 +00:00
});
2018-08-11 05:03:16 +00:00
}
@Override
public final void onRenderPass(RenderEvent event) {
2018-09-09 17:27:47 +00:00
listeners.forEach(l -> {
2018-09-09 03:45:40 +00:00
if (canDispatch(l)) {
l.onRenderPass(event);
}
2018-09-09 17:27:47 +00:00
});
2018-08-11 05:03:16 +00:00
}
@Override
public final void onWorldEvent(WorldEvent event) {
2018-08-23 20:39:13 +00:00
WorldProvider cache = WorldProvider.INSTANCE;
BlockStateInterface.clearCachedChunk();
2018-09-17 02:50:07 +00:00
if (event.getState() == EventState.POST) {
cache.closeWorld();
if (event.getWorld() != null) {
cache.initWorld(event.getWorld());
}
2018-08-11 05:03:16 +00:00
}
2018-09-09 17:27:47 +00:00
listeners.forEach(l -> {
2018-09-09 03:45:40 +00:00
if (canDispatch(l)) {
l.onWorldEvent(event);
}
2018-09-09 17:27:47 +00:00
});
2018-08-11 05:03:16 +00:00
}
@Override
public final void onSendPacket(PacketEvent event) {
2018-09-09 17:27:47 +00:00
listeners.forEach(l -> {
2018-09-09 03:45:40 +00:00
if (canDispatch(l)) {
l.onSendPacket(event);
}
2018-09-09 17:27:47 +00:00
});
2018-08-11 05:03:16 +00:00
}
@Override
public final void onReceivePacket(PacketEvent event) {
2018-09-09 17:27:47 +00:00
listeners.forEach(l -> {
2018-09-09 03:45:40 +00:00
if (canDispatch(l)) {
l.onReceivePacket(event);
}
2018-09-09 17:27:47 +00:00
});
2018-08-11 05:03:16 +00:00
}
2018-08-21 23:19:20 +00:00
@Override
public void onPlayerRotationMove(RotationMoveEvent event) {
2018-09-09 17:27:47 +00:00
listeners.forEach(l -> {
2018-09-09 03:45:40 +00:00
if (canDispatch(l)) {
l.onPlayerRotationMove(event);
2018-09-09 03:45:40 +00:00
}
2018-09-09 17:27:47 +00:00
});
}
@Override
public void onBlockInteract(BlockInteractEvent event) {
2018-09-09 17:27:47 +00:00
listeners.forEach(l -> {
2018-09-09 03:45:40 +00:00
if (canDispatch(l)) {
l.onBlockInteract(event);
}
2018-09-09 17:27:47 +00:00
});
}
@Override
public void onPlayerDeath() {
2018-09-09 17:27:47 +00:00
listeners.forEach(l -> {
2018-09-09 03:45:40 +00:00
if (canDispatch(l)) {
l.onPlayerDeath();
}
2018-09-09 17:27:47 +00:00
});
}
2018-08-23 22:39:02 +00:00
@Override
public void onPathEvent(PathEvent event) {
2018-09-09 17:27:47 +00:00
listeners.forEach(l -> {
2018-09-09 03:45:40 +00:00
if (canDispatch(l)) {
l.onPathEvent(event);
}
2018-09-09 17:27:47 +00:00
});
2018-08-23 22:39:02 +00:00
}
public final void registerEventListener(IGameEventListener listener) {
this.listeners.add(listener);
}
private boolean canDispatch(IGameEventListener listener) {
return !(listener instanceof Toggleable) || ((Toggleable) listener).isEnabled();
2018-08-11 05:03:16 +00:00
}
}