the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
1#include "stdafx.h"
2#include "net.minecraft.world.entity.item.h"
3#include "net.minecraft.world.item.h"
4#include "net.minecraft.world.inventory.h"
5#include "net.minecraft.world.level.h"
6#include "net.minecraft.world.level.redstone.h"
7#include "MinecartContainer.h"
8
9void MinecartContainer::_init()
10{
11 items = ItemInstanceArray(9 * 4);
12 dropEquipment = true;
13
14 // 4J Stu - This function call had to be moved here from the Entity ctor to ensure that
15 // the derived version of the function is called
16 this->defineSynchedData();
17}
18
19MinecartContainer::MinecartContainer(Level *level) : Minecart(level)
20{
21 _init();
22}
23
24MinecartContainer::MinecartContainer(Level *level, double x, double y, double z) : Minecart(level, x, y, z)
25{
26 _init();
27}
28
29void MinecartContainer::destroy(DamageSource *source)
30{
31 Minecart::destroy(source);
32
33 for (int i = 0; i < getContainerSize(); i++)
34 {
35 shared_ptr<ItemInstance> item = getItem(i);
36 if (item != NULL)
37 {
38 float xo = random->nextFloat() * 0.8f + 0.1f;
39 float yo = random->nextFloat() * 0.8f + 0.1f;
40 float zo = random->nextFloat() * 0.8f + 0.1f;
41
42 while (item->count > 0)
43 {
44 int count = random->nextInt(21) + 10;
45 if (count > item->count) count = item->count;
46 item->count -= count;
47
48 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()))) );
49 float pow = 0.05f;
50 itemEntity->xd = (float) random->nextGaussian() * pow;
51 itemEntity->yd = (float) random->nextGaussian() * pow + 0.2f;
52 itemEntity->zd = (float) random->nextGaussian() * pow;
53 level->addEntity(itemEntity);
54 }
55 }
56 }
57}
58
59shared_ptr<ItemInstance> MinecartContainer::getItem(unsigned int slot)
60{
61 return items[slot];
62}
63
64shared_ptr<ItemInstance> MinecartContainer::removeItem(unsigned int slot, int count)
65{
66 if (items[slot] != NULL)
67 {
68 if (items[slot]->count <= count)
69 {
70 shared_ptr<ItemInstance> item = items[slot];
71 items[slot] = nullptr;
72 return item;
73 }
74 else
75 {
76 shared_ptr<ItemInstance> i = items[slot]->remove(count);
77 if (items[slot]->count == 0) items[slot] = nullptr;
78 return i;
79 }
80 }
81 return nullptr;
82}
83
84shared_ptr<ItemInstance> MinecartContainer::removeItemNoUpdate(int slot)
85{
86 if (items[slot] != NULL)
87 {
88 shared_ptr<ItemInstance> item = items[slot];
89 items[slot] = nullptr;
90 return item;
91 }
92 return nullptr;
93}
94
95void MinecartContainer::setItem(unsigned int slot, shared_ptr<ItemInstance> item)
96{
97 items[slot] = item;
98 if (item != NULL && item->count > getMaxStackSize()) item->count = getMaxStackSize();
99}
100
101void MinecartContainer::setChanged()
102{
103}
104
105bool MinecartContainer::stillValid(shared_ptr<Player> player)
106{
107 if (removed) return false;
108 if (player->distanceToSqr(shared_from_this()) > 8 * 8) return false;
109 return true;
110}
111
112void MinecartContainer::startOpen()
113{
114}
115
116void MinecartContainer::stopOpen()
117{
118}
119
120bool MinecartContainer::canPlaceItem(int slot, shared_ptr<ItemInstance> item)
121{
122 return true;
123}
124
125wstring MinecartContainer::getName()
126{
127 return hasCustomName() ? getCustomName() : app.GetString(IDS_CONTAINER_MINECART);
128}
129
130int MinecartContainer::getMaxStackSize() const
131{
132 return Container::LARGE_MAX_STACK_SIZE;
133}
134
135void MinecartContainer::changeDimension(int i)
136{
137 dropEquipment = false;
138 Minecart::changeDimension(i);
139}
140
141void MinecartContainer::remove()
142{
143 if (dropEquipment)
144 {
145 for (int i = 0; i < getContainerSize(); i++)
146 {
147 shared_ptr<ItemInstance> item = getItem(i);
148 if (item != NULL)
149 {
150 float xo = random->nextFloat() * 0.8f + 0.1f;
151 float yo = random->nextFloat() * 0.8f + 0.1f;
152 float zo = random->nextFloat() * 0.8f + 0.1f;
153
154 while (item->count > 0)
155 {
156 int count = random->nextInt(21) + 10;
157 if (count > item->count) count = item->count;
158 item->count -= count;
159
160 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()))));
161
162 if (item->hasTag())
163 {
164 itemEntity->getItem()->setTag((CompoundTag *) item->getTag()->copy());
165 }
166
167 float pow = 0.05f;
168 itemEntity->xd = (float) random->nextGaussian() * pow;
169 itemEntity->yd = (float) random->nextGaussian() * pow + 0.2f;
170 itemEntity->zd = (float) random->nextGaussian() * pow;
171 level->addEntity(itemEntity);
172 }
173 }
174 }
175 }
176
177 Minecart::remove();
178}
179
180void MinecartContainer::addAdditonalSaveData(CompoundTag *base)
181{
182 Minecart::addAdditonalSaveData(base);
183
184 ListTag<CompoundTag> *listTag = new ListTag<CompoundTag>();
185
186 for (int i = 0; i < items.length; i++)
187 {
188 if (items[i] != NULL)
189 {
190 CompoundTag *tag = new CompoundTag();
191 tag->putByte(L"Slot", (byte) i);
192 items[i]->save(tag);
193 listTag->add(tag);
194 }
195 }
196 base->put(L"Items", listTag);
197}
198
199void MinecartContainer::readAdditionalSaveData(CompoundTag *base)
200{
201 Minecart::readAdditionalSaveData(base);
202
203 ListTag<CompoundTag> *inventoryList = (ListTag<CompoundTag> *) base->getList(L"Items");
204 delete [] items.data;
205 items = ItemInstanceArray(getContainerSize());
206 for (int i = 0; i < inventoryList->size(); i++)
207 {
208 CompoundTag *tag = inventoryList->get(i);
209 int slot = tag->getByte(L"Slot") & 0xff;
210 if (slot >= 0 && slot < items.length) items[slot] = ItemInstance::fromTag(tag);
211 }
212}
213
214bool MinecartContainer::interact(shared_ptr<Player> player)
215{
216 if (!level->isClientSide)
217 {
218 player->openContainer( dynamic_pointer_cast<Container>(shared_from_this()));
219 }
220
221 return true;
222}
223
224void MinecartContainer::applyNaturalSlowdown()
225{
226 shared_ptr<Container> container = dynamic_pointer_cast<Container>(shared_from_this());
227 int emptiness = Redstone::SIGNAL_MAX - AbstractContainerMenu::getRedstoneSignalFromContainer(container);
228 float keep = 0.98f + (emptiness * 0.001f);
229
230 xd *= keep;
231 yd *= 0;
232 zd *= keep;
233}