PatrolSpawner .java

net.minecraft.world.level.levelgen.PatrolSpawner (class): members: nextTick, tick. imports: PatrollingMonster, CustomSpawner, NaturalSpawner, GameRules.

net.minecraft.world.level.levelgenclass93 linescommon3.5 KB

Implements

API Surface (1)

void tick(final ServerLevel level, final boolean spawnEnemies)

Fields & Constants (1)

nextTick

Full Source

PatrolSpawner.java93 lines · Minecraft 26.3 · verbatim
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.util.RandomSource;
6
import net.minecraft.world.attribute.EnvironmentAttributes;
7
import net.minecraft.world.entity.EntitySpawnReason;
8
import net.minecraft.world.entity.EntityTypes;
9
import net.minecraft.world.entity.monster.PatrollingMonster;
10
import net.minecraft.world.entity.player.Player;
11
import net.minecraft.world.level.CustomSpawner;
12
import net.minecraft.world.level.NaturalSpawner;
13
import net.minecraft.world.level.block.state.BlockState;
14
import net.minecraft.world.level.gamerules.GameRules;
15
16
public class PatrolSpawner implements CustomSpawner {
17
	private int nextTick;
18
19
	@Override
20
	public void tick(final ServerLevel level, final boolean spawnEnemies) {
21
		if (spawnEnemies) {
22
			if (level.getGameRules().get(GameRules.SPAWN_PATROLS)) {
23
				RandomSource random = level.getRandom();
24
				this.nextTick--;
25
				if (this.nextTick <= 0) {
26
					this.nextTick = this.nextTick + 12000 + random.nextInt(1200);
27
					if (level.isBrightOutside()) {
28
						if (random.nextInt(5) == 0) {
29
							int playerCount = level.players().size();
30
							if (playerCount >= 1) {
31
								Player player = level.players().get(random.nextInt(playerCount));
32
								if (!player.isSpectator()) {
33
									if (!level.isCloseToVillage(player.blockPosition(), 2)) {
34
										int x = (24 + random.nextInt(24)) * (random.nextBoolean() ? -1 : 1);
35
										int z = (24 + random.nextInt(24)) * (random.nextBoolean() ? -1 : 1);
36
										BlockPos.MutableBlockPos spawnPos = player.blockPosition().mutable().move(x, 0, z);
37
										int delta = 10;
38
										if (level.hasChunksAt(spawnPos.getX() - 10, spawnPos.getZ() - 10, spawnPos.getX() + 10, spawnPos.getZ() + 10)) {
39
											if (level.environmentAttributes().getValue(EnvironmentAttributes.CAN_PILLAGER_PATROL_SPAWN, spawnPos)) {
40
												int groupSize = (int)Math.ceil(level.getCurrentDifficultyAt(spawnPos).getEffectiveDifficulty()) + 1;
41
42
												for (int i = 0; i < groupSize; i++) {
43
													spawnPos.setY(level.getHeightmapPos(Heightmap.Types.MOTION_BLOCKING_NO_LEAVES, spawnPos).getY());
44
													if (i == 0) {
45
														if (!this.spawnPatrolMember(level, spawnPos, random, true)) {
46
															break;
47
														}
48
													} else {
49
														this.spawnPatrolMember(level, spawnPos, random, false);
50
													}
51
52
													spawnPos.setX(spawnPos.getX() + random.nextInt(5) - random.nextInt(5));
53
													spawnPos.setZ(spawnPos.getZ() + random.nextInt(5) - random.nextInt(5));
54
												}
55
											}
56
										}
57
									}
58
								}
59
							}
60
						}
61
					}
62
				}
63
			}
64
		}
65
	}
66
67
	private boolean spawnPatrolMember(final ServerLevel level, final BlockPos pos, final RandomSource random, final boolean isLeader) {
68
		BlockState state = level.getBlockState(pos);
69
		if (!NaturalSpawner.isValidEmptySpawnBlock(level, pos, state, state.getFluidState(), EntityTypes.PILLAGER)) {
70
			return false;
71
		}
72
73
		if (!PatrollingMonster.checkPatrollingMonsterSpawnRules(EntityTypes.PILLAGER, level, EntitySpawnReason.PATROL, pos, random)) {
74
			return false;
75
		}
76
77
		PatrollingMonster mob = EntityTypes.PILLAGER.create(level, EntitySpawnReason.PATROL);
78
		if (mob != null) {
79
			if (isLeader) {
80
				mob.setPatrolLeader(true);
81
				mob.findPatrolTarget();
82
			}
83
84
			mob.setPos(pos.getX(), pos.getY(), pos.getZ());
85
			mob.finalizeSpawn(level, level.getCurrentDifficultyAt(pos), EntitySpawnReason.PATROL, null);
86
			level.addFreshEntityWithPassengers(mob);
87
			return true;
88
		} else {
89
			return false;
90
		}
91
	}
92
}
93

Decompiled Minecraft 26.3 source of PatrolSpawner (net/minecraft/world/level/levelgen/PatrolSpawner.java) with Mojang official mappings, stored verbatim on the site.