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 General Public License as published by
|
|
|
|
* 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Baritone.
|
|
|
|
*
|
|
|
|
* Baritone is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2018-08-28 00:37:21 +00:00
|
|
|
package baritone.api.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-08-28 22:24:43 +00:00
|
|
|
import baritone.chunk.WorldProvider;
|
2018-09-01 19:26:55 +00:00
|
|
|
import baritone.utils.BlockStateInterface;
|
2018-08-22 20:15:56 +00:00
|
|
|
import baritone.utils.Helper;
|
|
|
|
import baritone.utils.InputOverrideHandler;
|
|
|
|
import baritone.utils.interfaces.Toggleable;
|
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;
|
|
|
|
|
2018-08-21 01:32:18 +00:00
|
|
|
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-21 01:32:18 +00:00
|
|
|
|
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
|
2018-08-21 22:04:10 +00:00
|
|
|
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
|
2018-08-21 01:32:18 +00:00
|
|
|
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
|
2018-08-21 01:32:18 +00:00
|
|
|
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
|
2018-08-21 01:32:18 +00:00
|
|
|
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
|
2018-08-21 01:32:18 +00:00
|
|
|
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
|
2018-08-21 01:32:18 +00:00
|
|
|
public final void onWorldEvent(WorldEvent event) {
|
2018-08-23 20:39:13 +00:00
|
|
|
WorldProvider cache = WorldProvider.INSTANCE;
|
|
|
|
|
2018-09-01 19:26:55 +00:00
|
|
|
BlockStateInterface.clearCachedChunk();
|
|
|
|
|
2018-08-23 20:39:13 +00:00
|
|
|
switch (event.getState()) {
|
|
|
|
case PRE:
|
|
|
|
break;
|
|
|
|
case POST:
|
|
|
|
cache.closeWorld();
|
2018-09-08 04:32:25 +00:00
|
|
|
if (event.getWorld() != null) {
|
2018-08-23 20:39:13 +00:00
|
|
|
cache.initWorld(event.getWorld());
|
2018-09-08 04:32:25 +00:00
|
|
|
}
|
2018-08-23 20:39:13 +00:00
|
|
|
break;
|
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
|
2018-08-21 01:32:18 +00:00
|
|
|
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
|
2018-08-21 01:32:18 +00:00
|
|
|
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
|
2018-09-10 22:18:01 +00:00
|
|
|
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)) {
|
2018-09-10 22:18:01 +00:00
|
|
|
l.onPlayerRotationMove(event);
|
2018-09-09 03:45:40 +00:00
|
|
|
}
|
2018-09-09 17:27:47 +00:00
|
|
|
});
|
2018-08-21 01:32:18 +00:00
|
|
|
}
|
|
|
|
|
2018-08-23 02:04:37 +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
|
|
|
});
|
2018-08-23 02:04:37 +00:00
|
|
|
}
|
|
|
|
|
2018-08-23 02:26:45 +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 02:26:45 +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
|
|
|
}
|
|
|
|
|
2018-08-21 01:32:18 +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
|
|
|
}
|
|
|
|
}
|