Files
baritone/src/main/java/baritone/Baritone.java

221 lines
6.4 KiB
Java
Raw Normal View History

2018-08-07 22:16:53 -05: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-07 22:16:53 -05: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,
2018-08-07 22:16:53 -05:00
* 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-07 22:16:53 -05:00
*
* You should have received a copy of the GNU Lesser General Public License
2018-08-07 22:16:53 -05:00
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
2018-08-22 13:15:56 -07:00
package baritone;
2018-08-01 13:10:48 -04:00
import baritone.api.BaritoneAPI;
2018-11-03 22:11:52 -07:00
import baritone.api.IBaritone;
2018-09-23 18:35:55 -05:00
import baritone.api.Settings;
2018-11-14 16:29:15 -06:00
import baritone.api.event.listener.IEventBus;
import baritone.api.utils.IPlayerContext;
2018-12-07 16:57:12 -08:00
import baritone.behavior.*;
2018-10-27 18:45:17 -07:00
import baritone.cache.WorldProvider;
import baritone.event.GameEventHandler;
2018-12-25 21:07:17 -08:00
import baritone.process.*;
2019-01-17 18:54:37 -08:00
import baritone.utils.*;
2018-11-14 16:39:04 -08:00
import baritone.utils.player.PrimaryPlayerContext;
2018-08-22 14:59:54 -07:00
import net.minecraft.client.Minecraft;
2018-08-22 14:59:54 -07:00
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
2018-08-01 21:41:36 -07:00
import java.util.ArrayList;
import java.util.List;
2018-09-16 14:14:50 -07:00
import java.util.concurrent.Executor;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
2018-08-01 13:10:48 -04:00
/**
* @author Brady
* @since 7/31/2018
2018-08-01 13:10:48 -04:00
*/
2018-11-13 21:34:11 -08:00
public class Baritone implements IBaritone {
2018-08-01 13:10:48 -04:00
2018-11-11 17:45:01 -06:00
private static ThreadPoolExecutor threadPool;
private static File dir;
static {
threadPool = new ThreadPoolExecutor(4, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<>());
dir = new File(Minecraft.getMinecraft().gameDir, "baritone");
if (!Files.exists(dir.toPath())) {
try {
Files.createDirectories(dir.toPath());
} catch (IOException ignored) {}
}
}
2018-08-01 13:10:48 -04:00
/**
* Whether or not {@link Baritone#init()} has been called yet
*/
private boolean initialized;
private GameEventHandler gameEventHandler;
2018-09-16 14:14:50 -07:00
2018-10-27 18:45:17 -07:00
private List<Behavior> behaviors;
private PathingBehavior pathingBehavior;
private LookBehavior lookBehavior;
private MemoryBehavior memoryBehavior;
2019-01-12 19:51:21 -08:00
private InventoryBehavior inventoryBehavior;
2018-11-05 14:47:40 -08:00
private InputOverrideHandler inputOverrideHandler;
2018-11-03 22:11:52 -07:00
private FollowProcess followProcess;
private MineProcess mineProcess;
private GetToBlockProcess getToBlockProcess;
private CustomGoalProcess customGoalProcess;
2018-12-25 21:07:17 -08:00
private BuilderProcess builderProcess;
2018-11-03 22:11:52 -07:00
private PathingControlManager pathingControlManager;
2018-10-27 18:45:17 -07:00
2018-11-13 21:45:26 -06:00
private IPlayerContext playerContext;
private WorldProvider worldProvider;
2019-01-17 18:54:37 -08:00
public BlockStateInterface bsi;
Baritone() {
2018-11-05 20:01:46 -08:00
this.gameEventHandler = new GameEventHandler(this);
}
2019-02-19 14:43:48 -08:00
@Override
2018-08-13 20:57:29 -07:00
public synchronized void init() {
if (initialized) {
return;
}
2018-09-23 18:35:55 -05:00
// Define this before behaviors try and get it, or else it will be null and the builds will fail!
2018-11-14 16:39:04 -08:00
this.playerContext = PrimaryPlayerContext.INSTANCE;
2018-08-01 21:41:36 -07:00
this.behaviors = new ArrayList<>();
{
2018-10-27 18:45:17 -07:00
// the Behavior constructor calls baritone.registerBehavior(this) so this populates the behaviors arraylist
pathingBehavior = new PathingBehavior(this);
lookBehavior = new LookBehavior(this);
memoryBehavior = new MemoryBehavior(this);
2019-01-12 19:51:21 -08:00
inventoryBehavior = new InventoryBehavior(this);
2018-11-05 14:47:40 -08:00
inputOverrideHandler = new InputOverrideHandler(this);
new ExampleBaritoneControl(this);
}
this.pathingControlManager = new PathingControlManager(this);
{
2018-11-03 22:11:52 -07:00
followProcess = new FollowProcess(this);
mineProcess = new MineProcess(this);
2018-11-04 10:29:22 -08:00
customGoalProcess = new CustomGoalProcess(this); // very high iq
getToBlockProcess = new GetToBlockProcess(this);
2018-12-25 21:07:17 -08:00
builderProcess = new BuilderProcess(this);
}
2018-11-05 14:47:40 -08:00
this.worldProvider = new WorldProvider();
2018-09-26 16:21:53 -05:00
if (BaritoneAutoTest.ENABLE_AUTO_TEST) {
2018-11-14 16:29:15 -06:00
this.gameEventHandler.registerEventListener(BaritoneAutoTest.INSTANCE);
2018-09-25 11:19:25 -07:00
}
2018-08-01 13:10:48 -04:00
this.initialized = true;
}
@Override
2018-11-03 22:11:52 -07:00
public PathingControlManager getPathingControlManager() {
2018-11-13 21:47:40 -06:00
return this.pathingControlManager;
2018-11-03 22:11:52 -07:00
}
2018-09-16 14:14:50 -07:00
public List<Behavior> getBehaviors() {
return this.behaviors;
}
2018-08-13 20:57:29 -07:00
public void registerBehavior(Behavior behavior) {
this.behaviors.add(behavior);
2018-11-14 16:29:15 -06:00
this.gameEventHandler.registerEventListener(behavior);
}
2018-11-13 21:45:26 -06:00
@Override
public InputOverrideHandler getInputOverrideHandler() {
return this.inputOverrideHandler;
}
2018-11-05 14:41:17 -08:00
@Override
2018-11-11 17:45:01 -06:00
public CustomGoalProcess getCustomGoalProcess() { // Iffy
2018-11-13 21:47:40 -06:00
return this.customGoalProcess;
2018-11-04 10:29:22 -08:00
}
2018-11-03 22:11:52 -07:00
2018-11-05 14:41:17 -08:00
@Override
2018-11-11 17:45:01 -06:00
public GetToBlockProcess getGetToBlockProcess() { // Iffy
2018-11-13 21:47:40 -06:00
return this.getToBlockProcess;
2018-11-03 22:11:52 -07:00
}
@Override
public IPlayerContext getPlayerContext() {
2018-11-13 21:47:40 -06:00
return this.playerContext;
}
2018-12-03 17:20:56 -08:00
public MemoryBehavior getMemoryBehavior() {
return this.memoryBehavior;
}
2018-10-27 18:45:17 -07:00
@Override
2018-11-03 22:11:52 -07:00
public FollowProcess getFollowProcess() {
2018-11-13 21:47:40 -06:00
return this.followProcess;
2018-10-27 18:45:17 -07:00
}
@Override
2018-12-25 21:07:17 -08:00
public BuilderProcess getBuilderProcess() {
return this.builderProcess;
}
2019-01-12 19:51:21 -08:00
public InventoryBehavior getInventoryBehavior() {
return this.inventoryBehavior;
}
2018-10-27 18:45:17 -07:00
@Override
2018-10-27 23:21:30 -05:00
public LookBehavior getLookBehavior() {
2018-11-13 21:47:40 -06:00
return this.lookBehavior;
2018-10-27 18:45:17 -07:00
}
@Override
2018-11-03 22:11:52 -07:00
public MineProcess getMineProcess() {
2018-11-13 21:47:40 -06:00
return this.mineProcess;
2018-10-27 18:45:17 -07:00
}
@Override
2018-10-27 23:21:30 -05:00
public PathingBehavior getPathingBehavior() {
2018-11-13 21:47:40 -06:00
return this.pathingBehavior;
2018-10-27 18:45:17 -07:00
}
@Override
2018-10-27 23:21:30 -05:00
public WorldProvider getWorldProvider() {
2018-11-13 21:47:40 -06:00
return this.worldProvider;
2018-10-27 18:45:17 -07:00
}
@Override
2018-11-14 16:29:15 -06:00
public IEventBus getGameEventHandler() {
return this.gameEventHandler;
2018-08-13 20:57:29 -07:00
}
2018-08-13 17:15:59 -07:00
public static Settings settings() {
2018-11-11 17:45:01 -06:00
return BaritoneAPI.getSettings();
2018-08-13 17:15:59 -07:00
}
2018-08-22 14:59:54 -07:00
2018-11-05 20:01:46 -08:00
public static File getDir() {
2018-11-11 17:45:01 -06:00
return dir;
}
public static Executor getExecutor() {
return threadPool;
2018-08-22 14:59:54 -07:00
}
2018-08-01 13:10:48 -04:00
}