/
NetherBerryBush.java
357 lines (313 loc) · 9.95 KB
/
NetherBerryBush.java
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
package mods.natura.blocks.crops;
import java.util.List;
import java.util.Random;
import mods.natura.client.BerryRender;
import mods.natura.common.NContent;
import mods.natura.common.NaturaTab;
import net.minecraft.block.Block;
import net.minecraft.block.BlockLeavesBase;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.IIcon;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.EnumPlantType;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.common.IPlantable;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
public class NetherBerryBush extends BlockLeavesBase implements IPlantable
{
Random random;
public IIcon[] fastIcons;
public IIcon[] fancyIcons;
public static String[] textureNames = new String[] { "blightberry", "duskberry", "skyberry", "stingberry", "blightberry_ripe", "duskberry_ripe", "skyberry_ripe", "stingberry_ripe" };
public NetherBerryBush()
{
super(Material.leaves, false);
this.setTickRandomly(true);
random = new Random();
this.setHardness(0.3F);
this.setStepSound(Block.soundTypeGrass);
this.setBlockName("berrybush");
this.setCreativeTab(NaturaTab.tab);
}
/* Berries show up at meta 12-15 */
@SideOnly(Side.CLIENT)
@Override
public void registerBlockIcons (IIconRegister iconRegister)
{
this.fastIcons = new IIcon[textureNames.length];
this.fancyIcons = new IIcon[textureNames.length];
for (int i = 0; i < this.fastIcons.length; i++)
{
this.fastIcons[i] = iconRegister.registerIcon("natura:" + textureNames[i] + "_fast");
this.fancyIcons[i] = iconRegister.registerIcon("natura:" + textureNames[i] + "_fancy");
}
}
@Override
@SideOnly(Side.CLIENT)
public IIcon getIcon (int side, int metadata)
{
if (field_150121_P)
{
if (metadata < 12)
{
return fancyIcons[metadata % 4];
}
else
{
return fancyIcons[metadata % 4 + 4];
}
}
else
{
if (metadata < 12)
{
return fastIcons[metadata % 4];
}
else
{
return fastIcons[metadata % 4 + 4];
}
}
}
/* Bushes are stored by size then type */
@Override
public int damageDropped (int metadata)
{
return metadata % 4;
}
/* The following methods define a berry bush's size depending on metadata */
@Override
public AxisAlignedBB getCollisionBoundingBoxFromPool (World world, int x, int y, int z)
{
int l = world.getBlockMetadata(x, y, z);
if (l < 4)
{
return AxisAlignedBB.getBoundingBox((double) x + 0.25D, y, (double) z + 0.25D, (double) x + 0.75D, (double) y + 0.5D, (double) z + 0.75D);
}
else if (l < 8)
{
return AxisAlignedBB.getBoundingBox((double) x + 0.125D, y, (double) z + 0.125D, (double) x + 0.875D, (double) y + 0.75D, (double) z + 0.875D);
}
else
{
return AxisAlignedBB.getBoundingBox(x, y, z, (double) x + 1.0D, (double) y + 1.0D, (double) z + 1.0D);
}
}
public AxisAlignedBB getSelectedBoundingBoxFromPool (World world, int x, int y, int z)
{
int l = world.getBlockMetadata(x, y, z);
if (l < 4)
{
return AxisAlignedBB.getBoundingBox((double) x + 0.25D, y, (double) z + 0.25D, (double) x + 0.75D, (double) y + 0.5D, (double) z + 0.75D);
}
else if (l < 8)
{
return AxisAlignedBB.getBoundingBox((double) x + 0.125D, y, (double) z + 0.125D, (double) x + 0.875D, (double) y + 0.75D, (double) z + 0.875D);
}
else
{
return AxisAlignedBB.getBoundingBox(x, y, z, (double) x + 1.0D, (double) y + 1.0D, (double) z + 1.0D);
}
}
@Override
public void setBlockBoundsBasedOnState (IBlockAccess iblockaccess, int x, int y, int z)
{
int md = iblockaccess.getBlockMetadata(x, y, z);
float minX;
float minY = 0F;
float minZ;
float maxX;
float maxY;
float maxZ;
if (md < 4)
{
minX = minZ = 0.25F;
maxX = maxZ = 0.75F;
maxY = 0.5F;
}
else
if (md < 8)
{
minX = minZ = 0.125F;
maxX = maxZ = 0.875F;
maxY = 0.75F;
}
else
{
minX = minZ = 0.0F;
maxX = maxZ = 1.0F;
maxY = 1.0F;
}
setBlockBounds(minX, minY, minZ, maxX, maxY, maxZ);
}
/* Left-click harvests berries */
@Override
public void onBlockClicked (World world, int x, int y, int z, EntityPlayer player)
{
if (!world.isRemote)
{
int meta = world.getBlockMetadata(x, y, z);
if (meta >= 12)
{
world.setBlock(x, y, z, this, meta - 4, 3);
EntityItem entityitem = new EntityItem(world, player.posX, player.posY - 1.0D, player.posZ, new ItemStack(NContent.netherBerryItem, 1, meta - 12));
world.spawnEntityInWorld(entityitem);
entityitem.onCollideWithPlayer(player);
}
}
}
/* Right-click harvests berries */
@Override
public boolean onBlockActivated (World world, int x, int y, int z, EntityPlayer player, int par6, float par7, float par8, float par9)
{
if (world.isRemote)
return false;
int meta = world.getBlockMetadata(x, y, z);
if (meta >= 12)
{
if (world.isRemote)
return true;
world.setBlock(x, y, z, this, meta - 4, 3);
EntityItem entityitem = new EntityItem(world, player.posX, player.posY - 1.0D, player.posZ, new ItemStack(NContent.netherBerryItem, 1, meta - 12));
world.spawnEntityInWorld(entityitem);
entityitem.onCollideWithPlayer(player);
return true;
}
return false;
}
/* Render logic */
@Override
public boolean isOpaqueCube ()
{
return false;
}
public void setGraphicsLevel (boolean flag)
{
field_150121_P = flag;
}
@Override
public boolean renderAsNormalBlock ()
{
return false;
}
public int getRenderType ()
{
return BerryRender.berryModel;
}
public boolean shouldSideBeRendered (IBlockAccess iblockaccess, int i, int j, int k, int l)
{
if (l > 7 || field_150121_P)
{
return super.shouldSideBeRendered(iblockaccess, i, j, k, l);
}
else
{
return true;
}
}
/* Bush growth */
@Override
public void updateTick (World world, int x, int y, int z, Random random1)
{
if (world.isRemote)
{
return;
}
int height;
for (height = 1; world.getBlock(x, y - height, z) == this; ++height)
{
;
}
if (random1.nextInt(75) == 0)
{
int md = world.getBlockMetadata(x, y, z);
if (md < 12)
{
world.setBlock(x, y, z, this, md + 4, 3);
}
if (random1.nextInt(3) == 0 && height < 3 && world.getBlock(x, y + 1, z) == Blocks.air && md >= 8)
{
world.setBlock(x, y + 1, z, this, md % 4, 3);
}
}
}
/* Resistance to fire */
@Override
public int getFlammability (IBlockAccess world, int x, int y, int z, int metadata, ForgeDirection face)
{
return 0;
}
@Override
public boolean isFlammable (IBlockAccess world, int x, int y, int z, int metadata, ForgeDirection face)
{
return false;
}
@Override
public int getFireSpreadSpeed (World world, int x, int y, int z, int metadata, ForgeDirection face)
{
return 0;
}
/**
* returns a list of items with the same ID, but different meta (eg: dye returns 16 items)
*/
@SideOnly(Side.CLIENT)
@Override
public void getSubBlocks (Item par1, CreativeTabs par2CreativeTabs, List par3List)
{
for (int var4 = 12; var4 < 16; ++var4)
{
par3List.add(new ItemStack(par1, 1, var4));
}
}
@Override
public EnumPlantType getPlantType (IBlockAccess world, int x, int y, int z)
{
return EnumPlantType.Nether;
}
@Override
public Block getPlant (IBlockAccess world, int x, int y, int z)
{
return this;
}
@Override
public int getPlantMetadata (IBlockAccess world, int x, int y, int z)
{
return world.getBlockMetadata(x, y, z) - 4;
}
public boolean boneFertilize (World world, int x, int y, int z, Random random)
{
int meta = world.getBlockMetadata(x, y, z);
if (meta / 4 < 2)
{
if (random.nextBoolean())
{
int setMeta = random.nextInt(2) + 1 + meta / 4;
if (setMeta > 2)
setMeta = 2;
world.setBlockMetadataWithNotify(x, y, z, meta % 4 + setMeta * 4, 4);
}
return true;
}
Block block = world.getBlock(x, y + 1, z);
if (block == null || world.isAirBlock(x, y + 1, z))
{
if (random.nextBoolean())
{
if (random.nextInt(3) == 0)
world.setBlock(x, y + 1, z, this, meta % 4, 3);
}
return true;
}
return false;
}
}