Botcraft 26.3
Loading...
Searching...
No Matches
ConnectionClient.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <atomic>
4
6
7namespace Botcraft
8{
9 class NetworkManager;
10
11 /// @brief The base client handling connection with a server.
12 /// Only processes packets required to maintain the connection.
14 {
15 public:
17 virtual ~ConnectionClient();
18
19 /// @brief Connect the client in offline mode if login is not empty, fallback to Microsoft auth flow if empty.
20 /// @param address Address to connect to, same format as one would type it in a vanilla client
21 /// @param login Username the bot will use to connect, will default back to Microsoft auth if empty
22 void Connect(const std::string& address, const std::string& login);
23 /// @brief Connect the client in online mode. Will prompt user to auth with a valid Microsoft account
24 /// @param address Address to connect to, same format as one would type it in a vanilla client
25 /// @param cache_key Will be used to store the auth tokens in a cache file to speed up future connection with the same account.
26 /// Use different cache keys to store multiple accounts
27 void ConnectMicrosoft(const std::string& address, const std::string& cache_key = "");
28 /// @brief Connect the client in online mode using the provided minecraft token
29 /// @param address Address to connect to, same format as one would type it in a vanilla client
30 /// @param minecraft_token Minecraft token to use for auth. It's the user responsability to make sure it's valid.
31 void ConnectMinecraftToken(const std::string& address, const std::string& minecraft_token);
32 virtual void Disconnect();
33
34 bool GetShouldBeClosed() const;
35 void SetShouldBeClosed(const bool b);
36
37 std::shared_ptr<NetworkManager> GetNetworkManager() const;
38
39 /// @brief Send a message in the game chat
40 /// @param msg The message to send
41 void SendChatMessage(const std::string& msg);
42
43 /// @brief Send a command in the game chat
44 /// @param command The command to send (with no / at start)
45 void SendChatCommand(const std::string& command);
46
47 /// @brief Ask to respawn when dead
48 void Respawn();
49
50 protected:
51 using ProtocolCraft::Handler::Handle; // Don't hide all Handle() functions from base classes
52 virtual void Handle(ProtocolCraft::ClientboundLoginDisconnectPacket& packet) override;
53#if PROTOCOL_VERSION < 755 /* < 1.17 */
54 virtual void Handle(ProtocolCraft::ClientboundContainerAckPacket& packet) override;
55#endif
56 virtual void Handle(ProtocolCraft::ClientboundDisconnectPacket& packet) override;
57 virtual void Handle(ProtocolCraft::ClientboundPlayerPositionPacket& packet) override;
58#if PROTOCOL_VERSION > 763 /* > 1.20.1 */
60#endif
61#if PROTOCOL_VERSION > 768 /* > 1.21.3 */
62 virtual void Handle(ProtocolCraft::ClientboundLoginPacket& packet) override;
63 virtual void Handle(ProtocolCraft::ClientboundRespawnPacket& packet) override;
64#endif
65
66 protected:
67 std::shared_ptr<NetworkManager> network_manager;
68
69 std::atomic<bool> should_be_closed;
70 };
71} //Botcraft
The base client handling connection with a server.
void Respawn()
Ask to respawn when dead.
std::shared_ptr< NetworkManager > GetNetworkManager() const
std::atomic< bool > should_be_closed
void SendChatMessage(const std::string &msg)
Send a message in the game chat.
void ConnectMinecraftToken(const std::string &address, const std::string &minecraft_token)
Connect the client in online mode using the provided minecraft token.
void ConnectMicrosoft(const std::string &address, const std::string &cache_key="")
Connect the client in online mode.
void SendChatCommand(const std::string &command)
Send a command in the game chat.
virtual void Handle(ProtocolCraft::ClientboundLoginDisconnectPacket &packet) override
void Connect(const std::string &address, const std::string &login)
Connect the client in offline mode if login is not empty, fallback to Microsoft auth flow if empty.
std::shared_ptr< NetworkManager > network_manager
void SetShouldBeClosed(const bool b)