| 1 | package net.minecraft.world.level.levelgen.structure.structures;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.Dynamic;
|
| 6 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 7 | import java.util.List;
|
| 8 | import net.minecraft.core.BlockPos;
|
| 9 | import net.minecraft.core.Direction;
|
| 10 | import net.minecraft.core.HolderLookup;
|
| 11 | import net.minecraft.core.registries.Registries;
|
| 12 | import net.minecraft.nbt.CompoundTag;
|
| 13 | import net.minecraft.nbt.NbtOps;
|
| 14 | import net.minecraft.resources.Identifier;
|
| 15 | import net.minecraft.tags.BlockTags;
|
| 16 | import net.minecraft.util.RandomSource;
|
| 17 | import net.minecraft.util.StringRepresentable;
|
| 18 | import net.minecraft.world.level.ChunkPos;
|
| 19 | import net.minecraft.world.level.LevelAccessor;
|
| 20 | import net.minecraft.world.level.ServerLevelAccessor;
|
| 21 | import net.minecraft.world.level.StructureManager;
|
| 22 | import net.minecraft.world.level.WorldGenLevel;
|
| 23 | import net.minecraft.world.level.block.Block;
|
| 24 | import net.minecraft.world.level.block.Blocks;
|
| 25 | import net.minecraft.world.level.block.LeavesBlock;
|
| 26 | import net.minecraft.world.level.block.Mirror;
|
| 27 | import net.minecraft.world.level.block.Rotation;
|
| 28 | import net.minecraft.world.level.block.VineBlock;
|
| 29 | import net.minecraft.world.level.block.state.BlockState;
|
| 30 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 31 | import net.minecraft.world.level.chunk.ChunkGenerator;
|
| 32 | import net.minecraft.world.level.levelgen.Heightmap;
|
| 33 | import net.minecraft.world.level.levelgen.structure.BoundingBox;
|
| 34 | import net.minecraft.world.level.levelgen.structure.TemplateStructurePiece;
|
| 35 | import net.minecraft.world.level.levelgen.structure.pieces.StructurePieceSerializationContext;
|
| 36 | import net.minecraft.world.level.levelgen.structure.pieces.StructurePieceType;
|
| 37 | import net.minecraft.world.level.levelgen.structure.templatesystem.AlwaysTrueTest;
|
| 38 | import net.minecraft.world.level.levelgen.structure.templatesystem.BlackstoneReplaceProcessor;
|
| 39 | import net.minecraft.world.level.levelgen.structure.templatesystem.BlockAgeProcessor;
|
| 40 | import net.minecraft.world.level.levelgen.structure.templatesystem.BlockIgnoreProcessor;
|
| 41 | import net.minecraft.world.level.levelgen.structure.templatesystem.BlockMatchTest;
|
| 42 | import net.minecraft.world.level.levelgen.structure.templatesystem.LavaSubmergedBlockProcessor;
|
| 43 | import net.minecraft.world.level.levelgen.structure.templatesystem.ProcessorRule;
|
| 44 | import net.minecraft.world.level.levelgen.structure.templatesystem.ProtectedBlockProcessor;
|
| 45 | import net.minecraft.world.level.levelgen.structure.templatesystem.RandomBlockMatchTest;
|
| 46 | import net.minecraft.world.level.levelgen.structure.templatesystem.RuleProcessor;
|
| 47 | import net.minecraft.world.level.levelgen.structure.templatesystem.StructurePlaceSettings;
|
| 48 | import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplate;
|
| 49 | import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplateManager;
|
| 50 |
|
| 51 | public class RuinedPortalPiece extends TemplateStructurePiece {
|
| 52 | private static final float PROBABILITY_OF_GOLD_GONE = 0.3F;
|
| 53 | private static final float PROBABILITY_OF_MAGMA_INSTEAD_OF_NETHERRACK = 0.07F;
|
| 54 | private static final float PROBABILITY_OF_MAGMA_INSTEAD_OF_LAVA = 0.2F;
|
| 55 | private final RuinedPortalPiece.VerticalPlacement verticalPlacement;
|
| 56 | private final RuinedPortalPiece.Properties properties;
|
| 57 |
|
| 58 | public RuinedPortalPiece(
|
| 59 | final HolderLookup.Provider registries,
|
| 60 | final StructureTemplateManager structureTemplateManager,
|
| 61 | final BlockPos templatePosition,
|
| 62 | final RuinedPortalPiece.VerticalPlacement verticalPlacement,
|
| 63 | final RuinedPortalPiece.Properties properties,
|
| 64 | final Identifier templateLocation,
|
| 65 | final StructureTemplate template,
|
| 66 | final Rotation rotation,
|
| 67 | final Mirror mirror,
|
| 68 | final BlockPos pivot
|
| 69 | ) {
|
| 70 | super(
|
| 71 | StructurePieceType.RUINED_PORTAL,
|
| 72 | 0,
|
| 73 | structureTemplateManager,
|
| 74 | templateLocation,
|
| 75 | templateLocation.toString(),
|
| 76 | makeSettings(registries, mirror, rotation, verticalPlacement, pivot, properties),
|
| 77 | templatePosition
|
| 78 | );
|
| 79 | this.verticalPlacement = verticalPlacement;
|
| 80 | this.properties = properties;
|
| 81 | }
|
| 82 |
|
| 83 | public RuinedPortalPiece(final StructurePieceSerializationContext context, final CompoundTag tag) {
|
| 84 | super(
|
| 85 | StructurePieceType.RUINED_PORTAL,
|
| 86 | tag,
|
| 87 | context.structureTemplateManager(),
|
| 88 | location -> makeSettings(context.registryAccess(), context.structureTemplateManager(), tag, location)
|
| 89 | );
|
| 90 | this.verticalPlacement = tag.read("VerticalPlacement", RuinedPortalPiece.VerticalPlacement.CODEC).orElseThrow();
|
| 91 | this.properties = tag.read("Properties", RuinedPortalPiece.Properties.CODEC).orElseThrow();
|
| 92 | }
|
| 93 |
|
| 94 | @Override
|
| 95 | protected void addAdditionalSaveData(final StructurePieceSerializationContext context, final CompoundTag tag) {
|
| 96 | super.addAdditionalSaveData(context, tag);
|
| 97 | tag.store("Rotation", Rotation.LEGACY_CODEC, this.placeSettings.getRotation());
|
| 98 | tag.store("Mirror", Mirror.LEGACY_CODEC, this.placeSettings.getMirror());
|
| 99 | tag.store("VerticalPlacement", RuinedPortalPiece.VerticalPlacement.CODEC, this.verticalPlacement);
|
| 100 | tag.store("Properties", RuinedPortalPiece.Properties.CODEC, this.properties);
|
| 101 | }
|
| 102 |
|
| 103 | private static StructurePlaceSettings makeSettings(
|
| 104 | final HolderLookup.Provider registries, final StructureTemplateManager structureTemplateManager, final CompoundTag tag, final Identifier location
|
| 105 | ) {
|
| 106 | StructureTemplate template = structureTemplateManager.getOrCreate(location);
|
| 107 | BlockPos pivot = new BlockPos(template.getSize().getX() / 2, 0, template.getSize().getZ() / 2);
|
| 108 | return makeSettings(
|
| 109 | registries,
|
| 110 | tag.read("Mirror", Mirror.LEGACY_CODEC).orElseThrow(),
|
| 111 | tag.read("Rotation", Rotation.LEGACY_CODEC).orElseThrow(),
|
| 112 | tag.read("VerticalPlacement", RuinedPortalPiece.VerticalPlacement.CODEC).orElseThrow(),
|
| 113 | pivot,
|
| 114 | RuinedPortalPiece.Properties.CODEC.parse(new Dynamic<>(NbtOps.INSTANCE, tag.get("Properties"))).getPartialOrThrow()
|
| 115 | );
|
| 116 | }
|
| 117 |
|
| 118 | private static StructurePlaceSettings makeSettings(
|
| 119 | final HolderLookup.Provider registries,
|
| 120 | final Mirror mirror,
|
| 121 | final Rotation rotation,
|
| 122 | final RuinedPortalPiece.VerticalPlacement verticalPlacement,
|
| 123 | final BlockPos pivot,
|
| 124 | final RuinedPortalPiece.Properties properties
|
| 125 | ) {
|
| 126 | HolderLookup.RegistryLookup<Block> blocks = registries.lookupOrThrow(Registries.BLOCK);
|
| 127 | BlockIgnoreProcessor ignoreProcessor = properties.airPocket ? BlockIgnoreProcessor.STRUCTURE_BLOCK : BlockIgnoreProcessor.STRUCTURE_AND_AIR;
|
| 128 | List<ProcessorRule> rules = Lists.newArrayList();
|
| 129 | rules.add(getBlockReplaceRule(Blocks.GOLD_BLOCK, 0.3F, Blocks.AIR));
|
| 130 | rules.add(getLavaProcessorRule(verticalPlacement, properties));
|
| 131 | if (!properties.cold) {
|
| 132 | rules.add(getBlockReplaceRule(Blocks.NETHERRACK, 0.07F, Blocks.MAGMA_BLOCK));
|
| 133 | }
|
| 134 |
|
| 135 | StructurePlaceSettings settings = new StructurePlaceSettings()
|
| 136 | .setRotation(rotation)
|
| 137 | .setMirror(mirror)
|
| 138 | .setRotationPivot(pivot)
|
| 139 | .addProcessor(ignoreProcessor)
|
| 140 | .addProcessor(new RuleProcessor(rules))
|
| 141 | .addProcessor(new BlockAgeProcessor(properties.mossiness))
|
| 142 | .addProcessor(new ProtectedBlockProcessor(blocks.getOrThrow(BlockTags.FEATURES_CANNOT_REPLACE)))
|
| 143 | .addProcessor(new LavaSubmergedBlockProcessor());
|
| 144 | if (properties.replaceWithBlackstone) {
|
| 145 | settings.addProcessor(BlackstoneReplaceProcessor.INSTANCE);
|
| 146 | }
|
| 147 |
|
| 148 | return settings;
|
| 149 | }
|
| 150 |
|
| 151 | private static ProcessorRule getLavaProcessorRule(final RuinedPortalPiece.VerticalPlacement verticalPlacement, final RuinedPortalPiece.Properties properties) {
|
| 152 | if (verticalPlacement == RuinedPortalPiece.VerticalPlacement.ON_OCEAN_FLOOR) {
|
| 153 | return getBlockReplaceRule(Blocks.LAVA, Blocks.MAGMA_BLOCK);
|
| 154 | } else {
|
| 155 | return properties.cold ? getBlockReplaceRule(Blocks.LAVA, Blocks.NETHERRACK) : getBlockReplaceRule(Blocks.LAVA, 0.2F, Blocks.MAGMA_BLOCK);
|
| 156 | }
|
| 157 | }
|
| 158 |
|
| 159 | @Override
|
| 160 | public void postProcess(
|
| 161 | final WorldGenLevel level,
|
| 162 | final StructureManager structureManager,
|
| 163 | final ChunkGenerator generator,
|
| 164 | final RandomSource random,
|
| 165 | final BoundingBox chunkBB,
|
| 166 | final ChunkPos chunkPos,
|
| 167 | final BlockPos referencePos
|
| 168 | ) {
|
| 169 | BoundingBox boundingBox = this.template.getBoundingBox(this.placeSettings, this.templatePosition);
|
| 170 | if (chunkBB.isInside(boundingBox.getCenter())) {
|
| 171 | chunkBB.encapsulate(boundingBox);
|
| 172 | super.postProcess(level, structureManager, generator, random, chunkBB, chunkPos, referencePos);
|
| 173 | this.spreadNetherrack(random, level);
|
| 174 | this.addNetherrackDripColumnsBelowPortal(random, level);
|
| 175 | if (this.properties.vines || this.properties.overgrown) {
|
| 176 | BlockPos.betweenClosedStream(this.getBoundingBox()).forEach(pos -> {
|
| 177 | if (this.properties.vines) {
|
| 178 | this.maybeAddVines(random, level, pos);
|
| 179 | }
|
| 180 |
|
| 181 | if (this.properties.overgrown) {
|
| 182 | this.maybeAddLeavesAbove(random, level, pos);
|
| 183 | }
|
| 184 | });
|
| 185 | }
|
| 186 | }
|
| 187 | }
|
| 188 |
|
| 189 | @Override
|
| 190 | protected void handleDataMarker(
|
| 191 | final String markerId, final BlockPos pos, final ServerLevelAccessor level, final RandomSource random, final BoundingBox chunkBB
|
| 192 | ) {
|
| 193 | }
|
| 194 |
|
| 195 | private void maybeAddVines(final RandomSource random, final LevelAccessor level, final BlockPos pos) {
|
| 196 | BlockState state = level.getBlockState(pos);
|
| 197 | if (!state.isAir() && !state.is(Blocks.VINE)) {
|
| 198 | Direction direction = getRandomHorizontalDirection(random);
|
| 199 | BlockPos neighbourPos = pos.relative(direction);
|
| 200 | BlockState neighourState = level.getBlockState(neighbourPos);
|
| 201 | if (neighourState.isAir()) {
|
| 202 | if (Block.isFaceFull(state.getCollisionShape(level, pos), direction)) {
|
| 203 | BooleanProperty vineDir = VineBlock.getPropertyForFace(direction.getOpposite());
|
| 204 | level.setBlockAndUpdate(neighbourPos, Blocks.VINE.defaultBlockState().setValue(vineDir, true));
|
| 205 | }
|
| 206 | }
|
| 207 | }
|
| 208 | }
|
| 209 |
|
| 210 | private void maybeAddLeavesAbove(final RandomSource random, final LevelAccessor level, final BlockPos pos) {
|
| 211 | if (random.nextFloat() < 0.5F && level.getBlockState(pos).is(Blocks.NETHERRACK) && level.getBlockState(pos.above()).isAir()) {
|
| 212 | level.setBlockAndUpdate(pos.above(), Blocks.JUNGLE_LEAVES.defaultBlockState().setValue(LeavesBlock.PERSISTENT, true));
|
| 213 | }
|
| 214 | }
|
| 215 |
|
| 216 | private void addNetherrackDripColumnsBelowPortal(final RandomSource random, final LevelAccessor level) {
|
| 217 | for (int x = this.boundingBox.minX() + 1; x < this.boundingBox.maxX(); x++) {
|
| 218 | for (int z = this.boundingBox.minZ() + 1; z < this.boundingBox.maxZ(); z++) {
|
| 219 | BlockPos pos = new BlockPos(x, this.boundingBox.minY(), z);
|
| 220 | if (level.getBlockState(pos).is(Blocks.NETHERRACK)) {
|
| 221 | this.addNetherrackDripColumn(random, level, pos.below());
|
| 222 | }
|
| 223 | }
|
| 224 | }
|
| 225 | }
|
| 226 |
|
| 227 | private void addNetherrackDripColumn(final RandomSource random, final LevelAccessor level, final BlockPos pos) {
|
| 228 | BlockPos.MutableBlockPos currentPos = pos.mutable();
|
| 229 | this.placeNetherrackOrMagma(random, level, currentPos);
|
| 230 | int remainingCap = 8;
|
| 231 |
|
| 232 | while (remainingCap > 0 && random.nextFloat() < 0.5F) {
|
| 233 | currentPos.move(Direction.DOWN);
|
| 234 | remainingCap--;
|
| 235 | this.placeNetherrackOrMagma(random, level, currentPos);
|
| 236 | }
|
| 237 | }
|
| 238 |
|
| 239 | private void spreadNetherrack(final RandomSource random, final LevelAccessor level) {
|
| 240 | boolean followGroundSurface = this.verticalPlacement == RuinedPortalPiece.VerticalPlacement.ON_LAND_SURFACE
|
| 241 | || this.verticalPlacement == RuinedPortalPiece.VerticalPlacement.ON_OCEAN_FLOOR;
|
| 242 | BlockPos center = this.boundingBox.getCenter();
|
| 243 | int centerX = center.getX();
|
| 244 | int centerZ = center.getZ();
|
| 245 | float[] netherrackProbabilityByDistance = new float[]{1.0F, 1.0F, 1.0F, 1.0F, 1.0F, 1.0F, 1.0F, 0.9F, 0.9F, 0.8F, 0.7F, 0.6F, 0.4F, 0.2F};
|
| 246 | int maxDistance = netherrackProbabilityByDistance.length;
|
| 247 | int averageWidth = (this.boundingBox.getXSpan() + this.boundingBox.getZSpan()) / 2;
|
| 248 | int distanceAdjustment = random.nextInt(Math.max(1, 8 - averageWidth / 2));
|
| 249 | int maxYDiff = 3;
|
| 250 | BlockPos.MutableBlockPos pos = BlockPos.ZERO.mutable();
|
| 251 |
|
| 252 | for (int x = centerX - maxDistance; x <= centerX + maxDistance; x++) {
|
| 253 | for (int z = centerZ - maxDistance; z <= centerZ + maxDistance; z++) {
|
| 254 | int distance = Math.abs(x - centerX) + Math.abs(z - centerZ);
|
| 255 | int adjustedDistance = Math.max(0, distance + distanceAdjustment);
|
| 256 | if (adjustedDistance < maxDistance) {
|
| 257 | float probabilityOfNetherrack = netherrackProbabilityByDistance[adjustedDistance];
|
| 258 | if (random.nextDouble() < probabilityOfNetherrack) {
|
| 259 | int surfaceY = getSurfaceY(level, x, z, this.verticalPlacement);
|
| 260 | int y = followGroundSurface ? surfaceY : Math.min(this.boundingBox.minY(), surfaceY);
|
| 261 | pos.set(x, y, z);
|
| 262 | if (Math.abs(y - this.boundingBox.minY()) <= 3 && this.canBlockBeReplacedByNetherrackOrMagma(level, pos)) {
|
| 263 | this.placeNetherrackOrMagma(random, level, pos);
|
| 264 | if (this.properties.overgrown) {
|
| 265 | this.maybeAddLeavesAbove(random, level, pos);
|
| 266 | }
|
| 267 |
|
| 268 | this.addNetherrackDripColumn(random, level, pos.below());
|
| 269 | }
|
| 270 | }
|
| 271 | }
|
| 272 | }
|
| 273 | }
|
| 274 | }
|
| 275 |
|
| 276 | private boolean canBlockBeReplacedByNetherrackOrMagma(final LevelAccessor level, final BlockPos pos) {
|
| 277 | return this.canBlockBeReplacedByNetherrackOrMagma(level.getBlockState(pos));
|
| 278 | }
|
| 279 |
|
| 280 | private boolean canBlockBeReplacedByNetherrackOrMagma(final BlockState state) {
|
| 281 | return !state.is(Blocks.AIR)
|
| 282 | && !state.is(Blocks.OBSIDIAN)
|
| 283 | && !state.is(BlockTags.FEATURES_CANNOT_REPLACE)
|
| 284 | && (this.verticalPlacement == RuinedPortalPiece.VerticalPlacement.IN_NETHER || !state.is(Blocks.LAVA));
|
| 285 | }
|
| 286 |
|
| 287 | private void placeNetherrackOrMagma(final RandomSource random, final LevelAccessor level, final BlockPos pos) {
|
| 288 | if (this.canBlockBeReplacedByNetherrackOrMagma(level, pos)) {
|
| 289 | if (!this.properties.cold && random.nextFloat() < 0.07F) {
|
| 290 | level.setBlockAndUpdate(pos, Blocks.MAGMA_BLOCK.defaultBlockState());
|
| 291 | } else {
|
| 292 | level.setBlockAndUpdate(pos, Blocks.NETHERRACK.defaultBlockState());
|
| 293 | }
|
| 294 | }
|
| 295 | }
|
| 296 |
|
| 297 | private static int getSurfaceY(final LevelAccessor level, final int x, final int z, final RuinedPortalPiece.VerticalPlacement verticalPlacement) {
|
| 298 | return level.getHeight(getHeightMapType(verticalPlacement), x, z) - 1;
|
| 299 | }
|
| 300 |
|
| 301 | public static Heightmap.Types getHeightMapType(final RuinedPortalPiece.VerticalPlacement verticalPlacement) {
|
| 302 | return verticalPlacement == RuinedPortalPiece.VerticalPlacement.ON_OCEAN_FLOOR ? Heightmap.Types.OCEAN_FLOOR_WG : Heightmap.Types.WORLD_SURFACE_WG;
|
| 303 | }
|
| 304 |
|
| 305 | private static ProcessorRule getBlockReplaceRule(final Block source, final float probability, final Block target) {
|
| 306 | return new ProcessorRule(new RandomBlockMatchTest(source, probability), AlwaysTrueTest.INSTANCE, target.defaultBlockState());
|
| 307 | }
|
| 308 |
|
| 309 | private static ProcessorRule getBlockReplaceRule(final Block source, final Block target) {
|
| 310 | return new ProcessorRule(new BlockMatchTest(source), AlwaysTrueTest.INSTANCE, target.defaultBlockState());
|
| 311 | }
|
| 312 |
|
| 313 | public record Properties(boolean cold, float mossiness, boolean airPocket, boolean overgrown, boolean vines, boolean replaceWithBlackstone) {
|
| 314 | public static final Codec<RuinedPortalPiece.Properties> CODEC = RecordCodecBuilder.create(
|
| 315 | i -> i.group(
|
| 316 | Codec.BOOL.fieldOf("cold").forGetter(RuinedPortalPiece.Properties::cold),
|
| 317 | Codec.FLOAT.fieldOf("mossiness").forGetter(RuinedPortalPiece.Properties::mossiness),
|
| 318 | Codec.BOOL.fieldOf("air_pocket").forGetter(RuinedPortalPiece.Properties::airPocket),
|
| 319 | Codec.BOOL.fieldOf("overgrown").forGetter(RuinedPortalPiece.Properties::overgrown),
|
| 320 | Codec.BOOL.fieldOf("vines").forGetter(RuinedPortalPiece.Properties::vines),
|
| 321 | Codec.BOOL.fieldOf("replace_with_blackstone").forGetter(RuinedPortalPiece.Properties::replaceWithBlackstone)
|
| 322 | )
|
| 323 | .apply(i, RuinedPortalPiece.Properties::new)
|
| 324 | );
|
| 325 | }
|
| 326 |
|
| 327 | public enum VerticalPlacement implements StringRepresentable {
|
| 328 | ON_LAND_SURFACE("on_land_surface"),
|
| 329 | PARTLY_BURIED("partly_buried"),
|
| 330 | ON_OCEAN_FLOOR("on_ocean_floor"),
|
| 331 | IN_MOUNTAIN("in_mountain"),
|
| 332 | UNDERGROUND("underground"),
|
| 333 | IN_NETHER("in_nether");
|
| 334 |
|
| 335 | public static final Codec<RuinedPortalPiece.VerticalPlacement> CODEC = StringRepresentable.fromEnum(RuinedPortalPiece.VerticalPlacement::values);
|
| 336 | private final String name;
|
| 337 |
|
| 338 | VerticalPlacement(final String name) {
|
| 339 | this.name = name;
|
| 340 | }
|
| 341 |
|
| 342 | public String getName() {
|
| 343 | return this.name;
|
| 344 | }
|
| 345 |
|
| 346 | @Override
|
| 347 | public String getSerializedName() {
|
| 348 | return this.name;
|
| 349 | }
|
| 350 | }
|
| 351 | }
|
| 352 |
|