From c171b272d96453f1a9d13d4623ac4bd3aa7d19e8 Mon Sep 17 00:00:00 2001 From: Lukas Pioch Date: Fri, 28 Jul 2017 12:11:07 +0200 Subject: Started on adding books --- src/Protocol/Protocol.h | 1 + src/Protocol/ProtocolRecognizer.cpp | 11 ++++ src/Protocol/ProtocolRecognizer.h | 1 + src/Protocol/Protocol_1_8.h | 1 + src/Protocol/Protocol_1_9.cpp | 101 +++++++++++++++++++++++++++++++++++- src/Protocol/Protocol_1_9.h | 1 + 6 files changed, 115 insertions(+), 1 deletion(-) (limited to 'src/Protocol') diff --git a/src/Protocol/Protocol.h b/src/Protocol/Protocol.h index 54c5b7223..9d9497163 100644 --- a/src/Protocol/Protocol.h +++ b/src/Protocol/Protocol.h @@ -65,6 +65,7 @@ public: virtual void SendBlockBreakAnim (UInt32 a_EntityID, int a_BlockX, int a_BlockY, int a_BlockZ, char a_Stage) = 0; virtual void SendBlockChange (int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) = 0; virtual void SendBlockChanges (int a_ChunkX, int a_ChunkZ, const sSetBlockVector & a_Changes) = 0; + virtual void SendOpenBook (const short a_Hand) = 0; virtual void SendCameraSetTo (const cEntity & a_Entity) = 0; virtual void SendChat (const AString & a_Message, eChatType a_Type) = 0; virtual void SendChat (const cCompositeChat & a_Message, eChatType a_Type, bool a_ShouldUseChatPrefixes) = 0; diff --git a/src/Protocol/ProtocolRecognizer.cpp b/src/Protocol/ProtocolRecognizer.cpp index cb2a4fc9d..06e942716 100644 --- a/src/Protocol/ProtocolRecognizer.cpp +++ b/src/Protocol/ProtocolRecognizer.cpp @@ -191,6 +191,17 @@ void cProtocolRecognizer::SendBlockChanges(int a_ChunkX, int a_ChunkZ, const sSe + +void cProtocolRecognizer::SendOpenBook(const short a_Hand) +{ + ASSERT(m_Protocol != nullptr); + m_Protocol->SendOpenBook(a_Hand); +} + + + + + void cProtocolRecognizer::SendCameraSetTo(const cEntity & a_Entity) { ASSERT(m_Protocol != nullptr); diff --git a/src/Protocol/ProtocolRecognizer.h b/src/Protocol/ProtocolRecognizer.h index 295c6db16..544914cf3 100644 --- a/src/Protocol/ProtocolRecognizer.h +++ b/src/Protocol/ProtocolRecognizer.h @@ -61,6 +61,7 @@ public: virtual void SendBlockBreakAnim (UInt32 a_EntityID, int a_BlockX, int a_BlockY, int a_BlockZ, char a_Stage) override; virtual void SendBlockChange (int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) override; virtual void SendBlockChanges (int a_ChunkX, int a_ChunkZ, const sSetBlockVector & a_Changes) override; + virtual void SendOpenBook (const short a_Hand) override; virtual void SendCameraSetTo (const cEntity & a_Entity) override; virtual void SendChat (const AString & a_Message, eChatType a_Type) override; virtual void SendChat (const cCompositeChat & a_Message, eChatType a_Type, bool a_ShouldUseChatPrefixes) override; diff --git a/src/Protocol/Protocol_1_8.h b/src/Protocol/Protocol_1_8.h index b04e5c5f0..a76dbd8d8 100644 --- a/src/Protocol/Protocol_1_8.h +++ b/src/Protocol/Protocol_1_8.h @@ -54,6 +54,7 @@ public: virtual void SendBlockBreakAnim (UInt32 a_EntityID, int a_BlockX, int a_BlockY, int a_BlockZ, char a_Stage) override; virtual void SendBlockChange (int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) override; virtual void SendBlockChanges (int a_ChunkX, int a_ChunkZ, const sSetBlockVector & a_Changes) override; + virtual void SendOpenBook (const short a_Hand) override {} virtual void SendCameraSetTo (const cEntity & a_Entity) override; virtual void SendChat (const AString & a_Message, eChatType a_Type) override; virtual void SendChat (const cCompositeChat & a_Message, eChatType a_Type, bool a_ShouldUseChatPrefixes) override; diff --git a/src/Protocol/Protocol_1_9.cpp b/src/Protocol/Protocol_1_9.cpp index 7fc6cf5f1..f49a942b8 100644 --- a/src/Protocol/Protocol_1_9.cpp +++ b/src/Protocol/Protocol_1_9.cpp @@ -263,6 +263,17 @@ void cProtocol_1_9_0::SendBlockChanges(int a_ChunkX, int a_ChunkZ, const sSetBlo +void cProtocol_1_9_0::SendOpenBook(const short a_Hand) +{ + cPacketizer Pkt(*this, 0x18); // Plugin Channel + Pkt.WriteString("MC|BOpen"); + Pkt.WriteVarInt32(static_cast(a_Hand)); +} + + + + + void cProtocol_1_9_0::SendCameraSetTo(const cEntity & a_Entity) { cPacketizer Pkt(*this, 0x36); // Camera Packet (Attach the camera of a player at another entity in spectator mode) @@ -2917,6 +2928,55 @@ void cProtocol_1_9_0::HandleVanillaPluginMessage(cByteBuffer & a_ByteBuffer, con m_Client->HandleNPCTrade(SlotNum); return; } + else if ((a_Channel == "MC|BSign") || (a_Channel == "MC|BEdit")) + { + HANDLE_READ(a_ByteBuffer, ReadBEInt16, Int16, ItemID); + if (ItemID != E_ITEM_BOOK_AND_QUILL) + { + // Item is not a writeable book + return; + } + + // Skip item count (1 byte) and item damage (2 bytes) + a_ByteBuffer.SkipRead(3); + + // Read nbt content + AString BookData; + a_ByteBuffer.ReadString(BookData, a_ByteBuffer.GetReadableSpace() - 1); + cParsedNBT NBT(BookData.c_str(), BookData.size()); + + cItem BookItem; + if (a_Channel == "MC|BSign") + { + BookItem = cItem(E_ITEM_WRITTEN_BOOK); + // Add the text to a json string + cBookContent::ParseFromNBT(0, BookItem.m_BookContent, NBT, true); + } + else + { + BookItem = cItem(E_ITEM_BOOK_AND_QUILL); + cBookContent::ParseFromNBT(0, BookItem.m_BookContent, NBT); + } + + cPlayer * Player = m_Client->GetPlayer(); + + // If true, player has clicked on the sign button + bool IsSigned = (BookItem.m_ItemType == E_ITEM_WRITTEN_BOOK) ? true : false; + + if (cRoot::Get()->GetPluginManager()->CallHookPlayerEditingBook(*Player, BookItem.m_BookContent, IsSigned)) + { + // Plugin denied the editing of the book + return; + } + + // Book has been edited, inform plugins + cRoot::Get()->GetPluginManager()->CallHookPlayerEditedBook(*Player, BookItem.m_BookContent, IsSigned); + + cInventory & inv = Player->GetInventory(); + inv.SetHotbarSlot(inv.GetEquippedSlotNum(), BookItem); + SendWholeInventory(*Player->GetWindow()); // TODO: Use SendSlot + return; + } LOG("Unhandled vanilla plugin channel: \"%s\".", a_Channel.c_str()); // Read the payload and send it through to the clienthandle: @@ -3334,12 +3394,26 @@ void cProtocol_1_9_0::WriteItem(cPacketizer & a_Pkt, const cItem & a_Item) a_Pkt.WriteBEInt16(a_Item.m_ItemDamage); } - if (a_Item.m_Enchantments.IsEmpty() && a_Item.IsBothNameAndLoreEmpty() && (ItemType != E_ITEM_FIREWORK_ROCKET) && (ItemType != E_ITEM_FIREWORK_STAR) && !a_Item.m_ItemColor.IsValid() && (ItemType != E_ITEM_POTION) && (ItemType != E_ITEM_SPAWN_EGG)) + if ( + a_Item.m_Enchantments.IsEmpty() && + a_Item.IsBothNameAndLoreEmpty() && + (ItemType != E_ITEM_FIREWORK_ROCKET) && (ItemType != E_ITEM_FIREWORK_STAR) && + !a_Item.m_ItemColor.IsValid() && + (ItemType != E_ITEM_POTION) && + (ItemType != E_ITEM_SPAWN_EGG) && + (ItemType != E_ITEM_WRITTEN_BOOK) && (ItemType != E_ITEM_BOOK_AND_QUILL)) { a_Pkt.WriteBEInt8(0); return; } + if ((ItemType == E_ITEM_BOOK_AND_QUILL) && (a_Item.m_BookContent.GetPages().size() == 0)) + { + // Don't send any nbt tag if the book is writeable and has no pages + // If a tag with a empty pages list is send, the player can't enter anything + a_Pkt.WriteBEInt8(0); + return; + } // Send the enchantments and custom names: cFastNBTWriter Writer; @@ -3450,6 +3524,31 @@ void cProtocol_1_9_0::WriteItem(cPacketizer & a_Pkt, const cItem & a_Item) Writer.EndCompound(); } } + if ((a_Item.m_ItemType == E_ITEM_WRITTEN_BOOK) || (a_Item.m_ItemType == E_ITEM_BOOK_AND_QUILL)) + { + if (a_Item.m_ItemType == E_ITEM_WRITTEN_BOOK) + { + // Only send author and title for a signed book + Writer.AddString("author", a_Item.m_BookContent.GetAuthor()); + Writer.AddString("title", a_Item.m_BookContent.GetTitle()); + } + if (a_Item.m_BookContent.GetPages().size() > 0) + { + Writer.BeginList("pages", TAG_String); + for (auto & Page : a_Item.m_BookContent.GetPages()) + { + Writer.AddString("", Page); + } + Writer.EndList(); + } + else + { + // A signed book, has a empty page + Writer.BeginList("pages", TAG_String); + Writer.AddString("", ""); + Writer.EndList(); + } + } Writer.Finish(); diff --git a/src/Protocol/Protocol_1_9.h b/src/Protocol/Protocol_1_9.h index b4fdc7f67..4b941726c 100644 --- a/src/Protocol/Protocol_1_9.h +++ b/src/Protocol/Protocol_1_9.h @@ -60,6 +60,7 @@ public: virtual void SendBlockBreakAnim (UInt32 a_EntityID, int a_BlockX, int a_BlockY, int a_BlockZ, char a_Stage) override; virtual void SendBlockChange (int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) override; virtual void SendBlockChanges (int a_ChunkX, int a_ChunkZ, const sSetBlockVector & a_Changes) override; + virtual void SendOpenBook (const short a_Hand) override; virtual void SendCameraSetTo (const cEntity & a_Entity) override; virtual void SendChat (const AString & a_Message, eChatType a_Type) override; virtual void SendChat (const cCompositeChat & a_Message, eChatType a_Type, bool a_ShouldUseChatPrefixes) override; -- cgit v1.2.3