/
BlockNaturaSapling2.java
283 lines (243 loc) · 8.1 KB
/
BlockNaturaSapling2.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
package com.progwml6.natura.common.blocks.natural.plants;
import java.util.List;
import java.util.Random;
import com.progwml6.natura.common.blocks.util.variants.IBlockWithVariants;
import com.progwml6.natura.common.util.NaturaCreativeTabs;
import com.progwml6.natura.common.world.features.tree.EucalyptusTreeGenShort;
import net.minecraft.block.Block;
import net.minecraft.block.BlockPlanks;
import net.minecraft.block.IGrowable;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.properties.PropertyInteger;
import net.minecraft.block.state.BlockState;
import net.minecraft.block.state.IBlockState;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.BlockPos;
import net.minecraft.util.IStringSerializable;
import net.minecraft.util.StatCollector;
import net.minecraft.world.World;
import net.minecraft.world.gen.feature.WorldGenerator;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
public class BlockNaturaSapling2 extends BlockNaturaPlant implements IGrowable, IBlockWithVariants
{
public static final PropertyEnum<BlockNaturaSapling2.EnumType> TYPE = PropertyEnum.<BlockNaturaSapling2.EnumType> create("type", BlockNaturaSapling2.EnumType.class);
public static final PropertyInteger STAGE = PropertyInteger.create("stage", 0, 1);
public BlockNaturaSapling2()
{
super(Material.plants);
this.setDefaultState(this.blockState.getBaseState().withProperty(TYPE, BlockNaturaSapling2.EnumType.TIGER_SAPLING).withProperty(STAGE, Integer.valueOf(0)));
float f = 0.4F;
this.setBlockBounds(0.5F - f, 0.0F, 0.5F - f, 0.5F + f, f * 2.0F, 0.5F + f);
this.setCreativeTab(NaturaCreativeTabs.tabMisc);
this.setStepSound(Block.soundTypeGrass);
this.setHardness(0.0F);
}
/**
* Gets the localized name of this block. Used for the statistics page.
*/
@Override
public String getLocalizedName()
{
return StatCollector.translateToLocal(this.getUnlocalizedName() + "." + BlockPlanks.EnumType.OAK.getUnlocalizedName() + ".name");
}
@Override
public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand)
{
if (!worldIn.isRemote)
{
super.updateTick(worldIn, pos, state, rand);
if (worldIn.getLightFromNeighbors(pos.up()) >= 9 && rand.nextInt(7) == 0)
{
this.grow(worldIn, pos, state, rand);
}
}
}
public void grow(World worldIn, BlockPos pos, IBlockState state, Random rand)
{
if (state.getValue(STAGE).intValue() == 0)
{
worldIn.setBlockState(pos, state.cycleProperty(STAGE), 4);
}
else
{
this.generateTree(worldIn, pos, state, rand);
}
}
public void generateTree(World worldIn, BlockPos pos, IBlockState state, Random rand)
{
if (!net.minecraftforge.event.terraingen.TerrainGen.saplingGrowTree(worldIn, rand, pos))
return;
WorldGenerator worldgenerator = rand.nextInt(10) == 0 ? new EucalyptusTreeGenShort() : new EucalyptusTreeGenShort();
int i = 0;
int j = 0;
boolean flag = false;
switch (state.getValue(TYPE))
{
case PURPLEHEART_SAPLING:
case WILLOW_SAPLING:
}
IBlockState iblockstate2 = Blocks.air.getDefaultState();
if (flag)
{
worldIn.setBlockState(pos.add(i, 0, j), iblockstate2, 4);
worldIn.setBlockState(pos.add(i + 1, 0, j), iblockstate2, 4);
worldIn.setBlockState(pos.add(i, 0, j + 1), iblockstate2, 4);
worldIn.setBlockState(pos.add(i + 1, 0, j + 1), iblockstate2, 4);
}
else
{
worldIn.setBlockState(pos, iblockstate2, 4);
}
if (!worldgenerator.generate(worldIn, rand, pos.add(i, 0, j)))
{
if (flag)
{
worldIn.setBlockState(pos.add(i, 0, j), state, 4);
worldIn.setBlockState(pos.add(i + 1, 0, j), state, 4);
worldIn.setBlockState(pos.add(i, 0, j + 1), state, 4);
worldIn.setBlockState(pos.add(i + 1, 0, j + 1), state, 4);
}
else
{
worldIn.setBlockState(pos, state, 4);
}
}
}
private boolean func_181624_a(World p_181624_1_, BlockPos p_181624_2_, int p_181624_3_, int p_181624_4_, BlockNaturaSapling2.EnumType p_181624_5_)
{
return this.isTypeAt(p_181624_1_, p_181624_2_.add(p_181624_3_, 0, p_181624_4_), p_181624_5_) && this.isTypeAt(p_181624_1_, p_181624_2_.add(p_181624_3_ + 1, 0, p_181624_4_), p_181624_5_) && this.isTypeAt(p_181624_1_, p_181624_2_.add(p_181624_3_, 0, p_181624_4_ + 1), p_181624_5_) && this.isTypeAt(p_181624_1_, p_181624_2_.add(p_181624_3_ + 1, 0, p_181624_4_ + 1), p_181624_5_);
}
/**
* Check whether the given BlockPos has a Sapling of the given type
*/
public boolean isTypeAt(World worldIn, BlockPos pos, BlockNaturaSapling2.EnumType type)
{
IBlockState iblockstate = worldIn.getBlockState(pos);
return iblockstate.getBlock() == this && iblockstate.getValue(TYPE) == type;
}
/**
* Gets the metadata of the item this Block can drop. This method is called when the block gets destroyed. It
* returns the metadata of the dropped item based on the old metadata of the block.
*/
@Override
public int damageDropped(IBlockState state)
{
return state.getValue(TYPE).getMetadata();
}
/**
* returns a list of blocks with the same ID, but different meta (eg: wood returns 4 blocks)
*/
@Override
@SideOnly(Side.CLIENT)
public void getSubBlocks(Item itemIn, CreativeTabs tab, List<ItemStack> list)
{
for (BlockNaturaSapling2.EnumType blockplanks$enumtype : BlockNaturaSapling2.EnumType.values())
{
list.add(new ItemStack(itemIn, 1, blockplanks$enumtype.getMetadata()));
}
}
/**
* Whether this IGrowable can grow
*/
@Override
public boolean canGrow(World worldIn, BlockPos pos, IBlockState state, boolean isClient)
{
return true;
}
@Override
public boolean canUseBonemeal(World worldIn, Random rand, BlockPos pos, IBlockState state)
{
return worldIn.rand.nextFloat() < 0.45D;
}
@Override
public void grow(World worldIn, Random rand, BlockPos pos, IBlockState state)
{
this.grow(worldIn, pos, state, rand);
}
/**
* Convert the given metadata into a BlockState for this Block
*/
@Override
public IBlockState getStateFromMeta(int meta)
{
return this.getDefaultState().withProperty(TYPE, BlockNaturaSapling2.EnumType.byMetadata(meta & 7)).withProperty(STAGE, Integer.valueOf((meta & 8) >> 3));
}
/**
* Convert the BlockState into the correct metadata value
*/
@Override
public int getMetaFromState(IBlockState state)
{
int i = 0;
i = i | state.getValue(TYPE).getMetadata();
i = i | state.getValue(STAGE).intValue() << 3;
return i;
}
@Override
protected BlockState createBlockState()
{
return new BlockState(this, new IProperty[] { TYPE, STAGE });
}
@Override
public String getVariantUnlocalizedName(ItemStack stack)
{
return BlockNaturaSapling2.EnumType.byMetadata(stack.getMetadata()).getUnlocalizedName();
}
public static enum EnumType implements IStringSerializable
{
PURPLEHEART_SAPLING(0, "purpleheart_sapling"), TIGER_SAPLING(1, "tiger_sapling"), WILLOW_SAPLING(2, "willow_sapling"), DARKWOOD_SAPLING(3, "darkwood_sapling"), FUSEWOOD_SAPLING(4, "fusewood_sapling");
private static final BlockNaturaSapling2.EnumType[] META_LOOKUP = new BlockNaturaSapling2.EnumType[values().length];
private final int meta;
private final String name;
private final String unlocalizedName;
private EnumType(int meta, String name)
{
this(meta, name, name);
}
private EnumType(int meta, String name, String unlocalizedName)
{
this.meta = meta;
this.name = name;
this.unlocalizedName = unlocalizedName;
}
public int getMetadata()
{
return this.meta;
}
@Override
public String toString()
{
return this.name;
}
public static BlockNaturaSapling2.EnumType byMetadata(int meta)
{
if (meta < 0 || meta >= META_LOOKUP.length)
{
meta = 0;
}
return META_LOOKUP[meta];
}
@Override
public String getName()
{
return this.name;
}
public String getUnlocalizedName()
{
return this.unlocalizedName;
}
static
{
for (BlockNaturaSapling2.EnumType blockplanks$enumtype : values())
{
META_LOOKUP[blockplanks$enumtype.getMetadata()] = blockplanks$enumtype;
}
}
}
}