Skip to content

Commit

Permalink
fix: prevent sell items with duration to the npc (opentibiabr#2898)
Browse files Browse the repository at this point in the history
This fix prevents players from selling items with used duration in NPC trades.
  • Loading branch information
carlospess0a authored Oct 14, 2024
1 parent a60f27f commit 90eff79
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion data-otservbr-global/npc/perod.lua
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ npcConfig.shop = {
{ itemName = "shiver arrow", clientId = 762, buy = 5 },
{ itemName = "shovel", clientId = 3457, buy = 50, sell = 8 },
{ itemName = "sniper arrow", clientId = 7364, buy = 5 },
{ itemName = "spear", clientId = 3277, buy = 10, sell = 10 },
{ itemName = "spear", clientId = 3277, buy = 9, sell = 3 },
{ itemName = "spectral bolt", clientId = 35902, buy = 70 },
{ itemName = "tarsal arrow", clientId = 14251, buy = 6 },
{ itemName = "throwing star", clientId = 3287, buy = 42 },
Expand Down
8 changes: 8 additions & 0 deletions src/creatures/npcs/npc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,10 @@ void Npc::onPlayerSellItem(std::shared_ptr<Player> player, uint16_t itemId, uint
continue;
}

if (!item->hasMarketAttributes()) {
continue;
}

auto removeCount = std::min<uint16_t>(toRemove, item->getItemCount());

if (g_game().internalRemoveItem(item, removeCount) != RETURNVALUE_NOERROR) {
Expand All @@ -410,6 +414,10 @@ void Npc::onPlayerSellItem(std::shared_ptr<Player> player, uint16_t itemId, uint
}

auto totalRemoved = amount - toRemove;
if (totalRemoved == 0) {
return;
}

auto totalCost = static_cast<uint64_t>(sellPrice * totalRemoved);
g_logger().debug("[Npc::onPlayerSellItem] - Removing items from player {} amount {} of items with id {} on shop for npc {}", player->getName(), toRemove, itemId, getName());
if (totalRemoved > 0 && totalCost > 0) {
Expand Down
4 changes: 4 additions & 0 deletions src/creatures/players/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4118,6 +4118,10 @@ std::map<uint32_t, uint32_t> &Player::getAllItemTypeCount(std::map<uint32_t, uin

std::map<uint16_t, uint16_t> &Player::getAllSaleItemIdAndCount(std::map<uint16_t, uint16_t> &countMap) const {
for (const auto &item : getAllInventoryItems(false, true)) {
if (!item->hasMarketAttributes()) {
continue;
}

if (const auto &container = item->getContainer()) {
if (container->size() > 0) {
continue;
Expand Down

0 comments on commit 90eff79

Please sign in to comment.