aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/FireworksChargeItem.cpp
blob: 8b12c98c466312ac9b246fdf0629d0816cec65cf (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include "stdafx.h"
#include "net.minecraft.world.item.h"
#include "net.minecraft.world.h"
#include "FireworksChargeItem.h"

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

Icon *FireworksChargeItem::getLayerIcon(int auxValue, int spriteLayer)
{
	if (spriteLayer > 0)
	{
		return overlay;
	}
	return Item::getLayerIcon(auxValue, spriteLayer);
}

int FireworksChargeItem::getColor(shared_ptr<ItemInstance> item, int spriteLayer)
{
	if (spriteLayer == 1)
	{
		Tag *colorTag = getExplosionTagField(item, FireworksItem::TAG_E_COLORS);
		if (colorTag != nullptr)
		{
			IntArrayTag *colors = static_cast<IntArrayTag *>(colorTag);
			if (colors->data.length == 1)
			{
				return colors->data[0];
			}
			int totalRed = 0;
			int totalGreen = 0;
			int totalBlue = 0;
			for (unsigned int i = 0; i < colors->data.length; ++i)
			{
				int c = colors->data[i];
				totalRed += (c & 0xff0000) >> 16;
				totalGreen += (c & 0x00ff00) >> 8;
				totalBlue += (c & 0x0000ff) >> 0;
			}
			totalRed /= colors->data.length;
			totalGreen /= colors->data.length;
			totalBlue /= colors->data.length;
			return (totalRed << 16) | (totalGreen << 8) | totalBlue;
		}
		return 0x8a8a8a;
	}
	return Item::getColor(item, spriteLayer);
}

bool FireworksChargeItem::hasMultipleSpriteLayers()
{
	return true;
}

Tag *FireworksChargeItem::getExplosionTagField(shared_ptr<ItemInstance> instance, const wstring &field)
{
	if (instance->hasTag())
	{
		CompoundTag *explosion = instance->getTag()->getCompound(FireworksItem::TAG_EXPLOSION);
		if (explosion != nullptr)
		{
			return explosion->get(field);
		}
	}
	return nullptr;
}

void FireworksChargeItem::appendHoverText(shared_ptr<ItemInstance> itemInstance, shared_ptr<Player> player, vector<HtmlString> *lines, bool advanced)
{
	if (itemInstance->hasTag())
	{
		CompoundTag *explosion = itemInstance->getTag()->getCompound(FireworksItem::TAG_EXPLOSION);
		if (explosion != nullptr)
		{
			appendHoverText(explosion, lines);
		}
	}
}

const unsigned int FIREWORKS_CHARGE_TYPE_NAME[] = 
{
	IDS_FIREWORKS_CHARGE_TYPE_0, 
	IDS_FIREWORKS_CHARGE_TYPE_1, 
	IDS_FIREWORKS_CHARGE_TYPE_2, 
	IDS_FIREWORKS_CHARGE_TYPE_3, 
	IDS_FIREWORKS_CHARGE_TYPE_4
};

const unsigned int FIREWORKS_CHARGE_COLOUR_NAME[] = 
{ 
	IDS_FIREWORKS_CHARGE_BLACK,
	IDS_FIREWORKS_CHARGE_RED,
	IDS_FIREWORKS_CHARGE_GREEN,
	IDS_FIREWORKS_CHARGE_BROWN,
	IDS_FIREWORKS_CHARGE_BLUE,
	IDS_FIREWORKS_CHARGE_PURPLE,
	IDS_FIREWORKS_CHARGE_CYAN,
	IDS_FIREWORKS_CHARGE_SILVER,
	IDS_FIREWORKS_CHARGE_GRAY,
	IDS_FIREWORKS_CHARGE_PINK,
	IDS_FIREWORKS_CHARGE_LIME,
	IDS_FIREWORKS_CHARGE_YELLOW,
	IDS_FIREWORKS_CHARGE_LIGHT_BLUE,
	IDS_FIREWORKS_CHARGE_MAGENTA,
	IDS_FIREWORKS_CHARGE_ORANGE,
	IDS_FIREWORKS_CHARGE_WHITE
};

void FireworksChargeItem::appendHoverText(CompoundTag *expTag, vector<HtmlString> *lines)
{
	// shape
	byte type = expTag->getByte(FireworksItem::TAG_E_TYPE);
	if (type >= FireworksItem::TYPE_MIN && type <= FireworksItem::TYPE_MAX)
	{
		lines->push_back(HtmlString(app.GetString(FIREWORKS_CHARGE_TYPE_NAME[type])));
	}
	else
	{
		lines->push_back(HtmlString(app.GetString(IDS_FIREWORKS_CHARGE_TYPE)));
	}

	// colors
	intArray colorList = expTag->getIntArray(FireworksItem::TAG_E_COLORS);
	if (colorList.length > 0)
	{

		bool first = true;
		wstring output = L"";
		for (unsigned int i = 0; i < colorList.length; ++i)
		{
			int c = colorList[i];
			if (!first)
			{
				output += L",\n"; // 4J-PB  - without the newline, they tend to go offscreen in split-screen or localised languages
			}
			first = false;

			// find color name by lookup
			bool found = false;
			for (int dc = 0; dc < 16; dc++)
			{
				if (c == DyePowderItem::COLOR_RGB[dc])
				{
					found = true;
					output += app.GetString(FIREWORKS_CHARGE_COLOUR_NAME[dc]);
					break;
				}
			}
			if (!found)
			{
				output += app.GetString(IDS_FIREWORKS_CHARGE_CUSTOM);
			}
		}
		lines->push_back(output);
	}

	// has fade?
	intArray fadeList = expTag->getIntArray(FireworksItem::TAG_E_FADECOLORS);
	if (fadeList.length > 0)
	{
		bool first = true;
		wstring output = wstring(app.GetString(IDS_FIREWORKS_CHARGE_FADE_TO)) + L" ";
		for (unsigned int i = 0; i < fadeList.length; ++i)
		{
			int c = fadeList[i];
			if (!first)
			{
				output += L",\n";// 4J-PB  - without the newline, they tend to go offscreen in split-screen or localised languages
			}
			first = false;

			// find color name by lookup
			bool found = false;
			for (int dc = 0; dc < 16; dc++)
			{
				if (c == DyePowderItem::COLOR_RGB[dc])
				{
					found = true;
					output += app.GetString(FIREWORKS_CHARGE_COLOUR_NAME[dc]);
					break;
				}
			}
			if (!found)
			{
				output += app.GetString(IDS_FIREWORKS_CHARGE_CUSTOM);
			}
		}
		lines->push_back(output);
	}

	// has trail
	bool trail = expTag->getBoolean(FireworksItem::TAG_E_TRAIL);
	if (trail)
	{
		lines->push_back(HtmlString(app.GetString(IDS_FIREWORKS_CHARGE_TRAIL)));
	}

	// has flicker
	bool flicker = expTag->getBoolean(FireworksItem::TAG_E_FLICKER);
	if (flicker)
	{
		lines->push_back(HtmlString(app.GetString(IDS_FIREWORKS_CHARGE_FLICKER)));
	}
}

void FireworksChargeItem::registerIcons(IconRegister *iconRegister)
{
	Item::registerIcons(iconRegister);
	overlay = iconRegister->registerIcon(getIconName() + L"_overlay");
}