Skip to content

Commit 530326e

Browse files
committed
tweaks: improve trading
1 parent ec428d7 commit 530326e

File tree

4 files changed

+26
-12
lines changed

4 files changed

+26
-12
lines changed

Code/client/Services/Generic/InventoryService.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,22 @@ void InventoryService::OnEquipmentChangeEvent(const EquipmentChangeEvent& acEven
140140

141141
void InventoryService::OnNotifyInventoryChanges(const NotifyInventoryChanges& acMessage) noexcept
142142
{
143+
TESObjectREFR* pObject = Utils::GetByServerId<TESObjectREFR>(acMessage.ServerId);
144+
if (!pObject)
145+
{
146+
spdlog::error("{}: could not find server id {:X} for inventory change", __FUNCTION__, acMessage.ServerId);
147+
return;
148+
}
149+
150+
Actor* pActor = Cast<Actor>(pObject);
151+
152+
if (acMessage.Silent)
153+
{
154+
ScopedInventoryOverride _;
155+
pObject->AddOrRemoveItem(acMessage.Item);
156+
return;
157+
}
158+
143159
std::optional<uint32_t> dropInstanceId{};
144160
if (acMessage.HasDropInstanceId)
145161
dropInstanceId = acMessage.DropInstanceId;
@@ -161,8 +177,6 @@ void InventoryService::OnNotifyInventoryChanges(const NotifyInventoryChanges& ac
161177
pDropRotation = &dropRotation;
162178
}
163179

164-
Actor* pActor = Utils::GetByServerId<Actor>(acMessage.ServerId);
165-
166180
if (acMessage.Drop)
167181
{
168182
if (!pActor)
@@ -221,14 +235,10 @@ void InventoryService::OnNotifyInventoryChanges(const NotifyInventoryChanges& ac
221235
}
222236
}
223237

224-
TESObjectREFR* pObject = Utils::GetByServerId<TESObjectREFR>(acMessage.ServerId);
225-
if (!pObject)
226-
return;
227-
228238
ScopedInventoryOverride _;
229239

230-
if (auto* pActorObject = Cast<Actor>(pObject))
231-
pActorObject->DropOrPickUpObject(acMessage.Item, pDropLocation, pDropRotation);
240+
if (pActor)
241+
pActor->DropOrPickUpObject(acMessage.Item, pDropLocation, pDropRotation);
232242
else
233243
pObject->AddOrRemoveItem(acMessage.Item);
234244
}

Code/encoding/Messages/NotifyInventoryChanges.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ void NotifyInventoryChanges::SerializeRaw(TiltedPhoques::Buffer::Writer& aWriter
66
Serialization::WriteVarInt(aWriter, ServerId);
77
Item.Serialize(aWriter);
88
Serialization::WriteBool(aWriter, Drop);
9+
Serialization::WriteBool(aWriter, Silent);
910
Serialization::WriteBool(aWriter, HasDropInstanceId);
1011
if (HasDropInstanceId)
1112
Serialization::WriteVarInt(aWriter, DropInstanceId);
@@ -24,6 +25,7 @@ void NotifyInventoryChanges::DeserializeRaw(TiltedPhoques::Buffer::Reader& aRead
2425
ServerId = Serialization::ReadVarInt(aReader) & 0xFFFFFFFF;
2526
Item.Deserialize(aReader);
2627
Drop = Serialization::ReadBool(aReader);
28+
Silent = Serialization::ReadBool(aReader);
2729
HasDropInstanceId = Serialization::ReadBool(aReader);
2830
if (HasDropInstanceId)
2931
DropInstanceId = Serialization::ReadVarInt(aReader) & 0xFFFFFFFF;

Code/encoding/Messages/NotifyInventoryChanges.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@ struct NotifyInventoryChanges final : ServerMessage
1919

2020
bool operator==(const NotifyInventoryChanges& acRhs) const noexcept
2121
{
22-
return GetOpcode() == acRhs.GetOpcode() && ServerId == acRhs.ServerId && Item == acRhs.Item && Drop == acRhs.Drop && HasDropInstanceId == acRhs.HasDropInstanceId &&
22+
return GetOpcode() == acRhs.GetOpcode() && ServerId == acRhs.ServerId && Item == acRhs.Item && Drop == acRhs.Drop && Silent == acRhs.Silent && HasDropInstanceId == acRhs.HasDropInstanceId &&
2323
(!HasDropInstanceId || DropInstanceId == acRhs.DropInstanceId) && HasDropLocation == acRhs.HasDropLocation && (!HasDropLocation || DropLocation == acRhs.DropLocation) && HasDropRotation == acRhs.HasDropRotation &&
2424
(!HasDropRotation || DropRotation == acRhs.DropRotation);
2525
}
2626

2727
uint32_t ServerId{};
2828
Inventory::Entry Item{};
2929
bool Drop = false;
30+
bool Silent = false;
3031
bool HasDropInstanceId = false;
3132
uint32_t DropInstanceId = 0;
3233
bool HasDropLocation = false;

Code/server/Services/TradeService.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -510,10 +510,10 @@ void TradeService::FinalizeTrade(TradeSession& aSession) noexcept
510510
notifyRemoval.ServerId = World::ToInteger(*fromCharacter);
511511
notifyRemoval.Item = removal;
512512
notifyRemoval.Drop = false;
513-
if (!GameServer::Get()->SendToPlayersInRange(notifyRemoval, *fromCharacter))
513+
notifyRemoval.Silent = true;
514+
if (!GameServer::Get()->SendToPlayersInRange(notifyRemoval, *fromCharacter, apFrom))
514515
spdlog::error("[TradeService]: Failed to broadcast inventory removal for {}", apFrom->GetId());
515516
apFrom->Send(notifyRemoval);
516-
517517
Inventory::Entry addition = item;
518518
addition.Count = std::abs(item.Count);
519519
toInventory.AddOrRemoveEntry(addition);
@@ -522,7 +522,8 @@ void TradeService::FinalizeTrade(TradeSession& aSession) noexcept
522522
notifyAddition.ServerId = World::ToInteger(*toCharacter);
523523
notifyAddition.Item = addition;
524524
notifyAddition.Drop = false;
525-
if (!GameServer::Get()->SendToPlayersInRange(notifyAddition, *toCharacter))
525+
notifyAddition.Silent = true;
526+
if (!GameServer::Get()->SendToPlayersInRange(notifyAddition, *toCharacter, apTo))
526527
spdlog::error("[TradeService]: Failed to broadcast inventory addition for {}", apTo->GetId());
527528
apTo->Send(notifyAddition);
528529
}

0 commit comments

Comments
 (0)