-
Notifications
You must be signed in to change notification settings - Fork 81
/
BlockRedwoodSapling.java
236 lines (200 loc) · 7.04 KB
/
BlockRedwoodSapling.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
package com.progwml6.natura.overworld.block.saplings;
import java.util.Locale;
import java.util.Random;
import javax.annotation.Nonnull;
import com.progwml6.natura.Natura;
import com.progwml6.natura.library.NaturaRegistry;
import com.progwml6.natura.overworld.NaturaOverworld;
import com.progwml6.natura.overworld.block.leaves.BlockRedwoodLeaves;
import com.progwml6.natura.overworld.block.logs.BlockRedwoodLog;
import com.progwml6.natura.world.worldgen.trees.BaseTreeGenerator;
import com.progwml6.natura.world.worldgen.trees.overworld.RedwoodTreeGenerator;
import net.minecraft.block.BlockSapling;
import net.minecraft.block.SoundType;
import net.minecraft.block.properties.PropertyEnum;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.IStringSerializable;
import net.minecraft.util.NonNullList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.EnumPlantType;
import slimeknights.mantle.block.EnumBlock;
public class BlockRedwoodSapling extends BlockSapling
{
public static PropertyEnum<SaplingType> FOLIAGE = PropertyEnum.create("foliage", SaplingType.class);
public BlockRedwoodSapling()
{
this.setCreativeTab(NaturaRegistry.tabWorld);
this.setDefaultState(this.blockState.getBaseState());
this.setSoundType(SoundType.PLANT);
this.setHardness(0.0F);
}
@Override
public void getSubBlocks(CreativeTabs tab, NonNullList<ItemStack> list)
{
for (SaplingType type : SaplingType.values())
{
list.add(new ItemStack(this, 1, this.getMetaFromState(this.getDefaultState().withProperty(FOLIAGE, type))));
}
}
@Nonnull
@Override
protected BlockStateContainer createBlockState()
{
// TYPE has to be included because of the BlockSapling constructor.. but it's never used.
return new BlockStateContainer(this, FOLIAGE, STAGE, TYPE);
}
/**
* Convert the given metadata into a BlockState for this Block
*/
@Nonnull
@Override
public IBlockState getStateFromMeta(int meta)
{
if (meta < 0 || meta >= SaplingType.values().length)
{
meta = 0;
}
SaplingType sapling = SaplingType.values()[meta];
return this.getDefaultState().withProperty(FOLIAGE, sapling);
}
/**
* Convert the BlockState into the correct metadata value
*/
@Override
public int getMetaFromState(IBlockState state)
{
return state.getValue(FOLIAGE).ordinal();
}
@Override
public int damageDropped(IBlockState state)
{
return this.getMetaFromState(state);
}
@Override
public boolean isReplaceable(IBlockAccess worldIn, @Nonnull BlockPos pos)
{
return false;
}
@Nonnull
@Override
public EnumPlantType getPlantType(IBlockAccess world, BlockPos pos)
{
return EnumPlantType.Plains;
}
@Nonnull
@Override
public ItemStack getPickBlock(@Nonnull IBlockState state, RayTraceResult target, @Nonnull World world, @Nonnull BlockPos pos, EntityPlayer player)
{
IBlockState iblockstate = world.getBlockState(pos);
int meta = iblockstate.getBlock().getMetaFromState(iblockstate);
return new ItemStack(Item.getItemFromBlock(this), 1, meta);
}
@Override
public void generateTree(@Nonnull World worldIn, @Nonnull BlockPos pos, @Nonnull IBlockState state, @Nonnull Random rand)
{
if (!net.minecraftforge.event.terraingen.TerrainGen.saplingGrowTree(worldIn, rand, pos))
{
return;
}
BaseTreeGenerator gen = new BaseTreeGenerator();
IBlockState bark;
IBlockState heart;
IBlockState root;
IBlockState leaves;
switch (state.getValue(FOLIAGE))
{
case REDWOOD:
int numSaplings = this.checkRedwoodSaplings(worldIn, pos);
bark = NaturaOverworld.redwoodLog.getDefaultState().withProperty(BlockRedwoodLog.TYPE, BlockRedwoodLog.RedwoodType.BARK);
heart = NaturaOverworld.redwoodLog.getDefaultState().withProperty(BlockRedwoodLog.TYPE, BlockRedwoodLog.RedwoodType.HEART);
root = NaturaOverworld.redwoodLog.getDefaultState().withProperty(BlockRedwoodLog.TYPE, BlockRedwoodLog.RedwoodType.ROOT);
leaves = NaturaOverworld.redwoodLeaves.getDefaultState().withProperty(BlockRedwoodLeaves.TYPE, BlockRedwoodLeaves.RedwoodType.NORMAL);
if (numSaplings >= 40)
{
gen = new RedwoodTreeGenerator(bark, heart, root, leaves);
break;
}
break;
default:
Natura.log.warn("BlockRedwoodSapling Warning: Invalid sapling meta/foliage, " + state.getValue(FOLIAGE) + ". Please report!");
break;
}
// replace saplings with air
this.replaceBlocksWithAir(worldIn, pos);
// try generating
gen.generateTree(rand, worldIn, pos);
if (worldIn.isAirBlock(pos))
{
worldIn.setBlockState(pos, state, 4);
}
}
/**
* Check whether the given BlockPos has a Sapling of the given type
*/
public int checkRedwoodSaplings(World worldIn, BlockPos pos)
{
int numSaplings = 0;
for (int x = -3; x <= 3; x++)
{
for (int z = -3; z <= 3; z++)
{
if (this.isRedwoodComplete(worldIn, pos.add(x, 0, z), SaplingType.REDWOOD))
{
numSaplings++;
}
}
}
return numSaplings;
}
/**
* Check whether the given BlockPos has a Sapling of the given type
*/
public boolean isRedwoodComplete(World worldIn, BlockPos pos, SaplingType type)
{
IBlockState iblockstate = worldIn.getBlockState(pos);
return iblockstate.getBlock() == this && iblockstate.getValue(FOLIAGE) == type;
}
/**
* Replaces redwood sapling's with air
*/
public void replaceBlocksWithAir(World worldIn, BlockPos pos)
{
for (int x = -3; x <= 3; x++)
{
for (int z = -3; z <= 3; z++)
{
if (this.isRedwoodComplete(worldIn, pos.add(x, 0, z), SaplingType.REDWOOD))
{
worldIn.setBlockToAir(pos.add(x, 0, z));
}
}
}
}
public enum SaplingType implements IStringSerializable, EnumBlock.IEnumMeta
{
REDWOOD;
public final int meta;
SaplingType()
{
this.meta = this.ordinal();
}
@Override
public String getName()
{
return this.toString().toLowerCase(Locale.US);
}
@Override
public int getMeta()
{
return this.meta;
}
}
}