Botcraft 1.21.10
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 {
22 if (login.empty())
23 {
24 ConnectMicrosoft(address);
25 }
26 else
27 {
28 network_manager = std::make_shared<NetworkManager>(address, login, NetworkManager::AuthType::Offline, std::vector<Handler*>{ this });
29 }
30 }
31
32 void ConnectionClient::ConnectMicrosoft(const std::string& address, const std::string& cache_key)
33 {
34 network_manager = std::make_shared<NetworkManager>(address, cache_key, NetworkManager::AuthType::Microsoft, std::vector<Handler*>{ this });
35 }
36
37 void ConnectionClient::ConnectMinecraftToken(const std::string& address, const std::string& minecraft_token)
38 {
39 network_manager = std::make_shared<NetworkManager>(address, minecraft_token, NetworkManager::AuthType::McToken, std::vector<Handler*>{ this });
40 }
41
43 {
44 should_be_closed = true;
46 {
47 network_manager->Stop();
48 }
49 network_manager.reset();
50 }
51
53 {
55 }
56
58 {
60 }
62 std::shared_ptr<NetworkManager> ConnectionClient::GetNetworkManager() const
63 {
64 return network_manager;
65 }
66
67 void ConnectionClient::SendChatMessage(const std::string& msg)
68 {
69 if (network_manager && network_manager->GetConnectionState() == ConnectionState::Play)
70 {
71 network_manager->SendChatMessage(msg);
72 }
73 }
74
75 void ConnectionClient::SendChatCommand(const std::string& command)
76 {
77 if (network_manager && network_manager->GetConnectionState() == ConnectionState::Play)
78 {
79 network_manager->SendChatCommand(command);
80 }
81 }
82
84 {
85 if (network_manager && network_manager->GetConnectionState() == ConnectionState::Play)
86 {
87 std::shared_ptr<ServerboundClientCommandPacket> status_message = std::make_shared<ServerboundClientCommandPacket>();
88 status_message->SetAction(0);
89 network_manager->Send(status_message);
90 }
91 }
92
93
95 {
96#if PROTOCOL_VERSION < 765 /* < 1.20.3 */
97 LOG_INFO("Disconnect during login with reason: " << packet.GetReason().GetRawText());
98#else
99 LOG_INFO("Disconnect during login with reason: " << packet.GetReason());
100#endif
101
102 should_be_closed = true;
103 }
104
105#if PROTOCOL_VERSION < 755 /* < 1.17 */
106 void ConnectionClient::Handle(ClientboundContainerAckPacket& packet)
107 {
108 // If the transaction was not accepted, we must apologize
109 // else it's processed in InventoryManager
110 if (!packet.GetAccepted())
111 {
112 std::shared_ptr<ServerboundContainerAckPacket> apologize_packet = std::make_shared<ServerboundContainerAckPacket>();
113 apologize_packet->SetContainerId(packet.GetContainerId());
114 apologize_packet->SetUid(packet.GetUid());
115 apologize_packet->SetAccepted(packet.GetAccepted());
116
117 network_manager->Send(apologize_packet);
118 }
119 }
120#endif
121
123 {
124#if PROTOCOL_VERSION < 765 /* < 1.20.3 */
125 LOG_INFO("Disconnect during playing with reason: " << packet.GetReason().GetRawText());
126#else
127 LOG_INFO("Disconnect during playing with reason: " << packet.GetReason().Serialize().Dump());
128#endif
129
130 should_be_closed = true;
131 }
132
134 {
135 // Confirmations have to be sent from here, as there is no PhysicsManager with a ConnectionClient
136 std::shared_ptr<ServerboundAcceptTeleportationPacket> confirm_packet = std::make_shared<ServerboundAcceptTeleportationPacket>();
137 confirm_packet->SetId_(packet.GetId_());
138
139 network_manager->Send(confirm_packet);
140 }
141
142#if PROTOCOL_VERSION > 763 /* > 1.20.1 */
144 {
145#if PROTOCOL_VERSION < 765 /* < 1.20.3 */
146 LOG_INFO("Disconnect during configuration with reason: " << packet.GetReason().GetRawText());
147#else
148 LOG_INFO("Disconnect during configuration with reason: " << packet.GetReason().Serialize().Dump());
149#endif
150
151 should_be_closed = true;
152 }
153#endif
154
155#if PROTOCOL_VERSION > 768 /* > 1.21.3 */
157 {
158 network_manager->Send(std::make_shared<ServerboundPlayerLoadedPacket>());
159 }
160
162 {
163 network_manager->Send(std::make_shared<ServerboundPlayerLoadedPacket>());
164 }
165#endif
166} //Botcraft
#define LOG_INFO(osstream)
Definition Logger.hpp:43
void Respawn()
Ask to respawn when dead.
std::shared_ptr< NetworkManager > GetNetworkManager() const
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