aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/MerchantResultSlot.cpp
blob: fedbcc4adf0dee285fe4bb7631a753de8ae105df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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> merchant, shared_ptr<MerchantContainer> 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<ItemInstance> item)
{
	return false;
}

shared_ptr<ItemInstance> MerchantResultSlot::remove(int c)
{
	if (hasItem())
	{
		removeCount += min(c, getItem()->count);
	}
	return Slot::remove(c);
}

void MerchantResultSlot::onQuickCraft(shared_ptr<ItemInstance> picked, int count)
{
	removeCount += count;
	checkTakeAchievements(picked);
}

void MerchantResultSlot::checkTakeAchievements(shared_ptr<ItemInstance> carried)
{
	carried->onCraftedBy(player->level, dynamic_pointer_cast<Player>(player->shared_from_this()), removeCount);
	removeCount = 0;
}

void MerchantResultSlot::onTake(shared_ptr<Player> player, shared_ptr<ItemInstance> carried)
{
	checkTakeAchievements(carried);

	MerchantRecipe *activeRecipe = slots->getActiveRecipe();
	if (activeRecipe != NULL)
	{
		shared_ptr<ItemInstance> item1 = slots->getItem(MerchantMenu::PAYMENT1_SLOT);
		shared_ptr<ItemInstance> 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<ItemInstance> second)
{
	return false;
}

bool MerchantResultSlot::removePaymentItemsIfMatching(MerchantRecipe *activeRecipe, shared_ptr<ItemInstance> a, shared_ptr<ItemInstance> b)
{
	shared_ptr<ItemInstance> buyA = activeRecipe->getBuyAItem();
	shared_ptr<ItemInstance> 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;
}