| 1 | package net.minecraft.world.level.block.entity.trialspawner;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import java.util.Objects;
|
| 5 | import java.util.Optional;
|
| 6 | import java.util.Set;
|
| 7 | import java.util.UUID;
|
| 8 | import java.util.stream.Stream;
|
| 9 | import net.minecraft.core.BlockPos;
|
| 10 | import net.minecraft.core.Direction;
|
| 11 | import net.minecraft.core.particles.ParticleTypes;
|
| 12 | import net.minecraft.core.particles.SimpleParticleType;
|
| 13 | import net.minecraft.resources.ResourceKey;
|
| 14 | import net.minecraft.server.level.ServerLevel;
|
| 15 | import net.minecraft.sounds.SoundEvents;
|
| 16 | import net.minecraft.sounds.SoundSource;
|
| 17 | import net.minecraft.util.Mth;
|
| 18 | import net.minecraft.util.RandomSource;
|
| 19 | import net.minecraft.util.StringRepresentable;
|
| 20 | import net.minecraft.util.Util;
|
| 21 | import net.minecraft.world.entity.Entity;
|
| 22 | import net.minecraft.world.entity.OminousItemSpawner;
|
| 23 | import net.minecraft.world.entity.player.Player;
|
| 24 | import net.minecraft.world.item.ItemStack;
|
| 25 | import net.minecraft.world.level.ClipContext;
|
| 26 | import net.minecraft.world.level.Level;
|
| 27 | import net.minecraft.world.level.storage.loot.LootTable;
|
| 28 | import net.minecraft.world.phys.BlockHitResult;
|
| 29 | import net.minecraft.world.phys.Vec3;
|
| 30 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 31 | import org.jspecify.annotations.Nullable;
|
| 32 |
|
| 33 | public enum TrialSpawnerState implements StringRepresentable {
|
| 34 | INACTIVE("inactive", 0, TrialSpawnerState.ParticleEmission.NONE, -1.0, false),
|
| 35 | WAITING_FOR_PLAYERS("waiting_for_players", 4, TrialSpawnerState.ParticleEmission.SMALL_FLAMES, 200.0, true),
|
| 36 | ACTIVE("active", 8, TrialSpawnerState.ParticleEmission.FLAMES_AND_SMOKE, 1000.0, true),
|
| 37 | WAITING_FOR_REWARD_EJECTION("waiting_for_reward_ejection", 8, TrialSpawnerState.ParticleEmission.SMALL_FLAMES, -1.0, false),
|
| 38 | EJECTING_REWARD("ejecting_reward", 8, TrialSpawnerState.ParticleEmission.SMALL_FLAMES, -1.0, false),
|
| 39 | COOLDOWN("cooldown", 0, TrialSpawnerState.ParticleEmission.SMOKE_INSIDE_AND_TOP_FACE, -1.0, false);
|
| 40 |
|
| 41 | private static final float DELAY_BEFORE_EJECT_AFTER_KILLING_LAST_MOB = 40.0F;
|
| 42 | private static final int TIME_BETWEEN_EACH_EJECTION = Mth.floor(30.0F);
|
| 43 | private final String name;
|
| 44 | private final int lightLevel;
|
| 45 | private final double spinningMobSpeed;
|
| 46 | private final TrialSpawnerState.ParticleEmission particleEmission;
|
| 47 | private final boolean isCapableOfSpawning;
|
| 48 |
|
| 49 | TrialSpawnerState(
|
| 50 | final String name,
|
| 51 | final int lightLevel,
|
| 52 | final TrialSpawnerState.ParticleEmission particleEmission,
|
| 53 | final double spinningMobSpeed,
|
| 54 | final boolean isCapableOfSpawning
|
| 55 | ) {
|
| 56 | this.name = name;
|
| 57 | this.lightLevel = lightLevel;
|
| 58 | this.particleEmission = particleEmission;
|
| 59 | this.spinningMobSpeed = spinningMobSpeed;
|
| 60 | this.isCapableOfSpawning = isCapableOfSpawning;
|
| 61 | }
|
| 62 |
|
| 63 | TrialSpawnerState tickAndGetNext(final BlockPos spawnerPos, final TrialSpawner trialSpawner, final ServerLevel serverLevel) {
|
| 64 | TrialSpawnerStateData data = trialSpawner.getStateData();
|
| 65 | TrialSpawnerConfig config = trialSpawner.activeConfig();
|
| 66 | RandomSource random = serverLevel.getRandom();
|
| 67 |
|
| 68 | return switch (this) {
|
| 69 | case INACTIVE -> data.getOrCreateDisplayEntity(trialSpawner, serverLevel, WAITING_FOR_PLAYERS) == null ? this : WAITING_FOR_PLAYERS;
|
| 70 | case WAITING_FOR_PLAYERS -> {
|
| 71 | if (!trialSpawner.canSpawnInLevel(serverLevel)) {
|
| 72 | data.resetStatistics();
|
| 73 | yield this;
|
| 74 | } else if (!data.hasMobToSpawn(trialSpawner, random)) {
|
| 75 | yield INACTIVE;
|
| 76 | } else {
|
| 77 | data.tryDetectPlayers(serverLevel, spawnerPos, trialSpawner);
|
| 78 | yield data.detectedPlayers.isEmpty() ? this : ACTIVE;
|
| 79 | }
|
| 80 | }
|
| 81 | case ACTIVE -> {
|
| 82 | if (!trialSpawner.canSpawnInLevel(serverLevel)) {
|
| 83 | data.resetStatistics();
|
| 84 | yield WAITING_FOR_PLAYERS;
|
| 85 | } else if (!data.hasMobToSpawn(trialSpawner, random)) {
|
| 86 | yield INACTIVE;
|
| 87 | } else {
|
| 88 | int additionalPlayers = data.countAdditionalPlayers(spawnerPos);
|
| 89 | data.tryDetectPlayers(serverLevel, spawnerPos, trialSpawner);
|
| 90 | if (trialSpawner.isOminous()) {
|
| 91 | this.spawnOminousOminousItemSpawner(serverLevel, spawnerPos, trialSpawner);
|
| 92 | }
|
| 93 |
|
| 94 | if (data.hasFinishedSpawningAllMobs(config, additionalPlayers)) {
|
| 95 | if (data.haveAllCurrentMobsDied()) {
|
| 96 | data.cooldownEndsAt = serverLevel.getGameTime() + trialSpawner.getTargetCooldownLength();
|
| 97 | data.totalMobsSpawned = 0;
|
| 98 | data.nextMobSpawnsAt = 0L;
|
| 99 | yield WAITING_FOR_REWARD_EJECTION;
|
| 100 | }
|
| 101 | } else if (data.isReadyToSpawnNextMob(serverLevel, config, additionalPlayers)) {
|
| 102 | trialSpawner.spawnMob(serverLevel, spawnerPos).ifPresent(entityId -> {
|
| 103 | data.currentMobs.add(entityId);
|
| 104 | data.totalMobsSpawned++;
|
| 105 | data.nextMobSpawnsAt = serverLevel.getGameTime() + config.ticksBetweenSpawn();
|
| 106 | config.spawnPotentialsDefinition().getRandom(random).ifPresent(entry -> {
|
| 107 | data.nextSpawnData = Optional.of(entry);
|
| 108 | trialSpawner.markUpdated();
|
| 109 | });
|
| 110 | });
|
| 111 | }
|
| 112 |
|
| 113 | yield this;
|
| 114 | }
|
| 115 | }
|
| 116 | case WAITING_FOR_REWARD_EJECTION -> {
|
| 117 | if (data.isReadyToOpenShutter(serverLevel, 40.0F, trialSpawner.getTargetCooldownLength())) {
|
| 118 | serverLevel.playSound(null, spawnerPos, SoundEvents.TRIAL_SPAWNER_OPEN_SHUTTER, SoundSource.BLOCKS);
|
| 119 | yield EJECTING_REWARD;
|
| 120 | } else {
|
| 121 | yield this;
|
| 122 | }
|
| 123 | }
|
| 124 | case EJECTING_REWARD -> {
|
| 125 | if (!data.isReadyToEjectItems(serverLevel, TIME_BETWEEN_EACH_EJECTION, trialSpawner.getTargetCooldownLength())) {
|
| 126 | yield this;
|
| 127 | } else if (data.detectedPlayers.isEmpty()) {
|
| 128 | serverLevel.playSound(null, spawnerPos, SoundEvents.TRIAL_SPAWNER_CLOSE_SHUTTER, SoundSource.BLOCKS);
|
| 129 | data.ejectingLootTable = Optional.empty();
|
| 130 | yield COOLDOWN;
|
| 131 | } else {
|
| 132 | if (data.ejectingLootTable.isEmpty()) {
|
| 133 | data.ejectingLootTable = config.lootTablesToEject().getRandom(random);
|
| 134 | }
|
| 135 |
|
| 136 | data.ejectingLootTable.ifPresent(lootTable -> trialSpawner.ejectReward(serverLevel, spawnerPos, (ResourceKey<LootTable>)lootTable));
|
| 137 | data.detectedPlayers.remove(data.detectedPlayers.iterator().next());
|
| 138 | yield this;
|
| 139 | }
|
| 140 | }
|
| 141 | case COOLDOWN -> {
|
| 142 | data.tryDetectPlayers(serverLevel, spawnerPos, trialSpawner);
|
| 143 | if (!data.detectedPlayers.isEmpty()) {
|
| 144 | data.totalMobsSpawned = 0;
|
| 145 | data.nextMobSpawnsAt = 0L;
|
| 146 | yield ACTIVE;
|
| 147 | } else if (data.isCooldownFinished(serverLevel)) {
|
| 148 | trialSpawner.removeOminous(serverLevel, spawnerPos);
|
| 149 | data.reset();
|
| 150 | yield WAITING_FOR_PLAYERS;
|
| 151 | } else {
|
| 152 | yield this;
|
| 153 | }
|
| 154 | }
|
| 155 | };
|
| 156 | }
|
| 157 |
|
| 158 | private void spawnOminousOminousItemSpawner(final ServerLevel level, final BlockPos trialSpawnerPos, final TrialSpawner trialSpawner) {
|
| 159 | TrialSpawnerStateData data = trialSpawner.getStateData();
|
| 160 | TrialSpawnerConfig config = trialSpawner.activeConfig();
|
| 161 | ItemStack itemToDispense = data.getDispensingItems(level, config, trialSpawnerPos).getRandom(level.getRandom()).orElse(ItemStack.EMPTY);
|
| 162 | if (!itemToDispense.isEmpty()) {
|
| 163 | if (this.timeToSpawnItemSpawner(level, data)) {
|
| 164 | calculatePositionToSpawnSpawner(level, trialSpawnerPos, trialSpawner, data).ifPresent(pos -> {
|
| 165 | OminousItemSpawner itemSpawner = OminousItemSpawner.create(level, itemToDispense);
|
| 166 | itemSpawner.snapTo(pos);
|
| 167 | level.addFreshEntity(itemSpawner);
|
| 168 | float pitch = (level.getRandom().nextFloat() - level.getRandom().nextFloat()) * 0.2F + 1.0F;
|
| 169 | level.playSound(null, BlockPos.containing(pos), SoundEvents.TRIAL_SPAWNER_SPAWN_ITEM_BEGIN, SoundSource.BLOCKS, 1.0F, pitch);
|
| 170 | data.cooldownEndsAt = level.getGameTime() + trialSpawner.ominousConfig().ticksBetweenItemSpawners();
|
| 171 | });
|
| 172 | }
|
| 173 | }
|
| 174 | }
|
| 175 |
|
| 176 | private static Optional<Vec3> calculatePositionToSpawnSpawner(
|
| 177 | final ServerLevel level, final BlockPos trialSpawnerPos, final TrialSpawner trialSpawner, final TrialSpawnerStateData data
|
| 178 | ) {
|
| 179 | List<Player> nearbyPlayers = data.detectedPlayers
|
| 180 | .stream()
|
| 181 | .map(level::getPlayerByUUID)
|
| 182 | .filter(Objects::nonNull)
|
| 183 | .filter(
|
| 184 | player -> !player.isCreative()
|
| 185 | && !player.isSpectator()
|
| 186 | && player.isAlive()
|
| 187 | && player.distanceToSqr(Vec3.atCenterOf(trialSpawnerPos)) <= Mth.square(trialSpawner.getRequiredPlayerRange())
|
| 188 | )
|
| 189 | .toList();
|
| 190 | if (nearbyPlayers.isEmpty()) {
|
| 191 | return Optional.empty();
|
| 192 | }
|
| 193 |
|
| 194 | Entity entity = selectEntityToSpawnItemAbove(nearbyPlayers, data.currentMobs, trialSpawner, trialSpawnerPos, level);
|
| 195 | return entity == null ? Optional.empty() : calculatePositionAbove(entity, level);
|
| 196 | }
|
| 197 |
|
| 198 | private static Optional<Vec3> calculatePositionAbove(final Entity entityToSpawnItemAbove, final ServerLevel level) {
|
| 199 | Vec3 entityPos = entityToSpawnItemAbove.position();
|
| 200 | Vec3 trySpawnPos = entityPos.relative(Direction.UP, entityToSpawnItemAbove.getBbHeight() + 2.0F + level.getRandom().nextInt(4));
|
| 201 | BlockHitResult hitResult = level.clip(new ClipContext(entityPos, trySpawnPos, ClipContext.Block.VISUAL, ClipContext.Fluid.NONE, CollisionContext.empty()));
|
| 202 | Vec3 down = Vec3.atCenterOf(hitResult.getBlockPos()).relative(Direction.DOWN, 1.0);
|
| 203 | BlockPos blockPosDown = BlockPos.containing(down);
|
| 204 | return !level.getBlockState(blockPosDown).getCollisionShape(level, blockPosDown).isEmpty() ? Optional.empty() : Optional.of(down);
|
| 205 | }
|
| 206 |
|
| 207 | private static @Nullable Entity selectEntityToSpawnItemAbove(
|
| 208 | final List<Player> nearbyPlayers, final Set<UUID> mobIds, final TrialSpawner trialSpawner, final BlockPos spawnerPos, final ServerLevel level
|
| 209 | ) {
|
| 210 | Stream<Entity> nearbyMobs = mobIds.stream()
|
| 211 | .map(level::getEntity)
|
| 212 | .filter(Objects::nonNull)
|
| 213 | .filter(target -> target.isAlive() && target.distanceToSqr(Vec3.atCenterOf(spawnerPos)) <= Mth.square(trialSpawner.getRequiredPlayerRange()));
|
| 214 | RandomSource random = level.getRandom();
|
| 215 | List<? extends Entity> eligibleEntities = random.nextBoolean() ? nearbyMobs.toList() : nearbyPlayers;
|
| 216 | if (eligibleEntities.isEmpty()) {
|
| 217 | return null;
|
| 218 | } else {
|
| 219 | return eligibleEntities.size() == 1 ? eligibleEntities.getFirst() : Util.getRandom(eligibleEntities, random);
|
| 220 | }
|
| 221 | }
|
| 222 |
|
| 223 | private boolean timeToSpawnItemSpawner(final ServerLevel serverLevel, final TrialSpawnerStateData data) {
|
| 224 | return serverLevel.getGameTime() >= data.cooldownEndsAt;
|
| 225 | }
|
| 226 |
|
| 227 | public int lightLevel() {
|
| 228 | return this.lightLevel;
|
| 229 | }
|
| 230 |
|
| 231 | public double spinningMobSpeed() {
|
| 232 | return this.spinningMobSpeed;
|
| 233 | }
|
| 234 |
|
| 235 | public boolean hasSpinningMob() {
|
| 236 | return this.spinningMobSpeed >= 0.0;
|
| 237 | }
|
| 238 |
|
| 239 | public boolean isCapableOfSpawning() {
|
| 240 | return this.isCapableOfSpawning;
|
| 241 | }
|
| 242 |
|
| 243 | public void emitParticles(final Level level, final BlockPos blockPos, final boolean isOminous) {
|
| 244 | this.particleEmission.emit(level, level.getRandom(), blockPos, isOminous);
|
| 245 | }
|
| 246 |
|
| 247 | @Override
|
| 248 | public String getSerializedName() {
|
| 249 | return this.name;
|
| 250 | }
|
| 251 |
|
| 252 | private static class LightLevel {
|
| 253 | private static final int UNLIT = 0;
|
| 254 | private static final int HALF_LIT = 4;
|
| 255 | private static final int LIT = 8;
|
| 256 | }
|
| 257 |
|
| 258 | private interface ParticleEmission {
|
| 259 | TrialSpawnerState.ParticleEmission NONE = (level, random, pos, isOminous) -> {};
|
| 260 | TrialSpawnerState.ParticleEmission SMALL_FLAMES = (level, random, pos, isOminous) -> {
|
| 261 | if (random.nextInt(2) == 0) {
|
| 262 | Vec3 vec = Vec3.atCenterOf(pos).offsetRandom(random, 0.9F);
|
| 263 | addParticle(isOminous ? ParticleTypes.SOUL_FIRE_FLAME : ParticleTypes.SMALL_FLAME, vec, level);
|
| 264 | }
|
| 265 | };
|
| 266 | TrialSpawnerState.ParticleEmission FLAMES_AND_SMOKE = (level, random, pos, isOminous) -> {
|
| 267 | Vec3 vec = Vec3.atCenterOf(pos).offsetRandom(random, 1.0F);
|
| 268 | addParticle(ParticleTypes.SMOKE, vec, level);
|
| 269 | addParticle(isOminous ? ParticleTypes.SOUL_FIRE_FLAME : ParticleTypes.FLAME, vec, level);
|
| 270 | };
|
| 271 | TrialSpawnerState.ParticleEmission SMOKE_INSIDE_AND_TOP_FACE = (level, random, pos, isOminous) -> {
|
| 272 | Vec3 vec = Vec3.atCenterOf(pos).offsetRandom(random, 0.9F);
|
| 273 | if (random.nextInt(3) == 0) {
|
| 274 | addParticle(ParticleTypes.SMOKE, vec, level);
|
| 275 | }
|
| 276 |
|
| 277 | if (level.getGameTime() % 20L == 0L) {
|
| 278 | Vec3 topFaceVec = Vec3.atCenterOf(pos).add(0.0, 0.5, 0.0);
|
| 279 | int smokeCount = level.getRandom().nextInt(4) + 20;
|
| 280 |
|
| 281 | for (int i = 0; i < smokeCount; i++) {
|
| 282 | addParticle(ParticleTypes.SMOKE, topFaceVec, level);
|
| 283 | }
|
| 284 | }
|
| 285 | };
|
| 286 |
|
| 287 | private static void addParticle(final SimpleParticleType smoke, final Vec3 vec, final Level level) {
|
| 288 | level.addParticle(smoke, vec.x(), vec.y(), vec.z(), 0.0, 0.0, 0.0);
|
| 289 | }
|
| 290 |
|
| 291 | void emit(final Level level, final RandomSource random, final BlockPos blockPos, final boolean isOminous);
|
| 292 | }
|
| 293 |
|
| 294 | private static class SpinningMob {
|
| 295 | private static final double NONE = -1.0;
|
| 296 | private static final double SLOW = 200.0;
|
| 297 | private static final double FAST = 1000.0;
|
| 298 | }
|
| 299 | }
|
| 300 |
|