-
Notifications
You must be signed in to change notification settings - Fork 81
/
OverworldTree.java
112 lines (94 loc) · 3.36 KB
/
OverworldTree.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
package com.progwml6.natura.world.worldgen.trees;
import com.progwml6.natura.world.NaturaStructures;
import com.progwml6.natura.world.block.TreeType;
import com.progwml6.natura.world.worldgen.trees.config.BaseOverworldTreeFeatureConfig;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.trees.Tree;
import net.minecraft.tags.BlockTags;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IWorld;
import net.minecraft.world.gen.ChunkGenerator;
import net.minecraft.world.gen.feature.BaseTreeFeatureConfig;
import net.minecraft.world.gen.feature.ConfiguredFeature;
import net.minecraft.world.server.ServerWorld;
import javax.annotation.Nullable;
import java.util.Random;
public class OverworldTree extends Tree {
private final TreeType treeType;
public OverworldTree(TreeType treeType) {
this.treeType = treeType;
}
@Deprecated
@Nullable
@Override
protected ConfiguredFeature<BaseTreeFeatureConfig, ?> getTreeFeature(Random randomIn, boolean largeHive) {
switch (this.treeType) {
case MAPLE:
return NaturaStructures.MAPLE_TREE;
case SILVERBELL:
return NaturaStructures.SILVERBELL_TREE;
case AMARANTH:
return NaturaStructures.AMARANTH_TREE;
case TIGER:
return NaturaStructures.TIGER_TREE;
case SAKURA:
return NaturaStructures.SAKURA_TREE;
}
return null;
}
/**
* Get a {@link net.minecraft.world.gen.feature.ConfiguredFeature} of tree
*/
@Nullable
public ConfiguredFeature<BaseOverworldTreeFeatureConfig, ?> getOverworldTreeFeature(Random randomIn, boolean largeHive) {
switch (this.treeType) {
case WILLOW:
return NaturaStructures.WILLOW_TREE;
case EUCALYPTUS:
return NaturaStructures.EUCALYPTUS_TREE;
case HOPSEED:
return NaturaStructures.HOPSEED_TREE;
}
return null;
}
@Override
public boolean attemptGrowTree(ServerWorld world, ChunkGenerator chunkGenerator, BlockPos pos, BlockState state, Random rand) {
ConfiguredFeature<BaseTreeFeatureConfig, ?> configuredTreeFeature = this.getTreeFeature(rand, this.hasNearbyFlora(world, pos));
if(configuredTreeFeature == null) {
ConfiguredFeature<BaseOverworldTreeFeatureConfig, ?> configuredFeature = this.getOverworldTreeFeature(rand, this.hasNearbyFlora(world, pos));
if (configuredFeature == null) {
return false;
}
else {
world.setBlockState(pos, Blocks.AIR.getDefaultState(), 4);
configuredFeature.config.forcePlacement();
if (configuredFeature.generate(world, chunkGenerator, rand, pos)) {
return true;
}
else {
world.setBlockState(pos, state, 4);
return false;
}
}
} else {
world.setBlockState(pos, Blocks.AIR.getDefaultState(), 4);
configuredTreeFeature.config.forcePlacement();
if (configuredTreeFeature.generate(world, chunkGenerator, rand, pos)) {
return true;
}
else {
world.setBlockState(pos, state, 4);
return false;
}
}
}
private boolean hasNearbyFlora(IWorld world, BlockPos pos) {
for (BlockPos blockpos : BlockPos.Mutable.getAllInBoxMutable(pos.down().north(2).west(2), pos.up().south(2).east(2))) {
if (world.getBlockState(blockpos).isIn(BlockTags.FLOWERS)) {
return true;
}
}
return false;
}
}