| 1 | package net.minecraft.world.level.levelgen.feature;
|
| 2 |
|
| 3 | import com.google.common.cache.CacheBuilder;
|
| 4 | import com.google.common.cache.CacheLoader;
|
| 5 | import com.google.common.cache.LoadingCache;
|
| 6 | import com.google.common.collect.Lists;
|
| 7 | import com.mojang.serialization.Codec;
|
| 8 | import com.mojang.serialization.MapCodec;
|
| 9 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 10 | import it.unimi.dsi.fastutil.ints.IntArrayList;
|
| 11 | import java.util.List;
|
| 12 | import java.util.Optional;
|
| 13 | import java.util.concurrent.TimeUnit;
|
| 14 | import java.util.stream.IntStream;
|
| 15 | import net.minecraft.core.BlockPos;
|
| 16 | import net.minecraft.core.SectionPos;
|
| 17 | import net.minecraft.util.Mth;
|
| 18 | import net.minecraft.util.RandomSource;
|
| 19 | import net.minecraft.util.Util;
|
| 20 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 21 | import net.minecraft.world.entity.EntityTypes;
|
| 22 | import net.minecraft.world.entity.boss.enderdragon.EndCrystal;
|
| 23 | import net.minecraft.world.level.ServerLevelAccessor;
|
| 24 | import net.minecraft.world.level.WorldGenLevel;
|
| 25 | import net.minecraft.world.level.block.Blocks;
|
| 26 | import net.minecraft.world.level.block.FireBlock;
|
| 27 | import net.minecraft.world.level.block.IronBarsBlock;
|
| 28 | import net.minecraft.world.level.block.state.BlockState;
|
| 29 | import net.minecraft.world.level.chunk.ChunkGenerator;
|
| 30 | import net.minecraft.world.level.dimension.DimensionType;
|
| 31 | import net.minecraft.world.phys.AABB;
|
| 32 |
|
| 33 | public record EndSpikeFeature(List<EndSpikeFeature.EndSpike> spikes, boolean crystalInvulnerable, Optional<BlockPos> crystalBeamTarget) implements Feature {
|
| 34 | private static final int NUMBER_OF_SPIKES = 10;
|
| 35 | private static final int SPIKE_DISTANCE = 42;
|
| 36 | private static final LoadingCache<Long, List<EndSpikeFeature.EndSpike>> SPIKE_CACHE = CacheBuilder.newBuilder()
|
| 37 | .expireAfterWrite(5L, TimeUnit.MINUTES)
|
| 38 | .build(new EndSpikeFeature.SpikeCacheLoader());
|
| 39 | public static final MapCodec<EndSpikeFeature> CODEC = RecordCodecBuilder.mapCodec(
|
| 40 | i -> i.group(
|
| 41 | EndSpikeFeature.EndSpike.CODEC.listOf().fieldOf("spikes").forGetter(EndSpikeFeature::spikes),
|
| 42 | Codec.BOOL.optionalFieldOf("crystal_invulnerable", false).forGetter(EndSpikeFeature::crystalInvulnerable),
|
| 43 | BlockPos.CODEC.optionalFieldOf("crystal_beam_target").forGetter(EndSpikeFeature::crystalBeamTarget)
|
| 44 | )
|
| 45 | .apply(i, EndSpikeFeature::new)
|
| 46 | );
|
| 47 |
|
| 48 | public static List<EndSpikeFeature.EndSpike> getSpikesForLevel(final WorldGenLevel level) {
|
| 49 | RandomSource random = RandomSource.createThreadLocalInstance(level.getSeed());
|
| 50 | long key = random.nextLong() & 65535L;
|
| 51 | return SPIKE_CACHE.getUnchecked(key);
|
| 52 | }
|
| 53 |
|
| 54 | @Override
|
| 55 | public MapCodec<EndSpikeFeature> codec() {
|
| 56 | return CODEC;
|
| 57 | }
|
| 58 |
|
| 59 | @Override
|
| 60 | public boolean place(final WorldGenLevel level, final ChunkGenerator chunkGenerator, final RandomSource random, final BlockPos origin) {
|
| 61 | List<EndSpikeFeature.EndSpike> spikes = this.spikes;
|
| 62 | if (spikes.isEmpty()) {
|
| 63 | spikes = getSpikesForLevel(level);
|
| 64 | }
|
| 65 |
|
| 66 | for (EndSpikeFeature.EndSpike spike : spikes) {
|
| 67 | if (spike.isCenterWithinChunk(origin)) {
|
| 68 | this.placeSpike(level, random, spike);
|
| 69 | }
|
| 70 | }
|
| 71 |
|
| 72 | return true;
|
| 73 | }
|
| 74 |
|
| 75 | private void placeSpike(final ServerLevelAccessor level, final RandomSource random, final EndSpikeFeature.EndSpike spike) {
|
| 76 | int radius = spike.getRadius();
|
| 77 |
|
| 78 | for (BlockPos pos : BlockPos.betweenClosed(
|
| 79 | new BlockPos(spike.getCenterX() - radius, level.getMinY(), spike.getCenterZ() - radius),
|
| 80 | new BlockPos(spike.getCenterX() + radius, spike.getHeight() + 10, spike.getCenterZ() + radius)
|
| 81 | )) {
|
| 82 | if (pos.distToLowCornerSqr(spike.getCenterX(), pos.getY(), spike.getCenterZ()) <= radius * radius + 1 && pos.getY() < spike.getHeight()) {
|
| 83 | this.setBlock(level, pos, Blocks.OBSIDIAN.defaultBlockState());
|
| 84 | } else if (pos.getY() > 65) {
|
| 85 | this.setBlock(level, pos, Blocks.AIR.defaultBlockState());
|
| 86 | }
|
| 87 | }
|
| 88 |
|
| 89 | if (spike.isGuarded()) {
|
| 90 | int start = -2;
|
| 91 | int end = 2;
|
| 92 | int yEnd = 3;
|
| 93 | BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos();
|
| 94 |
|
| 95 | for (int dx = -2; dx <= 2; dx++) {
|
| 96 | for (int dz = -2; dz <= 2; dz++) {
|
| 97 | for (int dy = 0; dy <= 3; dy++) {
|
| 98 | boolean isXSide = Mth.abs(dx) == 2;
|
| 99 | boolean isZSide = Mth.abs(dz) == 2;
|
| 100 | boolean top = dy == 3;
|
| 101 | if (isXSide || isZSide || top) {
|
| 102 | boolean xEdge = dx == -2 || dx == 2 || top;
|
| 103 | boolean zEdge = dz == -2 || dz == 2 || top;
|
| 104 | BlockState state = Blocks.IRON_BARS
|
| 105 | .defaultBlockState()
|
| 106 | .setValue(IronBarsBlock.NORTH, xEdge && dz != -2)
|
| 107 | .setValue(IronBarsBlock.SOUTH, xEdge && dz != 2)
|
| 108 | .setValue(IronBarsBlock.WEST, zEdge && dx != -2)
|
| 109 | .setValue(IronBarsBlock.EAST, zEdge && dx != 2);
|
| 110 | this.setBlock(level, pos.set(spike.getCenterX() + dx, spike.getHeight() + dy, spike.getCenterZ() + dz), state);
|
| 111 | }
|
| 112 | }
|
| 113 | }
|
| 114 | }
|
| 115 | }
|
| 116 |
|
| 117 | EndCrystal endCrystal = EntityTypes.END_CRYSTAL.create(level.getLevel(), EntitySpawnReason.STRUCTURE);
|
| 118 | if (endCrystal != null) {
|
| 119 | endCrystal.setBeamTarget(this.crystalBeamTarget.orElse(null));
|
| 120 | endCrystal.setPermanentlyInvulnerable(this.crystalInvulnerable);
|
| 121 | endCrystal.snapTo(spike.getCenterX() + 0.5, spike.getHeight() + 1, spike.getCenterZ() + 0.5, random.nextFloat() * 360.0F, 0.0F);
|
| 122 | level.addFreshEntity(endCrystal);
|
| 123 | BlockPos crystalPos = endCrystal.blockPosition();
|
| 124 | this.setBlock(level, crystalPos.below(), Blocks.BEDROCK.defaultBlockState());
|
| 125 | this.setBlock(level, crystalPos, FireBlock.getState(level, crystalPos));
|
| 126 | }
|
| 127 | }
|
| 128 |
|
| 129 | public static class EndSpike {
|
| 130 | public static final Codec<EndSpikeFeature.EndSpike> CODEC = RecordCodecBuilder.create(
|
| 131 | i -> i.group(
|
| 132 | Codec.INT.optionalFieldOf("centerX", 0).forGetter(s -> s.centerX),
|
| 133 | Codec.INT.optionalFieldOf("centerZ", 0).forGetter(s -> s.centerZ),
|
| 134 | Codec.INT.optionalFieldOf("radius", 0).forGetter(s -> s.radius),
|
| 135 | Codec.INT.optionalFieldOf("height", 0).forGetter(s -> s.height),
|
| 136 | Codec.BOOL.optionalFieldOf("guarded", false).forGetter(s -> s.guarded)
|
| 137 | )
|
| 138 | .apply(i, EndSpikeFeature.EndSpike::new)
|
| 139 | );
|
| 140 | private final int centerX;
|
| 141 | private final int centerZ;
|
| 142 | private final int radius;
|
| 143 | private final int height;
|
| 144 | private final boolean guarded;
|
| 145 | private final AABB topBoundingBox;
|
| 146 |
|
| 147 | public EndSpike(final int centerX, final int centerZ, final int radius, final int height, final boolean guarded) {
|
| 148 | this.centerX = centerX;
|
| 149 | this.centerZ = centerZ;
|
| 150 | this.radius = radius;
|
| 151 | this.height = height;
|
| 152 | this.guarded = guarded;
|
| 153 | this.topBoundingBox = new AABB(centerX - radius, DimensionType.MIN_Y, centerZ - radius, centerX + radius, DimensionType.MAX_Y, centerZ + radius);
|
| 154 | }
|
| 155 |
|
| 156 | public boolean isCenterWithinChunk(final BlockPos chunkOrigin) {
|
| 157 | return SectionPos.blockToSectionCoord(chunkOrigin.getX()) == SectionPos.blockToSectionCoord(this.centerX)
|
| 158 | && SectionPos.blockToSectionCoord(chunkOrigin.getZ()) == SectionPos.blockToSectionCoord(this.centerZ);
|
| 159 | }
|
| 160 |
|
| 161 | public int getCenterX() {
|
| 162 | return this.centerX;
|
| 163 | }
|
| 164 |
|
| 165 | public int getCenterZ() {
|
| 166 | return this.centerZ;
|
| 167 | }
|
| 168 |
|
| 169 | public int getRadius() {
|
| 170 | return this.radius;
|
| 171 | }
|
| 172 |
|
| 173 | public int getHeight() {
|
| 174 | return this.height;
|
| 175 | }
|
| 176 |
|
| 177 | public boolean isGuarded() {
|
| 178 | return this.guarded;
|
| 179 | }
|
| 180 |
|
| 181 | public AABB getTopBoundingBox() {
|
| 182 | return this.topBoundingBox;
|
| 183 | }
|
| 184 | }
|
| 185 |
|
| 186 | private static class SpikeCacheLoader extends CacheLoader<Long, List<EndSpikeFeature.EndSpike>> {
|
| 187 | public List<EndSpikeFeature.EndSpike> load(final Long seed) {
|
| 188 | IntArrayList sizes = Util.toShuffledList(IntStream.range(0, 10), RandomSource.createThreadLocalInstance(seed));
|
| 189 | List<EndSpikeFeature.EndSpike> result = Lists.newArrayList();
|
| 190 |
|
| 191 | for (int i = 0; i < 10; i++) {
|
| 192 | int x = Mth.floor(42.0 * Math.cos(2.0 * (-Math.PI + (Math.PI / 10) * i)));
|
| 193 | int z = Mth.floor(42.0 * Math.sin(2.0 * (-Math.PI + (Math.PI / 10) * i)));
|
| 194 | int size = sizes.get(i);
|
| 195 | int radius = 2 + size / 3;
|
| 196 | int height = 76 + size * 3;
|
| 197 | boolean guarded = size == 1 || size == 2;
|
| 198 | result.add(new EndSpikeFeature.EndSpike(x, z, radius, height, guarded));
|
| 199 | }
|
| 200 |
|
| 201 | return result;
|
| 202 | }
|
| 203 | }
|
| 204 | }
|
| 205 |
|