aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.World/MinecartContainer.cpp
blob: 20bfc4ea8e60fcfa4c03f819d87eb3f8c04eec83 (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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include "stdafx.h"
#include "net.minecraft.world.entity.item.h"
#include "net.minecraft.world.item.h"
#include "net.minecraft.world.inventory.h"
#include "net.minecraft.world.level.h"
#include "net.minecraft.world.level.redstone.h"
#include "MinecartContainer.h"

void MinecartContainer::_init()
{
	items = ItemInstanceArray(9 * 4);
	dropEquipment = true;

	// 4J Stu - This function call had to be moved here from the Entity ctor to ensure that
	// the derived version of the function is called
	this->defineSynchedData();
}

MinecartContainer::MinecartContainer(Level *level) : Minecart(level)
{
	_init();
}

MinecartContainer::MinecartContainer(Level *level, double x, double y, double z) : Minecart(level, x, y, z)
{
	_init();
}

void MinecartContainer::destroy(DamageSource *source)
{
	Minecart::destroy(source);

	for (int i = 0; i < getContainerSize(); i++)
	{
		shared_ptr<ItemInstance> item = getItem(i);
		if (item != NULL)
		{
			float xo = random->nextFloat() * 0.8f + 0.1f;
			float yo = random->nextFloat() * 0.8f + 0.1f;
			float zo = random->nextFloat() * 0.8f + 0.1f;

			while (item->count > 0)
			{
				int count = random->nextInt(21) + 10;
				if (count > item->count) count = item->count;
				item->count -= count;

				shared_ptr<ItemEntity> itemEntity = shared_ptr<ItemEntity>( new ItemEntity(level, x + xo, y + yo, z + zo, shared_ptr<ItemInstance>( new ItemInstance(item->id, count, item->getAuxValue()))) );
				float pow = 0.05f;
				itemEntity->xd = (float) random->nextGaussian() * pow;
				itemEntity->yd = (float) random->nextGaussian() * pow + 0.2f;
				itemEntity->zd = (float) random->nextGaussian() * pow;
				level->addEntity(itemEntity);
			}
		}
	}
}

shared_ptr<ItemInstance> MinecartContainer::getItem(unsigned int slot)
{
	return items[slot];
}

shared_ptr<ItemInstance> MinecartContainer::removeItem(unsigned int slot, int count)
{
	if (items[slot] != NULL)
	{
		if (items[slot]->count <= count)
		{
			shared_ptr<ItemInstance> item = items[slot];
			items[slot] = nullptr;
			return item;
		}
		else
		{
			shared_ptr<ItemInstance> i = items[slot]->remove(count);
			if (items[slot]->count == 0) items[slot] = nullptr;
			return i;
		}
	}
	return nullptr;
}

shared_ptr<ItemInstance> MinecartContainer::removeItemNoUpdate(int slot)
{
	if (items[slot] != NULL)
	{
		shared_ptr<ItemInstance> item = items[slot];
		items[slot] = nullptr;
		return item;
	}
	return nullptr;
}

void MinecartContainer::setItem(unsigned int slot, shared_ptr<ItemInstance> item)
{
	items[slot] = item;
	if (item != NULL && item->count > getMaxStackSize()) item->count = getMaxStackSize();
}

void MinecartContainer::setChanged()
{
}

bool MinecartContainer::stillValid(shared_ptr<Player> player)
{
	if (removed) return false;
	if (player->distanceToSqr(shared_from_this()) > 8 * 8) return false;
	return true;
}

void MinecartContainer::startOpen()
{
}

void MinecartContainer::stopOpen()
{
}

bool MinecartContainer::canPlaceItem(int slot, shared_ptr<ItemInstance> item)
{
	return true;
}

wstring MinecartContainer::getName()
{
	return hasCustomName() ? getCustomName() : app.GetString(IDS_CONTAINER_MINECART);
}

int MinecartContainer::getMaxStackSize() const
{
	return Container::LARGE_MAX_STACK_SIZE;
}

void MinecartContainer::changeDimension(int i)
{
	dropEquipment = false;
	Minecart::changeDimension(i);
}

void MinecartContainer::remove()
{
	if (dropEquipment)
	{
		for (int i = 0; i < getContainerSize(); i++)
		{
			shared_ptr<ItemInstance> item = getItem(i);
			if (item != NULL)
			{
				float xo = random->nextFloat() * 0.8f + 0.1f;
				float yo = random->nextFloat() * 0.8f + 0.1f;
				float zo = random->nextFloat() * 0.8f + 0.1f;

				while (item->count > 0)
				{
					int count = random->nextInt(21) + 10;
					if (count > item->count) count = item->count;
					item->count -= count;

					shared_ptr<ItemEntity> itemEntity = shared_ptr<ItemEntity>( new ItemEntity(level, x + xo, y + yo, z + zo, shared_ptr<ItemInstance>( new ItemInstance(item->id, count, item->getAuxValue()))));

					if (item->hasTag())
					{
						itemEntity->getItem()->setTag((CompoundTag *) item->getTag()->copy());
					}

					float pow = 0.05f;
					itemEntity->xd = (float) random->nextGaussian() * pow;
					itemEntity->yd = (float) random->nextGaussian() * pow + 0.2f;
					itemEntity->zd = (float) random->nextGaussian() * pow;
					level->addEntity(itemEntity);
				}
			}
		}
	}

	Minecart::remove();
}

void MinecartContainer::addAdditonalSaveData(CompoundTag *base)
{
	Minecart::addAdditonalSaveData(base);

	ListTag<CompoundTag> *listTag = new ListTag<CompoundTag>();

	for (int i = 0; i < items.length; i++)
	{
		if (items[i] != NULL)
		{
			CompoundTag *tag = new CompoundTag();
			tag->putByte(L"Slot", (byte) i);
			items[i]->save(tag);
			listTag->add(tag);
		}
	}
	base->put(L"Items", listTag);
}

void MinecartContainer::readAdditionalSaveData(CompoundTag *base)
{
	Minecart::readAdditionalSaveData(base);

	ListTag<CompoundTag> *inventoryList = (ListTag<CompoundTag> *) base->getList(L"Items");
	delete [] items.data;
	items = ItemInstanceArray(getContainerSize());
	for (int i = 0; i < inventoryList->size(); i++)
	{
		CompoundTag *tag = inventoryList->get(i);
		int slot = tag->getByte(L"Slot") & 0xff;
		if (slot >= 0 && slot < items.length) items[slot] = ItemInstance::fromTag(tag);
	}
}

bool MinecartContainer::interact(shared_ptr<Player> player)
{
	if (!level->isClientSide)
	{
		player->openContainer( dynamic_pointer_cast<Container>(shared_from_this()));
	}

	return true;
}

void MinecartContainer::applyNaturalSlowdown()
{
	shared_ptr<Container> container = dynamic_pointer_cast<Container>(shared_from_this());
	int emptiness = Redstone::SIGNAL_MAX - AbstractContainerMenu::getRedstoneSignalFromContainer(container);
	float keep = 0.98f + (emptiness * 0.001f);

	xd *= keep;
	yd *= 0;
	zd *= keep;
}