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.h"
3#include "net.minecraft.world.level.h"
4#include "net.minecraft.world.entity.item.h"
5#include "net.minecraft.world.entity.animal.h"
6#include "net.minecraft.world.entity.player.h"
7#include "net.minecraft.world.item.h"
8#include "net.minecraft.world.level.redstone.h"
9#include "net.minecraft.world.level.tile.entity.h"
10#include "net.minecraft.world.phys.h"
11#include "ChestTile.h"
12#include "Facing.h"
13
14ChestTile::ChestTile(int id, int type) : BaseEntityTile(id, Material::wood, isSolidRender() )
15{
16 random = new Random();
17 this->type = type;
18
19 setShape(1 / 16.0f, 0, 1 / 16.0f, 15 / 16.0f, 14 / 16.0f, 15 / 16.0f);
20}
21
22ChestTile::~ChestTile()
23{
24 delete random;
25}
26
27bool ChestTile::isSolidRender(bool isServerLevel)
28{
29 return false;
30}
31
32bool ChestTile::isCubeShaped()
33{
34 return false;
35}
36
37int ChestTile::getRenderShape()
38{
39 return Tile::SHAPE_ENTITYTILE_ANIMATED;
40}
41
42void ChestTile::updateShape(LevelSource *level, int x, int y, int z, int forceData, shared_ptr<TileEntity> forceEntity)
43{
44 if (level->getTile(x, y, z - 1) == id)
45 {
46 setShape(1 / 16.0f, 0, 0, 15 / 16.0f, 14 / 16.0f, 15 / 16.0f);
47 }
48 else if (level->getTile(x, y, z + 1) == id)
49 {
50 setShape(1 / 16.0f, 0, 1 / 16.0f, 15 / 16.0f, 14 / 16.0f, 1);
51 }
52 else if (level->getTile(x - 1, y, z) == id)
53 {
54 setShape(0, 0, 1 / 16.0f, 15 / 16.0f, 14 / 16.0f, 15 / 16.0f);
55 }
56 else if (level->getTile(x + 1, y, z) == id)
57 {
58 setShape(1 / 16.0f, 0, 1 / 16.0f, 1, 14 / 16.0f, 15 / 16.0f);
59 }
60 else
61 {
62 setShape(1 / 16.0f, 0, 1 / 16.0f, 15 / 16.0f, 14 / 16.0f, 15 / 16.0f);
63 }
64}
65
66void ChestTile::onPlace(Level *level, int x, int y, int z)
67{
68 BaseEntityTile::onPlace(level, x, y, z);
69 recalcLockDir(level, x, y, z);
70
71 int n = level->getTile(x, y, z - 1); // face = 2
72 int s = level->getTile(x, y, z + 1); // face = 3
73 int w = level->getTile(x - 1, y, z); // face = 4
74 int e = level->getTile(x + 1, y, z); // face = 5
75 if (n == id) recalcLockDir(level, x, y, z - 1);
76 if (s == id) recalcLockDir(level, x, y, z + 1);
77 if (w == id) recalcLockDir(level, x - 1, y, z);
78 if (e == id) recalcLockDir(level, x + 1, y, z);
79}
80
81void ChestTile::setPlacedBy(Level *level, int x, int y, int z, shared_ptr<LivingEntity> by, shared_ptr<ItemInstance> itemInstance)
82{
83 int n = level->getTile(x, y, z - 1); // face = 2
84 int s = level->getTile(x, y, z + 1); // face = 3
85 int w = level->getTile(x - 1, y, z); // face = 4
86 int e = level->getTile(x + 1, y, z); // face = 5
87
88 int facing = 0;
89 int dir = (Mth::floor(by->yRot * 4 / (360) + 0.5)) & 3;
90
91 if (dir == 0) facing = Facing::NORTH;
92 if (dir == 1) facing = Facing::EAST;
93 if (dir == 2) facing = Facing::SOUTH;
94 if (dir == 3) facing = Facing::WEST;
95
96 if (n != id && s != id && w != id && e != id)
97 {
98 level->setData(x, y, z, facing, Tile::UPDATE_ALL);
99 }
100 else
101 {
102 if ((n == id || s == id) && (facing == Facing::WEST || facing == Facing::EAST))
103 {
104 if (n == id) level->setData(x, y, z - 1, facing, Tile::UPDATE_ALL);
105 else level->setData(x, y, z + 1, facing, Tile::UPDATE_ALL);
106 level->setData(x, y, z, facing, Tile::UPDATE_ALL);
107 }
108 if ((w == id || e == id) && (facing == Facing::NORTH || facing == Facing::SOUTH))
109 {
110 if (w == id) level->setData(x - 1, y, z, facing, Tile::UPDATE_ALL);
111 else level->setData(x + 1, y, z, facing, Tile::UPDATE_ALL);
112 level->setData(x, y, z, facing, Tile::UPDATE_ALL);
113 }
114 }
115
116 if (itemInstance->hasCustomHoverName())
117 {
118 dynamic_pointer_cast<ChestTileEntity>( level->getTileEntity(x, y, z))->setCustomName(itemInstance->getHoverName());
119 }
120
121}
122
123void ChestTile::recalcLockDir(Level *level, int x, int y, int z)
124{
125 if (level->isClientSide)
126 {
127 return;
128 }
129
130 int n = level->getTile(x, y, z - 1); // face = 2
131 int s = level->getTile(x, y, z + 1); // face = 3
132 int w = level->getTile(x - 1, y, z); // face = 4
133 int e = level->getTile(x + 1, y, z); // face = 5
134
135 // Long!
136 int lockDir = 4;
137 if (n == id || s == id)
138 {
139 int w2 = level->getTile(x - 1, y, n == id ? z - 1 : z + 1);
140 int e2 = level->getTile(x + 1, y, n == id ? z - 1 : z + 1);
141
142 lockDir = 5;
143
144 int otherDir = -1;
145 if (n == id) otherDir = level->getData(x, y, z - 1);
146 else otherDir = level->getData(x, y, z + 1);
147 if (otherDir == 4) lockDir = 4;
148
149 if ((Tile::solid[w] || Tile::solid[w2]) && !Tile::solid[e] && !Tile::solid[e2]) lockDir = 5;
150 if ((Tile::solid[e] || Tile::solid[e2]) && !Tile::solid[w] && !Tile::solid[w2]) lockDir = 4;
151 }
152 else if (w == id || e == id)
153 {
154 int n2 = level->getTile(w == id ? x - 1 : x + 1, y, z - 1);
155 int s2 = level->getTile(w == id ? x - 1 : x + 1, y, z + 1);
156
157 lockDir = 3;
158 int otherDir = -1;
159 if (w == id) otherDir = level->getData(x - 1, y, z);
160 else otherDir = level->getData(x + 1, y, z);
161 if (otherDir == 2) lockDir = 2;
162
163 if ((Tile::solid[n] || Tile::solid[n2]) && !Tile::solid[s] && !Tile::solid[s2]) lockDir = 3;
164 if ((Tile::solid[s] || Tile::solid[s2]) && !Tile::solid[n] && !Tile::solid[n2]) lockDir = 2;
165 }
166 else
167 {
168 lockDir = 3;
169 if (Tile::solid[n] && !Tile::solid[s]) lockDir = 3;
170 if (Tile::solid[s] && !Tile::solid[n]) lockDir = 2;
171 if (Tile::solid[w] && !Tile::solid[e]) lockDir = 5;
172 if (Tile::solid[e] && !Tile::solid[w]) lockDir = 4;
173 }
174
175 level->setData(x, y, z, lockDir, Tile::UPDATE_ALL);
176}
177
178bool ChestTile::mayPlace(Level *level, int x, int y, int z)
179{
180 int chestCount = 0;
181
182 if (level->getTile(x - 1, y, z) == id) chestCount++;
183 if (level->getTile(x + 1, y, z) == id) chestCount++;
184 if (level->getTile(x, y, z - 1) == id) chestCount++;
185 if (level->getTile(x, y, z + 1) == id) chestCount++;
186
187 if (chestCount > 1) return false;
188
189 if (isFullChest(level, x - 1, y, z)) return false;
190 if (isFullChest(level, x + 1, y, z)) return false;
191 if (isFullChest(level, x, y, z - 1)) return false;
192 if (isFullChest(level, x, y, z + 1)) return false;
193 return true;
194
195}
196
197bool ChestTile::isFullChest(Level *level, int x, int y, int z)
198{
199 if (level->getTile(x, y, z) != id) return false;
200 if (level->getTile(x - 1, y, z) == id) return true;
201 if (level->getTile(x + 1, y, z) == id) return true;
202 if (level->getTile(x, y, z - 1) == id) return true;
203 if (level->getTile(x, y, z + 1) == id) return true;
204 return false;
205}
206
207void ChestTile::neighborChanged(Level *level, int x, int y, int z, int type)
208{
209 BaseEntityTile::neighborChanged(level, x, y, z, type);
210 shared_ptr<ChestTileEntity>(cte) = dynamic_pointer_cast<ChestTileEntity>(level->getTileEntity(x, y, z));
211 if (cte != NULL) cte->clearCache();
212}
213
214void ChestTile::onRemove(Level *level, int x, int y, int z, int id, int data)
215{
216 shared_ptr<Container> container = dynamic_pointer_cast<ChestTileEntity>( level->getTileEntity(x, y, z) );
217 if (container != NULL )
218 {
219 for (unsigned int i = 0; i < container->getContainerSize(); i++)
220 {
221 shared_ptr<ItemInstance> item = container->getItem(i);
222 if (item != NULL)
223 {
224 float xo = random->nextFloat() * 0.8f + 0.1f;
225 float yo = random->nextFloat() * 0.8f + 0.1f;
226 float zo = random->nextFloat() * 0.8f + 0.1f;
227
228 while (item->count > 0)
229 {
230 int count = random->nextInt(21) + 10;
231 if (count > item->count) count = item->count;
232 item->count -= count;
233
234 shared_ptr<ItemInstance> newItem = shared_ptr<ItemInstance>( new ItemInstance(item->id, count, item->getAuxValue()) );
235 newItem->set4JData( item->get4JData() );
236 shared_ptr<ItemEntity> itemEntity = shared_ptr<ItemEntity>(new ItemEntity(level, x + xo, y + yo, z + zo, newItem ) );
237 float pow = 0.05f;
238 itemEntity->xd = (float) random->nextGaussian() * pow;
239 itemEntity->yd = (float) random->nextGaussian() * pow + 0.2f;
240 itemEntity->zd = (float) random->nextGaussian() * pow;
241 if (item->hasTag())
242 {
243 itemEntity->getItem()->setTag((CompoundTag *) item->getTag()->copy());
244 }
245
246 level->addEntity(itemEntity);
247 }
248
249 // 4J Stu - Fix for duplication glitch
250 container->setItem(i,nullptr);
251 }
252 }
253 level->updateNeighbourForOutputSignal(x, y, z, id);
254 }
255 BaseEntityTile::onRemove(level, x, y, z, id, data);
256}
257
258// 4J-PB - Adding a TestUse for tooltip display
259bool ChestTile::TestUse()
260{
261 return true;
262}
263
264// 4J-PB - changing to 1.5 equivalent
265bool ChestTile::use(Level *level, int x, int y, int z, shared_ptr<Player> player, int clickedFace, float clickX, float clickY, float clickZ, bool soundOnly/*=false*/) // 4J added soundOnly param
266{
267 if( soundOnly ) return true;
268
269 if (level->isClientSide)
270 {
271 return true;
272 }
273 shared_ptr<Container> container = getContainer(level, x, y, z);
274
275 if (container != NULL)
276 {
277 player->openContainer(container);
278 }
279
280 return true;
281}
282
283shared_ptr<Container> ChestTile::getContainer(Level *level, int x, int y, int z)
284{
285 shared_ptr<Container> container = dynamic_pointer_cast<ChestTileEntity>( level->getTileEntity(x, y, z) );
286 if (container == NULL) return nullptr;
287
288 if (level->isSolidBlockingTile(x, y + 1, z)) return nullptr;
289 if (isCatSittingOnChest(level,x, y, z)) return nullptr;
290
291 if (level->getTile(x - 1, y, z) == id && (level->isSolidBlockingTile(x - 1, y + 1, z) || isCatSittingOnChest(level, x - 1, y, z))) return nullptr;
292 if (level->getTile(x + 1, y, z) == id && (level->isSolidBlockingTile(x + 1, y + 1, z) || isCatSittingOnChest(level, x + 1, y, z))) return nullptr;
293 if (level->getTile(x, y, z - 1) == id && (level->isSolidBlockingTile(x, y + 1, z - 1) || isCatSittingOnChest(level, x, y, z - 1))) return nullptr;
294 if (level->getTile(x, y, z + 1) == id && (level->isSolidBlockingTile(x, y + 1, z + 1) || isCatSittingOnChest(level, x, y, z + 1))) return nullptr;
295
296 if (level->getTile(x - 1, y, z) == id) container = shared_ptr<Container>( new CompoundContainer(IDS_CHEST_LARGE, dynamic_pointer_cast<ChestTileEntity>( level->getTileEntity(x - 1, y, z) ), container) );
297 if (level->getTile(x + 1, y, z) == id) container = shared_ptr<Container>( new CompoundContainer(IDS_CHEST_LARGE, container, dynamic_pointer_cast<ChestTileEntity>( level->getTileEntity(x + 1, y, z) )) );
298 if (level->getTile(x, y, z - 1) == id) container = shared_ptr<Container>( new CompoundContainer(IDS_CHEST_LARGE, dynamic_pointer_cast<ChestTileEntity>( level->getTileEntity(x, y, z - 1) ), container) );
299 if (level->getTile(x, y, z + 1) == id) container = shared_ptr<Container>( new CompoundContainer(IDS_CHEST_LARGE, container, dynamic_pointer_cast<ChestTileEntity>( level->getTileEntity(x, y, z + 1) )) );
300
301 return container;
302}
303
304shared_ptr<TileEntity> ChestTile::newTileEntity(Level *level)
305{
306 MemSect(50);
307 shared_ptr<TileEntity> retval = shared_ptr<TileEntity>( new ChestTileEntity() );
308 MemSect(0);
309 return retval;
310}
311
312bool ChestTile::isSignalSource()
313{
314 return type == TYPE_TRAP;
315}
316
317int ChestTile::getSignal(LevelSource *level, int x, int y, int z, int dir)
318{
319 if (!isSignalSource()) return Redstone::SIGNAL_NONE;
320
321 int openCount = dynamic_pointer_cast<ChestTileEntity>( level->getTileEntity(x, y, z))->openCount;
322 return Mth::clamp(openCount, Redstone::SIGNAL_NONE, Redstone::SIGNAL_MAX);
323}
324
325int ChestTile::getDirectSignal(LevelSource *level, int x, int y, int z, int dir)
326{
327 if (dir == Facing::UP)
328 {
329 return getSignal(level, x, y, z, dir);
330 }
331 else
332 {
333 return Redstone::SIGNAL_NONE;
334 }
335}
336
337bool ChestTile::isCatSittingOnChest(Level *level, int x, int y, int z)
338{
339 vector<shared_ptr<Entity> > *entities = level->getEntitiesOfClass(typeid(Ocelot), AABB::newTemp(x, y + 1, z, x + 1, y + 2, z + 1));
340 for(AUTO_VAR(it, entities->begin()); it != entities->end(); ++it)
341 {
342 shared_ptr<Ocelot> ocelot = dynamic_pointer_cast<Ocelot>(*it);
343 if(ocelot->isSitting())
344 {
345 delete entities;
346 return true;
347 }
348 }
349 delete entities;
350 return false;
351}
352
353bool ChestTile::hasAnalogOutputSignal()
354{
355 return true;
356}
357
358int ChestTile::getAnalogOutputSignal(Level *level, int x, int y, int z, int dir)
359{
360 return AbstractContainerMenu::getRedstoneSignalFromContainer(getContainer(level, x, y, z));
361}
362
363void ChestTile::registerIcons(IconRegister *iconRegister)
364{
365 // Register wood as the chest's icon, because it's used by the particles
366 // when destroying the chest
367 icon = iconRegister->registerIcon(L"planks_oak");
368}