| 1 | package net.minecraft.world.level.dimension.end;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import com.google.common.collect.ContiguousSet;
|
| 5 | import com.google.common.collect.DiscreteDomain;
|
| 6 | import com.google.common.collect.Lists;
|
| 7 | import com.google.common.collect.Range;
|
| 8 | import com.google.common.collect.Sets;
|
| 9 | import com.mojang.logging.LogUtils;
|
| 10 | import com.mojang.serialization.Codec;
|
| 11 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 12 | import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
| 13 | import java.util.ArrayList;
|
| 14 | import java.util.List;
|
| 15 | import java.util.Objects;
|
| 16 | import java.util.Optional;
|
| 17 | import java.util.Set;
|
| 18 | import java.util.UUID;
|
| 19 | import java.util.function.Predicate;
|
| 20 | import net.minecraft.advancements.triggers.CriteriaTriggers;
|
| 21 | import net.minecraft.core.BlockPos;
|
| 22 | import net.minecraft.core.Direction;
|
| 23 | import net.minecraft.core.Holder;
|
| 24 | import net.minecraft.core.UUIDUtil;
|
| 25 | import net.minecraft.core.registries.Registries;
|
| 26 | import net.minecraft.data.worldgen.features.EndFeatures;
|
| 27 | import net.minecraft.network.chat.Component;
|
| 28 | import net.minecraft.resources.Identifier;
|
| 29 | import net.minecraft.server.level.FullChunkStatus;
|
| 30 | import net.minecraft.server.level.ServerBossEvent;
|
| 31 | import net.minecraft.server.level.ServerLevel;
|
| 32 | import net.minecraft.server.level.ServerPlayer;
|
| 33 | import net.minecraft.server.level.TicketType;
|
| 34 | import net.minecraft.util.ExtraCodecs;
|
| 35 | import net.minecraft.util.Mth;
|
| 36 | import net.minecraft.util.RandomSource;
|
| 37 | import net.minecraft.util.Util;
|
| 38 | import net.minecraft.util.datafix.DataFixTypes;
|
| 39 | import net.minecraft.world.BossEvent;
|
| 40 | import net.minecraft.world.damagesource.DamageSource;
|
| 41 | import net.minecraft.world.entity.Entity;
|
| 42 | import net.minecraft.world.entity.EntityReference;
|
| 43 | import net.minecraft.world.entity.EntitySelector;
|
| 44 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 45 | import net.minecraft.world.entity.EntityTypes;
|
| 46 | import net.minecraft.world.entity.boss.enderdragon.EndCrystal;
|
| 47 | import net.minecraft.world.entity.boss.enderdragon.EnderDragon;
|
| 48 | import net.minecraft.world.entity.boss.enderdragon.phases.EnderDragonPhase;
|
| 49 | import net.minecraft.world.level.ChunkPos;
|
| 50 | import net.minecraft.world.level.block.Blocks;
|
| 51 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 52 | import net.minecraft.world.level.block.entity.TheEndPortalBlockEntity;
|
| 53 | import net.minecraft.world.level.block.state.pattern.BlockInWorld;
|
| 54 | import net.minecraft.world.level.block.state.pattern.BlockPattern;
|
| 55 | import net.minecraft.world.level.block.state.pattern.BlockPatternBuilder;
|
| 56 | import net.minecraft.world.level.block.state.predicate.BlockPredicate;
|
| 57 | import net.minecraft.world.level.chunk.LevelChunk;
|
| 58 | import net.minecraft.world.level.chunk.status.ChunkStatus;
|
| 59 | import net.minecraft.world.level.levelgen.Heightmap;
|
| 60 | import net.minecraft.world.level.levelgen.feature.EndSpikeFeature;
|
| 61 | import net.minecraft.world.level.levelgen.feature.Feature;
|
| 62 | import net.minecraft.world.level.saveddata.SavedData;
|
| 63 | import net.minecraft.world.level.saveddata.SavedDataType;
|
| 64 | import net.minecraft.world.phys.AABB;
|
| 65 | import org.jspecify.annotations.Nullable;
|
| 66 | import org.slf4j.Logger;
|
| 67 |
|
| 68 | public class EnderDragonFight extends SavedData {
|
| 69 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 70 | private static final int MAX_TICKS_BEFORE_DRAGON_RESPAWN = 1200;
|
| 71 | private static final int TIME_BETWEEN_CRYSTAL_SCANS = 100;
|
| 72 | public static final int TIME_BETWEEN_PLAYER_SCANS = 20;
|
| 73 | private static final int ARENA_SIZE_CHUNKS = 8;
|
| 74 | public static final int ARENA_TICKET_LEVEL = 9;
|
| 75 | private static final int GATEWAY_COUNT = 20;
|
| 76 | private static final int GATEWAY_DISTANCE = 96;
|
| 77 | public static final int DRAGON_SPAWN_Y = 128;
|
| 78 | private static final Component EVENT_DISPLAY_NAME = Component.translatable("entity.minecraft.ender_dragon");
|
| 79 | private static final BlockPos END_PODIUM_LOCATION = BlockPos.ZERO;
|
| 80 | private Predicate<Entity> validPlayer;
|
| 81 | private ServerBossEvent dragonEvent;
|
| 82 | private ServerLevel level;
|
| 83 | private BlockPos origin;
|
| 84 | private final List<Integer> gateways;
|
| 85 | private final BlockPattern exitPortalPattern = BlockPatternBuilder.start()
|
| 86 | .aisle(" ", " ", " ", " # ", " ", " ", " ")
|
| 87 | .aisle(" ", " ", " ", " # ", " ", " ", " ")
|
| 88 | .aisle(" ", " ", " ", " # ", " ", " ", " ")
|
| 89 | .aisle(" ### ", " # # ", "# #", "# # #", "# #", " # # ", " ### ")
|
| 90 | .aisle(" ", " ### ", " ##### ", " ##### ", " ##### ", " ### ", " ")
|
| 91 | .where('#', BlockInWorld.hasState(BlockPredicate.forBlock(Blocks.BEDROCK)))
|
| 92 | .build();
|
| 93 | private int ticksSinceDragonSeen;
|
| 94 | private int aliveCrystals;
|
| 95 | private int ticksSinceCrystalsScanned;
|
| 96 | private int ticksSinceLastPlayerScan = 21;
|
| 97 | private boolean dragonKilled;
|
| 98 | private boolean hasPreviouslyKilledDragon;
|
| 99 | private boolean skipArenaLoadedCheck = false;
|
| 100 | private @Nullable UUID dragonUUID;
|
| 101 | private boolean needsStateScanning;
|
| 102 | private @Nullable BlockPos exitPortalLocation;
|
| 103 | private @Nullable DragonRespawnStage respawnStage;
|
| 104 | private int respawnTime;
|
| 105 | private List<EntityReference<EndCrystal>> respawnCrystals;
|
| 106 | public static final Codec<EnderDragonFight> CODEC = RecordCodecBuilder.create(
|
| 107 | i -> i.group(
|
| 108 | ExtraCodecs.optionalAlwaysPresentFieldOf(Codec.BOOL, "needs_state_scanning", true).forGetter(fight -> fight.needsStateScanning),
|
| 109 | ExtraCodecs.optionalAlwaysPresentFieldOf(Codec.BOOL, "dragon_killed", false).forGetter(fight -> fight.dragonKilled),
|
| 110 | ExtraCodecs.optionalAlwaysPresentFieldOf(Codec.BOOL, "previously_killed", false).forGetter(fight -> fight.hasPreviouslyKilledDragon),
|
| 111 | DragonRespawnStage.CODEC.optionalFieldOf("respawn_stage").forGetter(fight -> Optional.ofNullable(fight.respawnStage)),
|
| 112 | ExtraCodecs.optionalAlwaysPresentFieldOf(Codec.INT, "respawn_time", 0).forGetter(fight -> fight.respawnTime),
|
| 113 | UUIDUtil.CODEC.lenientOptionalFieldOf("dragon_uuid").forGetter(fight -> Optional.ofNullable(fight.dragonUUID)),
|
| 114 | BlockPos.CODEC.lenientOptionalFieldOf("exit_portal_location").forGetter(fight -> Optional.ofNullable(fight.exitPortalLocation)),
|
| 115 | Codec.list(Codec.INT).lenientOptionalFieldOf("gateways", new ArrayList<>()).forGetter(fight -> fight.gateways),
|
| 116 | Codec.list(EntityReference.codec()).optionalFieldOf("respawn_crystals", List.of()).forGetter(fight -> fight.respawnCrystals)
|
| 117 | )
|
| 118 | .apply(i, EnderDragonFight::new)
|
| 119 | );
|
| 120 | public static final SavedDataType<EnderDragonFight> TYPE = new SavedDataType<>(
|
| 121 | Identifier.withDefaultNamespace("ender_dragon_fight"), EnderDragonFight::createDefault, CODEC, DataFixTypes.SAVED_DATA_ENDER_DRAGON_FIGHT
|
| 122 | );
|
| 123 |
|
| 124 | public static EnderDragonFight createDefault() {
|
| 125 | return new EnderDragonFight(true, false, false, Optional.empty(), 0, Optional.empty(), Optional.empty(), new ObjectArrayList<>(), List.of());
|
| 126 | }
|
| 127 |
|
| 128 | public EnderDragonFight(
|
| 129 | final boolean needsStateScanning,
|
| 130 | final boolean dragonKilled,
|
| 131 | final boolean previouslyKilled,
|
| 132 | final Optional<DragonRespawnStage> respawnStage,
|
| 133 | final int respawnTime,
|
| 134 | final Optional<UUID> dragonUUID,
|
| 135 | final Optional<BlockPos> exitPortalLocation,
|
| 136 | final List<Integer> gateways,
|
| 137 | final List<EntityReference<EndCrystal>> respawnCrystals
|
| 138 | ) {
|
| 139 | this.needsStateScanning = needsStateScanning;
|
| 140 | this.dragonUUID = dragonUUID.orElse(null);
|
| 141 | this.dragonKilled = dragonKilled;
|
| 142 | this.hasPreviouslyKilledDragon = previouslyKilled;
|
| 143 | this.respawnStage = respawnStage.orElse(null);
|
| 144 | this.respawnTime = respawnTime;
|
| 145 | this.exitPortalLocation = exitPortalLocation.orElse(null);
|
| 146 | this.gateways = new ObjectArrayList<>(gateways);
|
| 147 | this.respawnCrystals = respawnCrystals;
|
| 148 | }
|
| 149 |
|
| 150 | public static BlockPos getPodiumLocation(final BlockPos offset) {
|
| 151 | return END_PODIUM_LOCATION.offset(offset);
|
| 152 | }
|
| 153 |
|
| 154 | public void init(final ServerLevel level, final long seed, final BlockPos origin) {
|
| 155 | this.level = level;
|
| 156 | this.origin = origin;
|
| 157 | this.dragonEvent = new ServerBossEvent(
|
| 158 | Mth.createInsecureUUID(level.getRandom()), EVENT_DISPLAY_NAME, BossEvent.BossBarColor.PINK, BossEvent.BossBarOverlay.PROGRESS
|
| 159 | );
|
| 160 | this.dragonEvent.setPlayBossMusic(true).setCreateWorldFog(true);
|
| 161 | this.validPlayer = EntitySelector.ENTITY_STILL_ALIVE.and(EntitySelector.withinDistance(origin.getX(), 128 + origin.getY(), origin.getZ(), 192.0));
|
| 162 | if (this.gateways.isEmpty()) {
|
| 163 | ObjectArrayList<Integer> newGateways = new ObjectArrayList<>(ContiguousSet.create(Range.closedOpen(0, 20), DiscreteDomain.integers()));
|
| 164 | Util.shuffle(newGateways, RandomSource.createThreadLocalInstance(seed));
|
| 165 | this.gateways.addAll(newGateways);
|
| 166 | this.setDirty();
|
| 167 | }
|
| 168 | }
|
| 169 |
|
| 170 | @Deprecated
|
| 171 | @VisibleForTesting
|
| 172 | public void skipArenaLoadedCheck() {
|
| 173 | this.skipArenaLoadedCheck = true;
|
| 174 | }
|
| 175 |
|
| 176 | public void tick() {
|
| 177 | this.dragonEvent.setVisible(!this.dragonKilled);
|
| 178 | if (++this.ticksSinceLastPlayerScan >= 20) {
|
| 179 | this.updatePlayers();
|
| 180 | this.ticksSinceLastPlayerScan = 0;
|
| 181 | }
|
| 182 |
|
| 183 | if (!this.dragonEvent.getPlayers().isEmpty()) {
|
| 184 | this.level.getChunkSource().addTicketWithRadius(TicketType.DRAGON, new ChunkPos(0, 0), 9);
|
| 185 | if (!this.isArenaLoaded()) {
|
| 186 | return;
|
| 187 | }
|
| 188 |
|
| 189 | if (this.needsStateScanning) {
|
| 190 | this.scanState();
|
| 191 | this.needsStateScanning = false;
|
| 192 | this.setDirty();
|
| 193 | }
|
| 194 |
|
| 195 | if (this.respawnStage != null) {
|
| 196 | List<EndCrystal> respawnCrystals = this.respawnCrystals.stream().map(e -> e.getEntity(this.level, EndCrystal.class)).filter(Objects::nonNull).toList();
|
| 197 | if (respawnCrystals.isEmpty()) {
|
| 198 | this.abortRespawnSequence();
|
| 199 | return;
|
| 200 | }
|
| 201 |
|
| 202 | this.respawnStage.tick(this.level, this, respawnCrystals, this.respawnTime++);
|
| 203 | this.setDirty();
|
| 204 | }
|
| 205 |
|
| 206 | if (!this.dragonKilled) {
|
| 207 | if (this.dragonUUID == null || ++this.ticksSinceDragonSeen >= 1200) {
|
| 208 | this.findOrCreateDragon();
|
| 209 | this.ticksSinceDragonSeen = 0;
|
| 210 | }
|
| 211 |
|
| 212 | if (++this.ticksSinceCrystalsScanned >= 100) {
|
| 213 | this.updateCrystalCount();
|
| 214 | this.ticksSinceCrystalsScanned = 0;
|
| 215 | }
|
| 216 | }
|
| 217 | } else {
|
| 218 | this.level.getChunkSource().removeTicketWithRadius(TicketType.DRAGON, new ChunkPos(0, 0), 9);
|
| 219 | }
|
| 220 | }
|
| 221 |
|
| 222 | private void scanState() {
|
| 223 | LOGGER.info("Scanning for legacy world dragon fight...");
|
| 224 | boolean activePortalExists = this.hasActiveExitPortal();
|
| 225 | if (activePortalExists) {
|
| 226 | LOGGER.info("Found that the dragon has been killed in this world already.");
|
| 227 | this.hasPreviouslyKilledDragon = true;
|
| 228 | } else {
|
| 229 | LOGGER.info("Found that the dragon has not yet been killed in this world.");
|
| 230 | this.hasPreviouslyKilledDragon = false;
|
| 231 | if (this.findExitPortal() == null) {
|
| 232 | this.spawnExitPortal(false);
|
| 233 | }
|
| 234 | }
|
| 235 |
|
| 236 | List<? extends EnderDragon> entities = this.level.getDragons();
|
| 237 | if (entities.isEmpty()) {
|
| 238 | this.dragonKilled = true;
|
| 239 | } else {
|
| 240 | EnderDragon dragon = entities.get(0);
|
| 241 | this.dragonUUID = dragon.getUUID();
|
| 242 | LOGGER.info("Found that there's a dragon still alive ({})", dragon);
|
| 243 | this.dragonKilled = false;
|
| 244 | if (!activePortalExists) {
|
| 245 | LOGGER.info("But we didn't have a portal, let's remove it.");
|
| 246 | dragon.discard();
|
| 247 | this.dragonUUID = null;
|
| 248 | }
|
| 249 | }
|
| 250 |
|
| 251 | if (!this.hasPreviouslyKilledDragon && this.dragonKilled) {
|
| 252 | this.dragonKilled = false;
|
| 253 | }
|
| 254 |
|
| 255 | this.setDirty();
|
| 256 | }
|
| 257 |
|
| 258 | private void findOrCreateDragon() {
|
| 259 | List<? extends EnderDragon> entities = this.level.getDragons();
|
| 260 | if (entities.isEmpty()) {
|
| 261 | LOGGER.debug("Haven't seen the dragon, respawning it");
|
| 262 | this.createNewDragon();
|
| 263 | } else {
|
| 264 | LOGGER.debug("Haven't seen our dragon, but found another one to use.");
|
| 265 | this.dragonUUID = entities.get(0).getUUID();
|
| 266 | this.setDirty();
|
| 267 | }
|
| 268 | }
|
| 269 |
|
| 270 | protected void setRespawnStage(final DragonRespawnStage stage) {
|
| 271 | if (this.respawnStage == null) {
|
| 272 | throw new IllegalStateException("Dragon respawn isn't in progress, can't skip ahead in the animation.");
|
| 273 | }
|
| 274 |
|
| 275 | this.respawnTime = 0;
|
| 276 | if (stage == DragonRespawnStage.END) {
|
| 277 | this.respawnStage = null;
|
| 278 | this.dragonKilled = false;
|
| 279 | EnderDragon dragon = this.createNewDragon();
|
| 280 | if (dragon != null) {
|
| 281 | for (ServerPlayer player : this.dragonEvent.getPlayers()) {
|
| 282 | CriteriaTriggers.SUMMONED_ENTITY.trigger(player, dragon);
|
| 283 | }
|
| 284 | }
|
| 285 | } else {
|
| 286 | this.respawnStage = stage;
|
| 287 | }
|
| 288 |
|
| 289 | this.setDirty();
|
| 290 | }
|
| 291 |
|
| 292 | private boolean hasActiveExitPortal() {
|
| 293 | for (int x = -8; x <= 8; x++) {
|
| 294 | for (int z = -8; z <= 8; z++) {
|
| 295 | LevelChunk chunk = this.level.getChunk(x, z);
|
| 296 |
|
| 297 | for (BlockEntity blockEntity : chunk.getBlockEntities().values()) {
|
| 298 | if (blockEntity instanceof TheEndPortalBlockEntity) {
|
| 299 | return true;
|
| 300 | }
|
| 301 | }
|
| 302 | }
|
| 303 | }
|
| 304 |
|
| 305 | return false;
|
| 306 | }
|
| 307 |
|
| 308 | private BlockPattern.@Nullable BlockPatternMatch findExitPortal() {
|
| 309 | ChunkPos chunkOrigin = ChunkPos.containing(this.origin);
|
| 310 |
|
| 311 | for (int x = -8 + chunkOrigin.x(); x <= 8 + chunkOrigin.x(); x++) {
|
| 312 | for (int z = -8 + chunkOrigin.z(); z <= 8 + chunkOrigin.z(); z++) {
|
| 313 | LevelChunk chunk = this.level.getChunk(x, z);
|
| 314 |
|
| 315 | for (BlockEntity blockEntity : chunk.getBlockEntities().values()) {
|
| 316 | if (blockEntity instanceof TheEndPortalBlockEntity) {
|
| 317 | BlockPattern.BlockPatternMatch match = this.exitPortalPattern.find(this.level, blockEntity.getBlockPos());
|
| 318 | if (match != null) {
|
| 319 | BlockPos posInWorld = match.getBlock(3, 3, 3).getPos();
|
| 320 | if (this.exitPortalLocation == null) {
|
| 321 | this.exitPortalLocation = posInWorld;
|
| 322 | this.setDirty();
|
| 323 | }
|
| 324 |
|
| 325 | return match;
|
| 326 | }
|
| 327 | }
|
| 328 | }
|
| 329 | }
|
| 330 | }
|
| 331 |
|
| 332 | BlockPos endPodiumLocation = getPodiumLocation(this.origin);
|
| 333 | int maxY = this.level.getHeightmapPos(Heightmap.Types.MOTION_BLOCKING, endPodiumLocation).getY();
|
| 334 |
|
| 335 | for (int y = maxY; y >= this.level.getMinY(); y--) {
|
| 336 | BlockPattern.BlockPatternMatch match = this.exitPortalPattern.find(this.level, new BlockPos(endPodiumLocation.getX(), y, endPodiumLocation.getZ()));
|
| 337 | if (match != null) {
|
| 338 | if (this.exitPortalLocation == null) {
|
| 339 | this.exitPortalLocation = match.getBlock(3, 3, 3).getPos();
|
| 340 | this.setDirty();
|
| 341 | }
|
| 342 |
|
| 343 | return match;
|
| 344 | }
|
| 345 | }
|
| 346 |
|
| 347 | return null;
|
| 348 | }
|
| 349 |
|
| 350 | private boolean isArenaLoaded() {
|
| 351 | if (this.skipArenaLoadedCheck) {
|
| 352 | return true;
|
| 353 | }
|
| 354 |
|
| 355 | ChunkPos chunkOrigin = ChunkPos.containing(this.origin);
|
| 356 |
|
| 357 | for (int x = -8 + chunkOrigin.x(); x <= 8 + chunkOrigin.x(); x++) {
|
| 358 | for (int z = 8 + chunkOrigin.z(); z <= 8 + chunkOrigin.z(); z++) {
|
| 359 | if (!(this.level.getChunk(x, z, ChunkStatus.FULL, false) instanceof LevelChunk levelChunk)) {
|
| 360 | return false;
|
| 361 | }
|
| 362 |
|
| 363 | FullChunkStatus status = levelChunk.getFullStatus();
|
| 364 | if (!status.isOrAfter(FullChunkStatus.BLOCK_TICKING)) {
|
| 365 | return false;
|
| 366 | }
|
| 367 | }
|
| 368 | }
|
| 369 |
|
| 370 | return ChunkPos.rangeClosed(chunkOrigin, 1).allMatch(pos -> this.level.areEntitiesLoaded(pos.pack()));
|
| 371 | }
|
| 372 |
|
| 373 | private void updatePlayers() {
|
| 374 | Set<ServerPlayer> newPlayers = Sets.newHashSet();
|
| 375 |
|
| 376 | for (ServerPlayer player : this.level.getPlayers(this.validPlayer)) {
|
| 377 | this.dragonEvent.addPlayer(player);
|
| 378 | newPlayers.add(player);
|
| 379 | }
|
| 380 |
|
| 381 | Set<ServerPlayer> toRemove = Sets.newHashSet(this.dragonEvent.getPlayers());
|
| 382 | toRemove.removeAll(newPlayers);
|
| 383 |
|
| 384 | for (ServerPlayer player : toRemove) {
|
| 385 | this.dragonEvent.removePlayer(player);
|
| 386 | }
|
| 387 | }
|
| 388 |
|
| 389 | private void updateCrystalCount() {
|
| 390 | this.ticksSinceCrystalsScanned = 0;
|
| 391 | this.aliveCrystals = 0;
|
| 392 |
|
| 393 | for (EndSpikeFeature.EndSpike spike : EndSpikeFeature.getSpikesForLevel(this.level)) {
|
| 394 | this.aliveCrystals = this.aliveCrystals + this.level.getEntitiesOfClass(EndCrystal.class, spike.getTopBoundingBox()).size();
|
| 395 | }
|
| 396 |
|
| 397 | LOGGER.debug("Found {} end crystals still alive", this.aliveCrystals);
|
| 398 | }
|
| 399 |
|
| 400 | public void setDragonKilled(final EnderDragon dragon) {
|
| 401 | if (dragon.getUUID().equals(this.dragonUUID)) {
|
| 402 | this.dragonEvent.setProgress(0.0F);
|
| 403 | this.dragonEvent.setVisible(false);
|
| 404 | this.spawnExitPortal(true);
|
| 405 | this.spawnNewGateway();
|
| 406 | if (!this.hasPreviouslyKilledDragon) {
|
| 407 | this.level
|
| 408 | .setBlockAndUpdate(this.level.getHeightmapPos(Heightmap.Types.MOTION_BLOCKING, getPodiumLocation(this.origin)), Blocks.DRAGON_EGG.defaultBlockState());
|
| 409 | }
|
| 410 |
|
| 411 | this.hasPreviouslyKilledDragon = true;
|
| 412 | this.dragonKilled = true;
|
| 413 | this.setDirty();
|
| 414 | }
|
| 415 | }
|
| 416 |
|
| 417 | @Deprecated
|
| 418 | @VisibleForTesting
|
| 419 | public void removeAllGateways() {
|
| 420 | this.gateways.clear();
|
| 421 | this.setDirty();
|
| 422 | }
|
| 423 |
|
| 424 | private void spawnNewGateway() {
|
| 425 | if (!this.gateways.isEmpty()) {
|
| 426 | int gateway = this.gateways.remove(this.gateways.size() - 1);
|
| 427 | int x = Mth.floor(96.0 * Math.cos(2.0 * (-Math.PI + (Math.PI / 20) * gateway)));
|
| 428 | int z = Mth.floor(96.0 * Math.sin(2.0 * (-Math.PI + (Math.PI / 20) * gateway)));
|
| 429 | this.spawnNewGateway(new BlockPos(x, 75, z));
|
| 430 | this.setDirty();
|
| 431 | }
|
| 432 | }
|
| 433 |
|
| 434 | private void spawnNewGateway(final BlockPos pos) {
|
| 435 | this.level.levelEvent(3000, pos, 0);
|
| 436 | this.level
|
| 437 | .registryAccess()
|
| 438 | .lookup(Registries.FEATURE)
|
| 439 | .flatMap(registry -> registry.get(EndFeatures.END_GATEWAY_DELAYED))
|
| 440 | .ifPresent(endGateway -> endGateway.value().place(this.level, this.level.getChunkSource().getGenerator(), RandomSource.create(), pos));
|
| 441 | }
|
| 442 |
|
| 443 | private void spawnExitPortal(final boolean activated) {
|
| 444 | if (this.exitPortalLocation == null) {
|
| 445 | this.exitPortalLocation = this.level.getHeightmapPos(Heightmap.Types.MOTION_BLOCKING_NO_LEAVES, getPodiumLocation(this.origin)).below();
|
| 446 |
|
| 447 | while (this.level.getBlockState(this.exitPortalLocation).is(Blocks.BEDROCK) && this.exitPortalLocation.getY() > 63) {
|
| 448 | this.exitPortalLocation = this.exitPortalLocation.below();
|
| 449 | }
|
| 450 |
|
| 451 | this.exitPortalLocation = this.exitPortalLocation.atY(Math.max(this.level.getMinY() + 1, this.exitPortalLocation.getY()));
|
| 452 | this.setDirty();
|
| 453 | }
|
| 454 |
|
| 455 | Holder.Reference<Feature> feature = this.level.registryAccess().getOrThrow(activated ? EndFeatures.END_PODIUM_ACTIVE : EndFeatures.END_PODIUM_INACTIVE);
|
| 456 | if (feature.value().place(this.level, this.level.getChunkSource().getGenerator(), RandomSource.create(), this.exitPortalLocation)) {
|
| 457 | int chunkRadius = Mth.positiveCeilDiv(4, 16);
|
| 458 | this.level.getChunkSource().chunkMap.waitForLightBeforeSending(ChunkPos.containing(this.exitPortalLocation), chunkRadius);
|
| 459 | }
|
| 460 | }
|
| 461 |
|
| 462 | private @Nullable EnderDragon createNewDragon() {
|
| 463 | this.level.getChunkAt(new BlockPos(this.origin.getX(), 128 + this.origin.getY(), this.origin.getZ()));
|
| 464 | EnderDragon dragon = EntityTypes.ENDER_DRAGON.create(this.level, EntitySpawnReason.EVENT);
|
| 465 | if (dragon != null) {
|
| 466 | dragon.setDragonFight(this);
|
| 467 | dragon.setFightOrigin(this.origin);
|
| 468 | dragon.getPhaseManager().setPhase(EnderDragonPhase.HOLDING_PATTERN);
|
| 469 | dragon.snapTo(this.origin.getX(), 128 + this.origin.getY(), this.origin.getZ(), this.level.getRandom().nextFloat() * 360.0F, 0.0F);
|
| 470 | this.level.addFreshEntity(dragon);
|
| 471 | this.dragonUUID = dragon.getUUID();
|
| 472 | this.setDirty();
|
| 473 | }
|
| 474 |
|
| 475 | return dragon;
|
| 476 | }
|
| 477 |
|
| 478 | public void updateDragon(final EnderDragon dragon) {
|
| 479 | if (dragon.getUUID().equals(this.dragonUUID)) {
|
| 480 | this.dragonEvent.setProgress(dragon.getHealth() / dragon.getMaxHealth());
|
| 481 | this.ticksSinceDragonSeen = 0;
|
| 482 | if (dragon.hasCustomName()) {
|
| 483 | this.dragonEvent.setName(dragon.getDisplayName());
|
| 484 | }
|
| 485 | }
|
| 486 | }
|
| 487 |
|
| 488 | public int aliveCrystals() {
|
| 489 | return this.aliveCrystals;
|
| 490 | }
|
| 491 |
|
| 492 | public void onCrystalDestroyed(final EndCrystal crystal, final DamageSource source) {
|
| 493 | if (this.respawnStage != null && this.respawnCrystals.stream().anyMatch(ref -> ref.matches(crystal))) {
|
| 494 | this.abortRespawnSequence();
|
| 495 | } else {
|
| 496 | this.updateCrystalCount();
|
| 497 | if (this.level.getEntity(this.dragonUUID) instanceof EnderDragon actuallyDragon) {
|
| 498 | actuallyDragon.onCrystalDestroyed(this.level, crystal, crystal.blockPosition(), source);
|
| 499 | }
|
| 500 | }
|
| 501 | }
|
| 502 |
|
| 503 | private void abortRespawnSequence() {
|
| 504 | LOGGER.debug("Aborting respawn sequence");
|
| 505 | this.respawnStage = null;
|
| 506 | this.respawnTime = 0;
|
| 507 | this.resetSpikeCrystals();
|
| 508 | this.spawnExitPortal(true);
|
| 509 | this.setDirty();
|
| 510 | }
|
| 511 |
|
| 512 | public boolean hasPreviouslyKilledDragon() {
|
| 513 | return this.hasPreviouslyKilledDragon;
|
| 514 | }
|
| 515 |
|
| 516 | public void tryRespawn() {
|
| 517 | if (this.dragonKilled && this.respawnStage == null) {
|
| 518 | BlockPos location = this.exitPortalLocation;
|
| 519 | if (location == null) {
|
| 520 | LOGGER.debug("Tried to respawn, but need to find the portal first.");
|
| 521 | BlockPattern.BlockPatternMatch match = this.findExitPortal();
|
| 522 | if (match == null) {
|
| 523 | LOGGER.debug("Couldn't find a portal, so we made one.");
|
| 524 | this.spawnExitPortal(true);
|
| 525 | } else {
|
| 526 | LOGGER.debug("Found the exit portal & saved its location for next time.");
|
| 527 | }
|
| 528 |
|
| 529 | location = this.exitPortalLocation;
|
| 530 | }
|
| 531 |
|
| 532 | List<EndCrystal> crystals = Lists.newArrayList();
|
| 533 | BlockPos center = location.above(1);
|
| 534 |
|
| 535 | for (Direction direction : Direction.Plane.HORIZONTAL) {
|
| 536 | List<EndCrystal> found = this.level.getEntitiesOfClass(EndCrystal.class, new AABB(center.relative(direction, 3)));
|
| 537 | if (found.isEmpty()) {
|
| 538 | return;
|
| 539 | }
|
| 540 |
|
| 541 | crystals.addAll(found);
|
| 542 | }
|
| 543 |
|
| 544 | LOGGER.debug("Found all crystals, respawning dragon.");
|
| 545 | this.respawnDragon(crystals);
|
| 546 | }
|
| 547 | }
|
| 548 |
|
| 549 | private void respawnDragon(final List<EndCrystal> crystals) {
|
| 550 | if (this.dragonKilled && this.respawnStage == null) {
|
| 551 | for (BlockPattern.BlockPatternMatch portal = this.findExitPortal(); portal != null; portal = this.findExitPortal()) {
|
| 552 | for (int x = 0; x < this.exitPortalPattern.getWidth(); x++) {
|
| 553 | for (int y = 0; y < this.exitPortalPattern.getHeight(); y++) {
|
| 554 | for (int z = 0; z < this.exitPortalPattern.getDepth(); z++) {
|
| 555 | BlockInWorld block = portal.getBlock(x, y, z);
|
| 556 | if (block.getState().is(Blocks.BEDROCK) || block.getState().is(Blocks.END_PORTAL)) {
|
| 557 | this.level.setBlockAndUpdate(block.getPos(), Blocks.END_STONE.defaultBlockState());
|
| 558 | }
|
| 559 | }
|
| 560 | }
|
| 561 | }
|
| 562 | }
|
| 563 |
|
| 564 | this.respawnStage = DragonRespawnStage.START;
|
| 565 | this.respawnTime = 0;
|
| 566 | this.spawnExitPortal(false);
|
| 567 | this.respawnCrystals = crystals.stream().map(EntityReference::of).toList();
|
| 568 | this.setDirty();
|
| 569 | }
|
| 570 | }
|
| 571 |
|
| 572 | public void resetSpikeCrystals() {
|
| 573 | for (EndSpikeFeature.EndSpike spike : EndSpikeFeature.getSpikesForLevel(this.level)) {
|
| 574 | for (EndCrystal crystal : this.level.getEntitiesOfClass(EndCrystal.class, spike.getTopBoundingBox())) {
|
| 575 | crystal.setPermanentlyInvulnerable(false);
|
| 576 | crystal.setBeamTarget(null);
|
| 577 | }
|
| 578 | }
|
| 579 | }
|
| 580 |
|
| 581 | public @Nullable UUID dragonUUID() {
|
| 582 | return this.dragonUUID;
|
| 583 | }
|
| 584 | }
|
| 585 |
|