From b691c43c44ff180d10e7d4a9afc83b98551ff586 Mon Sep 17 00:00:00 2001 From: daoge_cmd <3523206925@qq.com> Date: Sun, 1 Mar 2026 12:16:08 +0800 Subject: Initial commit --- Minecraft.World/MerchantRecipe.cpp | 146 +++++++++++++++++++++++++++++++++++++ 1 file changed, 146 insertions(+) create mode 100644 Minecraft.World/MerchantRecipe.cpp (limited to 'Minecraft.World/MerchantRecipe.cpp') diff --git a/Minecraft.World/MerchantRecipe.cpp b/Minecraft.World/MerchantRecipe.cpp new file mode 100644 index 00000000..b772b0c7 --- /dev/null +++ b/Minecraft.World/MerchantRecipe.cpp @@ -0,0 +1,146 @@ +#include "stdafx.h" + +#include "MerchantRecipe.h" + +void MerchantRecipe::_init(shared_ptr buyA, shared_ptr buyB, shared_ptr sell) +{ + this->buyA = buyA; + this->buyB = buyB; + this->sell = sell; + uses = 0; + maxUses = 7; +} + +MerchantRecipe::MerchantRecipe(CompoundTag *tag) +{ + buyA = nullptr; + buyB = nullptr; + sell = nullptr; + uses = 0; + load(tag); +} + +MerchantRecipe::MerchantRecipe(shared_ptr buyA, shared_ptr buyB, shared_ptr sell, int uses, int maxUses) +{ + _init(buyA, buyB, sell); + this->uses = uses; + this->maxUses = maxUses; +} + +MerchantRecipe::MerchantRecipe(shared_ptr buy, shared_ptr sell) +{ + _init(buy, nullptr, sell); +} + +MerchantRecipe::MerchantRecipe(shared_ptr buy, Item *sell) +{ + _init(buy, nullptr, shared_ptr(new ItemInstance(sell))); +} + +MerchantRecipe::MerchantRecipe(shared_ptr buy, Tile *sell) +{ + _init(buy, nullptr, shared_ptr(new ItemInstance(sell))); +} + +shared_ptr MerchantRecipe::getBuyAItem() +{ + return buyA; +} + +shared_ptr MerchantRecipe::getBuyBItem() +{ + return buyB; +} + +bool MerchantRecipe::hasSecondaryBuyItem() +{ + return buyB != NULL; +} + +shared_ptr MerchantRecipe::getSellItem() +{ + return sell; +} + +bool MerchantRecipe::isSame(MerchantRecipe *other) +{ + if (buyA->id != other->buyA->id || sell->id != other->sell->id) + { + return false; + } + return (buyB == NULL && other->buyB == NULL) || (buyB != NULL && other->buyB != NULL && buyB->id == other->buyB->id); +} + +bool MerchantRecipe::isSameSameButBetter(MerchantRecipe *other) +{ + // same deal, but cheaper + return isSame(other) && (buyA->count < other->buyA->count || (buyB != NULL && buyB->count < other->buyB->count)); +} + +int MerchantRecipe::getUses() +{ + return uses; +} + +int MerchantRecipe::getMaxUses() +{ + return maxUses; +} + +void MerchantRecipe::increaseUses() +{ + uses++; +} + +void MerchantRecipe::increaseMaxUses(int amount) +{ + maxUses += amount; +} + +bool MerchantRecipe::isDeprecated() +{ + return uses >= maxUses; +} + +void MerchantRecipe::enforceDeprecated() +{ + uses = maxUses; +} + +void MerchantRecipe::load(CompoundTag *tag) +{ + CompoundTag *buyTag = tag->getCompound(L"buy"); + buyA = ItemInstance::fromTag(buyTag); + CompoundTag *sellTag = tag->getCompound(L"sell"); + sell = ItemInstance::fromTag(sellTag); + if (tag->contains(L"buyB")) + { + buyB = ItemInstance::fromTag(tag->getCompound(L"buyB")); + } + if (tag->contains(L"uses")) + { + uses = tag->getInt(L"uses"); + } + if (tag->contains(L"maxUses")) + { + maxUses = tag->getInt(L"maxUses"); + } + else + { + maxUses = 7; + } +} + +CompoundTag *MerchantRecipe::createTag() +{ + CompoundTag *tag = new CompoundTag(); + tag->putCompound(L"buy", buyA->save(new CompoundTag(L"buy"))); + tag->putCompound(L"sell", sell->save(new CompoundTag(L"sell"))); + if (buyB != NULL) + { + tag->putCompound(L"buyB", buyB->save(new CompoundTag(L"buyB"))); + } + tag->putInt(L"uses", uses); + tag->putInt(L"maxUses", maxUses); + return tag; +} \ No newline at end of file -- cgit v1.2.3