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 "Dimension.h"
3#include "net.minecraft.h"
4#include "net.minecraft.world.entity.player.h"
5#include "net.minecraft.world.item.h"
6#include "net.minecraft.world.level.h"
7#include "net.minecraft.world.h"
8#include "BedTile.h"
9
10int BedTile::HEAD_DIRECTION_OFFSETS[4][2] =
11{
12 { 0, 1 }, { -1, 0 }, { 0, -1 }, { 1, 0 }
13};
14
15BedTile::BedTile(int id) : DirectionalTile(id, Material::cloth, isSolidRender())
16{
17 setShape();
18
19 iconEnd = NULL;
20 iconSide = NULL;
21 iconTop = NULL;
22}
23
24// 4J Added override
25void BedTile::updateDefaultShape()
26{
27 setShape();
28}
29
30// 4J-PB - Adding a TestUse for tooltip display
31bool BedTile::TestUse(Level *level, int x, int y, int z, shared_ptr<Player> player)
32{
33 //if (level->isClientSide) return true;
34
35 int data = level->getData(x, y, z);
36
37 if (!BedTile::isHeadPiece(data))
38 {
39 // fetch head piece instead
40 int direction = getDirection(data);
41 x += HEAD_DIRECTION_OFFSETS[direction][0];
42 z += HEAD_DIRECTION_OFFSETS[direction][1];
43 if (level->getTile(x, y, z) != id)
44 {
45 return true;
46 }
47 data = level->getData(x, y, z);
48 }
49
50 if (!level->dimension->mayRespawn())
51 {
52 return false;
53 }
54 if (BedTile::isOccupied(data))
55 {
56 return false;
57 }
58
59 Player::BedSleepingResult result = player->startSleepInBed(x, y, z, true); // true to just test the start sleep
60 if (result == Player::OK)
61 {
62 return true;
63 }
64
65 return false;
66}
67
68bool BedTile::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
69{
70 if( soundOnly) return false;
71 if (level->isClientSide) return true;
72
73 int data = level->getData(x, y, z);
74
75 if (!isHeadPiece(data))
76 {
77 // fetch head piece instead
78 int direction = getDirection(data);
79 x += HEAD_DIRECTION_OFFSETS[direction][0];
80 z += HEAD_DIRECTION_OFFSETS[direction][1];
81 if (level->getTile(x, y, z) != id)
82 {
83 return true;
84 }
85 data = level->getData(x, y, z);
86 }
87
88 if (!level->dimension->mayRespawn() || level->getBiome(x, z) == Biome::hell)
89 {
90 double xc = x + 0.5;
91 double yc = y + 0.5;
92 double zc = z + 0.5;
93 level->removeTile(x, y, z);
94 int direction = getDirection(data);
95 x += HEAD_DIRECTION_OFFSETS[direction][0];
96 z += HEAD_DIRECTION_OFFSETS[direction][1];
97 if (level->getTile(x, y, z) == id)
98 {
99 level->removeTile(x, y, z);
100 xc = (xc + x + 0.5) / 2;
101 yc = (yc + y + 0.5) / 2;
102 zc = (zc + z + 0.5) / 2;
103 }
104 level->explode(nullptr, x + 0.5f, y + 0.5f, z + 0.5f, 5, true, true);
105 return true;
106 }
107
108 if (isOccupied(data))
109 {
110 shared_ptr<Player> sleepingPlayer = nullptr;
111 AUTO_VAR(itEnd, level->players.end());
112 for (AUTO_VAR(it, level->players.begin()); it != itEnd; it++ )
113 {
114 shared_ptr<Player> p = *it;
115 if (p->isSleeping())
116 {
117 Pos pos = p->bedPosition;
118 if (pos.x == x && pos.y == y && pos.z == z)
119 {
120 sleepingPlayer = p;
121 }
122 }
123 }
124
125 if (sleepingPlayer == NULL)
126 {
127 setOccupied(level, x, y, z, false);
128 }
129 else
130 {
131 player->displayClientMessage(IDS_TILE_BED_OCCUPIED );
132
133 return true;
134 }
135 }
136
137 Player::BedSleepingResult result = player->startSleepInBed(x, y, z);
138 if (result == Player::OK)
139 {
140 setOccupied(level, x, y, z, true);
141 // 4J-PB added
142 // are there multiple players in the same world as us?
143 if(level->AllPlayersAreSleeping()==false)
144 {
145 player->displayClientMessage(IDS_TILE_BED_PLAYERSLEEP);
146 }
147 return true;
148 }
149
150 if (result == Player::NOT_POSSIBLE_NOW)
151 {
152 player->displayClientMessage(IDS_TILE_BED_NO_SLEEP);
153 }
154 else if (result == Player::NOT_SAFE)
155 {
156 player->displayClientMessage(IDS_TILE_BED_NOTSAFE);
157 }
158
159 return true;
160}
161
162Icon *BedTile::getTexture(int face, int data)
163{
164 if (face == Facing::DOWN)
165 {
166 return Tile::wood->getTexture(face);
167 }
168
169 int direction = getDirection(data);
170 int tileFacing = Direction::RELATIVE_DIRECTION_FACING[direction][face];
171
172 int part = isHeadPiece(data) ? PART_HEAD : PART_FOOT;
173
174 if ((part == PART_HEAD && tileFacing == Facing::NORTH) || (part == PART_FOOT && tileFacing == Facing::SOUTH))
175 {
176 return iconEnd[part];
177 }
178 if (tileFacing == Facing::EAST || tileFacing == Facing::WEST)
179 {
180 return iconSide[part];
181 }
182 return iconTop[part];
183}
184
185void BedTile::registerIcons(IconRegister *iconRegister)
186{
187 iconTop = new Icon *[2];
188 iconTop[0] = iconRegister->registerIcon(L"bed_feet_top");
189 iconTop[1] = iconRegister->registerIcon(L"bed_head_top");
190
191 iconEnd = new Icon *[2];
192 iconEnd[0] = iconRegister->registerIcon(L"bed_feet_end");
193 iconEnd[1] = iconRegister->registerIcon(L"bed_head_end");
194
195 iconSide = new Icon *[2];
196 iconSide[0] = iconRegister->registerIcon(L"bed_feet_side");
197 iconSide[1] = iconRegister->registerIcon(L"bed_head_side");
198}
199
200int BedTile::getRenderShape()
201{
202 return Tile::SHAPE_BED;
203}
204
205bool BedTile::isCubeShaped()
206{
207 return false;
208}
209
210bool BedTile::isSolidRender(bool isServerLevel)
211{
212 return false;
213}
214
215void BedTile::updateShape(LevelSource *level, int x, int y, int z, int forceData, shared_ptr<TileEntity> forceEntity) // 4J added forceData, forceEntity param
216{
217 setShape();
218}
219
220void BedTile::neighborChanged(Level *level, int x, int y, int z, int type)
221{
222 int data = level->getData(x, y, z);
223 int direction = getDirection(data);
224
225 if (isHeadPiece(data))
226 {
227 if (level->getTile(x - HEAD_DIRECTION_OFFSETS[direction][0], y, z - HEAD_DIRECTION_OFFSETS[direction][1]) != id)
228 {
229 level->removeTile(x, y, z);
230 }
231 } else
232 {
233 if (level->getTile(x + HEAD_DIRECTION_OFFSETS[direction][0], y, z + HEAD_DIRECTION_OFFSETS[direction][1]) != id)
234 {
235 level->removeTile(x, y, z);
236 if (!level->isClientSide)
237 {
238 Tile::spawnResources(level, x, y, z, data, 0); // 4J - had to add Tile:: here for C++ since this class doesn't have this overloaded method itself
239 }
240 }
241 }
242}
243
244int BedTile::getResource(int data, Random *random, int playerBonusLevel)
245{
246 if (isHeadPiece(data))
247 {
248 return 0;
249 }
250 return Item::bed->id;
251}
252
253void BedTile::setShape()
254{
255 Tile::setShape(0, 0, 0, 1, 9 / 16.0f, 1);
256}
257
258bool BedTile::isHeadPiece(int data)
259{
260 return (data & HEAD_PIECE_DATA) != 0;
261}
262
263bool BedTile::isOccupied(int data)
264{
265 return (data & OCCUPIED_DATA) != 0;
266}
267
268void BedTile::setOccupied(Level *level, int x, int y, int z, bool occupied)
269{
270 int data = level->getData(x, y, z);
271 if (occupied)
272 {
273 data = data | OCCUPIED_DATA;
274 } else
275 {
276 data = data & ~OCCUPIED_DATA;
277 }
278 level->setData(x, y, z, data, Tile::UPDATE_NONE);
279}
280
281Pos *BedTile::findStandUpPosition(Level *level, int x, int y, int z, int skipCount)
282{
283 int data = level->getData(x, y, z);
284 int direction = DirectionalTile::getDirection(data);
285
286 // try to find a clear location near the bed
287 for (int step = 0; step <= 1; step++)
288 {
289 int startX = x - HEAD_DIRECTION_OFFSETS[direction][0] * step - 1;
290 int startZ = z - HEAD_DIRECTION_OFFSETS[direction][1] * step - 1;
291 int endX = startX + 2;
292 int endZ = startZ + 2;
293
294 for (int standX = startX; standX <= endX; standX++)
295 {
296 for (int standZ = startZ; standZ <= endZ; standZ++)
297 {
298 // 4J Stu - Changed to check isSolidBlockingTile rather than isEmpty for the blocks that we wish to place the player
299 // This allows the player to spawn in blocks with snow, grass etc
300 if (level->isTopSolidBlocking(standX, y - 1, standZ) &&
301 !level->getMaterial(standX, y, standZ)->isSolidBlocking() &&
302 !level->getMaterial(standX, y + 1, standZ)->isSolidBlocking() )
303 {
304 if (skipCount > 0) {
305 skipCount--;
306 continue;
307 }
308 return new Pos(standX, y, standZ);
309 }
310 }
311 }
312 }
313
314 return NULL;
315}
316
317void BedTile::spawnResources(Level *level, int x, int y, int z, int data, float odds, int playerBonus)
318{
319 if (!isHeadPiece(data))
320 {
321 Tile::spawnResources(level, x, y, z, data, odds, 0);
322 }
323}
324
325int BedTile::getPistonPushReaction()
326{
327 return Material::PUSH_DESTROY;
328}
329
330int BedTile::cloneTileId(Level *level, int x, int y, int z)
331{
332 return Item::bed_Id;
333}
334
335void BedTile::playerWillDestroy(Level *level, int x, int y, int z, int data, shared_ptr<Player> player)
336{
337 if (player->abilities.instabuild)
338 {
339 if (isHeadPiece(data))
340 {
341 int direction = getDirection(data);
342 x -= HEAD_DIRECTION_OFFSETS[direction][0];
343 z -= HEAD_DIRECTION_OFFSETS[direction][1];
344 if (level->getTile(x, y, z) == id)
345 {
346 level->removeTile(x, y, z);
347 }
348 }
349 }
350}