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/MerchantResultSlot.cpp | 94 ++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 Minecraft.World/MerchantResultSlot.cpp (limited to 'Minecraft.World/MerchantResultSlot.cpp') diff --git a/Minecraft.World/MerchantResultSlot.cpp b/Minecraft.World/MerchantResultSlot.cpp new file mode 100644 index 00000000..fedbcc4a --- /dev/null +++ b/Minecraft.World/MerchantResultSlot.cpp @@ -0,0 +1,94 @@ +#include "stdafx.h" +#include "net.minecraft.world.inventory.h" +#include "net.minecraft.world.item.trading.h" +#include "MerchantResultSlot.h" + +MerchantResultSlot::MerchantResultSlot(Player *player, shared_ptr merchant, shared_ptr slots, int id, int x, int y) : Slot(slots, id, x, y) +{ + this->player = player; + this->merchant = merchant; + this->slots = slots; + removeCount = 0; +} + +bool MerchantResultSlot::mayPlace(shared_ptr item) +{ + return false; +} + +shared_ptr MerchantResultSlot::remove(int c) +{ + if (hasItem()) + { + removeCount += min(c, getItem()->count); + } + return Slot::remove(c); +} + +void MerchantResultSlot::onQuickCraft(shared_ptr picked, int count) +{ + removeCount += count; + checkTakeAchievements(picked); +} + +void MerchantResultSlot::checkTakeAchievements(shared_ptr carried) +{ + carried->onCraftedBy(player->level, dynamic_pointer_cast(player->shared_from_this()), removeCount); + removeCount = 0; +} + +void MerchantResultSlot::onTake(shared_ptr player, shared_ptr carried) +{ + checkTakeAchievements(carried); + + MerchantRecipe *activeRecipe = slots->getActiveRecipe(); + if (activeRecipe != NULL) + { + shared_ptr item1 = slots->getItem(MerchantMenu::PAYMENT1_SLOT); + shared_ptr item2 = slots->getItem(MerchantMenu::PAYMENT2_SLOT); + + // remove payment items, but remember slots may have switched + if (removePaymentItemsIfMatching(activeRecipe, item1, item2) || removePaymentItemsIfMatching(activeRecipe, item2, item1)) + { + merchant->notifyTrade(activeRecipe); + + if (item1 && item1->count <= 0) + { + item1 = nullptr; + } + if (item2 && item2->count <= 0) + { + item2 = nullptr; + } + slots->setItem(MerchantMenu::PAYMENT1_SLOT, item1); + slots->setItem(MerchantMenu::PAYMENT2_SLOT, item2); + } + } +} + +bool MerchantResultSlot::mayCombine(shared_ptr second) +{ + return false; +} + +bool MerchantResultSlot::removePaymentItemsIfMatching(MerchantRecipe *activeRecipe, shared_ptr a, shared_ptr b) +{ + shared_ptr buyA = activeRecipe->getBuyAItem(); + shared_ptr buyB = activeRecipe->getBuyBItem(); + + if (a != NULL && a->id == buyA->id) + { + if (buyB != NULL && b != NULL && buyB->id == b->id) + { + a->count -= buyA->count; + b->count -= buyB->count; + return true; + } + else if (buyB == NULL && b == NULL) + { + a->count -= buyA->count; + return true; + } + } + return false; +} \ No newline at end of file -- cgit v1.2.3