| 1 | package net.minecraft.world.level.levelgen;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.server.level.ServerLevel;
|
| 5 | import net.minecraft.server.level.ServerPlayer;
|
| 6 | import net.minecraft.stats.ServerStatsCounter;
|
| 7 | import net.minecraft.stats.Stats;
|
| 8 | import net.minecraft.util.Mth;
|
| 9 | import net.minecraft.util.RandomSource;
|
| 10 | import net.minecraft.world.DifficultyInstance;
|
| 11 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 12 | import net.minecraft.world.entity.EntityTypes;
|
| 13 | import net.minecraft.world.entity.SpawnGroupData;
|
| 14 | import net.minecraft.world.entity.monster.Phantom;
|
| 15 | import net.minecraft.world.level.CustomSpawner;
|
| 16 | import net.minecraft.world.level.NaturalSpawner;
|
| 17 | import net.minecraft.world.level.block.state.BlockState;
|
| 18 | import net.minecraft.world.level.gamerules.GameRules;
|
| 19 | import net.minecraft.world.level.material.FluidState;
|
| 20 |
|
| 21 | public class PhantomSpawner implements CustomSpawner {
|
| 22 | private int nextTick;
|
| 23 |
|
| 24 | @Override
|
| 25 | public void tick(final ServerLevel level, final boolean spawnEnemies) {
|
| 26 | if (spawnEnemies) {
|
| 27 | if (level.getGameRules().get(GameRules.SPAWN_PHANTOMS)) {
|
| 28 | RandomSource random = level.getRandom();
|
| 29 | this.nextTick--;
|
| 30 | if (this.nextTick <= 0) {
|
| 31 | this.nextTick = this.nextTick + (60 + random.nextInt(60)) * 20;
|
| 32 | if (level.getSkyDarken() >= 5 || !level.dimensionType().hasSkyLight()) {
|
| 33 | for (ServerPlayer player : level.players()) {
|
| 34 | if (!player.isSpectator()) {
|
| 35 | BlockPos playerPos = player.blockPosition();
|
| 36 | if (!level.dimensionType().hasSkyLight() || playerPos.getY() >= level.getSeaLevel() && level.canSeeSky(playerPos)) {
|
| 37 | DifficultyInstance difficulty = level.getCurrentDifficultyAt(playerPos);
|
| 38 | if (difficulty.isHarderThan(random.nextFloat() * 3.0F)) {
|
| 39 | ServerStatsCounter stats = player.getStats();
|
| 40 | int value = Mth.clamp(stats.getValue(Stats.CUSTOM.get(Stats.TIME_SINCE_REST)), 1, Integer.MAX_VALUE);
|
| 41 | int dayLength = 24000;
|
| 42 | if (random.nextInt(value) >= 72000) {
|
| 43 | BlockPos spawnPos = playerPos.above(20 + random.nextInt(15)).east(-10 + random.nextInt(21)).south(-10 + random.nextInt(21));
|
| 44 | BlockState blockState = level.getBlockState(spawnPos);
|
| 45 | FluidState fluidState = level.getFluidState(spawnPos);
|
| 46 | if (NaturalSpawner.isValidEmptySpawnBlock(level, spawnPos, blockState, fluidState, EntityTypes.PHANTOM)) {
|
| 47 | SpawnGroupData groupData = null;
|
| 48 | int groupSize = 1 + random.nextInt(difficulty.getDifficulty().getId() + 1);
|
| 49 |
|
| 50 | for (int i = 0; i < groupSize; i++) {
|
| 51 | Phantom phantom = EntityTypes.PHANTOM.create(level, EntitySpawnReason.NATURAL);
|
| 52 | if (phantom != null) {
|
| 53 | phantom.snapTo(spawnPos, 0.0F, 0.0F);
|
| 54 | groupData = phantom.finalizeSpawn(level, difficulty, EntitySpawnReason.NATURAL, groupData);
|
| 55 | level.addFreshEntityWithPassengers(phantom);
|
| 56 | }
|
| 57 | }
|
| 58 | }
|
| 59 | }
|
| 60 | }
|
| 61 | }
|
| 62 | }
|
| 63 | }
|
| 64 | }
|
| 65 | }
|
| 66 | }
|
| 67 | }
|
| 68 | }
|
| 69 | }
|
| 70 |
|