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
|
#include "stdafx.h"
#include "net.minecraft.world.entity.h"
#include "net.minecraft.world.level.tile.h"
#include "net.minecraft.world.level.h"
#include "net.minecraft.world.damagesource.h"
#include "com.mojang.nbt.h"
#include "FallingTile.h"
// 4J - added for common ctor code
void FallingTile::_init()
{
// 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();
tile = 0;
data = 0;
time = 0;
dropItem = true;
cancelDrop = false;
hurtEntities = false;
fallDamageMax = 40;
fallDamageAmount = 2;
tileData = nullptr;
// 4J Added so that client-side falling tiles can fall through blocks
// This fixes a bug on the host where the tile update from the server comes in before the client-side falling tile
// has reached that level, causing it to stop at one block higher.
m_ignoreVerticalCollisions = level->isClientSide;
}
FallingTile::FallingTile(Level *level) :
Entity( level )
{
_init();
}
FallingTile::FallingTile(Level *level, double x, double y, double z, int tile, int data) : Entity( level )
{
_init();
this->tile = tile;
this->data = data;
blocksBuilding = true;
setSize(0.98f, 0.98f);
heightOffset = bbHeight / 2.0f;
setPos(x, y, z);
xd = 0;
yd = 0;
zd = 0;
xo = x;
yo = y;
zo = z;
// 4J added - without this newly created falling tiles weren't interpolating their render positions correctly
xOld = x;
yOld = y;
zOld = z;
}
FallingTile::~FallingTile()
{
delete tileData;
}
bool FallingTile::makeStepSound()
{
return false;
}
void FallingTile::defineSynchedData()
{
}
bool FallingTile::isPickable()
{
return !removed;
}
void FallingTile::tick()
{
if (tile == 0)
{
remove();
return;
}
xo = x;
yo = y;
zo = z;
time++;
yd -= 0.04f;
move(xd, yd, zd);
xd *= 0.98f;
yd *= 0.98f;
zd *= 0.98f;
if(!level->isClientSide)
{
int xt = Mth::floor(x);
int yt = Mth::floor(y);
int zt = Mth::floor(z);
if(time == 1)
{
if (level->getTile(xt, yt, zt) == tile)
{
level->removeTile(xt, yt, zt);
}
else
{
remove();
return;
}
}
if (onGround)
{
xd *= 0.7f;
zd *= 0.7f;
yd *= -0.5f;
if (level->getTile(xt, yt, zt) != Tile::pistonMovingPiece_Id)
{
remove();
if (!cancelDrop && level->mayPlace(tile, xt, yt, zt, true, 1, nullptr, nullptr) && !HeavyTile::isFree(level, xt, yt - 1, zt) && level->setTileAndData(xt, yt, zt, tile, data, Tile::UPDATE_ALL))
{
HeavyTile *hv = dynamic_cast<HeavyTile *>(Tile::tiles[tile]);
if (hv)
{
hv->onLand(level, xt, yt, zt, data);
}
if (tileData != nullptr && Tile::tiles[tile]->isEntityTile())
{
shared_ptr<TileEntity> tileEntity = level->getTileEntity(xt, yt, zt);
if (tileEntity != nullptr)
{
CompoundTag *swap = new CompoundTag();
tileEntity->save(swap);
vector<Tag *> *allTags = tileData->getAllTags();
if ( allTags )
{
for ( Tag* tag : *allTags )
{
if (tag->getName().compare(L"x") == 0 || tag->getName().compare(L"y") == 0 || tag->getName().compare(L"z") == 0) continue;
swap->put(tag->getName(), tag->copy());
}
delete allTags;
}
tileEntity->load(swap);
tileEntity->setChanged();
}
}
}
else
{
if(dropItem && !cancelDrop) spawnAtLocation(std::make_shared<ItemInstance>(tile, 1, Tile::tiles[tile]->getSpawnResourcesAuxValue(data)), 0);
}
}
}
else if ( (time > 20 * 5 && !level->isClientSide && (yt < 1 || yt > Level::maxBuildHeight)) || (time > 20 * 30))
{
if(dropItem) spawnAtLocation(std::make_shared<ItemInstance>(tile, 1, Tile::tiles[tile]->getSpawnResourcesAuxValue(data)), 0);
remove();
}
}
}
void FallingTile::causeFallDamage(float distance)
{
if (hurtEntities)
{
int dmg = Mth::ceil(distance - 1);
if (dmg > 0)
{
// 4J: Copy vector since it might be modified when we hurt the entities (invalidating our iterator)
vector<shared_ptr<Entity> > *entities = new vector<shared_ptr<Entity> >(*level->getEntities(shared_from_this(), bb));
DamageSource *source = tile == Tile::anvil_Id ? DamageSource::anvil : DamageSource::fallingBlock;
if ( source )
{
for (auto& it : *entities)
{
it->hurt(source, static_cast<float>(std::min<int>(Mth::floor(dmg * fallDamageAmount), fallDamageMax)));
}
delete entities;
}
if (tile == Tile::anvil_Id && random->nextFloat() < 0.05f + (dmg * 0.05))
{
int damage = data >> 2;
int dir = data & 3;
if (++damage > 2)
{
cancelDrop = true;
}
else
{
data = dir | (damage << 2);
}
}
}
}
}
void FallingTile::addAdditonalSaveData(CompoundTag *tag)
{
tag->putByte(L"Tile", static_cast<byte>(tile));
tag->putInt(L"TileID", tile);
tag->putByte(L"Data", static_cast<byte>(data));
tag->putByte(L"Time", static_cast<byte>(time));
tag->putBoolean(L"DropItem", dropItem);
tag->putBoolean(L"HurtEntities", hurtEntities);
tag->putFloat(L"FallHurtAmount", fallDamageAmount);
tag->putInt(L"FallHurtMax", fallDamageMax);
if (tileData != nullptr) tag->putCompound(L"TileEntityData", tileData);
}
void FallingTile::readAdditionalSaveData(CompoundTag *tag)
{
if (tag->contains(L"TileID"))
{
tile = tag->getInt(L"TileID");
}
else
{
tile = tag->getByte(L"Tile") & 0xff;
}
data = tag->getByte(L"Data") & 0xff;
time = tag->getByte(L"Time") & 0xff;
if (tag->contains(L"HurtEntities"))
{
hurtEntities = tag->getBoolean(L"HurtEntities");
fallDamageAmount = tag->getFloat(L"FallHurtAmount");
fallDamageMax = tag->getInt(L"FallHurtMax");
}
else if (tile == Tile::anvil_Id)
{
hurtEntities = true;
}
if (tag->contains(L"DropItem"))
{
dropItem = tag->getBoolean(L"DropItem");
}
if (tag->contains(L"TileEntityData"))
{
tileData = tag->getCompound(L"TileEntityData");
}
if (tile == 0)
{
tile = Tile::sand_Id;
}
}
float FallingTile::getShadowHeightOffs()
{
return 0;
}
Level *FallingTile::getLevel()
{
return level;
}
void FallingTile::setHurtsEntities(bool value)
{
this->hurtEntities = value;
}
bool FallingTile::displayFireAnimation()
{
return false;
}
|