CatSpawner .java

net.minecraft.world.entity.npc.CatSpawner (class): members: nextTick, TICK_DELAY, tick. imports: SpawnPlacements, CustomSpawner, StructureTags, Cat.

net.minecraft.world.entity.npcclass78 linescommon2.9 KB

Implements

API Surface (1)

void tick(final ServerLevel level, final boolean spawnEnemies)

Fields & Constants (2)

TICK_DELAY
nextTick

Full Source

CatSpawner.java78 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.npc;
2
3
import java.util.List;
4
import net.minecraft.core.BlockPos;
5
import net.minecraft.server.level.ServerLevel;
6
import net.minecraft.tags.StructureTags;
7
import net.minecraft.util.RandomSource;
8
import net.minecraft.world.entity.EntitySpawnReason;
9
import net.minecraft.world.entity.EntityTypes;
10
import net.minecraft.world.entity.SpawnPlacements;
11
import net.minecraft.world.entity.ai.village.poi.PoiManager;
12
import net.minecraft.world.entity.ai.village.poi.PoiTypes;
13
import net.minecraft.world.entity.animal.feline.Cat;
14
import net.minecraft.world.entity.player.Player;
15
import net.minecraft.world.level.CustomSpawner;
16
import net.minecraft.world.phys.AABB;
17
18
public class CatSpawner implements CustomSpawner {
19
	private static final int TICK_DELAY = 1200;
20
	private int nextTick;
21
22
	@Override
23
	public void tick(final ServerLevel level, final boolean spawnEnemies) {
24
		this.nextTick--;
25
		if (this.nextTick <= 0) {
26
			this.nextTick = 1200;
27
			Player player = level.getRandomPlayer();
28
			if (player != null) {
29
				RandomSource random = level.getRandom();
30
				int x = (8 + random.nextInt(24)) * (random.nextBoolean() ? -1 : 1);
31
				int z = (8 + random.nextInt(24)) * (random.nextBoolean() ? -1 : 1);
32
				BlockPos spawnPos = player.blockPosition().offset(x, 0, z);
33
				int delta = 10;
34
				if (level.hasChunksAt(spawnPos.getX() - 10, spawnPos.getZ() - 10, spawnPos.getX() + 10, spawnPos.getZ() + 10)) {
35
					if (SpawnPlacements.isSpawnPositionOk(EntityTypes.CAT, level, spawnPos)) {
36
						if (level.isCloseToVillage(spawnPos, 2)) {
37
							this.spawnInVillage(level, spawnPos);
38
						} else if (level.structureManager().getStructureWithPieceAt(spawnPos, StructureTags.CATS_SPAWN_IN).isValid()) {
39
							this.spawnInHut(level, spawnPos);
40
						}
41
					}
42
				}
43
			}
44
		}
45
	}
46
47
	private void spawnInVillage(final ServerLevel serverLevel, final BlockPos spawnPos) {
48
		int radius = 48;
49
		if (serverLevel.getPoiManager().getCountInRange(p -> p.is(PoiTypes.HOME), spawnPos, 48, PoiManager.Occupancy.IS_OCCUPIED) > 4L) {
50
			List<Cat> cats = serverLevel.getEntitiesOfClass(Cat.class, new AABB(spawnPos).inflate(48.0, 8.0, 48.0));
51
			if (cats.size() < 5) {
52
				this.spawnCat(spawnPos, serverLevel, false);
53
			}
54
		}
55
	}
56
57
	private void spawnInHut(final ServerLevel level, final BlockPos spawnPos) {
58
		int radius = 16;
59
		List<Cat> cats = level.getEntitiesOfClass(Cat.class, new AABB(spawnPos).inflate(16.0, 8.0, 16.0));
60
		if (cats.isEmpty()) {
61
			this.spawnCat(spawnPos, level, true);
62
		}
63
	}
64
65
	private void spawnCat(final BlockPos spawnPos, final ServerLevel level, final boolean makePersistent) {
66
		Cat cat = EntityTypes.CAT.create(level, EntitySpawnReason.NATURAL);
67
		if (cat != null) {
68
			cat.finalizeSpawn(level, level.getCurrentDifficultyAt(spawnPos), EntitySpawnReason.NATURAL, null);
69
			if (makePersistent) {
70
				cat.setPersistenceRequired();
71
			}
72
73
			cat.snapTo(spawnPos, 0.0F, 0.0F);
74
			level.addFreshEntityWithPassengers(cat);
75
		}
76
	}
77
}
78

Decompiled Minecraft 26.3 source of CatSpawner (net/minecraft/world/entity/npc/CatSpawner.java) with Mojang official mappings, stored verbatim on the site.