From dac6c6341440609277fad663ea471e44a334ed47 Mon Sep 17 00:00:00 2001 From: Lucy <44952533+Skyliegirl33@users.noreply.github.com> Date: Wed, 8 Feb 2023 21:44:51 +0100 Subject: [PATCH] Fix dyeing and implement removeItem method --- src/common/Network/CommonActorControl.h | 1 + src/world/Actor/Player.cpp | 12 +++-- src/world/Actor/Player.h | 2 + src/world/Actor/PlayerInventory.cpp | 48 +++++++++++++++++++ .../Network/Handlers/PacketCommandHandler.cpp | 6 ++- 5 files changed, 64 insertions(+), 5 deletions(-) diff --git a/src/common/Network/CommonActorControl.h b/src/common/Network/CommonActorControl.h index 2c971c47..e6e36a3f 100644 --- a/src/common/Network/CommonActorControl.h +++ b/src/common/Network/CommonActorControl.h @@ -548,6 +548,7 @@ namespace Sapphire::Network::ActorControl REQUEST_SALVAGE_SUCCESS_RATE = 0x1B2, MOBHUNT_RECEIPT_ORDER = 0x1B3, MOBHUNT_BREAK_ORDER = 0x1B4, + DYE_ITEM = 0x1B5, EMOTE = 0x1F4, EMOTE_WITH_WARP = 0x1F5, EMOTE_CANCEL = 0x1F6, diff --git a/src/world/Actor/Player.cpp b/src/world/Actor/Player.cpp index b29d3cfd..1ff17bd0 100644 --- a/src/world/Actor/Player.cpp +++ b/src/world/Actor/Player.cpp @@ -1624,13 +1624,19 @@ void Player::dyeItemFromDyeingInfo() if( !itemToDye || !dyeToUse ) return; - uint32_t stainColorID = dyeToUse->getAdditionalData(); - itemToDye->setStain( stainColorID ); + if( !removeItem( dyeToUse->getId() ) ) + return; - // TODO: subtract/remove dye used + uint32_t stainColorID = dyeToUse->getAdditionalData(); + bool shouldDye = stainColorID != 0; + bool invalidateGearSet = stainColorID != itemToDye->getStain(); + itemToDye->setStain( stainColorID ); insertInventoryItem( static_cast< Sapphire::Common::InventoryType >( itemToDyeContainer ), static_cast< uint16_t >( itemToDyeSlot ), itemToDye ); writeItem( itemToDye ); + + auto dyePkt = makeActorControlSelf( getId(), DyeMsg, itemToDye->getId(), shouldDye, invalidateGearSet ); + queuePacket( dyePkt ); } void Player::resetObjSpawnIndex() diff --git a/src/world/Actor/Player.h b/src/world/Actor/Player.h index b5261d1a..2d45ba86 100644 --- a/src/world/Actor/Player.h +++ b/src/world/Actor/Player.h @@ -722,6 +722,8 @@ namespace Sapphire::Entity ItemPtr addItem( uint32_t catalogId, uint32_t quantity = 1, bool isHq = false, bool slient = false, bool canMerge = true ); + bool removeItem( uint32_t catalogId, uint32_t quantity = 1, bool isHq = false ); + void moveItem( uint16_t fromInventoryId, uint16_t fromSlotId, uint16_t toInventoryId, uint16_t toSlot ); void swapItem( uint16_t fromInventoryId, uint16_t fromSlotId, uint16_t toInventoryId, uint16_t toSlot ); diff --git a/src/world/Actor/PlayerInventory.cpp b/src/world/Actor/PlayerInventory.cpp index 1d5118c1..acff88c5 100644 --- a/src/world/Actor/PlayerInventory.cpp +++ b/src/world/Actor/PlayerInventory.cpp @@ -720,6 +720,54 @@ Sapphire::ItemPtr Sapphire::Entity::Player::addItem( uint32_t catalogId, uint32_ return item; } +bool Sapphire::Entity::Player::removeItem( uint32_t catalogId, uint32_t quantity, bool isHq ) +{ + std::vector< uint16_t > bags = { Bag0, Bag1, Bag2, Bag3 }; + + for( auto bag : bags ) + { + auto storage = m_storageMap[ bag ]; + + for( uint16_t slot = 0; slot < storage->getMaxSize(); slot++ ) + { + if( quantity == 0 ) + break; + + auto item = storage->getItem( slot ); + + // remove any matching items + if( item && item->getId() == catalogId ) + { + uint32_t count = item->getStackSize(); + uint32_t maxStack = item->getMaxStackSize(); + + // check slot is same quality + if( item->isHq() != isHq ) + continue; + + // update stack + int32_t newStackSize = count - quantity; + if( newStackSize <= 0 ) + { + quantity = std::abs( newStackSize ); + discardItem( bag, slot ); + } + else + { + quantity = 0; + item->setStackSize( newStackSize ); + + insertInventoryItem( static_cast< Sapphire::Common::InventoryType >( bag ), slot, item ); + + writeItem( item ); + } + } + } + } + + return quantity == 0; +} + void Sapphire::Entity::Player::moveItem( uint16_t fromInventoryId, uint16_t fromSlotId, uint16_t toInventoryId, uint16_t toSlot ) { diff --git a/src/world/Network/Handlers/PacketCommandHandler.cpp b/src/world/Network/Handlers/PacketCommandHandler.cpp index e72a5d4c..902bbb4a 100644 --- a/src/world/Network/Handlers/PacketCommandHandler.cpp +++ b/src/world/Network/Handlers/PacketCommandHandler.cpp @@ -182,6 +182,8 @@ const char* packetCommandToString( uint16_t commandId ) return "MOBHUNT_RECEIPT_ORDER"; case MOBHUNT_BREAK_ORDER: return "MOBHUNT_BREAK_ORDER"; + case DYE_ITEM: + return "DYE_ITEM"; case EMOTE: return "EMOTE"; case EMOTE_WITH_WARP: @@ -623,7 +625,7 @@ void Sapphire::Network::GameConnection::commandHandler( const Packets::FFXIVARR_ player.teleportQuery( static_cast< uint16_t >( param11 ) ); break; } - /* case PacketCommand::DyeItem: // Dye item + case PacketCommand::DYE_ITEM: // Dye item { // param11 = item to dye container // param12 = item to dye slot @@ -631,7 +633,7 @@ void Sapphire::Network::GameConnection::commandHandler( const Packets::FFXIVARR_ // param4 = dye bag slot player.setDyeingInfo( param11, param12, param2, param4 ); break; - }*/ + } case PacketCommand::DIRECTOR_INIT_RETURN: // Director init finish { pZone->onInitDirector( player );