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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
|
#include "stdafx.h"
#include "net.minecraft.world.h"
#include "net.minecraft.world.level.h"
#include "net.minecraft.world.entity.item.h"
#include "net.minecraft.world.entity.animal.h"
#include "net.minecraft.world.entity.player.h"
#include "net.minecraft.world.item.h"
#include "net.minecraft.world.level.redstone.h"
#include "net.minecraft.world.level.tile.entity.h"
#include "net.minecraft.world.phys.h"
#include "ChestTile.h"
#include "Facing.h"
ChestTile::ChestTile(int id, int type) : BaseEntityTile(id, Material::wood, isSolidRender() )
{
random = new Random();
this->type = type;
setShape(1 / 16.0f, 0, 1 / 16.0f, 15 / 16.0f, 14 / 16.0f, 15 / 16.0f);
}
ChestTile::~ChestTile()
{
delete random;
}
bool ChestTile::isSolidRender(bool isServerLevel)
{
return false;
}
bool ChestTile::isCubeShaped()
{
return false;
}
int ChestTile::getRenderShape()
{
return Tile::SHAPE_ENTITYTILE_ANIMATED;
}
void ChestTile::updateShape(LevelSource *level, int x, int y, int z, int forceData, shared_ptr<TileEntity> forceEntity)
{
if (level->getTile(x, y, z - 1) == id)
{
setShape(1 / 16.0f, 0, 0, 15 / 16.0f, 14 / 16.0f, 15 / 16.0f);
}
else if (level->getTile(x, y, z + 1) == id)
{
setShape(1 / 16.0f, 0, 1 / 16.0f, 15 / 16.0f, 14 / 16.0f, 1);
}
else if (level->getTile(x - 1, y, z) == id)
{
setShape(0, 0, 1 / 16.0f, 15 / 16.0f, 14 / 16.0f, 15 / 16.0f);
}
else if (level->getTile(x + 1, y, z) == id)
{
setShape(1 / 16.0f, 0, 1 / 16.0f, 1, 14 / 16.0f, 15 / 16.0f);
}
else
{
setShape(1 / 16.0f, 0, 1 / 16.0f, 15 / 16.0f, 14 / 16.0f, 15 / 16.0f);
}
}
void ChestTile::onPlace(Level *level, int x, int y, int z)
{
BaseEntityTile::onPlace(level, x, y, z);
recalcLockDir(level, x, y, z);
int n = level->getTile(x, y, z - 1); // face = 2
int s = level->getTile(x, y, z + 1); // face = 3
int w = level->getTile(x - 1, y, z); // face = 4
int e = level->getTile(x + 1, y, z); // face = 5
if (n == id) recalcLockDir(level, x, y, z - 1);
if (s == id) recalcLockDir(level, x, y, z + 1);
if (w == id) recalcLockDir(level, x - 1, y, z);
if (e == id) recalcLockDir(level, x + 1, y, z);
}
void ChestTile::setPlacedBy(Level *level, int x, int y, int z, shared_ptr<LivingEntity> by, shared_ptr<ItemInstance> itemInstance)
{
int n = level->getTile(x, y, z - 1); // face = 2
int s = level->getTile(x, y, z + 1); // face = 3
int w = level->getTile(x - 1, y, z); // face = 4
int e = level->getTile(x + 1, y, z); // face = 5
int facing = 0;
int dir = (Mth::floor(by->yRot * 4 / (360) + 0.5)) & 3;
if (dir == 0) facing = Facing::NORTH;
if (dir == 1) facing = Facing::EAST;
if (dir == 2) facing = Facing::SOUTH;
if (dir == 3) facing = Facing::WEST;
if (n != id && s != id && w != id && e != id)
{
level->setData(x, y, z, facing, Tile::UPDATE_ALL);
}
else
{
if ((n == id || s == id) && (facing == Facing::WEST || facing == Facing::EAST))
{
if (n == id) level->setData(x, y, z - 1, facing, Tile::UPDATE_ALL);
else level->setData(x, y, z + 1, facing, Tile::UPDATE_ALL);
level->setData(x, y, z, facing, Tile::UPDATE_ALL);
}
if ((w == id || e == id) && (facing == Facing::NORTH || facing == Facing::SOUTH))
{
if (w == id) level->setData(x - 1, y, z, facing, Tile::UPDATE_ALL);
else level->setData(x + 1, y, z, facing, Tile::UPDATE_ALL);
level->setData(x, y, z, facing, Tile::UPDATE_ALL);
}
}
if (itemInstance->hasCustomHoverName())
{
dynamic_pointer_cast<ChestTileEntity>( level->getTileEntity(x, y, z))->setCustomName(itemInstance->getHoverName());
}
}
void ChestTile::recalcLockDir(Level *level, int x, int y, int z)
{
if (level->isClientSide)
{
return;
}
int n = level->getTile(x, y, z - 1); // face = 2
int s = level->getTile(x, y, z + 1); // face = 3
int w = level->getTile(x - 1, y, z); // face = 4
int e = level->getTile(x + 1, y, z); // face = 5
// Long!
int lockDir = 4;
if (n == id || s == id)
{
int w2 = level->getTile(x - 1, y, n == id ? z - 1 : z + 1);
int e2 = level->getTile(x + 1, y, n == id ? z - 1 : z + 1);
lockDir = 5;
int otherDir = -1;
if (n == id) otherDir = level->getData(x, y, z - 1);
else otherDir = level->getData(x, y, z + 1);
if (otherDir == 4) lockDir = 4;
if ((Tile::solid[w] || Tile::solid[w2]) && !Tile::solid[e] && !Tile::solid[e2]) lockDir = 5;
if ((Tile::solid[e] || Tile::solid[e2]) && !Tile::solid[w] && !Tile::solid[w2]) lockDir = 4;
}
else if (w == id || e == id)
{
int n2 = level->getTile(w == id ? x - 1 : x + 1, y, z - 1);
int s2 = level->getTile(w == id ? x - 1 : x + 1, y, z + 1);
lockDir = 3;
int otherDir = -1;
if (w == id) otherDir = level->getData(x - 1, y, z);
else otherDir = level->getData(x + 1, y, z);
if (otherDir == 2) lockDir = 2;
if ((Tile::solid[n] || Tile::solid[n2]) && !Tile::solid[s] && !Tile::solid[s2]) lockDir = 3;
if ((Tile::solid[s] || Tile::solid[s2]) && !Tile::solid[n] && !Tile::solid[n2]) lockDir = 2;
}
else
{
lockDir = 3;
if (Tile::solid[n] && !Tile::solid[s]) lockDir = 3;
if (Tile::solid[s] && !Tile::solid[n]) lockDir = 2;
if (Tile::solid[w] && !Tile::solid[e]) lockDir = 5;
if (Tile::solid[e] && !Tile::solid[w]) lockDir = 4;
}
level->setData(x, y, z, lockDir, Tile::UPDATE_ALL);
}
bool ChestTile::mayPlace(Level *level, int x, int y, int z)
{
int chestCount = 0;
if (level->getTile(x - 1, y, z) == id) chestCount++;
if (level->getTile(x + 1, y, z) == id) chestCount++;
if (level->getTile(x, y, z - 1) == id) chestCount++;
if (level->getTile(x, y, z + 1) == id) chestCount++;
if (chestCount > 1) return false;
if (isFullChest(level, x - 1, y, z)) return false;
if (isFullChest(level, x + 1, y, z)) return false;
if (isFullChest(level, x, y, z - 1)) return false;
if (isFullChest(level, x, y, z + 1)) return false;
return true;
}
bool ChestTile::isFullChest(Level *level, int x, int y, int z)
{
if (level->getTile(x, y, z) != id) return false;
if (level->getTile(x - 1, y, z) == id) return true;
if (level->getTile(x + 1, y, z) == id) return true;
if (level->getTile(x, y, z - 1) == id) return true;
if (level->getTile(x, y, z + 1) == id) return true;
return false;
}
void ChestTile::neighborChanged(Level *level, int x, int y, int z, int type)
{
BaseEntityTile::neighborChanged(level, x, y, z, type);
shared_ptr<ChestTileEntity>(cte) = dynamic_pointer_cast<ChestTileEntity>(level->getTileEntity(x, y, z));
if (cte != NULL) cte->clearCache();
}
void ChestTile::onRemove(Level *level, int x, int y, int z, int id, int data)
{
shared_ptr<Container> container = dynamic_pointer_cast<ChestTileEntity>( level->getTileEntity(x, y, z) );
if (container != NULL )
{
for (unsigned int i = 0; i < container->getContainerSize(); i++)
{
shared_ptr<ItemInstance> item = container->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<ItemInstance> newItem = shared_ptr<ItemInstance>( new ItemInstance(item->id, count, item->getAuxValue()) );
newItem->set4JData( item->get4JData() );
shared_ptr<ItemEntity> itemEntity = shared_ptr<ItemEntity>(new ItemEntity(level, x + xo, y + yo, z + zo, newItem ) );
float pow = 0.05f;
itemEntity->xd = (float) random->nextGaussian() * pow;
itemEntity->yd = (float) random->nextGaussian() * pow + 0.2f;
itemEntity->zd = (float) random->nextGaussian() * pow;
if (item->hasTag())
{
itemEntity->getItem()->setTag((CompoundTag *) item->getTag()->copy());
}
level->addEntity(itemEntity);
}
// 4J Stu - Fix for duplication glitch
container->setItem(i,nullptr);
}
}
level->updateNeighbourForOutputSignal(x, y, z, id);
}
BaseEntityTile::onRemove(level, x, y, z, id, data);
}
// 4J-PB - Adding a TestUse for tooltip display
bool ChestTile::TestUse()
{
return true;
}
// 4J-PB - changing to 1.5 equivalent
bool 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
{
if( soundOnly ) return true;
if (level->isClientSide)
{
return true;
}
shared_ptr<Container> container = getContainer(level, x, y, z);
if (container != NULL)
{
player->openContainer(container);
}
return true;
}
shared_ptr<Container> ChestTile::getContainer(Level *level, int x, int y, int z)
{
shared_ptr<Container> container = dynamic_pointer_cast<ChestTileEntity>( level->getTileEntity(x, y, z) );
if (container == NULL) return nullptr;
if (level->isSolidBlockingTile(x, y + 1, z)) return nullptr;
if (isCatSittingOnChest(level,x, y, z)) return nullptr;
if (level->getTile(x - 1, y, z) == id && (level->isSolidBlockingTile(x - 1, y + 1, z) || isCatSittingOnChest(level, x - 1, y, z))) return nullptr;
if (level->getTile(x + 1, y, z) == id && (level->isSolidBlockingTile(x + 1, y + 1, z) || isCatSittingOnChest(level, x + 1, y, z))) return nullptr;
if (level->getTile(x, y, z - 1) == id && (level->isSolidBlockingTile(x, y + 1, z - 1) || isCatSittingOnChest(level, x, y, z - 1))) return nullptr;
if (level->getTile(x, y, z + 1) == id && (level->isSolidBlockingTile(x, y + 1, z + 1) || isCatSittingOnChest(level, x, y, z + 1))) return nullptr;
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) );
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) )) );
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) );
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) )) );
return container;
}
shared_ptr<TileEntity> ChestTile::newTileEntity(Level *level)
{
MemSect(50);
shared_ptr<TileEntity> retval = shared_ptr<TileEntity>( new ChestTileEntity() );
MemSect(0);
return retval;
}
bool ChestTile::isSignalSource()
{
return type == TYPE_TRAP;
}
int ChestTile::getSignal(LevelSource *level, int x, int y, int z, int dir)
{
if (!isSignalSource()) return Redstone::SIGNAL_NONE;
int openCount = dynamic_pointer_cast<ChestTileEntity>( level->getTileEntity(x, y, z))->openCount;
return Mth::clamp(openCount, Redstone::SIGNAL_NONE, Redstone::SIGNAL_MAX);
}
int ChestTile::getDirectSignal(LevelSource *level, int x, int y, int z, int dir)
{
if (dir == Facing::UP)
{
return getSignal(level, x, y, z, dir);
}
else
{
return Redstone::SIGNAL_NONE;
}
}
bool ChestTile::isCatSittingOnChest(Level *level, int x, int y, int z)
{
vector<shared_ptr<Entity> > *entities = level->getEntitiesOfClass(typeid(Ocelot), AABB::newTemp(x, y + 1, z, x + 1, y + 2, z + 1));
if ( entities )
{
for (auto& it : *entities)
{
shared_ptr<Ocelot> ocelot = dynamic_pointer_cast<Ocelot>(it);
if ( ocelot && ocelot->isSitting())
{
delete entities;
return true;
}
}
delete entities;
}
return false;
}
bool ChestTile::hasAnalogOutputSignal()
{
return true;
}
int ChestTile::getAnalogOutputSignal(Level *level, int x, int y, int z, int dir)
{
return AbstractContainerMenu::getRedstoneSignalFromContainer(getContainer(level, x, y, z));
}
void ChestTile::registerIcons(IconRegister *iconRegister)
{
// Register wood as the chest's icon, because it's used by the particles
// when destroying the chest
icon = iconRegister->registerIcon(L"planks_oak");
}
|