/
BlockSoil.java
263 lines (228 loc) · 9.51 KB
/
BlockSoil.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
package binnie.botany.gardening;
import binnie.Binnie;
import binnie.botany.Botany;
import binnie.botany.CreativeTabBotany;
import binnie.botany.api.EnumAcidity;
import binnie.botany.api.EnumMoisture;
import binnie.botany.api.EnumSoilType;
import binnie.botany.api.gardening.IBlockSoil;
import forestry.api.core.IItemModelRegister;
import forestry.api.core.IModelManager;
import net.minecraft.block.Block;
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
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.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.IPlantable;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import java.util.List;
import java.util.Random;
public class BlockSoil extends Block implements IBlockSoil, IItemModelRegister {
private final boolean weedKilled;
private final EnumSoilType type;
public static final PropertyEnum<EnumMoisture> MOISTURE = PropertyEnum.create("moisture", EnumMoisture.class);
public static final PropertyEnum<EnumAcidity> ACIDITY = PropertyEnum.create("acidity", EnumAcidity.class);
public static final AxisAlignedBB SOIL_BLOCK_AABB = new AxisAlignedBB(0.0D, 0.0D, 0.0D, 1.0D, 0.9375D, 1.0D);
public BlockSoil(EnumSoilType type, String blockName, boolean weedKilled) {
super(Material.GROUND);
this.weedKilled = weedKilled;
setUnlocalizedName("botany.soil." + type.getName());
this.setCreativeTab(CreativeTabBotany.instance);
this.setRegistryName(blockName);
this.setTickRandomly(true);
this.setLightOpacity(255);
this.setHardness(0.5f);
this.setSoundType(SoundType.GROUND);
this.type = type;
}
@Override
public String getUnlocalizedName() {
return super.getUnlocalizedName().replaceFirst("tile.", "");
}
@Override
public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos) {
return SOIL_BLOCK_AABB;
}
@Override
public AxisAlignedBB getCollisionBoundingBox(IBlockState blockState, World worldIn, BlockPos pos) {
return SOIL_BLOCK_AABB;
}
@Override
public AxisAlignedBB getSelectedBoundingBox(IBlockState state, World worldIn, BlockPos pos) {
return SOIL_BLOCK_AABB;
}
@Override
@SideOnly(Side.CLIENT)
public void getSubBlocks(final Item par1, final CreativeTabs par2CreativeTabs, final List par3List) {
for (int i = 0; i < 9; ++i) {
par3List.add(new ItemStack(this, 1, i));
}
}
@Override
@SideOnly(Side.CLIENT)
public void registerModel(Item item, IModelManager manager) {
int index = 0;
for(EnumMoisture moisture : EnumMoisture.values()){
for(EnumAcidity acidity : EnumAcidity.values()){
String modelName = "";
if(acidity != EnumAcidity.Neutral){
modelName +=acidity.getName();
}
if(moisture != EnumMoisture.Normal){
if(!modelName.isEmpty()){
modelName += "_";
}
modelName +=moisture.getName();
}
if(modelName.isEmpty()){
modelName = "normal";
}
manager.registerItemModel(item, index, type.getName() + (weedKilled ? "NoWeed" : "") + "/" + modelName);
index++;
}
}
}
@Override
protected BlockStateContainer createBlockState() {
return new BlockStateContainer(this, MOISTURE, ACIDITY);
}
@Override
public int getMetaFromState(IBlockState state) {
return getMeta(state.getValue(ACIDITY), state.getValue(MOISTURE));
}
@Override
public IBlockState getStateFromMeta(int meta) {
final EnumMoisture moisture = EnumMoisture.values()[meta % 3];
final EnumAcidity acidity = EnumAcidity.values()[meta / 3];
return getDefaultState().withProperty(MOISTURE, moisture).withProperty(ACIDITY, acidity);
}
public EnumSoilType getType() {
return this.type;
}
@Override
public int damageDropped(IBlockState state) {
return getMetaFromState(state);
}
@Override
public void updateTick(World world, BlockPos pos, IBlockState state, Random rand) {
int meta = getMetaFromState(state);
EnumMoisture moisture = EnumMoisture.values()[meta % 3];
EnumAcidity acidity = EnumAcidity.values()[meta / 3];
EnumMoisture desiredMoisture = Gardening.getNaturalMoisture(world, pos);
if (desiredMoisture.ordinal() > moisture.ordinal()) {
moisture = ((moisture == EnumMoisture.Dry) ? EnumMoisture.Normal : EnumMoisture.Damp);
}
if (desiredMoisture.ordinal() < moisture.ordinal()) {
moisture = ((moisture == EnumMoisture.Damp) ? EnumMoisture.Normal : EnumMoisture.Dry);
}
int meta2 = getMeta(acidity, moisture);
if (meta != meta2) {
world.setBlockState(pos, getStateFromMeta(meta2), 2);
}
if (!this.weedKilled && rand.nextInt(5 - getType(world, pos).ordinal()) == 0 && world.getBlockState(pos.up()).getBlock() == Blocks.AIR) {
world.setBlockState(pos.up(), Botany.plant.getStateFromMeta(BlockPlant.Type.Weeds.ordinal()), 2);
}
}
@Override
public boolean isOpaqueCube(IBlockState state) {
return false;
}
@Override
public EnumAcidity getPH(final World world, final BlockPos pos) {
return world.getBlockState(pos).getValue(ACIDITY);
}
@Override
public EnumMoisture getMoisture(final World world, final BlockPos pos) {
return world.getBlockState(pos).getValue(MOISTURE);
}
@Override
public EnumSoilType getType(final World world, final BlockPos pos) {
return this.type;
}
@Override
public boolean fertilise(final World world, final BlockPos pos, final EnumSoilType maxLevel) {
final EnumSoilType type = this.getType(world, pos);
if (type.ordinal() >= maxLevel.ordinal()) {
return false;
}
final IBlockState old = world.getBlockState(pos);
return world.setBlockState(pos, Gardening.getSoilBlock(maxLevel, this.weedKilled).getDefaultState().withProperty(ACIDITY, old.getValue(ACIDITY)).withProperty(MOISTURE, old.getValue(MOISTURE)), 2);
}
@Override
public boolean degrade(final World world, final BlockPos pos, final EnumSoilType minLevel) {
final EnumSoilType type = this.getType(world, pos);
if (type.ordinal() <= minLevel.ordinal()) {
return false;
}
final IBlockState old = world.getBlockState(pos);
return world.setBlockState(pos, Gardening.getSoilBlock(minLevel, this.weedKilled).getDefaultState().withProperty(ACIDITY, old.getValue(ACIDITY)).withProperty(MOISTURE, old.getValue(MOISTURE)), 2);
}
@Override
public boolean setPH(final World world, final BlockPos pos, final EnumAcidity pH) {
final int meta = getMeta(pH, this.getMoisture(world, pos));
return world.setBlockState(pos, world.getBlockState(pos).withProperty(ACIDITY, pH));
}
@Override
public boolean setMoisture(final World world, final BlockPos pos, final EnumMoisture moisture) {
final int meta = getMeta(this.getPH(world, pos), moisture);
return world.setBlockState(pos, world.getBlockState(pos).withProperty(MOISTURE, moisture));
}
public static int getMeta(final EnumAcidity acid, final EnumMoisture moisture) {
return acid.ordinal() * 3 + moisture.ordinal();
}
@Override
public void onNeighborChange(IBlockAccess world, BlockPos pos, BlockPos neighbor) {
super.onNeighborChange(world, pos, neighbor);
if (world.isSideSolid(pos.up(), EnumFacing.DOWN, false)) {
((World) world).setBlockState(pos, Blocks.DIRT.getDefaultState(), 2);
}
}
public static String getPH(ItemStack stack, boolean withColor, boolean byNeutralNone) {
EnumAcidity ph = EnumAcidity.values()[stack.getItemDamage() / 3];
if(byNeutralNone){
if(ph == EnumAcidity.Neutral){
return "";
}
}
return TextFormatting.GRAY + Binnie.Language.localise("botany.ph") + ": " + ph.getTranslated(withColor);
}
public static String getMoisture(ItemStack stack, boolean withColor, boolean byNormalNone) {
EnumMoisture moisure = EnumMoisture.values()[stack.getItemDamage() % 3];
if(byNormalNone){
if(moisure == EnumMoisture.Normal){
return "";
}
}
return TextFormatting.GRAY + Binnie.Language.localise("botany.moisture") + ": " + moisure.getTranslated(withColor);
}
@Override
public boolean canSustainPlant(IBlockState state, IBlockAccess world, BlockPos pos, EnumFacing direction, IPlantable plantable) {
IBlockState plant = plantable.getPlant(world, pos.up());
if (plant.getBlock() == Botany.flower) {
return true;
}
if (plant.getBlock() == Botany.plant) {
return !this.weedKilled || !BlockPlant.isWeed(world, pos);
}
return world instanceof World && Blocks.DIRT.canSustainPlant(state, world, pos, direction, plantable);
}
@Override
public boolean resistsWeeds(final World world, final BlockPos pos) {
return this.weedKilled;
}
public boolean isWeedKilled() {
return weedKilled;
}
}