aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/EnchantedBookItem.cpp
blob: 70b15be46a2ce21c0e6b2b8a82873e9c911e1d19 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "stdafx.h"
#include "net.minecraft.world.item.enchantment.h"
#include "net.minecraft.world.item.h"
#include "WeighedTreasure.h"
#include "EnchantedBookItem.h"

const wstring EnchantedBookItem::TAG_STORED_ENCHANTMENTS = L"StoredEnchantments";

EnchantedBookItem::EnchantedBookItem(int id) : Item(id)
{
}

bool EnchantedBookItem::isFoil(shared_ptr<ItemInstance> itemInstance)
{
	return true;
}

bool EnchantedBookItem::isEnchantable(shared_ptr<ItemInstance> itemInstance)
{
	return false;
}

const Rarity *EnchantedBookItem::getRarity(shared_ptr<ItemInstance> itemInstance)
{
	ListTag<CompoundTag> *enchantments = getEnchantments(itemInstance);
	if (enchantments && enchantments->size() > 0)
	{
		return Rarity::uncommon;
	}
	else
	{
		return Item::getRarity(itemInstance);
	}
}

ListTag<CompoundTag> *EnchantedBookItem::getEnchantments(shared_ptr<ItemInstance> item)
{
	if (item->tag == nullptr || !item->tag->contains((wchar_t *)TAG_STORED_ENCHANTMENTS.c_str()))
	{
		return new ListTag<CompoundTag>();
	}

	return static_cast<ListTag<CompoundTag> *>(item->tag->get((wchar_t *)TAG_STORED_ENCHANTMENTS.c_str()));
}

void EnchantedBookItem::appendHoverText(shared_ptr<ItemInstance> itemInstance, shared_ptr<Player> player, vector<HtmlString> *lines, bool advanced)
{
	Item::appendHoverText(itemInstance, player, lines, advanced);

	ListTag<CompoundTag> *list = getEnchantments(itemInstance);

	if (list != nullptr)
	{
		wstring unformatted = L"";
		for (int i = 0; i < list->size(); i++)
		{
			int type = list->get(i)->getShort((wchar_t *)ItemInstance::TAG_ENCH_ID);
			int level = list->get(i)->getShort((wchar_t *)ItemInstance::TAG_ENCH_LEVEL);

			if (Enchantment::enchantments[type] != nullptr)
			{
				lines->push_back(Enchantment::enchantments[type]->getFullname(level));
			}
		}
	}
}

void EnchantedBookItem::addEnchantment(shared_ptr<ItemInstance> item, EnchantmentInstance *enchantment)
{
	ListTag<CompoundTag> *enchantments = getEnchantments(item);
	bool add = true;

	for (int i = 0; i < enchantments->size(); i++)
	{
		CompoundTag *tag = enchantments->get(i);

		if (tag->getShort((wchar_t *)ItemInstance::TAG_ENCH_ID) == enchantment->enchantment->id)
		{
			if (tag->getShort((wchar_t *)ItemInstance::TAG_ENCH_LEVEL) < enchantment->level)
			{
				tag->putShort((wchar_t *)ItemInstance::TAG_ENCH_LEVEL, static_cast<short>(enchantment->level));
			}

			add = false;
			break;
		}
	}

	if (add)
	{
		CompoundTag *tag = new CompoundTag();

		tag->putShort((wchar_t *)ItemInstance::TAG_ENCH_ID, static_cast<short>(enchantment->enchantment->id));
		tag->putShort((wchar_t *)ItemInstance::TAG_ENCH_LEVEL, static_cast<short>(enchantment->level));

		enchantments->add(tag);
	}

	if (!item->hasTag()) item->setTag(new CompoundTag());
	item->getTag()->put((wchar_t *)TAG_STORED_ENCHANTMENTS.c_str(), enchantments);
}

shared_ptr<ItemInstance> EnchantedBookItem::createForEnchantment(EnchantmentInstance *enchant)
{
	shared_ptr<ItemInstance> item = std::make_shared<ItemInstance>(this);
	addEnchantment(item, enchant);
	return item;
}

void EnchantedBookItem::createForEnchantment(Enchantment *enchant, vector<shared_ptr<ItemInstance> > *items)
{
	for (int i = enchant->getMinLevel(); i <= enchant->getMaxLevel(); i++)
	{
		items->push_back(createForEnchantment(new EnchantmentInstance(enchant, i)));
	}
}

shared_ptr<ItemInstance> EnchantedBookItem::createForRandomLoot(Random *random)
{
	Enchantment *enchantment = Enchantment::validEnchantments[random->nextInt(Enchantment::validEnchantments.size())];
	shared_ptr<ItemInstance> book = std::make_shared<ItemInstance>(id, 1, 0);
	int level = Mth::nextInt(random, enchantment->getMinLevel(), enchantment->getMaxLevel());

	addEnchantment(book, new EnchantmentInstance(enchantment, level));

	return book;
}

WeighedTreasure *EnchantedBookItem::createForRandomTreasure(Random *random)
{
	return createForRandomTreasure(random, 1, 1, 1);
}

WeighedTreasure *EnchantedBookItem::createForRandomTreasure(Random *random, int minCount, int maxCount, int weight)
{
	Enchantment *enchantment = Enchantment::validEnchantments[random->nextInt(Enchantment::validEnchantments.size())];
	shared_ptr<ItemInstance> book = std::make_shared<ItemInstance>(id, 1, 0);
	int level = Mth::nextInt(random, enchantment->getMinLevel(), enchantment->getMaxLevel());

	addEnchantment(book, new EnchantmentInstance(enchantment, level));

	return new WeighedTreasure(book, minCount, maxCount, weight);
}