summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLaG1924 <12997935+LaG1924@users.noreply.github.com>2017-10-14 18:40:34 +0200
committerLaG1924 <12997935+LaG1924@users.noreply.github.com>2018-01-13 03:39:32 +0100
commitabba018da3c2c8011b1485ee8e9e5b2690659c76 (patch)
tree88642b34e91957bbb5e6f05aca4107b512b7b4f2 /src
parent2017-10-09 (diff)
downloadAltCraft-abba018da3c2c8011b1485ee8e9e5b2690659c76.tar
AltCraft-abba018da3c2c8011b1485ee8e9e5b2690659c76.tar.gz
AltCraft-abba018da3c2c8011b1485ee8e9e5b2690659c76.tar.bz2
AltCraft-abba018da3c2c8011b1485ee8e9e5b2690659c76.tar.lz
AltCraft-abba018da3c2c8011b1485ee8e9e5b2690659c76.tar.xz
AltCraft-abba018da3c2c8011b1485ee8e9e5b2690659c76.tar.zst
AltCraft-abba018da3c2c8011b1485ee8e9e5b2690659c76.zip
Diffstat (limited to 'src')
-rw-r--r--src/Chat.cpp36
-rw-r--r--src/Chat.hpp24
-rw-r--r--src/Event.hpp14
-rw-r--r--src/GameState.cpp6
-rw-r--r--src/GlobalState.cpp5
-rw-r--r--src/Network.cpp8
-rw-r--r--src/NetworkClient.cpp2
-rw-r--r--src/Packet.hpp42
-rw-r--r--src/Render.cpp242
-rw-r--r--src/Render.hpp4
-rw-r--r--src/Stream.cpp33
-rw-r--r--src/Stream.hpp19
-rw-r--r--src/Utility.hpp2
13 files changed, 296 insertions, 141 deletions
diff --git a/src/Chat.cpp b/src/Chat.cpp
new file mode 100644
index 0000000..0857d6c
--- /dev/null
+++ b/src/Chat.cpp
@@ -0,0 +1,36 @@
+#include "Chat.hpp"
+
+#include <nlohmann/json.hpp>
+#include <easylogging++.h>
+
+Chat::Chat(const std::string &str) {
+ using nlohmann::json;
+ json j = json::parse(str);
+
+ /*LOG(WARNING) << j.dump(4);
+
+ std::function<void(json::iterator)> iterating = [&](json::iterator iter) {
+ json val = *iter;
+
+ if (val.is_object() && val.find("text") != val.end()) {
+ text.append(val["text"].get<std::string>());
+ }
+
+ if (val.is_array() || val.is_object()) {
+ for (auto it = val.begin(); it != val.end(); ++it) {
+ iterating(it);
+ }
+ }
+ };
+
+ for (auto it = j.begin(); it != j.end(); ++it) {
+ iterating(it);
+ }*/
+
+ text = j.dump(4);
+}
+
+std::string Chat::ToJson() const {
+ throw std::logic_error("Chat not deserealizable");
+ return text;
+} \ No newline at end of file
diff --git a/src/Chat.hpp b/src/Chat.hpp
new file mode 100644
index 0000000..4d147be
--- /dev/null
+++ b/src/Chat.hpp
@@ -0,0 +1,24 @@
+#pragma once
+
+#include <vector>
+
+struct TextModifier {
+ size_t offset;
+ size_t length;
+ enum {
+ Italic,
+ Bold,
+ Underline,
+ } type;
+};
+
+struct Chat {
+ std::vector<TextModifier> modifiers;
+ std::string text;
+
+ Chat(const std::string &str);
+
+ Chat() = default;
+
+ std::string ToJson() const;
+}; \ No newline at end of file
diff --git a/src/Event.hpp b/src/Event.hpp
index 9ee445a..d2ddb15 100644
--- a/src/Event.hpp
+++ b/src/Event.hpp
@@ -43,6 +43,8 @@ enum class EventType {
BlockChange,
RendererWorkerTask,
ChunkDeleted,
+ ChatMessageReceived,
+ SendChatMessage,
};
struct EchoData {
@@ -165,13 +167,23 @@ struct ChunkDeletedData {
Vector pos;
};
+struct ChatMessageReceivedData {
+ Chat message;
+ unsigned char position;
+};
+
+struct SendChatMessageData {
+ std::string message;
+};
+
using EventData = std::variant<EchoData, ChunkChangedData, ConnectToServerData, ConnectionSuccessfullData,
DisconnectData, SendPacketData, ReceivePacketData, RequestNetworkClientData, RegisterNetworkClientData,
PlayerConnectedData, RemoveLoadingScreenData, ConnectionFailedData, ExitData, DisconnectedData,
ConnectingData, NetworkClientExceptionData, MouseMovedData, KeyPressedData, KeyReleasedData,
InitalizeSectionRenderData, CreateSectionRenderData, CreatedSectionRenderData, PlayerPosChangedData,
UpdateSectionsRenderData, DeleteSectionRenderData, EntityChangedData,NewRenderDataAvailableData,
- BlockChangeData, RendererWorkerTaskData, ChunkDeletedData>;
+ BlockChangeData, RendererWorkerTaskData, ChunkDeletedData, ChatMessageReceivedData,
+ SendChatMessageData>;
struct Event {
EventType type;
diff --git a/src/GameState.cpp b/src/GameState.cpp
index 3020bde..945824f 100644
--- a/src/GameState.cpp
+++ b/src/GameState.cpp
@@ -113,8 +113,12 @@ void GameState::UpdatePacket(NetworkClient *nc)
break;
case TabCompleteCB:
break;
- case ChatMessageCB:
+ case ChatMessageCB: {
+ auto packet = std::static_pointer_cast<PacketChatMessageCB>(ptr);
+ LOG(INFO) << "Message (" << int(packet->Position) << "): " << packet->JsonData.text;
+ EventAgregator::PushEvent(EventType::ChatMessageReceived, ChatMessageReceivedData{ packet->JsonData,packet->Position });
break;
+ }
case MultiBlockChange: {
auto packet = std::static_pointer_cast<PacketMultiBlockChange>(ptr);
world.ParseChunkData(packet);
diff --git a/src/GlobalState.cpp b/src/GlobalState.cpp
index 49dbf2b..78fba0e 100644
--- a/src/GlobalState.cpp
+++ b/src/GlobalState.cpp
@@ -78,13 +78,16 @@ void InitEvents() {
});
listener.RegisterHandler(EventType::Disconnected, [](EventData eventData) {
- std::this_thread::sleep_for(std::chrono::milliseconds(500));
if (!gs)
return;
isPhysRunning = false;
threadPhys.join();
gs.reset();
});
+
+ listener.RegisterHandler(EventType::SendChatMessage, [](EventData eventData) {
+ nc->SendPacket(std::make_shared<PacketChatMessageSB>(std::get<SendChatMessageData>(eventData).message));
+ });
}
void PhysExec() {
diff --git a/src/Network.cpp b/src/Network.cpp
index 9cb2097..2c14327 100644
--- a/src/Network.cpp
+++ b/src/Network.cpp
@@ -93,11 +93,7 @@ void Network::SendPacket(Packet &packet, int compressionThreshold) {
stream->WriteVarInt(packet.GetPacketId());
packet.ToStream(stream);
} else {
- throw std::runtime_error("Compressing data");
- /*StreamBuffer buffer(packetSize.GetCountedSize());
- packet.ToStream(&buffer);
-
- z_stream stream;*/
+ throw std::runtime_error("Compressing send data not supported");
}
}
else {
@@ -177,7 +173,7 @@ std::shared_ptr<Packet> Network::ParsePacketPlay(PacketNamePlayCB id) {
case TabCompleteCB:
break;
case ChatMessageCB:
- break;
+ return std::make_shared<PacketChatMessageCB>();
case MultiBlockChange:
return std::make_shared<PacketMultiBlockChange>();
case ConfirmTransactionCB:
diff --git a/src/NetworkClient.cpp b/src/NetworkClient.cpp
index dd467e8..36c6912 100644
--- a/src/NetworkClient.cpp
+++ b/src/NetworkClient.cpp
@@ -13,7 +13,7 @@ NetworkClient::NetworkClient(std::string address, unsigned short port, std::stri
state = Login;
PacketLoginStart loginStart;
- loginStart.Username = "HelloOne";
+ loginStart.Username = username;
network.SendPacket(loginStart);
diff --git a/src/Packet.hpp b/src/Packet.hpp
index 0470015..f31fa59 100644
--- a/src/Packet.hpp
+++ b/src/Packet.hpp
@@ -241,7 +241,7 @@ struct PacketDisconnectPlay : Packet {
}
void FromStream(StreamInput *stream) override {
- Reason = stream->ReadChat();
+ Reason = stream->ReadChat().text;
}
int GetPacketId() override {
@@ -861,7 +861,7 @@ struct PacketOpenWindow : Packet {
void FromStream(StreamInput *stream) override {
WindowId = stream->ReadUByte();
WindowType = stream->ReadString();
- WindowTitle = stream->ReadChat();
+ WindowTitle = stream->ReadChat().text;
NumberOfSlots = stream->ReadUByte();
if (WindowType == "EntityHorse")
@@ -1032,7 +1032,7 @@ struct PacketDisconnect : Packet {
}
void FromStream(StreamInput *stream) override {
- Reason = stream->ReadChat();
+ Reason = stream->ReadChat().text;
}
int GetPacketId() override {
@@ -1056,4 +1056,40 @@ struct PacketSetCompression : Packet {
}
int Threshold;
+};
+
+struct PacketChatMessageCB : Packet {
+ void ToStream(StreamOutput *stream) override {
+
+ }
+
+ void FromStream(StreamInput *stream) override {
+ JsonData = stream->ReadChat();
+ Position = stream->ReadByte();
+ }
+
+ int GetPacketId() override {
+ return PacketNamePlayCB::ChatMessageCB;
+ }
+
+ Chat JsonData;
+ unsigned char Position;
+};
+
+struct PacketChatMessageSB : Packet {
+ void ToStream(StreamOutput *stream) override {
+ stream->WriteString(Message);
+ }
+
+ void FromStream(StreamInput *stream) override {
+
+ }
+
+ int GetPacketId() override {
+ return PacketNamePlaySB::ChatMessageSB;
+ }
+
+ std::string Message;
+
+ PacketChatMessageSB(const std::string msg) : Message(msg) {};
}; \ No newline at end of file
diff --git a/src/Render.cpp b/src/Render.cpp
index 6bf3eb3..316efec 100644
--- a/src/Render.cpp
+++ b/src/Render.cpp
@@ -154,9 +154,8 @@ void Render::HandleEvents() {
case SDL_WINDOWEVENT_FOCUS_LOST:
HasFocus = false;
SetMouseCapture(false);
- if (state == GameState::Playing)
+ if (state == GameState::Inventory || state == GameState::Playing || state == GameState::Chat)
state = GameState::Paused;
- isDisplayInventory = false;
break;
}
break;
@@ -164,26 +163,49 @@ void Render::HandleEvents() {
case SDL_KEYDOWN:
switch (event.key.keysym.scancode) {
case SDL_SCANCODE_ESCAPE:
- if (state == GameState::Playing) {
+ switch (state) {
+ case GameState::Playing:
state = GameState::Paused;
SetMouseCapture(false);
- isDisplayInventory = false;
- }
- else if (state == GameState::Paused) {
+ break;
+ case GameState::Inventory:
state = GameState::Playing;
SetMouseCapture(true);
- }
- else if (state == GameState::MainMenu) {
+ break;
+ case GameState::Paused:
+ state = GameState::Playing;
+ SetMouseCapture(true);
+ break;
+ case GameState::MainMenu:
LOG(INFO) << "Received close event by esc";
isRunning = false;
- }
+ break;
+ }
break;
case SDL_SCANCODE_E:
- if (state != GameState::Playing)
- return;
- isDisplayInventory = !isDisplayInventory;
- SetMouseCapture(!isDisplayInventory);
- break;
+ switch (state) {
+ case GameState::Playing:
+ state = GameState::Inventory;
+ SetMouseCapture(false);
+ break;
+ case GameState::Inventory:
+ state = GameState::Playing;
+ SetMouseCapture(true);
+ break;
+ }
+ break;
+ case SDL_SCANCODE_T:
+ switch (state) {
+ case GameState::Playing:
+ state = GameState::Chat;
+ SetMouseCapture(false);
+ break;
+ case GameState::Chat:
+ state = GameState::Playing;
+ SetMouseCapture(true);
+ break;
+ }
+ break;
}
break;
case SDL_MOUSEMOTION:
@@ -264,6 +286,12 @@ void Render::ExecuteRenderLoop() {
state = GameState::Loading;
});
+ listener.RegisterHandler(EventType::ChatMessageReceived, [this](EventData eventData) {
+ auto data = std::get<ChatMessageReceivedData>(eventData);
+ std::string msg = "(" + std::to_string((int)data.position) + ") " + data.message.text;
+ chatMessages.push_back(msg);
+ });
+
state = GameState::MainMenu;
while (isRunning) {
@@ -290,21 +318,21 @@ void Render::RenderGui() {
}
const ImGuiWindowFlags windowFlags = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings;
- //ImGui::ShowTestWindow();
+ ImGui::ShowTestWindow();
ImGui::SetNextWindowPos(ImVec2(10, 10));
ImGui::Begin("DebugInfo", 0, ImVec2(0, 0), 0.4f, windowFlags);
ImGui::Text("Debug Info:");
ImGui::Separator();
ImGui::Text("State: %s", stateString.c_str());
- ImGui::Text("FPS: %.1f (%.3fms)", ImGui::GetIO().Framerate, 1000.0f / ImGui::GetIO().Framerate);
- float gameTime = DebugInfo::gameThreadTime / 100.0f;
+ ImGui::Text("FPS: %.1f (%.3fms)", ImGui::GetIO().Framerate, 1000.0f / ImGui::GetIO().Framerate);
+ float gameTime = DebugInfo::gameThreadTime / 100.0f;
if (world) {
ImGui::Text("TPS: %.1f (%.2fms)", 1000.0f / gameTime, gameTime);
ImGui::Text("Sections loaded: %d", (int)DebugInfo::totalSections);
ImGui::Text("SectionsRenderer: %d (%d)", (int)DebugInfo::renderSections, (int)DebugInfo::readyRenderer);
ImGui::Text("Culled sections: %d", (int)DebugInfo::renderSections - world->culledSections);
- ImGui::Text("Player pos: %.1f %.1f %.1f OnGround=%d", world->GameStatePtr()->player->pos.x, world->GameStatePtr()->player->pos.y, world->GameStatePtr()->player->pos.z,world->GameStatePtr()->player->onGround);
+ ImGui::Text("Player pos: %.1f %.1f %.1f OnGround=%d", world->GameStatePtr()->player->pos.x, world->GameStatePtr()->player->pos.y, world->GameStatePtr()->player->pos.z, world->GameStatePtr()->player->onGround);
ImGui::Text("Player vel: %.1f %.1f %.1f", world->GameStatePtr()->player->vel.x, world->GameStatePtr()->player->vel.y, world->GameStatePtr()->player->vel.z);
ImGui::Text("Player health: %.1f/%.1f", world->GameStatePtr()->g_PlayerHealth, 20.0f);
}
@@ -314,7 +342,7 @@ void Render::RenderGui() {
switch (state) {
case GameState::MainMenu: {
ImGui::SetNextWindowPosCenter();
- ImGui::Begin("Menu",0, windowFlags);
+ ImGui::Begin("Menu", 0, windowFlags);
static char buff[512] = "127.0.0.1";
static int port = 25565;
static char buffName[512] = "HelloOne";
@@ -323,7 +351,7 @@ void Render::RenderGui() {
}
ImGui::InputText("Username", buffName, 512);
ImGui::InputText("Address", buff, 512);
- ImGui::InputInt("Port", &port);
+ ImGui::InputInt("Port", &port);
ImGui::Separator();
if (ImGui::Button("Exit"))
isRunning = false;
@@ -332,96 +360,112 @@ void Render::RenderGui() {
}
case GameState::Loading:
break;
- case GameState::Playing:
- if (isDisplayInventory) {
- auto renderSlot = [](const SlotData &slot, int i) -> bool {
- return ImGui::Button(((slot.BlockId == -1 ? " ##" :
- AssetManager::Instance().GetAssetNameByBlockId(BlockId{ (unsigned short)slot.BlockId,0 }) +" x"+std::to_string(slot.ItemCount) + "##")
- + std::to_string(i)).c_str());
- };
- ImGui::SetNextWindowPosCenter();
- ImGui::Begin("Inventory", 0, windowFlags);
- Window& inventory = world->GameStatePtr()->playerInventory;
- //Hand and drop slots
- if (renderSlot(inventory.handSlot, -1)) {
+ case GameState::Chat: {
+ ImGui::SetNextWindowPosCenter();
+ ImGui::Begin("Chat", 0, windowFlags);
+ for (const auto& msg : chatMessages) {
+ ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(1,1,1,1));
+ ImGui::TextWrapped("%s", msg.c_str());
+ }
+ static char buff[256];
+ ImGui::InputText("", buff, 256);
+ ImGui::SameLine();
+ if (ImGui::Button("Send")) {
+ EventAgregator::PushEvent(EventType::SendChatMessage, SendChatMessageData{ buff });
+ }
+ ImGui::End();
+ break;
+ }
+ case GameState::Inventory: {
+ auto renderSlot = [](const SlotData &slot, int i) -> bool {
+ return ImGui::Button(((slot.BlockId == -1 ? " ##" :
+ AssetManager::Instance().GetAssetNameByBlockId(BlockId{ (unsigned short)slot.BlockId,0 }) + " x" + std::to_string(slot.ItemCount) + "##")
+ + std::to_string(i)).c_str());
+ };
+ ImGui::SetNextWindowPosCenter();
+ ImGui::Begin("Inventory", 0, windowFlags);
+ Window& inventory = world->GameStatePtr()->playerInventory;
+ //Hand and drop slots
+ if (renderSlot(inventory.handSlot, -1)) {
+ }
+ ImGui::SameLine();
+ if (ImGui::Button("Drop")) {
+ inventory.MakeClick(-1, true, true);
+ }
+ ImGui::SameLine();
+ ImGui::Text("Hand slot and drop mode");
+ ImGui::Separator();
+ //Crafting
+ if (renderSlot(inventory.slots[1], 1)) {
+ inventory.MakeClick(1, true);
+ }
+ ImGui::SameLine();
+ if (renderSlot(inventory.slots[2], 2)) {
+ inventory.MakeClick(2, true);
+ }
+ //Crafting result
+ ImGui::SameLine();
+ ImGui::Text("Result");
+ ImGui::SameLine();
+ if (renderSlot(inventory.slots[0], 0)) {
+ inventory.MakeClick(0, true);
+ }
+ //Crafting second line
+ if (renderSlot(inventory.slots[3], 3)) {
+ inventory.MakeClick(3, true);
+ }
+ ImGui::SameLine();
+ if (renderSlot(inventory.slots[4], 4)) {
+ inventory.MakeClick(4, true);
+ }
+ ImGui::Separator();
+ //Armor and offhand
+ for (int i = 5; i < 8 + 1; i++) {
+ if (renderSlot(inventory.slots[i], i)) {
+ inventory.MakeClick(i, true);
}
ImGui::SameLine();
- if (ImGui::Button("Drop")) {
- inventory.MakeClick(-1, true, true);
- }
- ImGui::SameLine();
- ImGui::Text("Hand slot and drop mode");
- ImGui::Separator();
- //Crafting
- if (renderSlot(inventory.slots[1], 1)) {
- inventory.MakeClick(1, true);
+ }
+ if (renderSlot(inventory.slots[45], 45)) {
+ inventory.MakeClick(45, true);
+ }
+ ImGui::SameLine();
+ ImGui::Text("Armor and offhand");
+ ImGui::Separator();
+ for (int i = 36; i < 44 + 1; i++) {
+ if (renderSlot(inventory.slots[i], i)) {
+ inventory.MakeClick(i, true);
}
ImGui::SameLine();
- if (renderSlot(inventory.slots[2], 2)) {
- inventory.MakeClick(2, true);
+ }
+ ImGui::Text("Hotbar");
+ ImGui::Separator();
+ ImGui::Text("Main inventory");
+ for (int i = 9; i < 17 + 1; i++) {
+ if (renderSlot(inventory.slots[i], i)) {
+ inventory.MakeClick(i, true);
}
- //Crafting result
- ImGui::SameLine();
- ImGui::Text("Result");
ImGui::SameLine();
- if (renderSlot(inventory.slots[0], 0)) {
- inventory.MakeClick(0, true);
- }
- //Crafting second line
- if (renderSlot(inventory.slots[3], 3)) {
- inventory.MakeClick(3, true);
+ }
+ ImGui::Text("");
+ for (int i = 18; i < 26 + 1; i++) {
+ if (renderSlot(inventory.slots[i], i)) {
+ inventory.MakeClick(i, true);
}
ImGui::SameLine();
- if (renderSlot(inventory.slots[4], 4)) {
- inventory.MakeClick(4, true);
- }
- ImGui::Separator();
- //Armor and offhand
- for (int i = 5; i < 8+1; i++) {
- if (renderSlot(inventory.slots[i], i)) {
- inventory.MakeClick(i, true);
- }
- ImGui::SameLine();
- }
- if (renderSlot(inventory.slots[45], 45)) {
- inventory.MakeClick(45, true);
+ }
+ ImGui::Text("");
+ for (int i = 27; i < 35 + 1; i++) {
+ if (renderSlot(inventory.slots[i], i)) {
+ inventory.MakeClick(i, true);
}
ImGui::SameLine();
- ImGui::Text("Armor and offhand");
- ImGui::Separator();
- for (int i = 36; i < 44+1; i++) {
- if (renderSlot(inventory.slots[i], i)) {
- inventory.MakeClick(i, true);
- }
- ImGui::SameLine();
- }
- ImGui::Text("Hotbar");
- ImGui::Separator();
- ImGui::Text("Main inventory");
- for (int i = 9; i < 17 + 1; i++) {
- if (renderSlot(inventory.slots[i], i)) {
- inventory.MakeClick(i, true);
- }
- ImGui::SameLine();
- }
- ImGui::Text("");
- for (int i = 18; i < 26 + 1; i++) {
- if (renderSlot(inventory.slots[i], i)) {
- inventory.MakeClick(i, true);
- }
- ImGui::SameLine();
- }
- ImGui::Text("");
- for (int i = 27; i < 35 + 1; i++) {
- if (renderSlot(inventory.slots[i], i)) {
- inventory.MakeClick(i, true);
- }
- ImGui::SameLine();
- }
- ImGui::End();
}
+ ImGui::End();
+
break;
+ }
case GameState::Paused: {
ImGui::SetNextWindowPosCenter();
ImGui::Begin("Pause Menu", 0, windowFlags);
@@ -453,10 +497,10 @@ void Render::RenderGui() {
sensetivity = sense;
isWireframe = wireframe;
- timer.SetDelayLength(std::chrono::duration<double,std::milli>(1.0/targetFps * 1000.0));
+ timer.SetDelayLength(std::chrono::duration<double, std::milli>(1.0 / targetFps * 1000.0));
}
ImGui::Separator();
-
+
if (ImGui::Button("Disconnect")) {
EventAgregator::PushEvent(EventType::Disconnect, DisconnectData{ "Disconnected by user" });
}
diff --git a/src/Render.hpp b/src/Render.hpp
index f9d3497..f35d90c 100644
--- a/src/Render.hpp
+++ b/src/Render.hpp
@@ -22,7 +22,7 @@ class Render {
bool HasFocus=true;
float sensetivity = 0.1f;
bool isWireframe = false;
- bool isDisplayInventory = false;
+ std::vector<std::string> chatMessages;
enum GameState {
InitialLoading,
@@ -30,6 +30,8 @@ class Render {
Loading,
Playing,
Paused,
+ Inventory,
+ Chat,
} state = InitialLoading;
std::string stateString;
diff --git a/src/Stream.cpp b/src/Stream.cpp
index c7935e6..6237451 100644
--- a/src/Stream.cpp
+++ b/src/Stream.cpp
@@ -74,8 +74,8 @@ std::string StreamInput::ReadString() {
return str;
}
-std::string StreamInput::ReadChat() {
- std::string str, jsonStr = ReadString();
+Chat StreamInput::ReadChat() {
+ /*std::string str, jsonStr = ReadString();
nlohmann::json json;
try {
json = nlohmann::json::parse(jsonStr);
@@ -89,7 +89,8 @@ std::string StreamInput::ReadChat() {
return "kicked by operator";
for (auto &it:json["extra"]) {
str += it["text"].get<std::string>();
- }
+ }*/
+ Chat str(ReadString());
return str;
}
@@ -226,13 +227,13 @@ void StreamOutput::WriteDouble(double value) {
WriteData((unsigned char *) &value, 8);
}
-void StreamOutput::WriteString(std::string value) {
+void StreamOutput::WriteString(const std::string &value) {
WriteVarInt(value.size());
WriteData((unsigned char *) value.data(), value.size());
}
-void StreamOutput::WriteChat(std::string value) {
- WriteString(value);
+void StreamOutput::WriteChat(const Chat &value) {
+ WriteString(value.ToJson());
}
void StreamOutput::WriteVarInt(int value) {
@@ -265,29 +266,24 @@ void StreamOutput::WriteVarLong(long long value) {
WriteData(buff, len);
}
-void StreamOutput::WriteEntityMetadata(std::vector<unsigned char> value) {
+void StreamOutput::WriteEntityMetadata(const std::vector<unsigned char> &value) {
LOG(FATAL) << "Used unimplemented WriteEntityMetadata: " << value.size();
}
-void StreamOutput::WriteSlot(SlotData value) {
+void StreamOutput::WriteSlot(const SlotData &value) {
WriteShort(value.BlockId);
if (value.BlockId == -1)
return;
WriteByte(value.ItemCount);
WriteShort(value.ItemDamage);
- /*unsigned char nbt[] = {
- //0x0a, 0x00, 0x02, 0x68, 0x69, 0x00,
- 0x01, 0x04, 0xCA, 0xFE, 0xBA, 0xBE,
- };
- WriteByteArray(std::vector<unsigned char>(nbt,nbt + sizeof(nbt)));*/
WriteByte(0);
}
-void StreamOutput::WriteNbtTag(std::vector<unsigned char> value) {
+void StreamOutput::WriteNbtTag(const std::vector<unsigned char> &value) {
LOG(FATAL) << "Used unimplemented WriteNbtTag " << value.size();
}
-void StreamOutput::WritePosition(Vector value) {
+void StreamOutput::WritePosition(const Vector &value) {
LOG(FATAL) << "Used unimplemented Position: " << value.x << ", " << value.y << " " << value.z;
}
@@ -295,12 +291,13 @@ void StreamOutput::WriteAngle(unsigned char value) {
WriteUByte(value);
}
-void StreamOutput::WriteUuid(Uuid value) {
+void StreamOutput::WriteUuid(const Uuid &value) {
WriteByteArray(value);
}
-void StreamOutput::WriteByteArray(std::vector<unsigned char> value) {
- WriteData(value.data(), value.size());
+void StreamOutput::WriteByteArray(const std::vector<unsigned char> &value) {
+ auto& val = const_cast<std::vector<unsigned char>&>(value);
+ WriteData(val.data(), val.size());
}
void StreamBuffer::ReadData(unsigned char *buffPtr, size_t buffLen) {
diff --git a/src/Stream.hpp b/src/Stream.hpp
index e2ba5cf..3fb92df 100644
--- a/src/Stream.hpp
+++ b/src/Stream.hpp
@@ -12,6 +12,7 @@
#include "Socket.hpp"
#include "Vector.hpp"
#include "Utility.hpp"
+#include "Chat.hpp"
struct SlotData {
short BlockId = -1;
@@ -39,7 +40,7 @@ public:
float ReadFloat();
double ReadDouble();
std::string ReadString();
- std::string ReadChat();
+ Chat ReadChat();
int ReadVarInt();
long long ReadVarLong();
std::vector<unsigned char> ReadEntityMetadata();
@@ -64,17 +65,17 @@ public:
void WriteLong(long long value);
void WriteFloat(float value);
void WriteDouble(double value);
- void WriteString(std::string value);
- void WriteChat(std::string value);
+ void WriteString(const std::string &value);
+ void WriteChat(const Chat &value);
void WriteVarInt(int value);
void WriteVarLong(long long value);
- void WriteEntityMetadata(std::vector<unsigned char> value);
- void WriteSlot(SlotData value);
- void WriteNbtTag(std::vector<unsigned char> value);
- void WritePosition(Vector value);
+ void WriteEntityMetadata(const std::vector<unsigned char> &value);
+ void WriteSlot(const SlotData &value);
+ void WriteNbtTag(const std::vector<unsigned char> &value);
+ void WritePosition(const Vector &value);
void WriteAngle(unsigned char value);
- void WriteUuid(Uuid value);
- void WriteByteArray(std::vector<unsigned char> value);
+ void WriteUuid(const Uuid &value);
+ void WriteByteArray(const std::vector<unsigned char> &value);
};
class StreamBuffer : public StreamInput, public StreamOutput {
diff --git a/src/Utility.hpp b/src/Utility.hpp
index 8333052..893f38e 100644
--- a/src/Utility.hpp
+++ b/src/Utility.hpp
@@ -7,7 +7,7 @@
#include <easylogging++.h>
#include <GL/glew.h>
-using Uuid = std::vector<uint8_t>;
+using Uuid = std::vector<unsigned char>;
template<class T>
void endswap(T *objp) {