/
CropBlock.java
344 lines (302 loc) · 10.1 KB
/
CropBlock.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
package mods.natura.blocks.crops;
import java.util.ArrayList;
import java.util.Random;
import mods.natura.Natura;
import mods.natura.client.CropRender;
import mods.natura.common.NContent;
import net.minecraft.block.Block;
import net.minecraft.block.BlockCrops;
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.IIcon;
import net.minecraft.world.World;
import net.minecraftforge.common.EnumPlantType;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
public class CropBlock extends BlockCrops
{
public CropBlock()
{
super();
this.setTickRandomly(true);
float var3 = 0.5F;
this.setBlockBounds(0.5F - var3, 0.0F, 0.5F - var3, 0.5F + var3, 0.25F, 0.5F + var3);
this.setCreativeTab((CreativeTabs) null);
this.setHardness(0.0F);
this.setStepSound(soundTypeGrass);
this.disableStats();
}
/**
* Ticks the block if it's been scheduled
*/
public void updateTick (World world, int x, int y, int z, Random random)
{
this.checkFlowerChange(world, x, y, z);
int light = world.getBlockLightValue(x, y, z);
if (light >= 8)
{
int meta = world.getBlockMetadata(x, y, z);
if (meta != 3 && meta != 8)
{
float grow = this.getGrowthRate(world, x, y, z, meta, light);
if (random.nextInt((int) (60.0F / grow) + 1) == 0)
{
meta++;
world.setBlockMetadataWithNotify(x, y, z, meta, 2);
}
}
}
}
/**
* Apply bonemeal to the crops.
*/
@Override
public void fertilize (World world, int x, int y, int z)
{
int meta = world.getBlockMetadata(x, y, z);
if (meta != 3 && meta != 8)
{
if (meta < 3)
{
int output = Natura.random.nextInt(3) + 1 + meta;
if (output > 3)
output = 3;
world.setBlockMetadataWithNotify(x, y, z, output, 3);
}
else
{
int output = Natura.random.nextInt(4) + 1 + meta;
if (output > 8)
output = 8;
world.setBlockMetadataWithNotify(x, y, z, output, 3);
}
}
}
/*public boolean boneFertilize (World world, int x, int y, int z, Random random)
{
int meta = world.getBlockMetadata(x, y, z);
if (meta != 3 && meta != 8)
{
System.out.println("Meta: "+meta);
if (meta < 3)
{
int output = random.nextInt(3) + 1 + meta;
if (output > 3)
output = 3;
world.setBlockMetadataWithNotify(x, y, z, output, 3);
}
else
{
int output = random.nextInt(4) + 1 + meta;
if (output > 7)
output = 7;
world.setBlockMetadataWithNotify(x, y, z, output, 3);
}
return true;
}
return false;
}*/
protected float getGrowthRate (World world, int x, int y, int z, int meta, int light)
{
float growth = 0.25f * (light - 7);
Block soil = world.getBlock(x, y - 1, z);
if (world.canBlockSeeTheSky(x, y, z) || !requiresSun(meta))
growth += 2f;
if (soil != null && soil.isFertile(world, x, y - 1, z))
growth *= 2f;
return 1f + growth;
}
boolean requiresSun (int meta)
{
return true;
}
protected boolean canThisPlantGrowOnThisBlock (Block par1)
{
return par1 == Blocks.farmland;
}
/* 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 == 8)
{
world.setBlock(x, y, z, this, 6, 3);
EntityItem entityitem = new EntityItem(world, player.posX, player.posY - 1.0D, player.posZ, new ItemStack(NContent.plantItem, 1, 3));
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 == 8)
{
if (world.isRemote)
return true;
world.setBlock(x, y, z, this, 6, 3);
EntityItem entityitem = new EntityItem(world, player.posX, player.posY - 1.0D, player.posZ, new ItemStack(NContent.plantItem, 1, 3));
world.spawnEntityInWorld(entityitem);
entityitem.onCollideWithPlayer(player);
return true;
}
return false;
}
public float getBlockHardness (World world, int x, int y, int z)
{
if (world.getBlockMetadata(x, y, z) > 3)
return 0.5f;
return this.blockHardness;
}
public IIcon[] icons;
public String[] textureNames = new String[] { "barley_1", "barley_2", "barley_3", "barley_4", "cotton_1", "cotton_2", "cotton_3", "cotton_4", "cotton_5" };
@Override
@SideOnly(Side.CLIENT)
public void registerBlockIcons (IIconRegister iconRegister)
{
this.icons = new IIcon[textureNames.length];
for (int i = 0; i < this.icons.length; ++i)
{
this.icons[i] = iconRegister.registerIcon("natura:" + textureNames[i]);
}
}
@Override
@SideOnly(Side.CLIENT)
public IIcon getIcon (int side, int meta)
{
return icons[meta];
}
/**
* The type of render function that is called for this block
*/
public int getRenderType ()
{
return CropRender.model;
}
public Item itemDropped (int meta, Random random, int fortune)
{
if (meta == 3 || meta == 8)
return this.getCropItem(meta);
return this.getSeedItem(meta);
}
protected Item getCropItem (int meta)
{
return NContent.plantItem;
}
protected Item getSeedItem (int meta)
{
return NContent.seeds;
}
public int damageDropped (int meta)
{
if (meta < 4)
return 0;
return 3;
}
public int seedDamageDropped (int meta)
{
if (meta < 4)
return 0;
return 1;
}
/**
* Drops the block items with a specified chance of dropping the specified items
*/
public void dropBlockAsItemWithChance (World par1World, int par2, int par3, int par4, int par5, float par6, int par7)
{
super.dropBlockAsItemWithChance(par1World, par2, par3, par4, par5, par6, 0);
}
@Override
public ArrayList<ItemStack> getBlockDropped (World world, int x, int y, int z, int metadata, int fortune)
{
ArrayList<ItemStack> ret = new ArrayList<ItemStack>();
if (metadata == 3 || metadata == 8)
{
int count = quantityDropped(metadata, fortune, world.rand);
for (int i = 0; i < count; i++)
{
Item id = itemDropped(metadata, world.rand, 0);
if (id != null)
{
ret.add(new ItemStack(id, 1, damageDropped(metadata)));
}
}
}
if (metadata >= 4)
{
ret.add(new ItemStack(this.getSeedItem(metadata), 1, seedDamageDropped(metadata)));
if (metadata >= 5 && world.rand.nextBoolean())
ret.add(new ItemStack(this.getSeedItem(metadata), 1, seedDamageDropped(metadata)));
if (metadata >= 7 && world.rand.nextBoolean())
ret.add(new ItemStack(this.getSeedItem(metadata), 1, seedDamageDropped(metadata)));
}
else
{
ret.add(new ItemStack(this.getSeedItem(metadata), 1, seedDamageDropped(metadata)));
if (metadata >= 2 && world.rand.nextInt(3) == 0)
ret.add(new ItemStack(this.getSeedItem(metadata), 1, seedDamageDropped(metadata)));
if (metadata >= 3 && world.rand.nextInt(4) == 0)
ret.add(new ItemStack(this.getSeedItem(metadata), 1, seedDamageDropped(metadata)));
}
return ret;
}
/*@Override
public int quantityDropped(int meta, int fortune, Random random)
{
if (meta % 4 == 0)
return 1+random.nextInt(fortune+1);
return random.nextInt(meta/4);
}*/
/**
* Returns the ID of the items to drop on destruction.
*/
@SideOnly(Side.CLIENT)
/**
* only called by clickMiddleMouseButton , and passed to inventory.setCurrentItem (along with isCreative)
*/
public Item idPicked (World world, int x, int y, int z)
{
return this.getSeedItem(world.getBlockMetadata(x, y, z));
}
public int getDamageValue (World par1World, int par2, int par3, int par4)
{
return seedDamageDropped(par1World.getBlockMetadata(par2, par3, par4));
}
@Override
public EnumPlantType getPlantType (World world, int x, int y, int z)
{
return EnumPlantType.Crop;
}
/**
* Can this block stay at this position. Similar to canPlaceBlockAt except gets checked often with plants.
*/
@Override
public boolean canBlockStay (World world, int x, int y, int z)
{
int meta = world.getBlockMetadata(x, y, z); //Wild crops can stay
if (meta == 3 || meta == 8)
return world.getBlock(x, y - 1, z) != Blocks.air;
return super.canBlockStay(world, x, y, z);
}
@Override
public int getPlantMetadata (World world, int x, int y, int z)
{
int meta = world.getBlockMetadata(x, y, z);
if (meta < 4)
return 0;
else
return 4;
}
}