sanity check on opening packages (#923)

This commit is contained in:
Aaron Kimbrell 2022-12-24 16:41:13 -06:00 committed by GitHub
parent 1470af99c3
commit 5cc7d47074
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 38 deletions

View File

@ -5763,11 +5763,7 @@ void GameMessages::HandleUseNonEquipmentItem(RakNet::BitStream* inStream, Entity
auto* item = inv->FindItemById(itemConsumed);
if (item == nullptr) {
return;
}
item->UseNonEquip();
if (item) item->UseNonEquip(item);
}
void GameMessages::HandleMatchRequest(RakNet::BitStream* inStream, Entity* entity) {

View File

@ -262,7 +262,7 @@ bool Item::Consume() {
return success;
}
void Item::UseNonEquip() {
void Item::UseNonEquip(Item* item) {
LOT thisLot = this->GetLot();
if (!GetInventory()) {
Game::logger->LogDebug("Item", "item %i has no inventory??", this->GetLot());
@ -292,45 +292,49 @@ void Item::UseNonEquip() {
}
// This precondition response is taken care of in SpawnPet().
} else {
auto* compRegistryTable = CDClientManager::Instance()->GetTable<CDComponentsRegistryTable>("ComponentsRegistry");
const auto packageComponentId = compRegistryTable->GetByIDAndType(lot, COMPONENT_TYPE_PACKAGE);
bool success = false;
auto inventory = item->GetInventory();
if (inventory && inventory->GetType() == eInventoryType::ITEMS) {
auto* compRegistryTable = CDClientManager::Instance()->GetTable<CDComponentsRegistryTable>("ComponentsRegistry");
const auto packageComponentId = compRegistryTable->GetByIDAndType(lot, COMPONENT_TYPE_PACKAGE);
if (packageComponentId == 0) return;
if (packageComponentId == 0) return;
auto* packCompTable = CDClientManager::Instance()->GetTable<CDPackageComponentTable>("PackageComponent");
auto packages = packCompTable->Query([=](const CDPackageComponent entry) {return entry.id == static_cast<uint32_t>(packageComponentId); });
auto* packCompTable = CDClientManager::Instance()->GetTable<CDPackageComponentTable>("PackageComponent");
auto packages = packCompTable->Query([=](const CDPackageComponent entry) {return entry.id == static_cast<uint32_t>(packageComponentId); });
auto success = !packages.empty();
if (success) {
if (this->GetPreconditionExpression()->Check(playerInventoryComponent->GetParent())) {
auto* entityParent = playerInventoryComponent->GetParent();
// Roll the loot for all the packages then see if it all fits. If it fits, give it to the player, otherwise don't.
std::unordered_map<LOT, int32_t> rolledLoot{};
for (auto& pack : packages) {
auto thisPackage = LootGenerator::Instance().RollLootMatrix(entityParent, pack.LootMatrixIndex);
for (auto& loot : thisPackage) {
// If we already rolled this lot, add it to the existing one, otherwise create a new entry.
auto existingLoot = rolledLoot.find(loot.first);
if (existingLoot == rolledLoot.end()) {
rolledLoot.insert(loot);
} else {
existingLoot->second += loot.second;
auto success = !packages.empty();
if (success) {
if (this->GetPreconditionExpression()->Check(playerInventoryComponent->GetParent())) {
auto* entityParent = playerInventoryComponent->GetParent();
// Roll the loot for all the packages then see if it all fits. If it fits, give it to the player, otherwise don't.
std::unordered_map<LOT, int32_t> rolledLoot{};
for (auto& pack : packages) {
auto thisPackage = LootGenerator::Instance().RollLootMatrix(entityParent, pack.LootMatrixIndex);
for (auto& loot : thisPackage) {
// If we already rolled this lot, add it to the existing one, otherwise create a new entry.
auto existingLoot = rolledLoot.find(loot.first);
if (existingLoot == rolledLoot.end()) {
rolledLoot.insert(loot);
} else {
existingLoot->second += loot.second;
}
}
}
}
if (playerInventoryComponent->HasSpaceForLoot(rolledLoot)) {
LootGenerator::Instance().GiveLoot(playerInventoryComponent->GetParent(), rolledLoot, eLootSourceType::LOOT_SOURCE_CONSUMPTION);
playerInventoryComponent->RemoveItem(lot, 1);
if (playerInventoryComponent->HasSpaceForLoot(rolledLoot)) {
LootGenerator::Instance().GiveLoot(playerInventoryComponent->GetParent(), rolledLoot, eLootSourceType::LOOT_SOURCE_CONSUMPTION);
item->SetCount(item->GetCount() - 1);
} else {
success = false;
}
} else {
GameMessages::SendUseItemRequirementsResponse(
playerInventoryComponent->GetParent()->GetObjectID(),
playerInventoryComponent->GetParent()->GetSystemAddress(),
UseItemResponse::FailedPrecondition
);
success = false;
}
} else {
GameMessages::SendUseItemRequirementsResponse(
playerInventoryComponent->GetParent()->GetObjectID(),
playerInventoryComponent->GetParent()->GetSystemAddress(),
UseItemResponse::FailedPrecondition
);
success = false;
}
}
Game::logger->LogDebug("Item", "Player %llu %s used item %i", playerEntity->GetObjectID(), success ? "successfully" : "unsuccessfully", thisLot);

View File

@ -195,7 +195,7 @@ public:
/**
* Uses this item if its non equip, essentially an interface for the linked GM
*/
void UseNonEquip();
void UseNonEquip(Item* item);
/**
* Disassembles the part LOTs of this item back into the inventory, if it has any