diff --git a/src/main/java/baritone/bot/BaritoneUser.java b/src/main/java/baritone/bot/BaritoneUser.java index 408d3831a..f75493d09 100644 --- a/src/main/java/baritone/bot/BaritoneUser.java +++ b/src/main/java/baritone/bot/BaritoneUser.java @@ -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 diff --git a/src/main/java/baritone/bot/BotPlayerContext.java b/src/main/java/baritone/bot/BotPlayerContext.java index 1cc9bd949..4928d7eee 100644 --- a/src/main/java/baritone/bot/BotPlayerContext.java +++ b/src/main/java/baritone/bot/BotPlayerContext.java @@ -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; } } diff --git a/src/main/java/baritone/bot/BotWorldProvider.java b/src/main/java/baritone/bot/BotWorldProvider.java index b9f435901..3372974ea 100644 --- a/src/main/java/baritone/bot/BotWorldProvider.java +++ b/src/main/java/baritone/bot/BotWorldProvider.java @@ -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); } diff --git a/src/main/java/baritone/bot/IBaritoneUser.java b/src/main/java/baritone/bot/IBaritoneUser.java index 6f1b22b0e..b2b1a4076 100644 --- a/src/main/java/baritone/bot/IBaritoneUser.java +++ b/src/main/java/baritone/bot/IBaritoneUser.java @@ -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. diff --git a/src/main/java/baritone/bot/handler/BotNetHandlerPlayClient.java b/src/main/java/baritone/bot/handler/BotNetHandlerPlayClient.java index 9dda0e94f..4e303bd8d 100644 --- a/src/main/java/baritone/bot/handler/BotNetHandlerPlayClient.java +++ b/src/main/java/baritone/bot/handler/BotNetHandlerPlayClient.java @@ -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 diff --git a/src/main/java/baritone/bot/spec/BotPlayerController.java b/src/main/java/baritone/bot/spec/BotPlayerController.java new file mode 100644 index 000000000..e55a7eabd --- /dev/null +++ b/src/main/java/baritone/bot/spec/BotPlayerController.java @@ -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 . + */ + +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); + } +} diff --git a/src/main/java/baritone/bot/spec/EntityBot.java b/src/main/java/baritone/bot/spec/EntityBot.java index f636474ea..209d0c632 100644 --- a/src/main/java/baritone/bot/spec/EntityBot.java +++ b/src/main/java/baritone/bot/spec/EntityBot.java @@ -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; - } }