| 1 | package net.minecraft.world.level.block.entity.trialspawner;
|
| 2 |
|
| 3 | import com.mojang.datafixers.util.Pair;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.MapCodec;
|
| 6 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 7 | import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
| 8 | import java.util.HashSet;
|
| 9 | import java.util.List;
|
| 10 | import java.util.Optional;
|
| 11 | import java.util.Set;
|
| 12 | import java.util.UUID;
|
| 13 | import net.minecraft.core.BlockPos;
|
| 14 | import net.minecraft.core.Holder;
|
| 15 | import net.minecraft.core.UUIDUtil;
|
| 16 | import net.minecraft.nbt.CompoundTag;
|
| 17 | import net.minecraft.resources.ResourceKey;
|
| 18 | import net.minecraft.server.level.ServerLevel;
|
| 19 | import net.minecraft.util.Mth;
|
| 20 | import net.minecraft.util.RandomSource;
|
| 21 | import net.minecraft.util.Util;
|
| 22 | import net.minecraft.util.random.WeightedList;
|
| 23 | import net.minecraft.world.effect.MobEffect;
|
| 24 | import net.minecraft.world.effect.MobEffectInstance;
|
| 25 | import net.minecraft.world.effect.MobEffects;
|
| 26 | import net.minecraft.world.entity.Entity;
|
| 27 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 28 | import net.minecraft.world.entity.EntitySpawnRequest;
|
| 29 | import net.minecraft.world.entity.EntityType;
|
| 30 | import net.minecraft.world.entity.Mob;
|
| 31 | import net.minecraft.world.entity.player.Player;
|
| 32 | import net.minecraft.world.item.ItemStack;
|
| 33 | import net.minecraft.world.level.BaseSpawner;
|
| 34 | import net.minecraft.world.level.Level;
|
| 35 | import net.minecraft.world.level.SpawnData;
|
| 36 | import net.minecraft.world.level.storage.loot.LootParams;
|
| 37 | import net.minecraft.world.level.storage.loot.LootTable;
|
| 38 | import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets;
|
| 39 | import org.jspecify.annotations.Nullable;
|
| 40 |
|
| 41 | public class TrialSpawnerStateData {
|
| 42 | private static final String TAG_SPAWN_DATA = "spawn_data";
|
| 43 | private static final String TAG_NEXT_MOB_SPAWNS_AT = "next_mob_spawns_at";
|
| 44 | private static final int DELAY_BETWEEN_PLAYER_SCANS = 20;
|
| 45 | private static final int TRIAL_OMEN_PER_BAD_OMEN_LEVEL = 18000;
|
| 46 | final Set<UUID> detectedPlayers = new HashSet<>();
|
| 47 | final Set<UUID> currentMobs = new HashSet<>();
|
| 48 | long cooldownEndsAt;
|
| 49 | long nextMobSpawnsAt;
|
| 50 | int totalMobsSpawned;
|
| 51 | Optional<SpawnData> nextSpawnData = Optional.empty();
|
| 52 | Optional<ResourceKey<LootTable>> ejectingLootTable = Optional.empty();
|
| 53 | private @Nullable Entity displayEntity;
|
| 54 | private @Nullable WeightedList<ItemStack> dispensing;
|
| 55 | double spin;
|
| 56 | double oSpin;
|
| 57 |
|
| 58 | public TrialSpawnerStateData.Packed pack() {
|
| 59 | return new TrialSpawnerStateData.Packed(
|
| 60 | Set.copyOf(this.detectedPlayers),
|
| 61 | Set.copyOf(this.currentMobs),
|
| 62 | this.cooldownEndsAt,
|
| 63 | this.nextMobSpawnsAt,
|
| 64 | this.totalMobsSpawned,
|
| 65 | this.nextSpawnData,
|
| 66 | this.ejectingLootTable
|
| 67 | );
|
| 68 | }
|
| 69 |
|
| 70 | public void apply(final TrialSpawnerStateData.Packed packed) {
|
| 71 | this.detectedPlayers.clear();
|
| 72 | this.detectedPlayers.addAll(packed.detectedPlayers);
|
| 73 | this.currentMobs.clear();
|
| 74 | this.currentMobs.addAll(packed.currentMobs);
|
| 75 | this.cooldownEndsAt = packed.cooldownEndsAt;
|
| 76 | this.nextMobSpawnsAt = packed.nextMobSpawnsAt;
|
| 77 | this.totalMobsSpawned = packed.totalMobsSpawned;
|
| 78 | this.nextSpawnData = packed.nextSpawnData;
|
| 79 | this.ejectingLootTable = packed.ejectingLootTable;
|
| 80 | }
|
| 81 |
|
| 82 | public void reset() {
|
| 83 | this.currentMobs.clear();
|
| 84 | this.nextSpawnData = Optional.empty();
|
| 85 | this.resetStatistics();
|
| 86 | }
|
| 87 |
|
| 88 | public void resetStatistics() {
|
| 89 | this.detectedPlayers.clear();
|
| 90 | this.totalMobsSpawned = 0;
|
| 91 | this.nextMobSpawnsAt = 0L;
|
| 92 | this.cooldownEndsAt = 0L;
|
| 93 | }
|
| 94 |
|
| 95 | public boolean hasMobToSpawn(final TrialSpawner trialSpawner, final RandomSource random) {
|
| 96 | boolean hasNextMobToSpawn = this.getOrCreateNextSpawnData(trialSpawner, random).getEntityToSpawn().getString("id").isPresent();
|
| 97 | return hasNextMobToSpawn || !trialSpawner.activeConfig().spawnPotentialsDefinition().isEmpty();
|
| 98 | }
|
| 99 |
|
| 100 | public boolean hasFinishedSpawningAllMobs(final TrialSpawnerConfig config, final int additionalPlayers) {
|
| 101 | return this.totalMobsSpawned >= config.calculateTargetTotalMobs(additionalPlayers);
|
| 102 | }
|
| 103 |
|
| 104 | public boolean haveAllCurrentMobsDied() {
|
| 105 | return this.currentMobs.isEmpty();
|
| 106 | }
|
| 107 |
|
| 108 | public boolean isReadyToSpawnNextMob(final ServerLevel serverLevel, final TrialSpawnerConfig config, final int additionalPlayers) {
|
| 109 | return serverLevel.getGameTime() >= this.nextMobSpawnsAt && this.currentMobs.size() < config.calculateTargetSimultaneousMobs(additionalPlayers);
|
| 110 | }
|
| 111 |
|
| 112 | public int countAdditionalPlayers(final BlockPos pos) {
|
| 113 | if (this.detectedPlayers.isEmpty()) {
|
| 114 | Util.logAndPauseIfInIde("Trial Spawner at " + pos + " has no detected players");
|
| 115 | }
|
| 116 |
|
| 117 | return Math.max(0, this.detectedPlayers.size() - 1);
|
| 118 | }
|
| 119 |
|
| 120 | public void tryDetectPlayers(final ServerLevel level, final BlockPos pos, final TrialSpawner trialSpawner) {
|
| 121 | boolean isThrottled = (pos.asLong() + level.getGameTime()) % 20L != 0L;
|
| 122 | if (!isThrottled) {
|
| 123 | if (!trialSpawner.getState().equals(TrialSpawnerState.COOLDOWN) || !trialSpawner.isOminous()) {
|
| 124 | List<UUID> inLineOfSightPlayers = trialSpawner.getPlayerDetector()
|
| 125 | .detect(level, trialSpawner.getEntitySelector(), pos, trialSpawner.getRequiredPlayerRange(), true);
|
| 126 | boolean becameOminous;
|
| 127 | if (!trialSpawner.isOminous() && !inLineOfSightPlayers.isEmpty()) {
|
| 128 | Optional<Pair<Player, Holder<MobEffect>>> playerWithOminousEffect = findPlayerWithOminousEffect(level, inLineOfSightPlayers);
|
| 129 | playerWithOminousEffect.ifPresent(playerAndEffect -> {
|
| 130 | Player player = playerAndEffect.getFirst();
|
| 131 | if (playerAndEffect.getSecond() == MobEffects.BAD_OMEN) {
|
| 132 | transformBadOmenIntoTrialOmen(player);
|
| 133 | }
|
| 134 |
|
| 135 | level.levelEvent(3020, BlockPos.containing(player.getEyePosition()), 0);
|
| 136 | trialSpawner.applyOminous(level, pos);
|
| 137 | });
|
| 138 | becameOminous = playerWithOminousEffect.isPresent();
|
| 139 | } else {
|
| 140 | becameOminous = false;
|
| 141 | }
|
| 142 |
|
| 143 | if (!trialSpawner.getState().equals(TrialSpawnerState.COOLDOWN) || becameOminous) {
|
| 144 | boolean isSearchingForFirstPlayer = trialSpawner.getStateData().detectedPlayers.isEmpty();
|
| 145 | List<UUID> foundPlayers = isSearchingForFirstPlayer
|
| 146 | ? inLineOfSightPlayers
|
| 147 | : trialSpawner.getPlayerDetector().detect(level, trialSpawner.getEntitySelector(), pos, trialSpawner.getRequiredPlayerRange(), false);
|
| 148 | if (this.detectedPlayers.addAll(foundPlayers)) {
|
| 149 | this.nextMobSpawnsAt = Math.max(level.getGameTime() + 40L, this.nextMobSpawnsAt);
|
| 150 | if (!becameOminous) {
|
| 151 | int event = trialSpawner.isOminous() ? 3019 : 3013;
|
| 152 | level.levelEvent(event, pos, this.detectedPlayers.size());
|
| 153 | }
|
| 154 | }
|
| 155 | }
|
| 156 | }
|
| 157 | }
|
| 158 | }
|
| 159 |
|
| 160 | private static Optional<Pair<Player, Holder<MobEffect>>> findPlayerWithOminousEffect(final ServerLevel level, final List<UUID> inLineOfSightPlayers) {
|
| 161 | Player playerWithBadOmen = null;
|
| 162 |
|
| 163 | for (UUID playerUuid : inLineOfSightPlayers) {
|
| 164 | Player player = level.getPlayerByUUID(playerUuid);
|
| 165 | if (player != null) {
|
| 166 | Holder<MobEffect> trialOmen = MobEffects.TRIAL_OMEN;
|
| 167 | if (player.hasEffect(trialOmen)) {
|
| 168 | return Optional.of(Pair.of(player, trialOmen));
|
| 169 | }
|
| 170 |
|
| 171 | if (player.hasEffect(MobEffects.BAD_OMEN)) {
|
| 172 | playerWithBadOmen = player;
|
| 173 | }
|
| 174 | }
|
| 175 | }
|
| 176 |
|
| 177 | return Optional.ofNullable(playerWithBadOmen).map(playerx -> Pair.of(playerx, MobEffects.BAD_OMEN));
|
| 178 | }
|
| 179 |
|
| 180 | public void resetAfterBecomingOminous(final TrialSpawner trialSpawner, final ServerLevel level) {
|
| 181 | this.currentMobs.stream().map(level::getEntity).forEach(entity -> {
|
| 182 | if (entity != null) {
|
| 183 | level.levelEvent(3012, entity.blockPosition(), TrialSpawner.FlameParticle.NORMAL.encode());
|
| 184 | if (entity instanceof Mob mob) {
|
| 185 | mob.dropPreservedEquipment(level);
|
| 186 | }
|
| 187 |
|
| 188 | entity.remove(Entity.RemovalReason.DISCARDED);
|
| 189 | }
|
| 190 | });
|
| 191 | if (!trialSpawner.ominousConfig().spawnPotentialsDefinition().isEmpty()) {
|
| 192 | this.nextSpawnData = Optional.empty();
|
| 193 | }
|
| 194 |
|
| 195 | this.totalMobsSpawned = 0;
|
| 196 | this.currentMobs.clear();
|
| 197 | this.nextMobSpawnsAt = level.getGameTime() + trialSpawner.ominousConfig().ticksBetweenSpawn();
|
| 198 | trialSpawner.markUpdated();
|
| 199 | this.cooldownEndsAt = level.getGameTime() + trialSpawner.ominousConfig().ticksBetweenItemSpawners();
|
| 200 | }
|
| 201 |
|
| 202 | private static void transformBadOmenIntoTrialOmen(final Player player) {
|
| 203 | MobEffectInstance badOmen = player.getEffect(MobEffects.BAD_OMEN);
|
| 204 | if (badOmen != null) {
|
| 205 | int amplifier = badOmen.getAmplifier() + 1;
|
| 206 | int duration = 18000 * amplifier;
|
| 207 | player.removeEffect(MobEffects.BAD_OMEN);
|
| 208 | player.addEffect(new MobEffectInstance(MobEffects.TRIAL_OMEN, duration, 0));
|
| 209 | }
|
| 210 | }
|
| 211 |
|
| 212 | public boolean isReadyToOpenShutter(final ServerLevel serverLevel, final float delayBeforeOpen, final int targetCooldownLength) {
|
| 213 | long cooldownStartedAt = this.cooldownEndsAt - targetCooldownLength;
|
| 214 | return (float)serverLevel.getGameTime() >= (float)cooldownStartedAt + delayBeforeOpen;
|
| 215 | }
|
| 216 |
|
| 217 | public boolean isReadyToEjectItems(final ServerLevel serverLevel, final float timeBetweenEjections, final int targetCooldownLength) {
|
| 218 | long cooldownStartedAt = this.cooldownEndsAt - targetCooldownLength;
|
| 219 | return (float)(serverLevel.getGameTime() - cooldownStartedAt) % timeBetweenEjections == 0.0F;
|
| 220 | }
|
| 221 |
|
| 222 | public boolean isCooldownFinished(final ServerLevel serverLevel) {
|
| 223 | return serverLevel.getGameTime() >= this.cooldownEndsAt;
|
| 224 | }
|
| 225 |
|
| 226 | protected SpawnData getOrCreateNextSpawnData(final TrialSpawner trialSpawner, final RandomSource random) {
|
| 227 | if (this.nextSpawnData.isPresent()) {
|
| 228 | return this.nextSpawnData.get();
|
| 229 | }
|
| 230 |
|
| 231 | WeightedList<SpawnData> spawnPotentials = trialSpawner.activeConfig().spawnPotentialsDefinition();
|
| 232 | Optional<SpawnData> selected = spawnPotentials.isEmpty() ? this.nextSpawnData : spawnPotentials.getRandom(random);
|
| 233 | this.nextSpawnData = Optional.of(selected.orElseGet(SpawnData::new));
|
| 234 | trialSpawner.markUpdated();
|
| 235 | return this.nextSpawnData.get();
|
| 236 | }
|
| 237 |
|
| 238 | public @Nullable Entity getOrCreateDisplayEntity(final TrialSpawner trialSpawner, final Level level, final TrialSpawnerState state) {
|
| 239 | if (!state.hasSpinningMob()) {
|
| 240 | return null;
|
| 241 | }
|
| 242 |
|
| 243 | if (this.displayEntity == null) {
|
| 244 | CompoundTag entityToSpawn = this.getOrCreateNextSpawnData(trialSpawner, level.getRandom()).getEntityToSpawn();
|
| 245 | if (entityToSpawn.getString("id").isPresent()) {
|
| 246 | this.displayEntity = EntityType.loadEntityRecursive(
|
| 247 | entityToSpawn, level, new EntitySpawnRequest(EntitySpawnReason.TRIAL_SPAWNER, true), BaseSpawner.SET_DISPLAY_ENTITY_ID
|
| 248 | );
|
| 249 | }
|
| 250 | }
|
| 251 |
|
| 252 | return this.displayEntity;
|
| 253 | }
|
| 254 |
|
| 255 | public CompoundTag getUpdateTag(final TrialSpawnerState state) {
|
| 256 | CompoundTag tag = new CompoundTag();
|
| 257 | if (state == TrialSpawnerState.ACTIVE) {
|
| 258 | tag.putLong("next_mob_spawns_at", this.nextMobSpawnsAt);
|
| 259 | }
|
| 260 |
|
| 261 | this.nextSpawnData.ifPresent(spawnData -> tag.store("spawn_data", SpawnData.CODEC, spawnData));
|
| 262 | return tag;
|
| 263 | }
|
| 264 |
|
| 265 | public double getSpin() {
|
| 266 | return this.spin;
|
| 267 | }
|
| 268 |
|
| 269 | public double getOSpin() {
|
| 270 | return this.oSpin;
|
| 271 | }
|
| 272 |
|
| 273 | public WeightedList<ItemStack> getDispensingItems(final ServerLevel level, final TrialSpawnerConfig config, final BlockPos pos) {
|
| 274 | if (this.dispensing != null) {
|
| 275 | return this.dispensing;
|
| 276 | }
|
| 277 |
|
| 278 | LootTable lootTable = level.getServer().reloadableRegistries().getLootTable(config.itemsToDropWhenOminous());
|
| 279 | LootParams params = new LootParams.Builder(level).create(LootContextParamSets.EMPTY);
|
| 280 | long simplePositionalSeed = lowResolutionPosition(level, pos);
|
| 281 | ObjectArrayList<ItemStack> lootDrops = lootTable.getRandomItems(params, simplePositionalSeed);
|
| 282 | if (lootDrops.isEmpty()) {
|
| 283 | return WeightedList.of();
|
| 284 | }
|
| 285 |
|
| 286 | WeightedList.Builder<ItemStack> builder = WeightedList.builder();
|
| 287 |
|
| 288 | for (ItemStack drop : lootDrops) {
|
| 289 | builder.add(drop.copyWithCount(1), drop.getCount());
|
| 290 | }
|
| 291 |
|
| 292 | this.dispensing = builder.build();
|
| 293 | return this.dispensing;
|
| 294 | }
|
| 295 |
|
| 296 | private static long lowResolutionPosition(final ServerLevel level, final BlockPos pos) {
|
| 297 | BlockPos lowResolutionPosition = new BlockPos(Mth.floor(pos.getX() / 30.0F), Mth.floor(pos.getY() / 20.0F), Mth.floor(pos.getZ() / 30.0F));
|
| 298 | return level.getSeed() + lowResolutionPosition.asLong();
|
| 299 | }
|
| 300 |
|
| 301 | public record Packed(
|
| 302 | Set<UUID> detectedPlayers,
|
| 303 | Set<UUID> currentMobs,
|
| 304 | long cooldownEndsAt,
|
| 305 | long nextMobSpawnsAt,
|
| 306 | int totalMobsSpawned,
|
| 307 | Optional<SpawnData> nextSpawnData,
|
| 308 | Optional<ResourceKey<LootTable>> ejectingLootTable
|
| 309 | ) {
|
| 310 | public static final MapCodec<TrialSpawnerStateData.Packed> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 311 | i -> i.group(
|
| 312 | UUIDUtil.CODEC_SET.lenientOptionalFieldOf("registered_players", Set.of()).forGetter(TrialSpawnerStateData.Packed::detectedPlayers),
|
| 313 | UUIDUtil.CODEC_SET.lenientOptionalFieldOf("current_mobs", Set.of()).forGetter(TrialSpawnerStateData.Packed::currentMobs),
|
| 314 | Codec.LONG.lenientOptionalFieldOf("cooldown_ends_at", 0L).forGetter(TrialSpawnerStateData.Packed::cooldownEndsAt),
|
| 315 | Codec.LONG.lenientOptionalFieldOf("next_mob_spawns_at", 0L).forGetter(TrialSpawnerStateData.Packed::nextMobSpawnsAt),
|
| 316 | Codec.intRange(0, Integer.MAX_VALUE).lenientOptionalFieldOf("total_mobs_spawned", 0).forGetter(TrialSpawnerStateData.Packed::totalMobsSpawned),
|
| 317 | SpawnData.CODEC.lenientOptionalFieldOf("spawn_data").forGetter(TrialSpawnerStateData.Packed::nextSpawnData),
|
| 318 | LootTable.KEY_CODEC.lenientOptionalFieldOf("ejecting_loot_table").forGetter(TrialSpawnerStateData.Packed::ejectingLootTable)
|
| 319 | )
|
| 320 | .apply(i, TrialSpawnerStateData.Packed::new)
|
| 321 | );
|
| 322 | }
|
| 323 | }
|
| 324 |
|