Botcraft 1.21.4
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, const bool force_microsoft_account)
21 {
22 network_manager = std::make_shared<NetworkManager>(address, login, force_microsoft_account, std::vector<Handler*>(1, this));
23 }
24
26 {
27 should_be_closed = true;
29 {
30 network_manager->Stop();
31 }
32 network_manager.reset();
33 }
34
36 {
37 return should_be_closed;
38 }
39
41 {
43 }
44
45 std::shared_ptr<NetworkManager> ConnectionClient::GetNetworkManager() const
46 {
48 }
50 void ConnectionClient::SendChatMessage(const std::string& msg)
51 {
52 if (network_manager && network_manager->GetConnectionState() == ConnectionState::Play)
53 {
54 network_manager->SendChatMessage(msg);
55 }
56 }
57
58 void ConnectionClient::SendChatCommand(const std::string& command)
59 {
60 if (network_manager && network_manager->GetConnectionState() == ConnectionState::Play)
61 {
62 network_manager->SendChatCommand(command);
63 }
64 }
65
67 {
68 if (network_manager && network_manager->GetConnectionState() == ConnectionState::Play)
69 {
70 std::shared_ptr<ServerboundClientCommandPacket> status_message = std::make_shared<ServerboundClientCommandPacket>();
71 status_message->SetAction(0);
72 network_manager->Send(status_message);
73 }
74 }
75
76
78 {
79#if PROTOCOL_VERSION < 765 /* < 1.20.3 */
80 LOG_INFO("Disconnect during login with reason: " << msg.GetReason().GetRawText());
81#else
82 LOG_INFO("Disconnect during login with reason: " << msg.GetReason());
83#endif
84
85 should_be_closed = true;
86 }
87
88#if PROTOCOL_VERSION < 755 /* < 1.17 */
89 void ConnectionClient::Handle(ClientboundContainerAckPacket& msg)
90 {
91 // If the transaction was not accepted, we must apologize
92 // else it's processed in InventoryManager
93 if (!msg.GetAccepted())
94 {
95 std::shared_ptr<ServerboundContainerAckPacket> apologize_msg = std::make_shared<ServerboundContainerAckPacket>();
96 apologize_msg->SetContainerId(msg.GetContainerId());
97 apologize_msg->SetUid(msg.GetUid());
98 apologize_msg->SetAccepted(msg.GetAccepted());
99
100 network_manager->Send(apologize_msg);
101 }
102 }
103#endif
104
106 {
107#if PROTOCOL_VERSION < 765 /* < 1.20.3 */
108 LOG_INFO("Disconnect during playing with reason: " << msg.GetReason().GetRawText());
109#else
110 LOG_INFO("Disconnect during playing with reason: " << msg.GetReason().Serialize().Dump());
111#endif
112
113 should_be_closed = true;
114 }
115
117 {
118 // Confirmations have to be sent from here, as there is no PhysicsManager with a ConnectionClient
119 std::shared_ptr<ServerboundAcceptTeleportationPacket> confirm_msg = std::make_shared<ServerboundAcceptTeleportationPacket>();
120 confirm_msg->SetId_(msg.GetId_());
121
122 network_manager->Send(confirm_msg);
123 }
124
125#if PROTOCOL_VERSION > 763 /* > 1.20.1 */
127 {
128#if PROTOCOL_VERSION < 765 /* < 1.20.3 */
129 LOG_INFO("Disconnect during configuration with reason: " << msg.GetReason().GetRawText());
130#else
131 LOG_INFO("Disconnect during configuration with reason: " << msg.GetReason().Serialize().Dump());
132#endif
133
134 should_be_closed = true;
135 }
136#endif
137
138#if PROTOCOL_VERSION > 768 /* > 1.21.3 */
140 {
141 network_manager->Send(std::make_shared<ServerboundPlayerLoadedPacket>());
142 }
143
145 {
146 network_manager->Send(std::make_shared<ServerboundPlayerLoadedPacket>());
147 }
148#endif
149} //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.
virtual void Handle(ProtocolCraft::ClientboundLoginDisconnectPacket &msg) override
void SendChatCommand(const std::string &command)
Send a command in the game chat.
void Connect(const std::string &address, const std::string &login, const bool force_microsoft_account=false)
Connect the client to the server at address.
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