/
BlockNaturaCottonCrop.java
234 lines (198 loc) · 7.24 KB
/
BlockNaturaCottonCrop.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
package com.progwml6.natura.blocks.crops;
import java.util.Random;
import net.minecraft.block.Block;
import net.minecraft.block.BlockBush;
import net.minecraft.block.IGrowable;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyInteger;
import net.minecraft.block.state.BlockState;
import net.minecraft.block.state.IBlockState;
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.BlockPos;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.MathHelper;
import net.minecraft.util.StatCollector;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import com.progwml6.natura.Natura;
public class BlockNaturaCottonCrop extends BlockBush implements IGrowable
{
public static final PropertyInteger AGE = PropertyInteger.create("age", 0, 4);
public BlockNaturaCottonCrop()
{
this.setDefaultState(this.blockState.getBaseState().withProperty(AGE, Integer.valueOf(0)));
this.setTickRandomly(true);
float f = 0.5F;
this.setBlockBounds(0.5F - f, 0.0F, 0.5F - f, 0.5F + f, 0.25F, 0.5F + f);
this.setCreativeTab(null);
this.setHardness(0.0F);
this.setStepSound(soundTypeGrass);
this.disableStats();
}
@Override
public String getLocalizedName()
{
return StatCollector.translateToLocal("natura." + this.getUnlocalizedName() + ".name");
}
protected static float getGrowthChance(Block blockIn, World worldIn, BlockPos pos)
{
float f = 1.0F;
BlockPos blockpos1 = pos.down();
for (int i = -1; i <= 1; ++i)
{
for (int j = -1; j <= 1; ++j)
{
float f1 = 0.0F;
IBlockState iblockstate = worldIn.getBlockState(blockpos1.add(i, 0, j));
if (iblockstate.getBlock().canSustainPlant(worldIn, blockpos1.add(i, 0, j), net.minecraft.util.EnumFacing.UP, (net.minecraftforge.common.IPlantable) blockIn))
{
f1 = 1.0F;
if (iblockstate.getBlock().isFertile(worldIn, blockpos1.add(i, 0, j)))
{
f1 = 3.0F;
}
}
if (i != 0 || j != 0)
{
f1 /= 4.0F;
}
f += f1;
}
}
BlockPos blockpos2 = pos.north();
BlockPos blockpos3 = pos.south();
BlockPos blockpos4 = pos.west();
BlockPos blockpos5 = pos.east();
boolean flag = blockIn == worldIn.getBlockState(blockpos4).getBlock() || blockIn == worldIn.getBlockState(blockpos5).getBlock();
boolean flag1 = blockIn == worldIn.getBlockState(blockpos2).getBlock() || blockIn == worldIn.getBlockState(blockpos3).getBlock();
if (flag && flag1)
{
f /= 2.0F;
}
else
{
boolean flag2 = blockIn == worldIn.getBlockState(blockpos4.north()).getBlock() || blockIn == worldIn.getBlockState(blockpos5.north()).getBlock() || blockIn == worldIn.getBlockState(blockpos5.south()).getBlock() || blockIn == worldIn.getBlockState(blockpos4.south()).getBlock();
if (flag2)
{
f /= 2.0F;
}
}
return f;
}
@Override
public void onBlockClicked(World world, BlockPos pos, EntityPlayer player)
{
if (!world.isRemote)
{
IBlockState state = world.getBlockState(pos);
if (((Integer) state.getValue(AGE)).intValue() == 4)
{
world.setBlockState(pos, state.withProperty(AGE, 2), 3);
EntityItem entityitem = new EntityItem(world, player.posX, player.posY - 1.0D, player.posZ, new ItemStack(Natura.INSTANCE.getItems().materials, 1, 3));
world.spawnEntityInWorld(entityitem);
entityitem.onCollideWithPlayer(player);
}
}
}
/* Right-click harvests berries */
@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ)
{
if (((Integer) state.getValue(AGE)).intValue() == 4)
{
if (worldIn.isRemote)
return true;
worldIn.setBlockState(pos, state.withProperty(AGE, 2), 3);
EntityItem entityitem = new EntityItem(worldIn, playerIn.posX, playerIn.posY - 1.0D, playerIn.posZ, new ItemStack(Natura.INSTANCE.getItems().materials, 1, 3));
worldIn.spawnEntityInWorld(entityitem);
entityitem.onCollideWithPlayer(playerIn);
return true;
}
return false;
}
/**
* is the block grass, dirt or farmland
*/
@Override
protected boolean canPlaceBlockOn(Block ground)
{
return ground == Blocks.farmland;
}
@Override
public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand)
{
super.updateTick(worldIn, pos, state, rand);
if (worldIn.getLightFromNeighbors(pos.up()) >= 9)
{
int i = ((Integer) state.getValue(AGE)).intValue();
if (i < 4)
{
float f = getGrowthChance(this, worldIn, pos);
if (rand.nextInt((int) (25.0F / f) + 1) == 0)
{
worldIn.setBlockState(pos, state.withProperty(AGE, Integer.valueOf(i + 1)), 2);
}
}
}
}
public void grow(World worldIn, BlockPos pos, IBlockState state)
{
int i = ((Integer) state.getValue(AGE)).intValue() + MathHelper.getRandomIntegerInRange(worldIn.rand, 2, 5);
if (i > 4)
{
i = 4;
}
worldIn.setBlockState(pos, state.withProperty(AGE, Integer.valueOf(i)), 2);
}
@Override
public boolean canGrow(World worldIn, BlockPos pos, IBlockState state, boolean isClient)
{
return ((Integer) state.getValue(AGE)).intValue() < 4;
}
@Override
public boolean canUseBonemeal(World worldIn, Random rand, BlockPos pos, IBlockState state)
{
return true;
}
@Override
@SideOnly(Side.CLIENT)
public Item getItem(World worldIn, BlockPos pos)
{
return this.getSeed();
}
protected Item getSeed()
{
return Natura.INSTANCE.getItems().cotton_seeds;
}
@Override
public void grow(World worldIn, Random rand, BlockPos pos, IBlockState state)
{
this.grow(worldIn, pos, state);
}
/**
* Convert the given metadata into a BlockState for this Block
*/
@Override
public IBlockState getStateFromMeta(int meta)
{
return this.getDefaultState().withProperty(AGE, Integer.valueOf(meta));
}
/**
* Convert the BlockState into the correct metadata value
*/
@Override
public int getMetaFromState(IBlockState state)
{
return ((Integer) state.getValue(AGE)).intValue();
}
@Override
protected BlockState createBlockState()
{
return new BlockState(this, new IProperty[] { AGE });
}
}