Botcraft 26.3
Loading...
Searching...
No Matches
ConnectionClient.cpp
Go to the documentation of this file.
4
5using namespace ProtocolCraft;
6
7namespace Botcraft
8{
14
19
20 void ConnectionClient::Connect(const std::string& address, const std::string& login)
21 {
23 {
24 network_manager->Stop();
25 network_manager.reset();
26 }
27 should_be_closed = false;
28 if (login.empty())
29 {
30 ConnectMicrosoft(address);
31 }
32 else
33 {
34 network_manager = std::make_shared<NetworkManager>(
35 address,
36 login,
38 std::vector<Handler*>{ this },
39 [this]() { should_be_closed = true; }
40 );
41 }
42 }
43
44 void ConnectionClient::ConnectMicrosoft(const std::string& address, const std::string& cache_key)
45 {
47 {
48 network_manager->Stop();
49 network_manager.reset();
50 }
51 should_be_closed = false;
52 network_manager = std::make_shared<NetworkManager>(
53 address,
54 cache_key,
56 std::vector<Handler*>{ this },
57 [this]() { should_be_closed = true; }
58 );
59 }
60
61 void ConnectionClient::ConnectMinecraftToken(const std::string& address, const std::string& minecraft_token)
62 {
64 {
65 network_manager->Stop();
66 network_manager.reset();
67 }
68 should_be_closed = false;
69 network_manager = std::make_shared<NetworkManager>(
70 address,
71 minecraft_token,
73 std::vector<Handler*>{ this },
74 [this]() { should_be_closed = true; }
75 );
76 }
77
79 {
80 should_be_closed = true;
82 {
83 network_manager->Stop();
84 }
85 network_manager.reset();
86 }
87
89 {
90 return should_be_closed;
91 }
92
94 {
96 }
97
98 std::shared_ptr<NetworkManager> ConnectionClient::GetNetworkManager() const
99 {
100 return network_manager;
101 }
102
103 void ConnectionClient::SendChatMessage(const std::string& msg)
104 {
105 if (network_manager && network_manager->GetConnectionState() == ConnectionState::Play)
106 {
107 network_manager->SendChatMessage(msg);
108 }
109 }
110
111 void ConnectionClient::SendChatCommand(const std::string& command)
112 {
113 if (network_manager && network_manager->GetConnectionState() == ConnectionState::Play)
114 {
115 network_manager->SendChatCommand(command);
116 }
117 }
118
120 {
121 if (network_manager && network_manager->GetConnectionState() == ConnectionState::Play)
122 {
123 std::shared_ptr<ServerboundClientCommandPacket> status_message = std::make_shared<ServerboundClientCommandPacket>();
124 status_message->SetAction(0);
125 network_manager->Send(status_message);
126 }
127 }
128
129
131 {
132#if PROTOCOL_VERSION < 765 /* < 1.20.3 */
133 LOG_INFO("Disconnect during login with reason: " << packet.GetReason().GetText());
134#else
135 LOG_INFO("Disconnect during login with reason: " << packet.GetReason());
136#endif
137
138 should_be_closed = true;
139 }
140
141#if PROTOCOL_VERSION < 755 /* < 1.17 */
142 void ConnectionClient::Handle(ClientboundContainerAckPacket& packet)
143 {
144 // If the transaction was not accepted, we must apologize
145 // else it's processed in InventoryManager
146 if (!packet.GetAccepted())
147 {
148 std::shared_ptr<ServerboundContainerAckPacket> apologize_packet = std::make_shared<ServerboundContainerAckPacket>();
149 apologize_packet->SetContainerId(packet.GetContainerId());
150 apologize_packet->SetUid(packet.GetUid());
151 apologize_packet->SetAccepted(packet.GetAccepted());
152
153 network_manager->Send(apologize_packet);
154 }
155 }
156#endif
157
159 {
160#if PROTOCOL_VERSION < 765 /* < 1.20.3 */
161 LOG_INFO("Disconnect during playing with reason: " << packet.GetReason().GetText());
162#else
163 LOG_INFO("Disconnect during playing with reason: " << packet.GetReason().Serialize().Dump());
164#endif
165
166 should_be_closed = true;
167 }
168
170 {
171 // Confirmations have to be sent from here, as there is no PhysicsManager with a ConnectionClient
172 std::shared_ptr<ServerboundAcceptTeleportationPacket> confirm_packet = std::make_shared<ServerboundAcceptTeleportationPacket>();
173 confirm_packet->SetId_(packet.GetId_());
174
175 network_manager->Send(confirm_packet);
176 }
177
178#if PROTOCOL_VERSION > 763 /* > 1.20.1 */
180 {
181#if PROTOCOL_VERSION < 765 /* < 1.20.3 */
182 LOG_INFO("Disconnect during configuration with reason: " << packet.GetReason().GetText());
183#else
184 LOG_INFO("Disconnect during configuration with reason: " << packet.GetReason().Serialize().Dump());
185#endif
186
187 should_be_closed = true;
188 }
189#endif
190
191#if PROTOCOL_VERSION > 768 /* > 1.21.3 */
193 {
194 network_manager->Send(std::make_shared<ServerboundPlayerLoadedPacket>());
195 }
196
198 {
199 network_manager->Send(std::make_shared<ServerboundPlayerLoadedPacket>());
200 }
201#endif
202} //Botcraft
#define LOG_INFO(osstream)
Definition Logger.hpp:43
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)
std::string Dump(const int indent=-1, const char indent_char=' ') const
public dump interface
Definition Json.cpp:287
virtual Json::Value Serialize() const