Skip to content

Commit

Permalink
Some updates for enchanting (2)
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel0916 committed Jan 31, 2014
1 parent 5609fed commit 36120db
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 15 deletions.
36 changes: 36 additions & 0 deletions src/ClientHandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
}






3 changes: 3 additions & 0 deletions src/ClientHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ class cClientHandle : // tolua_export

/// 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:

Expand Down
5 changes: 4 additions & 1 deletion src/Enchantments.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<cEnchantments> cEnchantmentsArray;

// tolua_begin
class cEnchantments
{
Expand Down Expand Up @@ -62,7 +65,7 @@ class cEnchantments
enchLuckOfTheSea = 61,
enchLure = 62,
} ;

/// Creates an empty enchantments container
cEnchantments(void);

Expand Down
106 changes: 106 additions & 0 deletions src/Item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
3 changes: 3 additions & 0 deletions src/Item.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ class cItem
/// 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;
Expand Down
15 changes: 15 additions & 0 deletions src/Protocol/Protocol125.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/Protocol/Protocol125.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ class cProtocol125 :
virtual int ParseSlotSelected (void);
virtual int ParseUpdateSign (void);
virtual int ParseUseEntity (void);
virtual int ParseEnchantItem (void);
virtual int ParseWindowClick (void);
virtual int ParseWindowClose (void);

Expand Down
17 changes: 4 additions & 13 deletions src/Protocol/Protocol17x.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

}

Expand Down
2 changes: 1 addition & 1 deletion src/Protocol/Protocol17x.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ class cProtocol172 :
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);

Expand Down

0 comments on commit 36120db

Please sign in to comment.