aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/WeighedTreasure.cpp
blob: 3c20ae66615713e1beaee9f3b78af07aaa848f2c (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
#include "stdafx.h"
#include "net.minecraft.world.level.tile.entity.h"
#include "net.minecraft.world.item.h"
#include "WeighedRandom.h"
#include "WeighedTreasure.h"

WeighedTreasure::WeighedTreasure(int itemId, int auxValue, int minCount, int maxCount, int weight) : WeighedRandomItem(weight)
{
	this->item = std::make_shared<ItemInstance>(itemId, 1, auxValue);
	this->minCount = minCount;
	this->maxCount = maxCount;
}

WeighedTreasure::WeighedTreasure(shared_ptr<ItemInstance> item, int minCount, int maxCount, int weight) : WeighedRandomItem(weight)
{
	this->item = item;
	this->minCount = minCount;
	this->maxCount = maxCount;
}

void WeighedTreasure::addChestItems(Random *random, WeighedTreasureArray items, shared_ptr<Container> dest, int numRolls)
{
	for (int r = 0; r < numRolls; r++)
	{
		WeighedTreasure *treasure = static_cast<WeighedTreasure *>(WeighedRandom::getRandomItem(random, *((WeighedRandomItemArray *)&items)));

		int count = treasure->minCount + random->nextInt(treasure->maxCount - treasure->minCount + 1);
		if (treasure->item->getMaxStackSize() >= count)
		{
			shared_ptr<ItemInstance> copy = treasure->item->copy();
			copy->count = count;
			dest->setItem(random->nextInt(dest->getContainerSize()), copy);
		}
		else
		{
			// use multiple slots
			for (int c = 0; c < count; c++)
			{
				shared_ptr<ItemInstance> copy = treasure->item->copy();
				copy->count = 1;
				dest->setItem(random->nextInt(dest->getContainerSize()), copy);
			}
		}
	}
}

void WeighedTreasure::addDispenserItems(Random *random, WeighedTreasureArray items, shared_ptr<DispenserTileEntity> dest, int numRolls)
{
	for (int r = 0; r < numRolls; r++)
	{
		WeighedTreasure *treasure = static_cast<WeighedTreasure *>(WeighedRandom::getRandomItem(random, *((WeighedRandomItemArray *)&items)));

		int count = treasure->minCount + random->nextInt(treasure->maxCount - treasure->minCount + 1);
		if (treasure->item->getMaxStackSize() >= count)
		{
			shared_ptr<ItemInstance> copy = treasure->item->copy();
			copy->count = count;
			dest->setItem(random->nextInt(dest->getContainerSize()), copy);
		}
		else
		{
			// use multiple slots
			for (int c = 0; c < count; c++)
			{
				shared_ptr<ItemInstance> copy = treasure->item->copy();
				copy->count = 1;
				dest->setItem(random->nextInt(dest->getContainerSize()), copy);
			}
		}
	}
}

WeighedTreasureArray WeighedTreasure::addToTreasure(WeighedTreasureArray items, WeighedTreasure *extra)
{
	WeighedTreasureArray result(items.length + 1);
	int i = 0;

	for (int j = 0; j < items.length; j++)
	{
		result[i++] = items[j];
	}

	result[i++] = extra;

	return result;
}