| 1 | package net.minecraft.data.worldgen.placement;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import net.minecraft.core.Holder;
|
| 5 | import net.minecraft.core.registries.Registries;
|
| 6 | import net.minecraft.data.worldgen.BootstrapContext;
|
| 7 | import net.minecraft.resources.Identifier;
|
| 8 | import net.minecraft.resources.ResourceKey;
|
| 9 | import net.minecraft.util.random.WeightedList;
|
| 10 | import net.minecraft.util.valueproviders.ConstantInt;
|
| 11 | import net.minecraft.util.valueproviders.IntProvider;
|
| 12 | import net.minecraft.util.valueproviders.WeightedListInt;
|
| 13 | import net.minecraft.world.level.block.Block;
|
| 14 | import net.minecraft.world.level.levelgen.Heightmap;
|
| 15 | import net.minecraft.world.level.levelgen.VerticalAnchor;
|
| 16 | import net.minecraft.world.level.levelgen.blockpredicates.BlockPredicate;
|
| 17 | import net.minecraft.world.level.levelgen.feature.Feature;
|
| 18 | import net.minecraft.world.level.levelgen.placement.BlockPredicateFilter;
|
| 19 | import net.minecraft.world.level.levelgen.placement.CountPlacement;
|
| 20 | import net.minecraft.world.level.levelgen.placement.HeightRangePlacement;
|
| 21 | import net.minecraft.world.level.levelgen.placement.HeightmapPlacement;
|
| 22 | import net.minecraft.world.level.levelgen.placement.PlacedFeature;
|
| 23 | import net.minecraft.world.level.levelgen.placement.PlacementFilter;
|
| 24 | import net.minecraft.world.level.levelgen.placement.PlacementModifier;
|
| 25 |
|
| 26 | public class PlacementUtils {
|
| 27 | public static final PlacementModifier HEIGHTMAP = HeightmapPlacement.onHeightmap(Heightmap.Types.MOTION_BLOCKING);
|
| 28 | public static final PlacementModifier HEIGHTMAP_NO_LEAVES = HeightmapPlacement.onHeightmap(Heightmap.Types.MOTION_BLOCKING_NO_LEAVES);
|
| 29 | public static final PlacementModifier HEIGHTMAP_TOP_SOLID = HeightmapPlacement.onHeightmap(Heightmap.Types.OCEAN_FLOOR_WG);
|
| 30 | public static final PlacementModifier HEIGHTMAP_WORLD_SURFACE = HeightmapPlacement.onHeightmap(Heightmap.Types.WORLD_SURFACE_WG);
|
| 31 | public static final PlacementModifier HEIGHTMAP_OCEAN_FLOOR = HeightmapPlacement.onHeightmap(Heightmap.Types.OCEAN_FLOOR);
|
| 32 | public static final PlacementModifier FULL_RANGE = HeightRangePlacement.uniform(VerticalAnchor.bottom(), VerticalAnchor.top());
|
| 33 | public static final PlacementModifier RANGE_10_10 = HeightRangePlacement.uniform(VerticalAnchor.aboveBottom(10), VerticalAnchor.belowTop(10));
|
| 34 | public static final PlacementModifier RANGE_8_8 = HeightRangePlacement.uniform(VerticalAnchor.aboveBottom(8), VerticalAnchor.belowTop(8));
|
| 35 | public static final PlacementModifier RANGE_4_4 = HeightRangePlacement.uniform(VerticalAnchor.aboveBottom(4), VerticalAnchor.belowTop(4));
|
| 36 | public static final PlacementModifier RANGE_BOTTOM_TO_MAX_TERRAIN_HEIGHT = HeightRangePlacement.uniform(VerticalAnchor.bottom(), VerticalAnchor.absolute(256));
|
| 37 |
|
| 38 | public static void bootstrap(final BootstrapContext<PlacedFeature> context) {
|
| 39 | AquaticPlacements.bootstrap(context);
|
| 40 | CavePlacements.bootstrap(context);
|
| 41 | EndPlacements.bootstrap(context);
|
| 42 | MiscOverworldPlacements.bootstrap(context);
|
| 43 | NetherPlacements.bootstrap(context);
|
| 44 | OrePlacements.bootstrap(context);
|
| 45 | TreePlacements.bootstrap(context);
|
| 46 | VegetationPlacements.bootstrap(context);
|
| 47 | VillagePlacements.bootstrap(context);
|
| 48 | }
|
| 49 |
|
| 50 | public static ResourceKey<PlacedFeature> createKey(final String name) {
|
| 51 | return ResourceKey.create(Registries.PLACED_FEATURE, Identifier.withDefaultNamespace(name));
|
| 52 | }
|
| 53 |
|
| 54 | public static void register(
|
| 55 | final BootstrapContext<PlacedFeature> context,
|
| 56 | final ResourceKey<PlacedFeature> id,
|
| 57 | final Holder<Feature> feature,
|
| 58 | final List<PlacementModifier> placementModifiers
|
| 59 | ) {
|
| 60 | context.register(id, new PlacedFeature(feature, List.copyOf(placementModifiers)));
|
| 61 | }
|
| 62 |
|
| 63 | public static void register(
|
| 64 | final BootstrapContext<PlacedFeature> context,
|
| 65 | final ResourceKey<PlacedFeature> id,
|
| 66 | final Holder<Feature> feature,
|
| 67 | final PlacementModifier... placementModifiers
|
| 68 | ) {
|
| 69 | register(context, id, feature, List.of(placementModifiers));
|
| 70 | }
|
| 71 |
|
| 72 | public static PlacementModifier countExtra(final int count, final float chance, final int extra) {
|
| 73 | float weight = 1.0F / chance;
|
| 74 | if (Math.abs(weight - (int)weight) > 1.0E-5F) {
|
| 75 | throw new IllegalStateException("Chance data cannot be represented as list weight");
|
| 76 | }
|
| 77 |
|
| 78 | WeightedList<IntProvider> distribution = WeightedList.<IntProvider>builder()
|
| 79 | .add(ConstantInt.of(count), (int)weight - 1)
|
| 80 | .add(ConstantInt.of(count + extra), 1)
|
| 81 | .build();
|
| 82 | return CountPlacement.of(new WeightedListInt(distribution));
|
| 83 | }
|
| 84 |
|
| 85 | public static PlacementFilter isEmpty() {
|
| 86 | return BlockPredicateFilter.forPredicate(BlockPredicate.ONLY_IN_AIR_PREDICATE);
|
| 87 | }
|
| 88 |
|
| 89 | public static BlockPredicateFilter filteredByBlockSurvival(final Block block) {
|
| 90 | return BlockPredicateFilter.forPredicate(BlockPredicate.wouldSurvive(block));
|
| 91 | }
|
| 92 |
|
| 93 | public static Holder<PlacedFeature> inlinePlaced(final Holder<Feature> feature, final List<PlacementModifier> placementModifiers) {
|
| 94 | return Holder.direct(new PlacedFeature(feature, placementModifiers));
|
| 95 | }
|
| 96 |
|
| 97 | public static Holder<PlacedFeature> inlinePlaced(final Holder<Feature> feature, final PlacementModifier... placementModifiers) {
|
| 98 | return inlinePlaced(feature, List.of(placementModifiers));
|
| 99 | }
|
| 100 |
|
| 101 | public static Holder<PlacedFeature> inlinePlaced(final Feature feature, final PlacementModifier... placementModifiers) {
|
| 102 | return inlinePlaced(Holder.direct(feature), placementModifiers);
|
| 103 | }
|
| 104 |
|
| 105 | public static Holder<PlacedFeature> inlinePlaced(final Feature feature, final List<PlacementModifier> placementModifiers) {
|
| 106 | return inlinePlaced(Holder.direct(feature), placementModifiers);
|
| 107 | }
|
| 108 | }
|
| 109 |
|