mirror of
https://git.neolegacy.dev/neoStudiosLCE/neoLegacy.git
synced 2026-11-21 17:23:56 +01:00
fix crash when exiting servers sometimes
This commit is contained in:
1 parent
aabf6ebd9f
commit
c24978fed8
6 files changed
+207
-26
No files matched your search
@@ -3568,6 +3568,8 @@ void PlayerConnection::handleDebugOptions(shared_ptr<DebugOptionsPacket> packet)
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
|
||||
void PlayerConnection::handleCraftItem(shared_ptr<CraftItemPacket> packet)
|
||||
{
|
||||
if (packet->isBanner) {
|
||||
@@ -3575,23 +3577,23 @@ void PlayerConnection::handleCraftItem(shared_ptr<CraftItemPacket> packet)
|
||||
server->warn(L"Player " + player->getName() + L" tried to craft a 3x3 banner recipe without a crafting bench");
|
||||
return;
|
||||
}
|
||||
|
||||
if (packet->patternColors.size() != packet->patternIndexs.size() || packet->patternIndexs.empty() || packet->patternIndexs.size() > 6) return;
|
||||
|
||||
shared_ptr<ItemInstance> foundBannerItem = nullptr;
|
||||
|
||||
if (packet->patternColors.size() == packet->patternIndexs.size()) return;
|
||||
|
||||
for (int i = 0; i < player->inventory->items.length && foundBannerItem == nullptr; i++) {
|
||||
shared_ptr<ItemInstance>& item = player->inventory->items[i];
|
||||
if (item->id != Tile::standing_banner_Id) continue;
|
||||
if (item == nullptr || item->id != Tile::standing_banner_Id) continue;
|
||||
if (item->getAuxValue() != packet->findBannerAux) continue;
|
||||
|
||||
std::vector<char> bannerPatternIndexs = BannerPatternRecipe::getBannerPatternIndexs(item);
|
||||
if (bannerPatternIndexs.size() != packet->findPatternIndexs.size()) continue;
|
||||
|
||||
bool failedPatternMatches = false;
|
||||
|
||||
for (int j = 0; j < bannerPatternIndexs.size(); j++) {
|
||||
if (packet->findPatternIndexs[j] != bannerPatternIndexs[j]) {
|
||||
failedPatternMatches = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (failedPatternMatches) continue;
|
||||
@@ -3602,6 +3604,7 @@ void PlayerConnection::handleCraftItem(shared_ptr<CraftItemPacket> packet)
|
||||
for (int j = 0; j < bannerPatternColors.size(); j++) {
|
||||
if (packet->findPatternColors[j] != bannerPatternColors[j]) {
|
||||
failedPatternMatches = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (failedPatternMatches) continue;
|
||||
@@ -3609,23 +3612,86 @@ void PlayerConnection::handleCraftItem(shared_ptr<CraftItemPacket> packet)
|
||||
foundBannerItem = item;
|
||||
}
|
||||
|
||||
if (foundBannerItem != nullptr) {
|
||||
auto newBanner = ItemInstance::clone(foundBannerItem);
|
||||
if (foundBannerItem == nullptr) return;
|
||||
|
||||
bool craftAnything = app.DebugSettingsOn() && (player->GetDebugOptions() & (1L << eDebugSetting_CraftAnything));
|
||||
bool canCraft = true;
|
||||
|
||||
//forward wrappers from IUIScene_CraftingMenu
|
||||
auto hasItem = [&](int id, int requiredCount = 1, int aux = -1) {
|
||||
int count = 0;
|
||||
for (int i = 0; i < player->inventory->items.length; i++) {
|
||||
const shared_ptr<ItemInstance>& item = player->inventory->items[i];
|
||||
if (item == nullptr || item->id != id) continue;
|
||||
if (aux != -1 && item->getAuxValue() != aux) continue;
|
||||
count += item->count;
|
||||
if (count >= requiredCount) return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
auto hasItemCounts = [&](int id, int aux = -1) {
|
||||
int count = 0;
|
||||
for (int i = 0; i < player->inventory->items.length; i++) {
|
||||
const shared_ptr<ItemInstance>& item = player->inventory->items[i];
|
||||
if (item == nullptr || item->id != id) continue;
|
||||
if (aux != -1 && item->getAuxValue() != aux) continue;
|
||||
count += item->count;
|
||||
}
|
||||
return count;
|
||||
};
|
||||
|
||||
if (canCraft && !craftAnything) {
|
||||
int requiredDyes[16] = {};
|
||||
for (int i = 0; i < packet->patternIndexs.size(); i++) {
|
||||
char patternIndex = packet->patternIndexs[i];
|
||||
|
||||
BannerPatternRecipe::addBannerPattern(newBanner, BannerPatternRecipe::PATTERNS[patternIndex].id, packet->patternColors[i]);
|
||||
const BannerPatternRecipe::PatternInfo& pattern = BannerPatternRecipe::PATTERNS[static_cast<unsigned char>(packet->patternIndexs[i])];
|
||||
if (pattern.specialItemId != 0) continue;
|
||||
|
||||
int color = static_cast<unsigned char>(packet->patternColors[i]) & 15;
|
||||
for (int row = 0; row < 3; row++)
|
||||
for (int col = 0; col < 3; col++)
|
||||
if (pattern.rows[row][col] != ' ') ++requiredDyes[color];
|
||||
}
|
||||
|
||||
if (app.DebugSettingsOn() && (player->GetDebugOptions() & (1L << eDebugSetting_CraftAnything))) {
|
||||
if (player->inventory->add(foundBannerItem) == false) {
|
||||
player->drop(foundBannerItem);
|
||||
for (int color = 0; color < 16; color++) {
|
||||
if (hasItemCounts(Item::dye_Id, color) < requiredDyes[color]) {
|
||||
canCraft = false;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if (!canCraft) return;
|
||||
|
||||
shared_ptr<ItemInstance> newBanner = ItemInstance::clone(foundBannerItem);
|
||||
for (int i = 0; i < packet->patternIndexs.size(); i++) {
|
||||
int patternIndex = static_cast<unsigned char>(packet->patternIndexs[i]);
|
||||
BannerPatternRecipe::addBannerPattern(newBanner, BannerPatternRecipe::PATTERNS[patternIndex].id, packet->patternColors[i]);
|
||||
}
|
||||
|
||||
if (!craftAnything) {
|
||||
for (int i = 0; i < packet->patternIndexs.size(); i++) {
|
||||
int patternIndex = static_cast<unsigned char>(packet->patternIndexs[i]);
|
||||
const BannerPatternRecipe::PatternInfo& pattern = BannerPatternRecipe::PATTERNS[patternIndex];
|
||||
|
||||
if (pattern.specialItemId != 0) {
|
||||
player->inventory->removeResource(pattern.specialItemId, pattern.specialItemAux);
|
||||
} else {
|
||||
int color = static_cast<unsigned char>(packet->patternColors[i]) & 15;
|
||||
int dyeCount = 0;
|
||||
for (int row = 0; row < 3; row++)
|
||||
for (int col = 0; col < 3; col++)
|
||||
if (pattern.rows[row][col] != ' ') ++dyeCount;
|
||||
|
||||
for (int j = 0; j < dyeCount; j++)
|
||||
player->inventory->removeResource(Item::dye_Id, color);
|
||||
}
|
||||
}
|
||||
|
||||
player->inventory->removeResources(foundBannerItem);
|
||||
}
|
||||
|
||||
if (!player->inventory->add(newBanner)) player->drop(newBanner);
|
||||
} else {
|
||||
int iRecipe = packet->recipe;
|
||||
|
||||
|
||||
Reference in new issue
Block a user