Less cancer

This commit is contained in:
Brady
2018-11-14 20:55:37 -06:00
parent 7cb38352ac
commit 3d5cf9772e
7 changed files with 288 additions and 23 deletions

View File

@@ -19,6 +19,8 @@ package baritone.bot;
import baritone.Baritone;
import baritone.api.IBaritone;
import baritone.bot.spec.BotPlayerController;
import baritone.bot.spec.BotWorld;
import baritone.bot.spec.EntityBot;
import com.mojang.authlib.GameProfile;
import net.minecraft.network.NetworkManager;
@@ -40,6 +42,10 @@ class BaritoneUser implements IBaritoneUser {
private GameProfile profile;
private INetHandlerPlayClient netHandlerPlayClient;
private BotWorld world;
private EntityBot player;
private BotPlayerController playerController;
private final Baritone baritone;
BaritoneUser(UserManager manager, NetworkManager networkManager, Session session) {
@@ -50,6 +56,18 @@ class BaritoneUser implements IBaritoneUser {
this.baritone.init(); // actually massive iq
}
@Override
public void onWorldLoad(BotWorld world, EntityBot player, BotPlayerController playerController) {
this.world = world;
this.player = player;
this.playerController = playerController;
}
@Override
public BotPlayerController getPlayerController() {
return this.playerController;
}
@Override
public void onLoginSuccess(GameProfile profile, INetHandlerPlayClient netHandlerPlayClient) {
this.profile = profile;
@@ -68,8 +86,7 @@ class BaritoneUser implements IBaritoneUser {
@Override
public EntityBot getEntity() {
// TODO
return null;
return this.player;
}
@Override

View File

@@ -17,10 +17,8 @@
package baritone.bot;
import baritone.api.BaritoneAPI;
import baritone.api.cache.IWorldData;
import baritone.api.utils.IPlayerContext;
import baritone.bot.handler.BotNetHandlerPlayClient;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.client.multiplayer.PlayerControllerMP;
@@ -28,6 +26,10 @@ import net.minecraft.util.math.RayTraceResult;
import net.minecraft.world.World;
public class BotPlayerContext implements IPlayerContext {
/**
* The backing {@link IBaritoneUser}
*/
private final IBaritoneUser bot;
public BotPlayerContext(IBaritoneUser bot) {
@@ -36,32 +38,36 @@ public class BotPlayerContext implements IPlayerContext {
@Override
public EntityPlayerSP player() {
if (bot.getConnection() == null) {
if (bot.getEntity() == null) {
return null;
}
return ((BotNetHandlerPlayClient) bot.getConnection()).player();
return bot.getEntity();
}
@Override
public PlayerControllerMP playerController() {
return Minecraft.getMinecraft().playerController; // idk LOL
if (bot.getEntity() == null) {
return null;
}
return bot.getPlayerController();
}
@Override
public World world() {
if (bot.getConnection() == null) {
if (bot.getEntity() == null) {
return null;
}
return ((BotNetHandlerPlayClient) bot.getConnection()).world();
return bot.getEntity().world;
}
@Override
public IWorldData worldData() {
return BaritoneAPI.getProvider().getBaritoneForPlayer(player()).getWorldProvider().getCurrentWorld();
return bot.getBaritone().getWorldProvider().getCurrentWorld();
}
@Override
public RayTraceResult objectMouseOver() {
return Minecraft.getMinecraft().objectMouseOver; // idk LOL
// TODO-yeet lol fix this
return Minecraft.getMinecraft().objectMouseOver;
}
}

View File

@@ -50,6 +50,12 @@ public class BotWorldProvider {
return worlds.computeIfAbsent(dimension, this::createWorldForDim);
}
/**
* Creates a new {@link BotWorld} for the given dimension id.
*
* @param dimension The dimension id
* @return The new world
*/
private BotWorld createWorldForDim(int dimension) {
return new BotWorld(GENERIC_WORLD_SETTINGS, dimension);
}

View File

@@ -18,6 +18,8 @@
package baritone.bot;
import baritone.api.IBaritone;
import baritone.bot.spec.BotPlayerController;
import baritone.bot.spec.BotWorld;
import baritone.bot.spec.EntityBot;
import com.mojang.authlib.GameProfile;
import net.minecraft.network.INetHandler;
@@ -39,6 +41,15 @@ public interface IBaritoneUser {
*/
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, BotPlayerController playerController);
/**
* @return The network manager that is responsible for the current connection.
*/
@@ -58,6 +69,11 @@ public interface IBaritoneUser {
*/
EntityBot getEntity();
/**
* @return The bot player controller
*/
BotPlayerController getPlayerController();
/**
* Returns the user login session. Should never be {@code null}, as this should be set when the
* user is constructed.

View File

@@ -18,6 +18,7 @@
package baritone.bot.handler;
import baritone.bot.IBaritoneUser;
import baritone.bot.spec.BotPlayerController;
import baritone.bot.spec.BotWorld;
import baritone.bot.spec.EntityBot;
import baritone.utils.Helper;
@@ -97,6 +98,11 @@ public class BotNetHandlerPlayClient extends NetHandlerPlayClient {
*/
private BotWorld world;
/**
* The current player controller
*/
private BotPlayerController playerController;
public BotNetHandlerPlayClient(NetworkManager networkManager, IBaritoneUser user, Minecraft client, GameProfile profile) {
// noinspection ConstantConditions
super(client, null, networkManager, profile);
@@ -349,13 +355,14 @@ public class BotNetHandlerPlayClient extends NetHandlerPlayClient {
public void handleJoinGame(@Nonnull SPacketJoinGame packetIn) {
PacketThreadUtil.checkThreadAndEnqueue(packetIn, this, this.client);
this.playerController = new BotPlayerController((Minecraft) this.client, this);
this.world = this.user.getManager().getWorldProvider().getWorld(packetIn.getDimension());
this.player = new EntityBot(this.user, (Minecraft) this.client, this.world, this, new StatisticsManager(), new RecipeBookClient());
this.player.preparePlayerToSpawn();
this.world.spawnEntity(this.player);
this.player.setEntityId(packetIn.getPlayerId());
this.player.dimension = packetIn.getDimension();
this.player.setGameType(packetIn.getGameType());
this.playerController.setGameType(packetIn.getGameType());
packetIn.getGameType().configurePlayerCapabilities(this.player.capabilities);
this.networkManager.sendPacket(new CPacketClientSettings("en_us", 8, EntityPlayer.EnumChatVisibility.FULL, true, 0, EnumHandSide.RIGHT));
@@ -363,6 +370,8 @@ public class BotNetHandlerPlayClient extends NetHandlerPlayClient {
this.world.registerBot(packetIn.getPlayerId(), this.player);
this.user.onWorldLoad(this.world, this.player, this.playerController);
Helper.HELPER.logDirect("Initialized Player and World");
}
@@ -477,7 +486,9 @@ public class BotNetHandlerPlayClient extends NetHandlerPlayClient {
this.player.setEntityId(prev.getEntityId());
this.player.dimension = packetIn.getDimensionID();
this.player.setServerBrand(prev.getServerBrand());
this.player.setGameType(packetIn.getGameType());
this.playerController.setGameType(packetIn.getGameType());
this.user.onWorldLoad(this.world, this.player, this.playerController);
}
@Override

View File

@@ -0,0 +1,219 @@
/*
* 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.bot.spec;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.client.multiplayer.PlayerControllerMP;
import net.minecraft.client.multiplayer.WorldClient;
import net.minecraft.client.network.NetHandlerPlayClient;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ClickType;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.stats.RecipeBook;
import net.minecraft.stats.StatisticsManager;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.GameType;
import net.minecraft.world.World;
import javax.annotation.Nonnull;
/**
* @author Brady
* @since 11/14/2018
*/
public class BotPlayerController extends PlayerControllerMP {
public BotPlayerController(Minecraft mcIn, NetHandlerPlayClient netHandler) {
super(mcIn, netHandler);
}
@Override
public void setPlayerCapabilities(EntityPlayer player) {
super.setPlayerCapabilities(player);
}
@Override
public boolean isSpectator() {
return super.isSpectator();
}
@Override
public void setGameType(@Nonnull GameType type) {
super.setGameType(type);
}
@Override
public void flipPlayer(EntityPlayer playerIn) {
super.flipPlayer(playerIn);
}
@Override
public boolean shouldDrawHUD() {
return super.shouldDrawHUD();
}
@Override
public boolean onPlayerDestroyBlock(@Nonnull BlockPos pos) {
return super.onPlayerDestroyBlock(pos);
}
@Override
public boolean clickBlock(@Nonnull BlockPos loc, @Nonnull EnumFacing face) {
return super.clickBlock(loc, face);
}
@Override
public void resetBlockRemoving() {
super.resetBlockRemoving();
}
@Override
public boolean onPlayerDamageBlock(@Nonnull BlockPos posBlock, @Nonnull EnumFacing directionFacing) {
return super.onPlayerDamageBlock(posBlock, directionFacing);
}
@Override
public float getBlockReachDistance() {
return super.getBlockReachDistance();
}
@Override
public void updateController() {
super.updateController();
}
@Nonnull
@Override
public EnumActionResult processRightClickBlock(EntityPlayerSP player, @Nonnull WorldClient worldIn, BlockPos pos, @Nonnull EnumFacing direction, Vec3d vec, @Nonnull EnumHand hand) {
return super.processRightClickBlock(player, worldIn, pos, direction, vec, hand);
}
@Nonnull
@Override
public EnumActionResult processRightClick(@Nonnull EntityPlayer player, @Nonnull World worldIn, @Nonnull EnumHand hand) {
return super.processRightClick(player, worldIn, hand);
}
@Nonnull
@Override
public EntityPlayerSP createPlayer(World p_192830_1_, @Nonnull StatisticsManager p_192830_2_, @Nonnull RecipeBook p_192830_3_) {
return super.createPlayer(p_192830_1_, p_192830_2_, p_192830_3_);
}
@Override
public void attackEntity(@Nonnull EntityPlayer playerIn, Entity targetEntity) {
super.attackEntity(playerIn, targetEntity);
}
@Nonnull
@Override
public EnumActionResult interactWithEntity(@Nonnull EntityPlayer player, Entity target, @Nonnull EnumHand hand) {
return super.interactWithEntity(player, target, hand);
}
@Nonnull
@Override
public EnumActionResult interactWithEntity(@Nonnull EntityPlayer player, Entity target, RayTraceResult ray, @Nonnull EnumHand hand) {
return super.interactWithEntity(player, target, ray, hand);
}
@Nonnull
@Override
public ItemStack windowClick(int windowId, int slotId, int mouseButton, @Nonnull ClickType type, EntityPlayer player) {
return super.windowClick(windowId, slotId, mouseButton, type, player);
}
@Override
public void func_194338_a(int p_194338_1_, @Nonnull IRecipe p_194338_2_, boolean p_194338_3_, EntityPlayer p_194338_4_) {
super.func_194338_a(p_194338_1_, p_194338_2_, p_194338_3_, p_194338_4_);
}
@Override
public void sendEnchantPacket(int windowID, int button) {
super.sendEnchantPacket(windowID, button);
}
@Override
public void sendSlotPacket(@Nonnull ItemStack itemStackIn, int slotId) {
super.sendSlotPacket(itemStackIn, slotId);
}
@Override
public void sendPacketDropItem(@Nonnull ItemStack itemStackIn) {
super.sendPacketDropItem(itemStackIn);
}
@Override
public void onStoppedUsingItem(EntityPlayer playerIn) {
super.onStoppedUsingItem(playerIn);
}
@Override
public boolean gameIsSurvivalOrAdventure() {
return super.gameIsSurvivalOrAdventure();
}
@Override
public boolean isNotCreative() {
return super.isNotCreative();
}
@Override
public boolean isInCreativeMode() {
return super.isInCreativeMode();
}
@Override
public boolean extendedReach() {
return super.extendedReach();
}
@Override
public boolean isRidingHorse() {
return super.isRidingHorse();
}
@Override
public boolean isSpectatorMode() {
return super.isSpectatorMode();
}
@Nonnull
@Override
public GameType getCurrentGameType() {
return super.getCurrentGameType();
}
@Override
public boolean getIsHittingBlock() {
return super.getIsHittingBlock();
}
@Override
public void pickItem(int index) {
super.pickItem(index);
}
}

View File

@@ -62,7 +62,6 @@ public class EntityBot extends EntityPlayerSP {
private final IBaritoneUser user;
private NetworkPlayerInfo playerInfo;
private GameType gameType;
public EntityBot(IBaritoneUser user, Minecraft mc, World world, NetHandlerPlayClient netHandlerPlayClient, StatisticsManager statisticsManager, RecipeBook recipeBook) {
super(mc, world, netHandlerPlayClient, statisticsManager, recipeBook);
@@ -167,13 +166,4 @@ public class EntityBot extends EntityPlayerSP {
protected NetworkPlayerInfo getPlayerInfo() {
return this.playerInfo == null ? (this.playerInfo = this.connection.getPlayerInfo(this.getUniqueID())) : null;
}
@Override
public void setGameType(GameType gameType) {
this.gameType = gameType;
}
public GameType getGameType() {
return this.gameType;
}
}