/
BlockNaturaBarleyCrop.java
148 lines (126 loc) · 3.84 KB
/
BlockNaturaBarleyCrop.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
package com.progwml6.natura.common.blocks.natural.plants;
import java.util.Random;
import com.progwml6.natura.common.items.ItemsNatura;
import net.minecraft.block.Block;
import net.minecraft.block.IGrowable;
import net.minecraft.block.material.Material;
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.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.BlockPos;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
public class BlockNaturaBarleyCrop extends BlockNaturaPlant implements IGrowable
{
public static final PropertyInteger AGE = PropertyInteger.create("age", 0, 3);
public BlockNaturaBarleyCrop()
{
super(Material.plants);
this.setDefaultState(this.blockState.getBaseState().withProperty(AGE, Integer.valueOf(0)));
this.setTickRandomly(true);
this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 0.25F, 1.0F);
this.setCreativeTab(null);
this.setHardness(0.0F);
this.setStepSound(soundTypeGrass);
this.disableStats();
}
/**
* is the block grass, dirt or farmland
*/
@Override
public boolean isSuitableSoilBlock(Block ground)
{
return ground == Blocks.farmland;
}
@Override
public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand)
{
int i = ((Integer) state.getValue(AGE)).intValue();
if (i < 3 && rand.nextInt(5) == 0)
{
state = state.withProperty(AGE, Integer.valueOf(i + 1));
worldIn.setBlockState(pos, state, 2);
}
super.updateTick(worldIn, pos, state, rand);
}
public void grow(World worldIn, BlockPos pos, IBlockState state)
{
int i = ((Integer) state.getValue(AGE)).intValue() + MathHelper.getRandomIntegerInRange(worldIn.rand, 2, 5);
if (i > 3)
{
i = 3;
}
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() < 3;
}
@Override
public boolean canUseBonemeal(World worldIn, Random rand, BlockPos pos, IBlockState state)
{
return true;
}
@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 });
}
@Override
@SideOnly(Side.CLIENT)
public Item getItem(World worldIn, BlockPos pos)
{
return ItemsNatura.barley_seeds;
}
@Override
public Item getItemDropped(IBlockState state, Random rand, int fortune)
{
return ((Integer) state.getValue(AGE)).intValue() == 3 ? new ItemStack(ItemsNatura.materials, 1).getItem() : ItemsNatura.barley_seeds;
}
@Override
public java.util.List<ItemStack> getDrops(net.minecraft.world.IBlockAccess world, BlockPos pos, IBlockState state, int fortune)
{
java.util.List<ItemStack> ret = super.getDrops(world, pos, state, fortune);
int age = ((Integer) state.getValue(AGE)).intValue();
Random rand = world instanceof World ? ((World) world).rand : new Random();
if (age >= 3)
{
for (int i = 0; i < 3 + fortune; ++i)
{
if (rand.nextInt(15) <= age)
{
ret.add(new ItemStack(ItemsNatura.barley_seeds, 1));
}
}
}
return ret;
}
}