/
BlockPlantTFC.java
379 lines (335 loc) · 12.7 KB
/
BlockPlantTFC.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
* Work under Copyright. Licensed under the EUPL.
* See the project README.md and LICENSE.txt for more information.
*/
package net.dries007.tfc.objects.blocks.plants;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import net.minecraft.block.Block;
import net.minecraft.block.BlockBush;
import net.minecraft.block.SoundType;
import net.minecraft.block.properties.PropertyInteger;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.EnumSkyBlock;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.EnumPlantType;
import net.dries007.tfc.ConfigTFC;
import net.dries007.tfc.api.capability.size.IItemSize;
import net.dries007.tfc.api.capability.size.Size;
import net.dries007.tfc.api.capability.size.Weight;
import net.dries007.tfc.api.types.Plant;
import net.dries007.tfc.objects.blocks.BlocksTFC;
import net.dries007.tfc.util.OreDictionaryHelper;
import net.dries007.tfc.world.classic.CalendarTFC;
import net.dries007.tfc.world.classic.ClimateTFC;
import net.dries007.tfc.world.classic.chunkdata.ChunkDataTFC;
@ParametersAreNonnullByDefault
public class BlockPlantTFC extends BlockBush implements IItemSize
{
public static final PropertyInteger AGE = PropertyInteger.create("age", 0, 3);
/*
* Time of day, used for rendering plants that bloom at different times
* 0 = midnight-dawn
* 1 = dawn-noon
* 2 = noon-dusk
* 3 = dusk-midnight
*/
public final static PropertyInteger DAYPERIOD = PropertyInteger.create("dayperiod", 0, 3);
private static final AxisAlignedBB PLANT_AABB = new AxisAlignedBB(0.125D, 0.0D, 0.125D, 0.875D, 1.0D, 0.875D);
private static final Map<Plant, BlockPlantTFC> MAP = new HashMap<>();
public static BlockPlantTFC get(Plant plant)
{
return MAP.get(plant);
}
/* Growth Stage of the plant, tied to the month of year */
public final PropertyInteger growthStageProperty;
protected final Plant plant;
protected final BlockStateContainer blockState;
public BlockPlantTFC(Plant plant)
{
super(plant.getMaterial());
if (MAP.put(plant, this) != null) throw new IllegalStateException("There can only be one.");
plant.getOreDictName().ifPresent(name -> OreDictionaryHelper.register(this, name));
this.plant = plant;
this.growthStageProperty = PropertyInteger.create("stage", 0, plant.getNumStages());
this.setTickRandomly(true);
setSoundType(SoundType.PLANT);
setHardness(0.0F);
Blocks.FIRE.setFireInfo(this, 5, 20);
blockState = this.createPlantBlockState();
this.setDefaultState(this.blockState.getBaseState());
}
@SuppressWarnings("deprecation")
@Override
@Nonnull
public IBlockState getStateFromMeta(int meta)
{
return this.getDefaultState().withProperty(AGE, meta);
}
@Override
public int getMetaFromState(IBlockState state)
{
return state.getValue(AGE);
}
@SuppressWarnings("deprecation")
@Override
@Nonnull
public IBlockState getActualState(IBlockState state, IBlockAccess worldIn, BlockPos pos)
{
return state.withProperty(DAYPERIOD, getDayPeriod()).withProperty(growthStageProperty, plant.getStages()[CalendarTFC.getMonthOfYear().id()]);
}
@Override
public boolean isReplaceable(IBlockAccess worldIn, BlockPos pos)
{
return true;
}
@Override
public void randomTick(World worldIn, BlockPos pos, IBlockState state, Random random)
{
if (!worldIn.isAreaLoaded(pos, 1)) return;
CalendarTFC.Month currentMonth = CalendarTFC.getMonthOfYear();
int currentStage = state.getValue(growthStageProperty);
int expectedStage = plant.getStages()[currentMonth.id()];
int currentTime = state.getValue(DAYPERIOD);
int expectedTime = getDayPeriod();
if (currentTime != expectedTime)
{
worldIn.setBlockState(pos, state.withProperty(DAYPERIOD, expectedTime).withProperty(growthStageProperty, currentStage));
}
if (currentStage != expectedStage && random.nextDouble() < 0.5)
{
worldIn.setBlockState(pos, state.withProperty(DAYPERIOD, expectedTime).withProperty(growthStageProperty, expectedStage));
}
this.updateTick(worldIn, pos, state, random);
}
@Override
public int tickRate(World worldIn)
{
return 10;
}
@Override
public void onBlockAdded(World world, BlockPos pos, IBlockState state)
{
world.setBlockState(pos, state.withProperty(DAYPERIOD, getDayPeriod()).withProperty(growthStageProperty, plant.getStages()[CalendarTFC.getMonthOfYear().id()]));
checkAndDropBlock(world, pos, state);
}
@Override
@Nonnull
public Item getItemDropped(IBlockState state, Random rand, int fortune)
{
if (!plant.getOreDictName().isPresent()) return Items.AIR;
return Item.getItemFromBlock(this);
}
@Override
public void onEntityCollision(World worldIn, BlockPos pos, IBlockState state, Entity entityIn)
{
double movementMod = plant.getMovementMod();
//Entity X/Z motion is reduced by plants.
// todo: make this specific to the plant type (i.e. smaller grasses affect you less than larger ones)
entityIn.motionX *= movementMod;
entityIn.motionZ *= movementMod;
}
@Override
public void harvestBlock(World worldIn, EntityPlayer player, BlockPos pos, IBlockState state, @Nullable TileEntity te, ItemStack stack)
{
if (!plant.getOreDictName().isPresent() && !worldIn.isRemote && stack.getItem().getHarvestLevel(stack, "knife", player, state) != -1 && plant.getPlantType() != Plant.PlantType.SHORT_GRASS && plant.getPlantType() != Plant.PlantType.TALL_GRASS)
{
spawnAsEntity(worldIn, pos, new ItemStack(this, 1));
}
super.harvestBlock(worldIn, player, pos, state, te, stack);
}
@Override
public void onBlockPlacedBy(World worldIn, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack)
{
if (!canBlockStay(worldIn, pos, state) && placer instanceof EntityPlayer)
{
if (!((EntityPlayer) placer).isCreative() && !plant.getOreDictName().isPresent())
{
spawnAsEntity(worldIn, pos, new ItemStack(this));
}
}
}
@Nonnull
@Override
public BlockStateContainer getBlockState()
{
return this.blockState;
}
@Override
@Nonnull
public Block.EnumOffsetType getOffsetType()
{
return Block.EnumOffsetType.XYZ;
}
public Plant getPlant()
{
return plant;
}
@Nonnull
@Override
public Size getSize(ItemStack stack)
{
return Size.SMALL;
}
@Nonnull
@Override
public Weight getWeight(ItemStack stack)
{
return Weight.LIGHT;
}
public double getGrowthRate(World world, BlockPos pos)
{
if (world.isRainingAt(pos)) return ConfigTFC.GENERAL.plantGrowthRate * 5d;
else return ConfigTFC.GENERAL.plantGrowthRate;
}
@Override
public boolean canPlaceBlockAt(World worldIn, BlockPos pos)
{
IBlockState soil = worldIn.getBlockState(pos.down());
return worldIn.getBlockState(pos).getBlock().isReplaceable(worldIn, pos) && worldIn.getBlockState(pos).getBlock() != this && soil.getBlock().canSustainPlant(soil, worldIn, pos.down(), net.minecraft.util.EnumFacing.UP, this);
}
@Override
protected boolean canSustainBush(IBlockState state)
{
if (plant.getIsClayMarking()) return BlocksTFC.isClay(state) || isValidSoil(state);
else return isValidSoil(state);
}
@Override
public void updateTick(World worldIn, BlockPos pos, IBlockState state, Random rand)
{
if (!worldIn.isAreaLoaded(pos, 1)) return;
if (plant.isValidGrowthTemp(ClimateTFC.getHeightAdjustedTemp(worldIn, pos)) && plant.isValidSunlight(Math.subtractExact(worldIn.getLightFor(EnumSkyBlock.SKY, pos), worldIn.getSkylightSubtracted())))
{
int j = state.getValue(AGE);
if (rand.nextDouble() < getGrowthRate(worldIn, pos) && net.minecraftforge.common.ForgeHooks.onCropsGrowPre(worldIn, pos.up(), state, true))
{
if (j < 3)
{
worldIn.setBlockState(pos, state.withProperty(AGE, j + 1));
}
net.minecraftforge.common.ForgeHooks.onCropsGrowPost(worldIn, pos, state, worldIn.getBlockState(pos));
}
}
else if (!plant.isValidGrowthTemp(ClimateTFC.getHeightAdjustedTemp(worldIn, pos)) || !plant.isValidSunlight(worldIn.getLightFor(EnumSkyBlock.SKY, pos)))
{
int j = state.getValue(AGE);
if (rand.nextDouble() < getGrowthRate(worldIn, pos) && net.minecraftforge.common.ForgeHooks.onCropsGrowPre(worldIn, pos, state, true))
{
if (j > 0)
{
worldIn.setBlockState(pos, state.withProperty(AGE, j - 1));
}
net.minecraftforge.common.ForgeHooks.onCropsGrowPost(worldIn, pos, state, worldIn.getBlockState(pos));
}
}
checkAndDropBlock(worldIn, pos, state);
}
@Override
public boolean canBlockStay(World worldIn, BlockPos pos, IBlockState state)
{
IBlockState soil = worldIn.getBlockState(pos.down());
if (state.getBlock() == this)
{
return soil.getBlock().canSustainPlant(soil, worldIn, pos.down(), net.minecraft.util.EnumFacing.UP, this) && plant.isValidTemp(ClimateTFC.getHeightAdjustedTemp(worldIn, pos)) && plant.isValidRain(ChunkDataTFC.getRainfall(worldIn, pos));
}
return this.canSustainBush(soil);
}
@SuppressWarnings("deprecation")
@Override
@Nonnull
public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos)
{
return PLANT_AABB.offset(state.getOffset(source, pos));
}
@SuppressWarnings("deprecation")
@Nullable
@Override
public AxisAlignedBB getCollisionBoundingBox(IBlockState blockState, IBlockAccess worldIn, BlockPos pos)
{
if (plant.getMovementMod() == 0.0D)
{
return blockState.getBoundingBox(worldIn, pos);
}
else
{
return NULL_AABB;
}
}
@Override
@Nonnull
public EnumPlantType getPlantType(net.minecraft.world.IBlockAccess world, BlockPos pos)
{
switch (plant.getPlantType())
{
case CACTUS:
case DESERT:
case DESERT_TALL_PLANT:
return EnumPlantType.Desert;
case FLOATING:
case FLOATING_SEA:
return EnumPlantType.Water;
case MUSHROOM:
return EnumPlantType.Cave;
default:
return EnumPlantType.Plains;
}
}
@Nonnull
public Plant.EnumPlantTypeTFC getPlantTypeTFC()
{
return plant.getEnumPlantTypeTFC();
}
@Nonnull
protected BlockStateContainer createPlantBlockState()
{
return new BlockStateContainer(this, growthStageProperty, DAYPERIOD, AGE);
}
int getDayPeriod()
{
return Math.floorDiv(CalendarTFC.getHourOfDay(), CalendarTFC.HOURS_IN_DAY / 4);
}
private boolean isValidSoil(IBlockState state)
{
switch (plant.getPlantType())
{
case CACTUS:
case DESERT:
case DESERT_TALL_PLANT:
return BlocksTFC.isSand(state);
case DRY:
case DRY_TALL_PLANT:
return BlocksTFC.isSand(state) || BlocksTFC.isDryGrass(state);
case REED:
case REED_SEA:
case TALL_REED:
case TALL_REED_SEA:
return BlocksTFC.isSand(state) || BlocksTFC.isSoil(state);
case WATER:
case TALL_WATER:
case EMERGENT_TALL_WATER:
return BlocksTFC.isSoilOrGravel(state);
case WATER_SEA:
case TALL_WATER_SEA:
case EMERGENT_TALL_WATER_SEA:
return BlocksTFC.isSand(state) || BlocksTFC.isSoilOrGravel(state);
default:
return BlocksTFC.isSoil(state);
}
}
}