| 1 | package net.minecraft.world.level.block.grower;
|
| 2 |
|
| 3 | import com.mojang.datafixers.util.Pair;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
|
| 6 | import java.util.List;
|
| 7 | import java.util.Map;
|
| 8 | import java.util.Optional;
|
| 9 | import java.util.OptionalInt;
|
| 10 | import net.minecraft.core.BlockPos;
|
| 11 | import net.minecraft.core.Holder;
|
| 12 | import net.minecraft.core.registries.Registries;
|
| 13 | import net.minecraft.data.worldgen.features.TreeFeatures;
|
| 14 | import net.minecraft.resources.ResourceKey;
|
| 15 | import net.minecraft.server.level.ServerLevel;
|
| 16 | import net.minecraft.tags.BlockTags;
|
| 17 | import net.minecraft.util.RandomSource;
|
| 18 | import net.minecraft.util.random.Weighted;
|
| 19 | import net.minecraft.util.random.WeightedList;
|
| 20 | import net.minecraft.world.level.LevelAccessor;
|
| 21 | import net.minecraft.world.level.block.Block;
|
| 22 | import net.minecraft.world.level.block.state.BlockState;
|
| 23 | import net.minecraft.world.level.chunk.ChunkGenerator;
|
| 24 | import net.minecraft.world.level.levelgen.feature.Feature;
|
| 25 | import net.minecraft.world.level.levelgen.feature.TreeFeature;
|
| 26 | import org.jspecify.annotations.Nullable;
|
| 27 |
|
| 28 | public final class TreeGrower {
|
| 29 | private static final Map<String, TreeGrower> GROWERS = new Object2ObjectArrayMap<>();
|
| 30 | public static final Codec<TreeGrower> CODEC = Codec.stringResolver(g -> g.name, GROWERS::get);
|
| 31 | public static final TreeGrower OAK = new TreeGrower(
|
| 32 | "oak",
|
| 33 | WeightedList.of(new Weighted<>(TreeFeatures.OAK, 9), new Weighted<>(TreeFeatures.FANCY_OAK, 1)),
|
| 34 | WeightedList.of(),
|
| 35 | WeightedList.of(new Weighted<>(TreeFeatures.OAK_BEES_005, 9), new Weighted<>(TreeFeatures.FANCY_OAK_BEES_005, 1)),
|
| 36 | TreeFeatures.OAK
|
| 37 | );
|
| 38 | public static final TreeGrower SPRUCE = new TreeGrower(
|
| 39 | "spruce",
|
| 40 | WeightedList.of(TreeFeatures.SPRUCE),
|
| 41 | WeightedList.of(new Weighted<>(TreeFeatures.MEGA_SPRUCE, 1), new Weighted<>(TreeFeatures.MEGA_PINE, 1)),
|
| 42 | WeightedList.of(),
|
| 43 | TreeFeatures.SPRUCE
|
| 44 | );
|
| 45 | public static final TreeGrower MANGROVE = new TreeGrower(
|
| 46 | "mangrove",
|
| 47 | WeightedList.of(new Weighted<>(TreeFeatures.MANGROVE, 15), new Weighted<>(TreeFeatures.TALL_MANGROVE, 85)),
|
| 48 | WeightedList.of(),
|
| 49 | WeightedList.of(),
|
| 50 | TreeFeatures.MANGROVE
|
| 51 | );
|
| 52 | public static final TreeGrower AZALEA = new TreeGrower(
|
| 53 | "azalea", WeightedList.of(TreeFeatures.AZALEA_TREE), WeightedList.of(), WeightedList.of(), TreeFeatures.AZALEA_TREE
|
| 54 | );
|
| 55 | public static final TreeGrower BIRCH = new TreeGrower(
|
| 56 | "birch", WeightedList.of(TreeFeatures.BIRCH), WeightedList.of(), WeightedList.of(TreeFeatures.BIRCH_BEES_005), TreeFeatures.BIRCH
|
| 57 | );
|
| 58 | public static final TreeGrower JUNGLE = new TreeGrower(
|
| 59 | "jungle",
|
| 60 | WeightedList.of(TreeFeatures.JUNGLE_TREE_NO_VINE),
|
| 61 | WeightedList.of(TreeFeatures.MEGA_JUNGLE_TREE),
|
| 62 | WeightedList.of(),
|
| 63 | TreeFeatures.JUNGLE_TREE_NO_VINE
|
| 64 | );
|
| 65 | public static final TreeGrower ACACIA = new TreeGrower(
|
| 66 | "acacia", WeightedList.of(TreeFeatures.ACACIA), WeightedList.of(), WeightedList.of(), TreeFeatures.ACACIA
|
| 67 | );
|
| 68 | public static final TreeGrower CHERRY = new TreeGrower(
|
| 69 | "cherry", WeightedList.of(TreeFeatures.CHERRY), WeightedList.of(), WeightedList.of(TreeFeatures.CHERRY_BEES_005), TreeFeatures.CHERRY
|
| 70 | );
|
| 71 | public static final TreeGrower DARK_OAK = new TreeGrower("dark_oak", WeightedList.of(), WeightedList.of(TreeFeatures.DARK_OAK), WeightedList.of(), null);
|
| 72 | public static final TreeGrower PALE_OAK = new TreeGrower(
|
| 73 | "pale_oak", WeightedList.of(), WeightedList.of(TreeFeatures.PALE_OAK_BONEMEAL), WeightedList.of(), null
|
| 74 | );
|
| 75 | public static final TreeGrower POPLAR = new TreeGrower(
|
| 76 | "poplar",
|
| 77 | WeightedList.of(new Weighted<>(TreeFeatures.RED_POPLAR, 1), new Weighted<>(TreeFeatures.ORANGE_POPLAR, 1), new Weighted<>(TreeFeatures.YELLOW_POPLAR, 1)),
|
| 78 | WeightedList.of(),
|
| 79 | WeightedList.of(),
|
| 80 | TreeFeatures.RED_POPLAR
|
| 81 | );
|
| 82 | private final String name;
|
| 83 | private final WeightedList<ResourceKey<Feature>> trees;
|
| 84 | private final WeightedList<ResourceKey<Feature>> megaTrees;
|
| 85 | private final WeightedList<ResourceKey<Feature>> flowerTrees;
|
| 86 | private final @Nullable ResourceKey<Feature> shortestTreeType;
|
| 87 |
|
| 88 | public TreeGrower(
|
| 89 | final String name,
|
| 90 | final WeightedList<ResourceKey<Feature>> trees,
|
| 91 | final WeightedList<ResourceKey<Feature>> megaTrees,
|
| 92 | final WeightedList<ResourceKey<Feature>> flowerTrees,
|
| 93 | final @Nullable ResourceKey<Feature> shortestTreeType
|
| 94 | ) {
|
| 95 | this.name = name;
|
| 96 | this.trees = trees;
|
| 97 | this.megaTrees = megaTrees;
|
| 98 | this.flowerTrees = flowerTrees;
|
| 99 | this.shortestTreeType = shortestTreeType;
|
| 100 | GROWERS.put(name, this);
|
| 101 | }
|
| 102 |
|
| 103 | private @Nullable ResourceKey<Feature> getConfiguredFeature(final RandomSource random, final boolean hasFlowers) {
|
| 104 | return hasFlowers && !this.flowerTrees.isEmpty() ? this.flowerTrees.getRandom(random).orElse(null) : this.trees.getRandom(random).orElse(null);
|
| 105 | }
|
| 106 |
|
| 107 | private @Nullable ResourceKey<Feature> getConfiguredMegaFeature(final RandomSource random) {
|
| 108 | return this.megaTrees.getRandom(random).orElse(null);
|
| 109 | }
|
| 110 |
|
| 111 | public boolean growTree(final ServerLevel level, final ChunkGenerator generator, final BlockPos pos, final BlockState state, final RandomSource random) {
|
| 112 | ResourceKey<Feature> megaFeatureKey = this.getConfiguredMegaFeature(random);
|
| 113 | if (megaFeatureKey != null) {
|
| 114 | Holder<Feature> featureHolder = level.registryAccess().lookupOrThrow(Registries.FEATURE).get(megaFeatureKey).orElse(null);
|
| 115 | if (featureHolder != null) {
|
| 116 | Optional<TreeGrower.TwoByTwoSaplingPos> twoByTwoSaplingPos = findTwoByTwoSaplingPos(level, state, pos);
|
| 117 | if (twoByTwoSaplingPos.isPresent()) {
|
| 118 | int dx = twoByTwoSaplingPos.get().offsetX();
|
| 119 | int dz = twoByTwoSaplingPos.get().offsetZ();
|
| 120 | List<Pair<BlockState, BlockPos>> groundLevelSurroundingBlocks = twoByTwoSaplingPos.get().groundLevelSurroundingBlocks();
|
| 121 | Feature feature = featureHolder.value();
|
| 122 | removeSaplings(level, groundLevelSurroundingBlocks);
|
| 123 | if (feature.place(level, generator, random, pos.offset(dx, 0, dz))) {
|
| 124 | return true;
|
| 125 | }
|
| 126 |
|
| 127 | resetSaplings(level, groundLevelSurroundingBlocks);
|
| 128 | return false;
|
| 129 | }
|
| 130 | }
|
| 131 | }
|
| 132 |
|
| 133 | ResourceKey<Feature> featureKey = this.getConfiguredFeature(random, this.hasFlowers(level, pos));
|
| 134 | if (featureKey == null) {
|
| 135 | return false;
|
| 136 | }
|
| 137 |
|
| 138 | Holder<Feature> featureHolder = level.registryAccess().lookupOrThrow(Registries.FEATURE).get(featureKey).orElse(null);
|
| 139 | if (featureHolder == null) {
|
| 140 | return false;
|
| 141 | }
|
| 142 |
|
| 143 | Feature feature = featureHolder.value();
|
| 144 | removeSapling(level, pos);
|
| 145 | if (feature.place(level, generator, random, pos)) {
|
| 146 | return true;
|
| 147 | }
|
| 148 |
|
| 149 | resetSaplings(level, List.of(Pair.of(state, pos)));
|
| 150 | return false;
|
| 151 | }
|
| 152 |
|
| 153 | private static List<Pair<BlockState, BlockPos>> getSurroundingBlockStates(final ServerLevel level, final BlockPos pos, final int dx, final int dz) {
|
| 154 | return List.of(
|
| 155 | Pair.of(level.getBlockState(pos.offset(dx, 0, dz)), pos.offset(dx, 0, dz)),
|
| 156 | Pair.of(level.getBlockState(pos.offset(dx + 1, 0, dz)), pos.offset(dx + 1, 0, dz)),
|
| 157 | Pair.of(level.getBlockState(pos.offset(dx, 0, dz + 1)), pos.offset(dx, 0, dz + 1)),
|
| 158 | Pair.of(level.getBlockState(pos.offset(dx + 1, 0, dz + 1)), pos.offset(dx + 1, 0, dz + 1))
|
| 159 | );
|
| 160 | }
|
| 161 |
|
| 162 | private static void removeSaplings(final ServerLevel level, final List<Pair<BlockState, BlockPos>> saplingBlocks) {
|
| 163 | for (Pair<BlockState, BlockPos> saplingBlock : saplingBlocks) {
|
| 164 | BlockPos saplingPosition = saplingBlock.getSecond();
|
| 165 | removeSapling(level, saplingPosition);
|
| 166 | }
|
| 167 | }
|
| 168 |
|
| 169 | private static void removeSapling(final ServerLevel level, final BlockPos saplingPosition) {
|
| 170 | BlockState emptyBlock = level.getFluidState(saplingPosition).createLegacyBlock();
|
| 171 | level.setBlock(saplingPosition, emptyBlock, 818);
|
| 172 | }
|
| 173 |
|
| 174 | private static void resetSaplings(final ServerLevel level, final List<Pair<BlockState, BlockPos>> saplingBlocks) {
|
| 175 | for (Pair<BlockState, BlockPos> saplingBlock : saplingBlocks) {
|
| 176 | level.setBlock(saplingBlock.getSecond(), saplingBlock.getFirst(), 260);
|
| 177 | }
|
| 178 | }
|
| 179 |
|
| 180 | private static boolean isTwoByTwoSapling(final BlockState state, final List<Pair<BlockState, BlockPos>> surroundingBlocks) {
|
| 181 | Block block = state.getBlock();
|
| 182 |
|
| 183 | for (Pair<BlockState, BlockPos> surroundingBlock : surroundingBlocks) {
|
| 184 | BlockState surroundingBlockState = surroundingBlock.getFirst();
|
| 185 | if (!surroundingBlockState.is(block)) {
|
| 186 | return false;
|
| 187 | }
|
| 188 | }
|
| 189 |
|
| 190 | return true;
|
| 191 | }
|
| 192 |
|
| 193 | private boolean hasFlowers(final LevelAccessor level, final BlockPos pos) {
|
| 194 | return level.findBlocksIn(pos.offset(-2, -1, -2), pos.offset(2, 1, 2)).filterState(state -> state.is(BlockTags.FLOWERS)).anyMatched();
|
| 195 | }
|
| 196 |
|
| 197 | public OptionalInt getMinimumHeight(final ServerLevel level) {
|
| 198 | ResourceKey<Feature> featureKey = this.shortestTreeType;
|
| 199 | if (featureKey == null) {
|
| 200 | return OptionalInt.empty();
|
| 201 | }
|
| 202 |
|
| 203 | Holder<Feature> featureHolder = level.registryAccess().lookupOrThrow(Registries.FEATURE).get(featureKey).orElse(null);
|
| 204 | return featureHolder != null && featureHolder.value() instanceof TreeFeature treeFeature
|
| 205 | ? OptionalInt.of(treeFeature.trunkPlacer().getBaseHeight())
|
| 206 | : OptionalInt.empty();
|
| 207 | }
|
| 208 |
|
| 209 | public boolean canGrow(final ServerLevel level, final BlockPos pos, final BlockState state) {
|
| 210 | ResourceKey<Feature> featureKey = this.getConfiguredFeature(level.getRandom(), this.hasFlowers(level, pos));
|
| 211 | ResourceKey<Feature> megaFeatureKey = this.getConfiguredMegaFeature(level.getRandom());
|
| 212 | return featureKey == null && megaFeatureKey != null ? findTwoByTwoSaplingPos(level, state, pos).isPresent() : true;
|
| 213 | }
|
| 214 |
|
| 215 | private static Optional<TreeGrower.TwoByTwoSaplingPos> findTwoByTwoSaplingPos(final ServerLevel level, final BlockState state, final BlockPos pos) {
|
| 216 | for (int dx = 0; dx >= -1; dx--) {
|
| 217 | for (int dz = 0; dz >= -1; dz--) {
|
| 218 | List<Pair<BlockState, BlockPos>> groundLevelSurroundingBlocks = getSurroundingBlockStates(level, pos, dx, dz);
|
| 219 | if (isTwoByTwoSapling(state, groundLevelSurroundingBlocks)) {
|
| 220 | return Optional.of(new TreeGrower.TwoByTwoSaplingPos(dx, dz, groundLevelSurroundingBlocks));
|
| 221 | }
|
| 222 | }
|
| 223 | }
|
| 224 |
|
| 225 | return Optional.empty();
|
| 226 | }
|
| 227 |
|
| 228 | private record TwoByTwoSaplingPos(int offsetX, int offsetZ, List<Pair<BlockState, BlockPos>> groundLevelSurroundingBlocks) {
|
| 229 | }
|
| 230 | }
|
| 231 |
|