| 1 | package net.minecraft.world.entity.ai.village;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import java.util.Optional;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.core.Holder;
|
| 7 | import net.minecraft.server.level.ServerLevel;
|
| 8 | import net.minecraft.tags.BiomeTags;
|
| 9 | import net.minecraft.util.Mth;
|
| 10 | import net.minecraft.util.RandomSource;
|
| 11 | import net.minecraft.world.clock.ClockTimeMarkers;
|
| 12 | import net.minecraft.world.clock.WorldClock;
|
| 13 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 14 | import net.minecraft.world.entity.EntityTypes;
|
| 15 | import net.minecraft.world.entity.monster.Monster;
|
| 16 | import net.minecraft.world.entity.monster.zombie.Zombie;
|
| 17 | import net.minecraft.world.entity.player.Player;
|
| 18 | import net.minecraft.world.level.CustomSpawner;
|
| 19 | import net.minecraft.world.level.levelgen.Heightmap;
|
| 20 | import net.minecraft.world.phys.Vec3;
|
| 21 | import org.jspecify.annotations.Nullable;
|
| 22 | import org.slf4j.Logger;
|
| 23 |
|
| 24 | public class VillageSiege implements CustomSpawner {
|
| 25 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 26 | private boolean hasSetupSiege;
|
| 27 | private VillageSiege.State siegeState = VillageSiege.State.SIEGE_DONE;
|
| 28 | private int zombiesToSpawn;
|
| 29 | private int nextSpawnTime;
|
| 30 | private int spawnX;
|
| 31 | private int spawnY;
|
| 32 | private int spawnZ;
|
| 33 |
|
| 34 | @Override
|
| 35 | public void tick(final ServerLevel level, final boolean spawnEnemies) {
|
| 36 | if (!level.isBrightOutside() && spawnEnemies) {
|
| 37 | Optional<Holder<WorldClock>> defaultClock = level.dimensionType().defaultClock();
|
| 38 | if (defaultClock.isPresent() && level.clockManager().isAtTimeMarker(defaultClock.get(), ClockTimeMarkers.ROLL_VILLAGE_SIEGE)) {
|
| 39 | this.siegeState = level.getRandom().nextInt(10) == 0 ? VillageSiege.State.SIEGE_TONIGHT : VillageSiege.State.SIEGE_DONE;
|
| 40 | }
|
| 41 |
|
| 42 | if (this.siegeState != VillageSiege.State.SIEGE_DONE) {
|
| 43 | if (!this.hasSetupSiege) {
|
| 44 | if (!this.tryToSetupSiege(level)) {
|
| 45 | return;
|
| 46 | }
|
| 47 |
|
| 48 | this.hasSetupSiege = true;
|
| 49 | }
|
| 50 |
|
| 51 | if (this.nextSpawnTime > 0) {
|
| 52 | this.nextSpawnTime--;
|
| 53 | } else {
|
| 54 | this.nextSpawnTime = 2;
|
| 55 | if (this.zombiesToSpawn > 0) {
|
| 56 | this.trySpawn(level);
|
| 57 | this.zombiesToSpawn--;
|
| 58 | } else {
|
| 59 | this.siegeState = VillageSiege.State.SIEGE_DONE;
|
| 60 | }
|
| 61 | }
|
| 62 | }
|
| 63 | } else {
|
| 64 | this.siegeState = VillageSiege.State.SIEGE_DONE;
|
| 65 | this.hasSetupSiege = false;
|
| 66 | }
|
| 67 | }
|
| 68 |
|
| 69 | private boolean tryToSetupSiege(final ServerLevel level) {
|
| 70 | RandomSource random = level.getRandom();
|
| 71 |
|
| 72 | for (Player player : level.players()) {
|
| 73 | if (!player.isSpectator()) {
|
| 74 | BlockPos center = player.blockPosition();
|
| 75 | if (level.isVillage(center) && !level.getBiome(center).is(BiomeTags.WITHOUT_ZOMBIE_SIEGES)) {
|
| 76 | for (int i = 0; i < 10; i++) {
|
| 77 | float angle = random.nextFloat() * (float) (Math.PI * 2);
|
| 78 | this.spawnX = center.getX() + Mth.floor(Mth.cos(angle) * 32.0F);
|
| 79 | this.spawnY = center.getY();
|
| 80 | this.spawnZ = center.getZ() + Mth.floor(Mth.sin(angle) * 32.0F);
|
| 81 | if (this.findRandomSpawnPos(level, new BlockPos(this.spawnX, this.spawnY, this.spawnZ)) != null) {
|
| 82 | this.nextSpawnTime = 0;
|
| 83 | this.zombiesToSpawn = 20;
|
| 84 | break;
|
| 85 | }
|
| 86 | }
|
| 87 |
|
| 88 | return true;
|
| 89 | }
|
| 90 | }
|
| 91 | }
|
| 92 |
|
| 93 | return false;
|
| 94 | }
|
| 95 |
|
| 96 | private void trySpawn(final ServerLevel level) {
|
| 97 | Vec3 spawnPos = this.findRandomSpawnPos(level, new BlockPos(this.spawnX, this.spawnY, this.spawnZ));
|
| 98 | if (spawnPos != null) {
|
| 99 | Zombie zombie;
|
| 100 | try {
|
| 101 | zombie = new Zombie(level);
|
| 102 | zombie.finalizeSpawn(level, level.getCurrentDifficultyAt(zombie.blockPosition()), EntitySpawnReason.EVENT, null);
|
| 103 | } catch (Exception e) {
|
| 104 | LOGGER.warn("Failed to create zombie for village siege at {}", spawnPos, e);
|
| 105 | return;
|
| 106 | }
|
| 107 |
|
| 108 | zombie.snapTo(spawnPos.x, spawnPos.y, spawnPos.z, level.getRandom().nextFloat() * 360.0F, 0.0F);
|
| 109 | level.addFreshEntityWithPassengers(zombie);
|
| 110 | }
|
| 111 | }
|
| 112 |
|
| 113 | private @Nullable Vec3 findRandomSpawnPos(final ServerLevel level, final BlockPos pos) {
|
| 114 | RandomSource random = level.getRandom();
|
| 115 |
|
| 116 | for (int i = 0; i < 10; i++) {
|
| 117 | int x = pos.getX() + random.nextInt(16) - 8;
|
| 118 | int z = pos.getZ() + random.nextInt(16) - 8;
|
| 119 | int y = level.getHeight(Heightmap.Types.WORLD_SURFACE, x, z);
|
| 120 | BlockPos offset = new BlockPos(x, y, z);
|
| 121 | if (level.isVillage(offset) && Monster.checkMonsterSpawnRules(EntityTypes.ZOMBIE, level, EntitySpawnReason.EVENT, offset, random)) {
|
| 122 | return Vec3.atBottomCenterOf(offset);
|
| 123 | }
|
| 124 | }
|
| 125 |
|
| 126 | return null;
|
| 127 | }
|
| 128 |
|
| 129 | private enum State {
|
| 130 | SIEGE_CAN_ACTIVATE,
|
| 131 | SIEGE_TONIGHT,
|
| 132 | SIEGE_DONE;
|
| 133 | }
|
| 134 | }
|
| 135 |
|