Bot system API exposure

This commit is contained in:
Brady
2019-01-17 14:43:44 -06:00
parent 830c8190de
commit 7c66762f48
15 changed files with 203 additions and 99 deletions
@@ -17,6 +17,7 @@
package baritone.api;
import baritone.api.bot.IUserManager;
import baritone.api.cache.IWorldScanner;
import net.minecraft.client.entity.EntityPlayerSP;
@@ -62,10 +63,15 @@ public interface IBaritoneProvider {
}
/**
* Returns the {@link IWorldScanner} instance. This is not a type returned by
* {@link IBaritone} implementation, because it is not linked with {@link IBaritone}.
* Returns the {@link IWorldScanner} instance. This is not a type returned by a
* {@link IBaritone} implementation because it is not linked with {@link IBaritone}.
*
* @return The {@link IWorldScanner} instance.
*/
IWorldScanner getWorldScanner();
/**
* @return The {@link IUserManager} instance.
*/
IUserManager getUserManager();
}
@@ -15,14 +15,12 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.bot;
package baritone.api.bot;
import baritone.api.IBaritone;
import baritone.api.utils.IPlayerController;
import baritone.bot.spec.BotPlayerController;
import baritone.bot.spec.BotWorld;
import baritone.bot.spec.EntityBot;
import com.mojang.authlib.GameProfile;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.network.INetHandler;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.play.INetHandlerPlayClient;
@@ -34,23 +32,6 @@ import net.minecraft.util.Session;
*/
public interface IBaritoneUser {
/**
* Called when the user successfully logs into a server.
*
* @param profile The game profile returned by the server on login
* @param netHandlerPlayClient The client play network handler
*/
void onLoginSuccess(GameProfile profile, INetHandlerPlayClient netHandlerPlayClient);
/**
* Called when the user loads into a world.
*
* @param world The world object
* @param player The player object
* @param playerController The player controller
*/
void onWorldLoad(BotWorld world, EntityBot player, IPlayerController playerController);
/**
* @return The network manager that is responsible for the current connection.
*/
@@ -68,7 +49,7 @@ public interface IBaritoneUser {
/**
* @return The locally managed entity for this user.
*/
EntityBot getEntity();
EntityPlayerSP getEntity();
/**
* @return The bot player controller
@@ -93,7 +74,7 @@ public interface IBaritoneUser {
/**
* @return The manager that spawned this {@link IBaritoneUser}.
*/
UserManager getManager();
IUserManager getManager();
IBaritone getBaritone();
}
@@ -0,0 +1,73 @@
/*
* 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
* 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.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.bot;
import baritone.api.bot.connect.IConnectionResult;
import com.mojang.authlib.GameProfile;
import net.minecraft.util.Session;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
/**
* @author Brady
* @since 1/17/2019
*/
public interface IUserManager {
/**
* Connects a new user with the specified {@link Session} to the current server.
*
* @param session The user session
* @return The result of the attempted connection
*/
IConnectionResult connect(Session session);
/**
* Disconnects the specified {@link IBaritoneUser} from its current server.
*
* @param user The user to disconnect
*/
void disconnect(IBaritoneUser user);
/**
* Finds the {@link IBaritoneUser} associated with the specified {@link GameProfile}
*
* @param profile The game profile of the user
* @return The user, {@link Optional#empty()} if no match or {@code profile} is {@code null}
*/
default Optional<IBaritoneUser> getUserByProfile(GameProfile profile) {
return profile == null ? Optional.empty() : users().stream().filter(user -> user.getProfile().equals(profile)).findFirst();
}
/**
* Finds the {@link IBaritoneUser} associated with the specified {@link UUID}
*
* @param uuid The uuid of the user
* @return The user, {@link Optional#empty()} if no match or {@code uuid} is {@code null}
*/
default Optional<IBaritoneUser> getUserByUUID(UUID uuid) {
return uuid == null ? Optional.empty() : users().stream().filter(user -> user.getProfile().getId().equals(uuid)).findFirst();
}
/**
* @return All of the users held by this manager
*/
List<IBaritoneUser> users();
}
@@ -15,7 +15,7 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.bot.connect;
package baritone.api.bot.connect;
/**
* @author Brady
@@ -0,0 +1,44 @@
/*
* 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
* 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.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.bot.connect;
import baritone.api.bot.IBaritoneUser;
import java.util.Optional;
/**
* @author Brady
* @since 1/17/2019
*/
public interface IConnectionResult {
/**
* @return The actual status of the connection attempt.
* @see ConnectionStatus
*/
ConnectionStatus getStatus();
/**
* Returns the user that was created in this connection this result reflects, if
* {@link #getStatus()} is {@link ConnectionStatus#SUCCESS}, otherwise it will
* return {@link Optional#empty()}.
*
* @return The user created in the connection
*/
Optional<IBaritoneUser> getUser();
}
+7 -1
View File
@@ -19,8 +19,9 @@ package baritone;
import baritone.api.IBaritone;
import baritone.api.IBaritoneProvider;
import baritone.api.bot.IUserManager;
import baritone.api.cache.IWorldScanner;
import baritone.bot.IBaritoneUser;
import baritone.api.bot.IBaritoneUser;
import baritone.bot.UserManager;
import baritone.cache.WorldScanner;
import baritone.utils.player.PrimaryPlayerContext;
@@ -55,4 +56,9 @@ public final class BaritoneProvider implements IBaritoneProvider {
public IWorldScanner getWorldScanner() {
return WorldScanner.INSTANCE;
}
@Override
public IUserManager getUserManager() {
return UserManager.INSTANCE;
}
}
+13 -13
View File
@@ -19,6 +19,8 @@ package baritone.bot;
import baritone.Baritone;
import baritone.api.IBaritone;
import baritone.api.bot.IBaritoneUser;
import baritone.api.bot.IUserManager;
import baritone.api.utils.IPlayerController;
import baritone.bot.spec.BotWorld;
import baritone.bot.spec.EntityBot;
@@ -33,7 +35,7 @@ import net.minecraft.util.Session;
* @author Brady
* @since 11/6/2018
*/
class BaritoneUser implements IBaritoneUser {
public class BaritoneUser implements IBaritoneUser {
private final UserManager manager;
private final NetworkManager networkManager;
@@ -56,24 +58,17 @@ class BaritoneUser implements IBaritoneUser {
this.baritone.init(); // actually massive iq
}
@Override
public void onLoginSuccess(GameProfile profile, INetHandlerPlayClient netHandlerPlayClient) {
this.profile = profile;
this.netHandlerPlayClient = netHandlerPlayClient;
}
public void onWorldLoad(BotWorld world, EntityBot player, IPlayerController playerController) {
this.world = world;
this.player = player;
this.playerController = playerController;
}
@Override
public IPlayerController getPlayerController() {
return this.playerController;
}
@Override
public void onLoginSuccess(GameProfile profile, INetHandlerPlayClient netHandlerPlayClient) {
this.profile = profile;
this.netHandlerPlayClient = netHandlerPlayClient;
}
@Override
public NetworkManager getNetworkManager() {
return this.networkManager;
@@ -89,6 +84,11 @@ class BaritoneUser implements IBaritoneUser {
return this.player;
}
@Override
public IPlayerController getPlayerController() {
return this.playerController;
}
@Override
public Session getSession() {
return this.session;
@@ -17,12 +17,11 @@
package baritone.bot;
import baritone.api.bot.IBaritoneUser;
import baritone.api.cache.IWorldData;
import baritone.api.utils.IPlayerContext;
import baritone.api.utils.IPlayerController;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.world.World;
public class BotPlayerContext implements IPlayerContext {
+19 -39
View File
@@ -18,13 +18,15 @@
package baritone.bot;
import baritone.api.BaritoneAPI;
import baritone.api.bot.IBaritoneUser;
import baritone.api.bot.IUserManager;
import baritone.api.bot.connect.IConnectionResult;
import baritone.api.event.events.TickEvent;
import baritone.api.event.events.type.EventState;
import baritone.api.event.listener.AbstractGameEventListener;
import baritone.bot.connect.ConnectionResult;
import baritone.bot.handler.BotNetHandlerLoginClient;
import baritone.utils.Helper;
import com.mojang.authlib.GameProfile;
import net.minecraft.client.multiplayer.ServerAddress;
import net.minecraft.client.multiplayer.ServerData;
import net.minecraft.network.EnumConnectionState;
@@ -37,17 +39,15 @@ import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;
import static baritone.bot.connect.ConnectionStatus.*;
import static baritone.api.bot.connect.ConnectionStatus.*;
/**
* @author Brady
* @since 11/6/2018
*/
public final class UserManager implements Helper {
public final class UserManager implements IUserManager, Helper {
public static final UserManager INSTANCE = new UserManager();
@@ -79,24 +79,27 @@ public final class UserManager implements Helper {
* @param session The user session
* @return The result of the attempted connection
*/
public final ConnectionResult connect(Session session) {
@Override
public final IConnectionResult connect(Session session) {
ServerData data = mc.getCurrentServerData();
if (data == null) {
return ConnectionResult.failed(NO_CURRENT_CONNECTION);
}
// Connect to the server from the parsed server data
return connect(session, ServerAddress.fromString(data.serverIP));
return connect0(session, ServerAddress.fromString(data.serverIP));
}
/**
* Connects a new user with the specified {@link Session} to the specified server.
* <p>
* Hi Mickey :)
*
* @param session The user session
* @param address The address of the server to connect to
* @return The result of the attempted connection
*/
private ConnectionResult connect(Session session, ServerAddress address) {
private IConnectionResult connect0(Session session, ServerAddress address) {
InetAddress inetAddress;
try {
@@ -114,7 +117,7 @@ public final class UserManager implements Helper {
);
// Create User
IBaritoneUser user = new BaritoneUser(this, networkManager, session);
BaritoneUser user = new BaritoneUser(this, networkManager, session);
this.users.add(user);
// Setup login handler and send connection packets
@@ -140,10 +143,13 @@ public final class UserManager implements Helper {
}
/**
* Disconnects the specified {@link IBaritoneUser} from its current server.
*
* @param user The user to disconnect
* @return The bot world provider
*/
public final BotWorldProvider getWorldProvider() {
return this.worldProvider;
}
@Override
public final void disconnect(IBaritoneUser user) {
// It's probably fine to pass null to this, because the handlers aren't doing anything with it
// noinspection ConstantConditions
@@ -151,33 +157,7 @@ public final class UserManager implements Helper {
this.users.remove(user);
}
/**
* Finds the {@link IBaritoneUser} associated with the specified {@link GameProfile}
*
* @param profile The game profile of the user
* @return The user, {@link Optional#empty()} if no match or {@code profile} is {@code null}
*/
public final Optional<IBaritoneUser> getUserByProfile(GameProfile profile) {
return profile == null ? Optional.empty() : this.users.stream().filter(user -> user.getProfile().equals(profile)).findFirst();
}
/**
* Finds the {@link IBaritoneUser} associated with the specified {@link UUID}
*
* @param uuid The uuid of the user
* @return The user, {@link Optional#empty()} if no match or {@code uuid} is {@code null}
*/
public final Optional<IBaritoneUser> getUserByUUID(UUID uuid) {
return uuid == null ? Optional.empty() : this.users.stream().filter(user -> user.getProfile().getId().equals(uuid)).findFirst();
}
/**
* @return The bot world provider
*/
public final BotWorldProvider getWorldProvider() {
return this.worldProvider;
}
@Override
public final List<IBaritoneUser> users() {
return Collections.unmodifiableList(this.users);
}
@@ -17,33 +17,46 @@
package baritone.bot.connect;
import baritone.bot.IBaritoneUser;
import baritone.api.bot.IBaritoneUser;
import baritone.api.bot.connect.ConnectionStatus;
import baritone.api.bot.connect.IConnectionResult;
import java.util.Objects;
import java.util.Optional;
import static baritone.bot.connect.ConnectionStatus.SUCCESS;
import static baritone.api.bot.connect.ConnectionStatus.SUCCESS;
/**
* @author Brady
* @since 11/6/2018
*/
public class ConnectionResult {
public final class ConnectionResult implements IConnectionResult {
/**
* The result status
*/
public final ConnectionStatus status;
private final ConnectionStatus status;
/**
* The user created, if the status is {@link ConnectionStatus#SUCCESS}
*/
public final IBaritoneUser user;
private final IBaritoneUser user;
private ConnectionResult(ConnectionStatus status, IBaritoneUser user) {
this.status = status;
this.user = user;
}
@Override
public ConnectionStatus getStatus() {
return this.status;
}
@Override
public Optional<IBaritoneUser> getUser() {
return Optional.ofNullable(user);
}
/**
* Creates a new failed {@link ConnectionResult}.
*
@@ -17,7 +17,8 @@
package baritone.bot.handler;
import baritone.bot.IBaritoneUser;
import baritone.api.bot.IBaritoneUser;
import baritone.bot.BaritoneUser;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.exceptions.AuthenticationException;
import com.mojang.authlib.exceptions.AuthenticationUnavailableException;
@@ -59,9 +60,9 @@ public class BotNetHandlerLoginClient extends NetHandlerLoginClient {
/**
* The bot of this connection
*/
private final IBaritoneUser user;
private final BaritoneUser user;
public BotNetHandlerLoginClient(NetworkManager networkManager, IBaritoneUser user) {
public BotNetHandlerLoginClient(NetworkManager networkManager, BaritoneUser user) {
super(networkManager, Minecraft.getMinecraft(), null);
this.networkManager = networkManager;
this.mc = Minecraft.getMinecraft();
@@ -17,7 +17,7 @@
package baritone.bot.handler;
import baritone.bot.IBaritoneUser;
import baritone.bot.BaritoneUser;
import baritone.bot.spec.BotPlayerController;
import baritone.bot.spec.BotWorld;
import baritone.bot.spec.EntityBot;
@@ -86,7 +86,7 @@ public class BotNetHandlerPlayClient extends NetHandlerPlayClient {
/**
* The bot of this connection
*/
private final IBaritoneUser user;
private final BaritoneUser user;
/**
* The bot entity
@@ -103,7 +103,7 @@ public class BotNetHandlerPlayClient extends NetHandlerPlayClient {
*/
private BotPlayerController playerController;
public BotNetHandlerPlayClient(NetworkManager networkManager, IBaritoneUser user, Minecraft client, GameProfile profile) {
public BotNetHandlerPlayClient(NetworkManager networkManager, BaritoneUser user, Minecraft client, GameProfile profile) {
// noinspection ConstantConditions
super(client, null, networkManager, profile);
this.networkManager = networkManager;
@@ -18,18 +18,18 @@
package baritone.bot.spec;
import baritone.api.utils.IPlayerController;
import baritone.bot.IBaritoneUser;
import baritone.api.bot.IBaritoneUser;
import net.minecraft.block.Block;
import net.minecraft.block.BlockCommandBlock;
import net.minecraft.block.BlockStructure;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.inventory.ClickType;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemSword;
import net.minecraft.network.play.client.CPacketClickWindow;
import net.minecraft.network.play.client.CPacketHeldItemChange;
import net.minecraft.network.play.client.CPacketPlayerDigging;
@@ -66,7 +66,7 @@ public class BotPlayerController implements IPlayerController {
public boolean onPlayerDamageBlock(BlockPos pos, EnumFacing side) {
this.syncHeldItem();
EntityBot player = this.user.getEntity();
EntityPlayerSP player = this.user.getEntity();
World world = player.world;
if (this.blockHitDelay > 0) {
@@ -145,7 +145,7 @@ public class BotPlayerController implements IPlayerController {
}
private boolean clickBlock(BlockPos pos, EnumFacing side) {
EntityBot player = this.user.getEntity();
EntityPlayerSP player = this.user.getEntity();
World world = player.world;
if (!canBreak(player, pos)) {
@@ -180,7 +180,7 @@ public class BotPlayerController implements IPlayerController {
}
private void handleBreak(BlockPos pos) {
EntityBot player = this.user.getEntity();
EntityPlayerSP player = this.user.getEntity();
World world = player.world;
IBlockState state = world.getBlockState(pos);
@@ -17,7 +17,7 @@
package baritone.bot.spec;
import baritone.bot.IBaritoneUser;
import baritone.api.bot.IBaritoneUser;
import baritone.utils.PlayerMovementInput;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
@@ -19,6 +19,7 @@ package baritone.utils;
import baritone.Baritone;
import baritone.api.Settings;
import baritone.api.bot.connect.IConnectionResult;
import baritone.api.cache.IRememberedInventory;
import baritone.api.cache.IWaypoint;
import baritone.api.event.events.ChatEvent;
@@ -544,7 +545,7 @@ public class ExampleBaritoneControl extends Behavior implements Helper {
// TODO: Temporary command to test bots offline
if (msg.equals("bot")) {
System.out.println("DOING A BOT");
ConnectionResult result = UserManager.INSTANCE.connect(new Session("Bot" + System.currentTimeMillis() % 1000, UUID.randomUUID().toString(), "", ""));
IConnectionResult result = UserManager.INSTANCE.connect(new Session("Bot" + System.currentTimeMillis() % 1000, UUID.randomUUID().toString(), "", ""));
System.out.println(result);
return true;
}