From 36120db400691be75199ff404e35f5c289b8818f Mon Sep 17 00:00:00 2001 From: daniel0916 Date: Fri, 31 Jan 2014 19:46:51 +0100 Subject: [PATCH] Some updates for enchanting (2) --- src/ClientHandle.cpp | 36 ++++++++++++ src/ClientHandle.h | 3 + src/Enchantments.h | 5 +- src/Item.cpp | 106 +++++++++++++++++++++++++++++++++++ src/Item.h | 3 + src/Protocol/Protocol125.cpp | 15 +++++ src/Protocol/Protocol125.h | 1 + src/Protocol/Protocol17x.cpp | 17 ++---- src/Protocol/Protocol17x.h | 2 +- 9 files changed, 173 insertions(+), 15 deletions(-) diff --git a/src/ClientHandle.cpp b/src/ClientHandle.cpp index 74d192129..979298c59 100644 --- a/src/ClientHandle.cpp +++ b/src/ClientHandle.cpp @@ -2453,4 +2453,40 @@ void cClientHandle::SocketClosed(void) +void cClientHandle::HandleEnchantItem(Byte & WindowID, Byte & Enchantment) +{ + //Get Item + cItem EnchantItem = m_Player->GetDraggingItem(); + + cEnchantmentsArray enchantments; + cItem::GetApplicableEnchantmentsForType(EnchantItem.m_ItemType, enchantments); + + m_Player->SendMessage(Printf("ItemType: %d", EnchantItem.m_ItemType)); + + + m_Player->SendMessage(enchantments[1].ToString()); + + //shuffle enchantments (i don't know if this is good) + std::random_shuffle(enchantments.begin(), enchantments.end()); + + m_Player->SendMessage(enchantments[1].ToString()); + + + + + + + //Enchant Item + EnchantItem.m_Enchantments.AddFromString(enchantments[1].ToString()); + + //Set Enchanted Item to Window Slot + m_Player->GetWindow()->SetSlot(*m_Player, 0, EnchantItem); + + LOGWARN("Item enchanted!"); +} + + + + + diff --git a/src/ClientHandle.h b/src/ClientHandle.h index 4add022a6..1a1e22a19 100644 --- a/src/ClientHandle.h +++ b/src/ClientHandle.h @@ -214,6 +214,9 @@ public: /// Handles the block placing packet when it is a real block placement (not block-using, item-using or eating) void HandlePlaceBlock(int a_BlockX, int a_BlockY, int a_BlockZ, char a_BlockFace, int a_CursorX, int a_CursorY, int a_CursorZ, cItemHandler & a_ItemHandler); + + ///Handle item enchanting + void HandleEnchantItem(Byte & WindowID, Byte & Enchantment); private: diff --git a/src/Enchantments.h b/src/Enchantments.h index e984df92e..e0861aa1e 100644 --- a/src/Enchantments.h +++ b/src/Enchantments.h @@ -29,6 +29,9 @@ mapping each enchantment's id onto its level. ID may be either a number or the e Level value of 0 means no such enchantment, and it will not be stored in the m_Enchantments. Serialization will never put zero-level enchantments into the stringspec and will always use numeric IDs. */ + +typedef std::vector cEnchantmentsArray; + // tolua_begin class cEnchantments { @@ -62,7 +65,7 @@ public: enchLuckOfTheSea = 61, enchLure = 62, } ; - + /// Creates an empty enchantments container cEnchantments(void); diff --git a/src/Item.cpp b/src/Item.cpp index 9170006b6..fb561d7b2 100644 --- a/src/Item.cpp +++ b/src/Item.cpp @@ -189,6 +189,112 @@ bool cItem::IsEnchantable(short item) +void cItem::GetApplicableEnchantmentsForType(short a_ItemType, cEnchantmentsArray & a_Enchantments) +{ + LOGWARN("Get Enchantments"); + + if (ItemCategory::IsSword(a_ItemType)) + { + a_Enchantments.push_back(cEnchantments("Sharpness=4")); + a_Enchantments.push_back(cEnchantments("Smite=5")); + a_Enchantments.push_back(cEnchantments("BaneOfArthropods=5")); + a_Enchantments.push_back(cEnchantments("Knockback=2")); + a_Enchantments.push_back(cEnchantments("FireAspect=2")); + a_Enchantments.push_back(cEnchantments("Looting=3")); + a_Enchantments.push_back(cEnchantments("Unbreaking=3")); + } + + else if (ItemCategory::IsPickaxe(a_ItemType)) + { + a_Enchantments.push_back(cEnchantments("Efficiency=4")); + a_Enchantments.push_back(cEnchantments("SilkTouch=1")); + a_Enchantments.push_back(cEnchantments("Unbreaking=3")); + a_Enchantments.push_back(cEnchantments("Fortune=3")); + } + + else if (ItemCategory::IsAxe(a_ItemType)) + { + a_Enchantments.push_back(cEnchantments("Efficiency=4")); + a_Enchantments.push_back(cEnchantments("SilkTouch=1")); + a_Enchantments.push_back(cEnchantments("Unbreaking=3")); + a_Enchantments.push_back(cEnchantments("Fortune=3")); + } + + else if (ItemCategory::IsShovel(a_ItemType)) + { + a_Enchantments.push_back(cEnchantments("Efficiency=4")); + a_Enchantments.push_back(cEnchantments("SilkTouch=1")); + a_Enchantments.push_back(cEnchantments("Unbreaking=3")); + a_Enchantments.push_back(cEnchantments("Fortune=3")); + } + + else if (ItemCategory::IsHoe(a_ItemType)) + { + a_Enchantments.push_back(cEnchantments("Unbreaking=3")); + } + + else if (ItemCategory::IsArmor(a_ItemType)) + { + a_Enchantments.push_back(cEnchantments("Protection=4")); + a_Enchantments.push_back(cEnchantments("FireProtection=4")); + a_Enchantments.push_back(cEnchantments("BlastProtection=4")); + a_Enchantments.push_back(cEnchantments("ProjectileProtection=4")); + a_Enchantments.push_back(cEnchantments("Thorns=3")); + a_Enchantments.push_back(cEnchantments("Unbreaking=3")); + + if (ItemCategory::IsHelmet(a_ItemType)) + { + a_Enchantments.push_back(cEnchantments("Respiration=3")); + a_Enchantments.push_back(cEnchantments("AquaAffinity=1")); + } + + else if (ItemCategory::IsBoots(a_ItemType)) + { + a_Enchantments.push_back(cEnchantments("FeatherFalling=4")); + } + } + + //Bow + else if (a_ItemType == 261) + { + a_Enchantments.push_back(cEnchantments("Power=4")); + a_Enchantments.push_back(cEnchantments("Punch=2")); + a_Enchantments.push_back(cEnchantments("Flame=1")); + a_Enchantments.push_back(cEnchantments("Infinity=1")); + a_Enchantments.push_back(cEnchantments("Unbreaking=3")); + } + + //Fishing Rod + else if (a_ItemType == 346) + { + a_Enchantments.push_back(cEnchantments("LuckOfTheSea=3")); + a_Enchantments.push_back(cEnchantments("Lure=3")); + a_Enchantments.push_back(cEnchantments("Unbreaking=3")); + } + + //Shears + else if (a_ItemType == 359) + { + a_Enchantments.push_back(cEnchantments("Unbreaking=3")); + } + + //Flint and Steel + else if (a_ItemType == 259) + { + a_Enchantments.push_back(cEnchantments("Unbreaking=3")); + } + + //Carrot on a Stick + else if (a_ItemType == 398) + { + a_Enchantments.push_back(cEnchantments("Unbreaking=3")); + } +} + + + + + /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // cItems: diff --git a/src/Item.h b/src/Item.h index 727965112..1a070eafd 100644 --- a/src/Item.h +++ b/src/Item.h @@ -169,6 +169,9 @@ public: /// Returns true if the specified item type is enchantable (as per 1.2.5 protocol requirements) static bool IsEnchantable(short a_ItemType); + /** Fills a_Enchantments with the list of enchantments applicable to the specified item type */ + static void cItem::GetApplicableEnchantmentsForType(short a_ItemType, cEnchantmentsArray & a_Enchantments); + // tolua_begin short m_ItemType; diff --git a/src/Protocol/Protocol125.cpp b/src/Protocol/Protocol125.cpp index 323a13992..b2b61acc9 100644 --- a/src/Protocol/Protocol125.cpp +++ b/src/Protocol/Protocol125.cpp @@ -1535,6 +1535,21 @@ int cProtocol125::ParseUseEntity(void) +int cProtocol125::ParseEnchantItem(void) +{ + HANDLE_PACKET_READ(ReadByte, byte, WindowID); + HANDLE_PACKET_READ(ReadByte, byte, Enchantment); + + // TODO: Enchant Handling for older Protocols + + + return PARSE_OK; +} + + + + + int cProtocol125::ParseWindowClick(void) { HANDLE_PACKET_READ(ReadChar, char, WindowID); diff --git a/src/Protocol/Protocol125.h b/src/Protocol/Protocol125.h index 310f9dd78..e3c8765b1 100644 --- a/src/Protocol/Protocol125.h +++ b/src/Protocol/Protocol125.h @@ -133,6 +133,7 @@ protected: virtual int ParseSlotSelected (void); virtual int ParseUpdateSign (void); virtual int ParseUseEntity (void); + virtual int ParseEnchantItem (void); virtual int ParseWindowClick (void); virtual int ParseWindowClose (void); diff --git a/src/Protocol/Protocol17x.cpp b/src/Protocol/Protocol17x.cpp index 949498734..05b7ebd45 100644 --- a/src/Protocol/Protocol17x.cpp +++ b/src/Protocol/Protocol17x.cpp @@ -1143,7 +1143,7 @@ bool cProtocol172::HandlePacket(cByteBuffer & a_ByteBuffer, UInt32 a_PacketType) case 0x0e: HandlePacketWindowClick (a_ByteBuffer); return true; case 0x0f: // Confirm transaction - not used in MCS case 0x10: HandlePacketCreativeInventoryAction(a_ByteBuffer); return true; - case 0x11: HandlePacketEnchanting (a_ByteBuffer); return true; + case 0x11: HandlePacketEnchantItem (a_ByteBuffer); return true; case 0x12: HandlePacketUpdateSign (a_ByteBuffer); return true; case 0x13: HandlePacketPlayerAbilities (a_ByteBuffer); return true; case 0x14: HandlePacketTabComplete (a_ByteBuffer); return true; @@ -1546,23 +1546,14 @@ void cProtocol172::HandlePacketUseEntity(cByteBuffer & a_ByteBuffer) -void cProtocol172::HandlePacketEnchanting(cByteBuffer & a_ByteBuffer) +void cProtocol172::HandlePacketEnchantItem(cByteBuffer & a_ByteBuffer) { HANDLE_READ(a_ByteBuffer, ReadByte, Byte, WindowID); HANDLE_READ(a_ByteBuffer, ReadByte, Byte, Enchantment); - LOGWARN("Enchantment Packet received!"); + LOGWARN("Protocol 1.7: Enchantment Packet received!"); - //Get Item from Window Slot - cItem EnchantItem = *m_Client->GetPlayer()->GetWindow()->GetSlot(*m_Client->GetPlayer(), 0); - - //Enchant item with Sharpness 5 - EnchantItem.m_Enchantments.SetLevel(cEnchantments::enchSharpness, 5); - - //Set Enchanted Item to Window Slot - m_Client->GetPlayer()->GetWindow()->SetSlot(*m_Client->GetPlayer(), 0, EnchantItem); - - LOGWARN("Item enchanted!"); + m_Client->HandleEnchantItem(WindowID, Enchantment); } diff --git a/src/Protocol/Protocol17x.h b/src/Protocol/Protocol17x.h index 7877e783a..1f3039edb 100644 --- a/src/Protocol/Protocol17x.h +++ b/src/Protocol/Protocol17x.h @@ -258,7 +258,7 @@ protected: void HandlePacketTabComplete (cByteBuffer & a_ByteBuffer); void HandlePacketUpdateSign (cByteBuffer & a_ByteBuffer); void HandlePacketUseEntity (cByteBuffer & a_ByteBuffer); - void HandlePacketEnchanting (cByteBuffer & a_ByteBuffer); + void HandlePacketEnchantItem (cByteBuffer & a_ByteBuffer); void HandlePacketWindowClick (cByteBuffer & a_ByteBuffer); void HandlePacketWindowClose (cByteBuffer & a_ByteBuffer);