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 "..\Minecraft.Client\Minecraft.h"
3#include "LeafTile.h"
4#include "net.minecraft.world.level.h"
5#include "net.minecraft.world.level.biome.h"
6#include "net.minecraft.world.item.h"
7#include "net.minecraft.stats.h"
8#include "net.minecraft.world.h"
9
10const unsigned int LeafTile::LEAF_NAMES[LEAF_NAMES_LENGTH] = { IDS_TILE_LEAVES_OAK,
11 IDS_TILE_LEAVES_SPRUCE,
12 IDS_TILE_LEAVES_BIRCH,
13 IDS_TILE_LEAVES_JUNGLE,
14};
15
16const wstring LeafTile::TEXTURES[2][4] = { {L"leaves", L"leaves_spruce", L"leaves", L"leaves_jungle"}, {L"leaves_opaque", L"leaves_spruce_opaque", L"leaves_opaque", L"leaves_jungle_opaque"},};
17
18LeafTile::LeafTile(int id) : TransparentTile(id, Material::leaves, false, isSolidRender())
19{
20 checkBuffer = NULL;
21 fancyTextureSet = 0;
22 setTicking(true);
23}
24
25LeafTile::~LeafTile()
26{
27 delete [] checkBuffer;
28}
29
30int LeafTile::getColor() const
31{
32 // 4J Stu - Not using this any more
33 //double temp = 0.5;
34 //double rain = 1.0;
35
36 //return FoliageColor::get(temp, rain);
37
38 return Minecraft::GetInstance()->getColourTable()->getColor( eMinecraftColour_Foliage_Common );
39}
40
41int LeafTile::getColor(int data)
42{
43 if ((data & LEAF_TYPE_MASK) == EVERGREEN_LEAF)
44 {
45 return FoliageColor::getEvergreenColor();
46 }
47 if ((data & LEAF_TYPE_MASK) == BIRCH_LEAF)
48 {
49 return FoliageColor::getBirchColor();
50 }
51
52 return FoliageColor::getDefaultColor();
53}
54
55int LeafTile::getColor(LevelSource *level, int x, int y, int z)
56{
57 return getColor(level, x, y, z, level->getData(x, y, z) );
58}
59
60// 4J - changed interface to have data passed in, and put existing interface as wrapper above
61int LeafTile::getColor(LevelSource *level, int x, int y, int z, int data)
62{
63 if ((data & LEAF_TYPE_MASK) == EVERGREEN_LEAF)
64 {
65 return FoliageColor::getEvergreenColor();
66 }
67 if ((data & LEAF_TYPE_MASK) == BIRCH_LEAF)
68 {
69 return FoliageColor::getBirchColor();
70 }
71
72 int totalRed = 0;
73 int totalGreen = 0;
74 int totalBlue = 0;
75
76 for (int oz = -1; oz <= 1; oz++)
77 {
78 for (int ox = -1; ox <= 1; ox++)
79 {
80 int foliageColor = level->getBiome(x + ox, z + oz)->getFolageColor();
81
82 totalRed += (foliageColor & 0xff0000) >> 16;
83 totalGreen += (foliageColor & 0xff00) >> 8;
84 totalBlue += (foliageColor & 0xff);
85 }
86 }
87
88 return (((totalRed / 9) & 0xFF) << 16) | (((totalGreen / 9) & 0xFF) << 8) | (((totalBlue / 9) & 0xFF));
89}
90
91void LeafTile::onRemove(Level *level, int x, int y, int z, int id, int data)
92{
93 int r = 1;
94 int r2 = r + 1;
95
96 if (level->hasChunksAt(x - r2, y - r2, z - r2, x + r2, y + r2, z + r2))
97 {
98 for (int xo = -r; xo <= r; xo++)
99 for (int yo = -r; yo <= r; yo++)
100 for (int zo = -r; zo <= r; zo++)
101 {
102 int t = level->getTile(x + xo, y + yo, z + zo);
103 if (t == Tile::leaves_Id)
104 {
105 int currentData = level->getData(x + xo, y + yo, z + zo);
106 level->setData(x + xo, y + yo, z + zo, currentData | UPDATE_LEAF_BIT, Tile::UPDATE_NONE);
107 }
108 }
109 }
110
111}
112
113void LeafTile::tick(Level *level, int x, int y, int z, Random *random)
114{
115 if (level->isClientSide) return;
116
117 int currentData = level->getData(x, y, z);
118 if ((currentData & UPDATE_LEAF_BIT) != 0 && (currentData & PERSISTENT_LEAF_BIT) == 0)
119 {
120 int r = REQUIRED_WOOD_RANGE;
121 int r2 = r + 1;
122
123 int W = 32;
124 int WW = W * W;
125 int WO = W / 2;
126 if (checkBuffer == NULL)
127 {
128 checkBuffer = new int[W * W * W];
129 }
130
131 if (level->hasChunksAt(x - r2, y - r2, z - r2, x + r2, y + r2, z + r2))
132 {
133 // 4J Stu - Assuming we remain in the same chunk, getTile accesses an array that varies least by y
134 // Changing the ordering here to loop by y last
135 for (int xo = -r; xo <= r; xo++)
136 for (int zo = -r; zo <= r; zo++)
137 for (int yo = -r; yo <= r; yo++)
138 {
139 int t = level->getTile(x + xo, y + yo, z + zo);
140 if (t == Tile::treeTrunk_Id)
141 {
142 checkBuffer[(xo + WO) * WW + (yo + WO) * W + (zo + WO)] = 0;
143 }
144 else if (t == Tile::leaves_Id)
145 {
146 checkBuffer[(xo + WO) * WW + (yo + WO) * W + (zo + WO)] = -2;
147 }
148 else
149 {
150 checkBuffer[(xo + WO) * WW + (yo + WO) * W + (zo + WO)] = -1;
151 }
152 }
153 for (int i = 1; i <= REQUIRED_WOOD_RANGE; i++)
154 {
155 for (int xo = -r; xo <= r; xo++)
156 for (int yo = -r; yo <= r; yo++)
157 for (int zo = -r; zo <= r; zo++)
158 {
159 if (checkBuffer[(xo + WO) * WW + (yo + WO) * W + (zo + WO)] == i - 1)
160 {
161 if (checkBuffer[(xo + WO - 1) * WW + (yo + WO) * W + (zo + WO)] == -2)
162 {
163 checkBuffer[(xo + WO - 1) * WW + (yo + WO) * W + (zo + WO)] = i;
164 }
165 if (checkBuffer[(xo + WO + 1) * WW + (yo + WO) * W + (zo + WO)] == -2)
166 {
167 checkBuffer[(xo + WO + 1) * WW + (yo + WO) * W + (zo + WO)] = i;
168 }
169 if (checkBuffer[(xo + WO) * WW + (yo + WO - 1) * W + (zo + WO)] == -2)
170 {
171 checkBuffer[(xo + WO) * WW + (yo + WO - 1) * W + (zo + WO)] = i;
172 }
173 if (checkBuffer[(xo + WO) * WW + (yo + WO + 1) * W + (zo + WO)] == -2)
174 {
175 checkBuffer[(xo + WO) * WW + (yo + WO + 1) * W + (zo + WO)] = i;
176 }
177 if (checkBuffer[(xo + WO) * WW + (yo + WO) * W + (zo + WO - 1)] == -2)
178 {
179 checkBuffer[(xo + WO) * WW + (yo + WO) * W + (zo + WO - 1)] = i;
180 }
181 if (checkBuffer[(xo + WO) * WW + (yo + WO) * W + (zo + WO + 1)] == -2)
182 {
183 checkBuffer[(xo + WO) * WW + (yo + WO) * W + (zo + WO + 1)] = i;
184 }
185 }
186 }
187 }
188 }
189
190 int mid = checkBuffer[(WO) * WW + (WO) * W + (WO)];
191 if (mid >= 0)
192 {
193 level->setData(x, y, z, currentData & ~UPDATE_LEAF_BIT, Tile::UPDATE_NONE);
194 }
195 else
196 {
197 die(level, x, y, z);
198 }
199 }
200
201}
202
203void LeafTile::animateTick(Level *level, int x, int y, int z, Random *random)
204{
205 if (level->isRainingAt(x, y + 1, z) && !level->isTopSolidBlocking(x, y - 1, z) && random->nextInt(15) == 1)
206 {
207 double xx = x + random->nextFloat();
208 double yy = y - 0.05;
209 double zz = z + random->nextFloat();
210
211 level->addParticle(eParticleType_dripWater, xx, yy, zz, 0, 0, 0);
212 }
213}
214
215void LeafTile::die(Level *level, int x, int y, int z)
216{
217 Tile::spawnResources(level, x, y, z, level->getData(x, y, z), 0);
218 level->removeTile(x, y, z);
219}
220
221int LeafTile::getResourceCount(Random *random)
222{
223 return random->nextInt(20) == 0 ? 1 : 0;
224}
225
226int LeafTile::getResource(int data, Random *random, int playerBonusLevel)
227{
228 return Tile::sapling_Id;
229}
230
231// 4J DCR: Brought forward from 1.2
232void LeafTile::spawnResources(Level *level, int x, int y, int z, int data, float odds, int playerBonusLevel)
233{
234 if (!level->isClientSide)
235 {
236 int chance = 20;
237 if ((data & LEAF_TYPE_MASK) == JUNGLE_LEAF)
238 {
239 chance = 40;
240 }
241 if (playerBonusLevel > 0)
242 {
243 chance -= 2 << playerBonusLevel;
244 if (chance < 10)
245 {
246 chance = 10;
247 }
248 }
249 if (level->random->nextInt(chance) == 0)
250 {
251 int type = getResource(data, level->random,playerBonusLevel);
252 popResource(level, x, y, z, shared_ptr<ItemInstance>( new ItemInstance(type, 1, getSpawnResourcesAuxValue(data))));
253 }
254
255 chance = 200;
256 if (playerBonusLevel > 0)
257 {
258 chance -= 10 << playerBonusLevel;
259 if (chance < 40)
260 {
261 chance = 40;
262 }
263 }
264 if ((data & LEAF_TYPE_MASK) == NORMAL_LEAF && level->random->nextInt(chance) == 0)
265 {
266 popResource(level, x, y, z, shared_ptr<ItemInstance>(new ItemInstance(Item::apple_Id, 1, 0)));
267 }
268 }
269}
270
271void LeafTile::playerDestroy(Level *level, shared_ptr<Player> player, int x, int y, int z, int data)
272{
273 if (!level->isClientSide && player->getSelectedItem() != NULL && player->getSelectedItem()->id == Item::shears->id)
274 {
275 player->awardStat(
276 GenericStats::blocksMined(id),
277 GenericStats::param_blocksMined(id,data,1)
278 );
279
280 // drop leaf block instead of sapling
281 popResource(level, x, y, z, shared_ptr<ItemInstance>(new ItemInstance(Tile::leaves_Id, 1, data & LEAF_TYPE_MASK)));
282 }
283 else
284 {
285 TransparentTile::playerDestroy(level, player, x, y, z, data);
286 }
287}
288
289int LeafTile::getSpawnResourcesAuxValue(int data)
290{
291 return data & LEAF_TYPE_MASK;
292}
293
294bool LeafTile::isSolidRender(bool isServerLevel)
295{
296 // 4J Stu - The server level shouldn't care how the tile is rendered!
297 // Fix for #9407 - Gameplay: Destroying a block of snow on top of trees, removes any adjacent snow.
298 if(isServerLevel) return true;
299 return !allowSame;
300}
301
302Icon *LeafTile::getTexture(int face, int data)
303{
304 if ((data & LEAF_TYPE_MASK) == EVERGREEN_LEAF)
305 {
306 return icons[fancyTextureSet][EVERGREEN_LEAF];
307 }
308 if ((data & LEAF_TYPE_MASK) == JUNGLE_LEAF)
309 {
310 return icons[fancyTextureSet][JUNGLE_LEAF];
311 }
312 if ((data & LEAF_TYPE_MASK) == BIRCH_LEAF)
313 {
314 return icons[fancyTextureSet][BIRCH_LEAF];
315 }
316 return icons[fancyTextureSet][0];
317}
318
319void LeafTile::setFancy(bool fancyGraphics)
320{
321 allowSame = fancyGraphics;
322 fancyTextureSet = (fancyGraphics ? 0 : 1);
323}
324
325shared_ptr<ItemInstance> LeafTile::getSilkTouchItemInstance(int data)
326{
327 return shared_ptr<ItemInstance>( new ItemInstance(id, 1, data & LEAF_TYPE_MASK) );
328}
329
330void LeafTile::stepOn(Level *level, int x, int y, int z, shared_ptr<Entity> entity)
331{
332 TransparentTile::stepOn(level, x, y, z, entity);
333}
334
335bool LeafTile::shouldTileTick(Level *level, int x,int y,int z)
336{
337 int currentData = level->getData(x, y, z);
338 return (currentData & UPDATE_LEAF_BIT) != 0;
339}
340
341unsigned int LeafTile::getDescriptionId(int iData /*= -1*/)
342{
343 int leafIndex = iData & LEAF_TYPE_MASK;
344 return LeafTile::LEAF_NAMES[leafIndex];
345}
346
347void LeafTile::registerIcons(IconRegister *iconRegister)
348{
349 for (int fancy = 0; fancy < 2; fancy++)
350 {
351 //icons[fancy] = new Icon[TEXTURES[fancy].length];
352
353 for (int i = 0; i < 4; i++)
354 {
355 icons[fancy][i] = iconRegister->registerIcon(TEXTURES[fancy][i]);
356 }
357 }
358}