/
SmoothGrowthListener.java
76 lines (64 loc) · 2.42 KB
/
SmoothGrowthListener.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
package knightminer.inspirations.tweaks.util;
import knightminer.inspirations.common.Config;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IWorld;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeHooks;
import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.event.world.BlockEvent.CropGrowEvent.Pre;
import net.minecraftforge.eventbus.api.Event.Result;
import java.util.function.Consumer;
import java.util.function.Supplier;
public class SmoothGrowthListener implements Consumer<Pre> {
private final Supplier<Block> crop, seed;
/**
* Creates a new event listener for smooth growth
* @param crop Original crop to listen for
* @param seed Seed to replace for crop growth
*/
public SmoothGrowthListener(Block crop, Block seed) {
this.crop = crop.delegate;
this.seed = seed.delegate;
}
@Override
public void accept(BlockEvent.CropGrowEvent.Pre event) {
if (!Config.smoothBlockCropGrowth.get()) {
return;
}
// at half growth place the seed, gives us 8 ticks on the block, 8 on the seed instead of 16 on the block
Block crop = this.crop.get();
if (event.getState().getBlock() != crop) {
return;
}
// first, place the seed
IWorld world = event.getWorld();
BlockPos dest, source;
// sugar cane fires the event at the source, cactus at the destination
// however, configurable cane does not fire consistently with Forge, so we just use if the block is at the position as our flag
BlockPos pos = event.getPos();
if (world.getBlockState(pos).getBlock() == crop) {
source = pos;
dest = pos.up();
} else {
// we probably have air at the position, so the crop is one block down
source = pos.down();
dest = pos;
}
BlockState state = seed.get().getDefaultState();
world.setBlockState(dest, state, 3);
// clear age on the block below
if (world.getBlockState(source).getBlock() == crop) {
world.setBlockState(source, crop.getDefaultState(), 4);
}
// prevent normal growth logic
event.setResult(Result.DENY);
// update the block above and fire relevant events
if (world instanceof World) {
World casted = (World)world;
state.neighborChanged(casted, dest, seed.get(), source, false);
ForgeHooks.onCropsGrowPost(casted, source, state);
}
}
}