| 1 | package net.minecraft.world.level;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import java.util.Optional;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.core.particles.ParticleTypes;
|
| 7 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 8 | import net.minecraft.nbt.CompoundTag;
|
| 9 | import net.minecraft.server.level.ServerLevel;
|
| 10 | import net.minecraft.util.ProblemReporter;
|
| 11 | import net.minecraft.util.RandomSource;
|
| 12 | import net.minecraft.util.random.WeightedList;
|
| 13 | import net.minecraft.world.Difficulty;
|
| 14 | import net.minecraft.world.entity.Entity;
|
| 15 | import net.minecraft.world.entity.EntityProcessor;
|
| 16 | import net.minecraft.world.entity.EntitySelector;
|
| 17 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 18 | import net.minecraft.world.entity.EntitySpawnRequest;
|
| 19 | import net.minecraft.world.entity.EntityType;
|
| 20 | import net.minecraft.world.entity.Mob;
|
| 21 | import net.minecraft.world.entity.SpawnPlacements;
|
| 22 | import net.minecraft.world.item.component.TypedEntityData;
|
| 23 | import net.minecraft.world.level.entity.EntityTypeTest;
|
| 24 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 25 | import net.minecraft.world.level.storage.TagValueInput;
|
| 26 | import net.minecraft.world.level.storage.ValueInput;
|
| 27 | import net.minecraft.world.level.storage.ValueOutput;
|
| 28 | import net.minecraft.world.phys.AABB;
|
| 29 | import net.minecraft.world.phys.Vec3;
|
| 30 | import org.jspecify.annotations.Nullable;
|
| 31 | import org.slf4j.Logger;
|
| 32 |
|
| 33 | public abstract class BaseSpawner {
|
| 34 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 35 | private static final int SPAWNER_ENTITY_ID = -1;
|
| 36 | public static final EntityProcessor SET_DISPLAY_ENTITY_ID = e -> {
|
| 37 | e.setId(-1);
|
| 38 | return e;
|
| 39 | };
|
| 40 | public static final String SPAWN_DATA_TAG = "SpawnData";
|
| 41 | private static final int EVENT_SPAWN = 1;
|
| 42 | private static final int DEFAULT_SPAWN_DELAY = 20;
|
| 43 | private static final int DEFAULT_MIN_SPAWN_DELAY = 200;
|
| 44 | private static final int DEFAULT_MAX_SPAWN_DELAY = 800;
|
| 45 | private static final int DEFAULT_SPAWN_COUNT = 4;
|
| 46 | private static final int DEFAULT_MAX_NEARBY_ENTITIES = 6;
|
| 47 | private static final int DEFAULT_REQUIRED_PLAYER_RANGE = 16;
|
| 48 | private static final int DEFAULT_SPAWN_RANGE = 4;
|
| 49 | private int spawnDelay = 20;
|
| 50 | private WeightedList<SpawnData> spawnPotentials = WeightedList.of();
|
| 51 | private @Nullable SpawnData nextSpawnData;
|
| 52 | private double spin;
|
| 53 | private double oSpin;
|
| 54 | private int minSpawnDelay = 200;
|
| 55 | private int maxSpawnDelay = 800;
|
| 56 | private int spawnCount = 4;
|
| 57 | private @Nullable Entity displayEntity;
|
| 58 | private int maxNearbyEntities = 6;
|
| 59 | private int requiredPlayerRange = 16;
|
| 60 | private int spawnRange = 4;
|
| 61 |
|
| 62 | public void setEntityId(final EntityType<?> type, final @Nullable Level level, final RandomSource random, final BlockPos pos) {
|
| 63 | this.getOrCreateNextSpawnData(level, random, pos).getEntityToSpawn().putString("id", BuiltInRegistries.ENTITY_TYPE.getKey(type).toString());
|
| 64 | }
|
| 65 |
|
| 66 | public void setEntityData(final TypedEntityData<EntityType<?>> entityData, final @Nullable Level level, final RandomSource random, final BlockPos pos) {
|
| 67 | SpawnData spawnData = this.getOrCreateNextSpawnData(level, random, pos);
|
| 68 | entityData.loadInto(spawnData, BuiltInRegistries.ENTITY_TYPE);
|
| 69 | }
|
| 70 |
|
| 71 | private boolean isNearPlayer(final Level level, final BlockPos pos) {
|
| 72 | return level.hasNearbyAlivePlayer(pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, this.requiredPlayerRange);
|
| 73 | }
|
| 74 |
|
| 75 | public void clientTick(final Level level, final BlockPos pos) {
|
| 76 | if (!this.isNearPlayer(level, pos)) {
|
| 77 | this.oSpin = this.spin;
|
| 78 | } else if (this.displayEntity != null) {
|
| 79 | RandomSource random = level.getRandom();
|
| 80 | double xP = pos.getX() + random.nextDouble();
|
| 81 | double yP = pos.getY() + random.nextDouble();
|
| 82 | double zP = pos.getZ() + random.nextDouble();
|
| 83 | level.addParticle(ParticleTypes.SMOKE, xP, yP, zP, 0.0, 0.0, 0.0);
|
| 84 | level.addParticle(ParticleTypes.FLAME, xP, yP, zP, 0.0, 0.0, 0.0);
|
| 85 | if (this.spawnDelay > 0) {
|
| 86 | this.spawnDelay--;
|
| 87 | }
|
| 88 |
|
| 89 | this.oSpin = this.spin;
|
| 90 | this.spin = (this.spin + 1000.0F / (this.spawnDelay + 200.0F)) % 360.0;
|
| 91 | }
|
| 92 | }
|
| 93 |
|
| 94 | public void serverTick(final ServerLevel level, final BlockPos pos) {
|
| 95 | if (this.isNearPlayer(level, pos) && level.isSpawnerBlockEnabled()) {
|
| 96 | if (this.spawnDelay == -1) {
|
| 97 | this.delay(level, pos);
|
| 98 | }
|
| 99 |
|
| 100 | if (this.spawnDelay > 0) {
|
| 101 | this.spawnDelay--;
|
| 102 | } else {
|
| 103 | boolean delay = false;
|
| 104 | RandomSource random = level.getRandom();
|
| 105 | SpawnData nextSpawnData = this.getOrCreateNextSpawnData(level, random, pos);
|
| 106 |
|
| 107 | for (int c = 0; c < this.spawnCount; c++) {
|
| 108 | try (ProblemReporter.ScopedCollector reporter = new ProblemReporter.ScopedCollector(this::toString, LOGGER)) {
|
| 109 | ValueInput input = TagValueInput.create(reporter, level.registryAccess(), nextSpawnData.getEntityToSpawn());
|
| 110 | Optional<EntityType<?>> entityType = EntityType.by(input);
|
| 111 | if (entityType.isEmpty()) {
|
| 112 | this.delay(level, pos);
|
| 113 | return;
|
| 114 | }
|
| 115 |
|
| 116 | Vec3 spawnPos = input.read("Pos", Vec3.CODEC)
|
| 117 | .orElseGet(
|
| 118 | () -> new Vec3(
|
| 119 | pos.getX() + (random.nextDouble() - random.nextDouble()) * this.spawnRange + 0.5,
|
| 120 | pos.getY() + random.nextInt(3) - 1,
|
| 121 | pos.getZ() + (random.nextDouble() - random.nextDouble()) * this.spawnRange + 0.5
|
| 122 | )
|
| 123 | );
|
| 124 | if (level.noCollision(entityType.get().getSpawnAABB(spawnPos.x, spawnPos.y, spawnPos.z))) {
|
| 125 | BlockPos spawnBlockPos = BlockPos.containing(spawnPos);
|
| 126 | if (nextSpawnData.getCustomSpawnRules().isPresent()) {
|
| 127 | if (!entityType.get().getCategory().isFriendly() && level.getDifficulty() == Difficulty.PEACEFUL) {
|
| 128 | continue;
|
| 129 | }
|
| 130 |
|
| 131 | SpawnData.CustomSpawnRules customSpawnRules = nextSpawnData.getCustomSpawnRules().get();
|
| 132 | if (!customSpawnRules.isValidPosition(spawnBlockPos, level)) {
|
| 133 | continue;
|
| 134 | }
|
| 135 | } else if (!SpawnPlacements.checkSpawnRules(entityType.get(), level, EntitySpawnReason.SPAWNER, spawnBlockPos, level.getRandom())) {
|
| 136 | continue;
|
| 137 | }
|
| 138 |
|
| 139 | Entity entity = EntityType.loadEntityRecursive(input, level, EntitySpawnReason.SPAWNER, e -> {
|
| 140 | e.snapTo(spawnPos.x, spawnPos.y, spawnPos.z, e.getYRot(), e.getXRot());
|
| 141 | return e;
|
| 142 | });
|
| 143 | if (entity == null) {
|
| 144 | this.delay(level, pos);
|
| 145 | return;
|
| 146 | }
|
| 147 |
|
| 148 | int nearBy = level.getEntities(
|
| 149 | EntityTypeTest.forExactClass(entity.getClass()),
|
| 150 | new AABB(pos.getX(), pos.getY(), pos.getZ(), pos.getX() + 1, pos.getY() + 1, pos.getZ() + 1).inflate(this.spawnRange),
|
| 151 | EntitySelector.NO_SPECTATORS
|
| 152 | )
|
| 153 | .size();
|
| 154 | if (nearBy >= this.maxNearbyEntities) {
|
| 155 | this.delay(level, pos);
|
| 156 | return;
|
| 157 | }
|
| 158 |
|
| 159 | entity.snapTo(entity.getX(), entity.getY(), entity.getZ(), random.nextFloat() * 360.0F, 0.0F);
|
| 160 | if (entity instanceof Mob mob) {
|
| 161 | if (nextSpawnData.getCustomSpawnRules().isEmpty() && !mob.checkSpawnRules(level, EntitySpawnReason.SPAWNER) || !mob.checkSpawnObstruction(level)) {
|
| 162 | continue;
|
| 163 | }
|
| 164 |
|
| 165 | boolean hasNoConfiguration = nextSpawnData.getEntityToSpawn().size() == 1 && nextSpawnData.getEntityToSpawn().getString("id").isPresent();
|
| 166 | if (hasNoConfiguration) {
|
| 167 | ((Mob)entity).finalizeSpawn(level, level.getCurrentDifficultyAt(entity.blockPosition()), EntitySpawnReason.SPAWNER, null);
|
| 168 | }
|
| 169 |
|
| 170 | nextSpawnData.getEquipment().ifPresent(mob::equip);
|
| 171 | }
|
| 172 |
|
| 173 | if (!level.tryAddFreshEntityWithPassengers(entity)) {
|
| 174 | this.delay(level, pos);
|
| 175 | return;
|
| 176 | }
|
| 177 |
|
| 178 | level.levelEvent(2004, pos, 0);
|
| 179 | level.gameEvent(entity, GameEvent.ENTITY_PLACE, spawnBlockPos);
|
| 180 | if (entity instanceof Mob mob) {
|
| 181 | mob.spawnAnim();
|
| 182 | }
|
| 183 |
|
| 184 | delay = true;
|
| 185 | }
|
| 186 | }
|
| 187 | }
|
| 188 |
|
| 189 | if (delay) {
|
| 190 | this.delay(level, pos);
|
| 191 | }
|
| 192 |
|
| 193 | return;
|
| 194 | }
|
| 195 | }
|
| 196 | }
|
| 197 |
|
| 198 | private void delay(final Level level, final BlockPos pos) {
|
| 199 | RandomSource random = level.random;
|
| 200 | if (this.maxSpawnDelay <= this.minSpawnDelay) {
|
| 201 | this.spawnDelay = this.minSpawnDelay;
|
| 202 | } else {
|
| 203 | this.spawnDelay = this.minSpawnDelay + random.nextInt(this.maxSpawnDelay - this.minSpawnDelay);
|
| 204 | }
|
| 205 |
|
| 206 | this.spawnPotentials.getRandom(random).ifPresent(entry -> this.setNextSpawnData(level, pos, entry));
|
| 207 | this.broadcastEvent(level, pos, 1);
|
| 208 | }
|
| 209 |
|
| 210 | public void load(final @Nullable Level level, final BlockPos pos, final ValueInput input) {
|
| 211 | this.spawnDelay = input.getShortOr("Delay", (short)20);
|
| 212 | input.read("SpawnData", SpawnData.CODEC).ifPresent(nextSpawnData -> this.setNextSpawnData(level, pos, nextSpawnData));
|
| 213 | this.spawnPotentials = input.read("SpawnPotentials", SpawnData.LIST_CODEC)
|
| 214 | .orElseGet(() -> WeightedList.of(this.nextSpawnData != null ? this.nextSpawnData : new SpawnData()));
|
| 215 | this.minSpawnDelay = input.getIntOr("MinSpawnDelay", 200);
|
| 216 | this.maxSpawnDelay = input.getIntOr("MaxSpawnDelay", 800);
|
| 217 | this.spawnCount = input.getIntOr("SpawnCount", 4);
|
| 218 | this.maxNearbyEntities = input.getIntOr("MaxNearbyEntities", 6);
|
| 219 | this.requiredPlayerRange = input.getIntOr("RequiredPlayerRange", 16);
|
| 220 | this.spawnRange = input.getIntOr("SpawnRange", 4);
|
| 221 | this.displayEntity = null;
|
| 222 | }
|
| 223 |
|
| 224 | public void save(final ValueOutput output) {
|
| 225 | output.putShort("Delay", (short)this.spawnDelay);
|
| 226 | output.putShort("MinSpawnDelay", (short)this.minSpawnDelay);
|
| 227 | output.putShort("MaxSpawnDelay", (short)this.maxSpawnDelay);
|
| 228 | output.putShort("SpawnCount", (short)this.spawnCount);
|
| 229 | output.putShort("MaxNearbyEntities", (short)this.maxNearbyEntities);
|
| 230 | output.putShort("RequiredPlayerRange", (short)this.requiredPlayerRange);
|
| 231 | output.putShort("SpawnRange", (short)this.spawnRange);
|
| 232 | output.storeNullable("SpawnData", SpawnData.CODEC, this.nextSpawnData);
|
| 233 | output.store("SpawnPotentials", SpawnData.LIST_CODEC, this.spawnPotentials);
|
| 234 | }
|
| 235 |
|
| 236 | public @Nullable Entity getOrCreateDisplayEntity(final Level level, final BlockPos pos) {
|
| 237 | if (this.displayEntity == null) {
|
| 238 | CompoundTag entityToSpawn = this.getOrCreateNextSpawnData(level, level.getRandom(), pos).getEntityToSpawn();
|
| 239 | if (entityToSpawn.getString("id").isEmpty()) {
|
| 240 | return null;
|
| 241 | }
|
| 242 |
|
| 243 | this.displayEntity = EntityType.loadEntityRecursive(entityToSpawn, level, new EntitySpawnRequest(EntitySpawnReason.SPAWNER, true), SET_DISPLAY_ENTITY_ID);
|
| 244 | if (entityToSpawn.size() == 1 && this.displayEntity instanceof Mob) {
|
| 245 | }
|
| 246 | }
|
| 247 |
|
| 248 | return this.displayEntity;
|
| 249 | }
|
| 250 |
|
| 251 | public boolean onEventTriggered(final Level level, final int id) {
|
| 252 | if (id == 1) {
|
| 253 | if (level.isClientSide()) {
|
| 254 | this.spawnDelay = this.minSpawnDelay;
|
| 255 | }
|
| 256 |
|
| 257 | return true;
|
| 258 | } else {
|
| 259 | return false;
|
| 260 | }
|
| 261 | }
|
| 262 |
|
| 263 | protected void setNextSpawnData(final @Nullable Level level, final BlockPos pos, final SpawnData nextSpawnData) {
|
| 264 | this.nextSpawnData = nextSpawnData;
|
| 265 | }
|
| 266 |
|
| 267 | private SpawnData getOrCreateNextSpawnData(final @Nullable Level level, final RandomSource random, final BlockPos pos) {
|
| 268 | if (this.nextSpawnData != null) {
|
| 269 | return this.nextSpawnData;
|
| 270 | }
|
| 271 |
|
| 272 | this.setNextSpawnData(level, pos, this.spawnPotentials.getRandom(random).orElseGet(SpawnData::new));
|
| 273 | return this.nextSpawnData;
|
| 274 | }
|
| 275 |
|
| 276 | public abstract void broadcastEvent(final Level level, final BlockPos pos, int id);
|
| 277 |
|
| 278 | public double getSpin() {
|
| 279 | return this.spin;
|
| 280 | }
|
| 281 |
|
| 282 | public double getOSpin() {
|
| 283 | return this.oSpin;
|
| 284 | }
|
| 285 | }
|
| 286 |
|