| 1 | package net.minecraft.server.network.config;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import java.util.Optional;
|
| 5 | import java.util.concurrent.CompletableFuture;
|
| 6 | import java.util.function.Consumer;
|
| 7 | import net.minecraft.core.BlockPos;
|
| 8 | import net.minecraft.network.Connection;
|
| 9 | import net.minecraft.network.protocol.Packet;
|
| 10 | import net.minecraft.server.MinecraftServer;
|
| 11 | import net.minecraft.server.level.ChunkLoadCounter;
|
| 12 | import net.minecraft.server.level.PlayerSpawnFinder;
|
| 13 | import net.minecraft.server.level.ServerLevel;
|
| 14 | import net.minecraft.server.level.ServerPlayer;
|
| 15 | import net.minecraft.server.level.TicketType;
|
| 16 | import net.minecraft.server.level.progress.LevelLoadListener;
|
| 17 | import net.minecraft.server.network.CommonListenerCookie;
|
| 18 | import net.minecraft.server.network.ConfigurationTask;
|
| 19 | import net.minecraft.server.players.NameAndId;
|
| 20 | import net.minecraft.util.ProblemReporter;
|
| 21 | import net.minecraft.world.level.ChunkPos;
|
| 22 | import net.minecraft.world.level.storage.LevelData;
|
| 23 | import net.minecraft.world.level.storage.TagValueInput;
|
| 24 | import net.minecraft.world.level.storage.ValueInput;
|
| 25 | import net.minecraft.world.phys.Vec2;
|
| 26 | import net.minecraft.world.phys.Vec3;
|
| 27 | import org.jspecify.annotations.Nullable;
|
| 28 | import org.slf4j.Logger;
|
| 29 |
|
| 30 | public class PrepareSpawnTask implements ConfigurationTask {
|
| 31 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 32 | public static final ConfigurationTask.Type TYPE = new ConfigurationTask.Type("prepare_spawn");
|
| 33 | public static final int PREPARE_CHUNK_RADIUS = 3;
|
| 34 | private final MinecraftServer server;
|
| 35 | private final NameAndId nameAndId;
|
| 36 | private final LevelLoadListener loadListener;
|
| 37 | private PrepareSpawnTask.@Nullable State state;
|
| 38 |
|
| 39 | public PrepareSpawnTask(final MinecraftServer server, final NameAndId nameAndId) {
|
| 40 | this.server = server;
|
| 41 | this.nameAndId = nameAndId;
|
| 42 | this.loadListener = server.getLevelLoadListener();
|
| 43 | }
|
| 44 |
|
| 45 | @Override
|
| 46 | public void start(final Consumer<Packet<?>> connection) {
|
| 47 | try (ProblemReporter.ScopedCollector reporter = new ProblemReporter.ScopedCollector(LOGGER)) {
|
| 48 | Optional<ValueInput> loadedData = this.server
|
| 49 | .getPlayerList()
|
| 50 | .loadPlayerData(this.nameAndId)
|
| 51 | .map(tag -> TagValueInput.create(reporter, this.server.registryAccess(), tag));
|
| 52 | ServerPlayer.SavedPosition loadedPosition = loadedData.<ServerPlayer.SavedPosition>flatMap(tag -> tag.read(ServerPlayer.SavedPosition.MAP_CODEC))
|
| 53 | .orElse(ServerPlayer.SavedPosition.EMPTY);
|
| 54 | LevelData.RespawnData respawnData = this.server.getWorldData().overworldData().getRespawnData();
|
| 55 | ServerLevel spawnLevel = loadedPosition.dimension().map(this.server::getLevel).orElseGet(() -> {
|
| 56 | ServerLevel spawnDataLevel = this.server.getLevel(respawnData.dimension());
|
| 57 | return spawnDataLevel != null ? spawnDataLevel : this.server.overworld();
|
| 58 | });
|
| 59 | CompletableFuture<Vec3> spawnPosition = loadedPosition.position()
|
| 60 | .map(CompletableFuture::completedFuture)
|
| 61 | .orElseGet(() -> PlayerSpawnFinder.findSpawn(spawnLevel, respawnData.pos()));
|
| 62 | Vec2 spawnAngle = loadedPosition.rotation().orElse(new Vec2(respawnData.yaw(), respawnData.pitch()));
|
| 63 | this.state = new PrepareSpawnTask.Preparing(spawnLevel, spawnPosition, spawnAngle);
|
| 64 | }
|
| 65 | }
|
| 66 |
|
| 67 | @Override
|
| 68 | public boolean tick() {
|
| 69 | return switch (this.state) {
|
| 70 | case null -> false;
|
| 71 | case PrepareSpawnTask.Preparing preparing -> {
|
| 72 | PrepareSpawnTask.Ready ready = preparing.tick();
|
| 73 | if (ready != null) {
|
| 74 | this.state = ready;
|
| 75 | yield true;
|
| 76 | } else {
|
| 77 | yield false;
|
| 78 | }
|
| 79 | }
|
| 80 | case PrepareSpawnTask.Ready ignored -> true;
|
| 81 | default -> throw new MatchException(null, null);
|
| 82 | };
|
| 83 | }
|
| 84 |
|
| 85 | public ServerPlayer spawnPlayer(final Connection connection, final CommonListenerCookie cookie) {
|
| 86 | if (this.state instanceof PrepareSpawnTask.Ready ready) {
|
| 87 | return ready.spawn(connection, cookie);
|
| 88 | } else {
|
| 89 | throw new IllegalStateException("Player spawn was not ready");
|
| 90 | }
|
| 91 | }
|
| 92 |
|
| 93 | public void keepAlive() {
|
| 94 | if (this.state instanceof PrepareSpawnTask.Ready ready) {
|
| 95 | ready.keepAlive();
|
| 96 | }
|
| 97 | }
|
| 98 |
|
| 99 | public void close() {
|
| 100 | if (this.state instanceof PrepareSpawnTask.Preparing preparing) {
|
| 101 | preparing.cancel();
|
| 102 | }
|
| 103 |
|
| 104 | this.state = null;
|
| 105 | }
|
| 106 |
|
| 107 | @Override
|
| 108 | public ConfigurationTask.Type type() {
|
| 109 | return TYPE;
|
| 110 | }
|
| 111 |
|
| 112 | private final class Preparing implements PrepareSpawnTask.State {
|
| 113 | private final ServerLevel spawnLevel;
|
| 114 | private final CompletableFuture<Vec3> spawnPosition;
|
| 115 | private final Vec2 spawnAngle;
|
| 116 | private @Nullable CompletableFuture<?> chunkLoadFuture;
|
| 117 | private final ChunkLoadCounter chunkLoadCounter = new ChunkLoadCounter();
|
| 118 |
|
| 119 | private Preparing(final ServerLevel spawnLevel, final CompletableFuture<Vec3> spawnPosition, final Vec2 spawnAngle) {
|
| 120 | this.spawnLevel = spawnLevel;
|
| 121 | this.spawnPosition = spawnPosition;
|
| 122 | this.spawnAngle = spawnAngle;
|
| 123 | }
|
| 124 |
|
| 125 | public void cancel() {
|
| 126 | this.spawnPosition.cancel(false);
|
| 127 | }
|
| 128 |
|
| 129 | public PrepareSpawnTask.@Nullable Ready tick() {
|
| 130 | if (!this.spawnPosition.isDone()) {
|
| 131 | return null;
|
| 132 | }
|
| 133 |
|
| 134 | Vec3 spawnPosition = this.spawnPosition.join();
|
| 135 | if (this.chunkLoadFuture == null) {
|
| 136 | ChunkPos spawnChunk = ChunkPos.containing(BlockPos.containing(spawnPosition));
|
| 137 | this.chunkLoadCounter
|
| 138 | .track(this.spawnLevel, () -> this.chunkLoadFuture = this.spawnLevel.getChunkSource().addTicketAndLoadWithRadius(TicketType.PLAYER_SPAWN, spawnChunk, 3));
|
| 139 | PrepareSpawnTask.this.loadListener.start(LevelLoadListener.Stage.LOAD_PLAYER_CHUNKS, this.chunkLoadCounter.totalChunks());
|
| 140 | PrepareSpawnTask.this.loadListener.updateFocus(this.spawnLevel.dimension(), spawnChunk);
|
| 141 | }
|
| 142 |
|
| 143 | PrepareSpawnTask.this.loadListener
|
| 144 | .update(LevelLoadListener.Stage.LOAD_PLAYER_CHUNKS, this.chunkLoadCounter.readyChunks(), this.chunkLoadCounter.totalChunks());
|
| 145 | if (!this.chunkLoadFuture.isDone()) {
|
| 146 | return null;
|
| 147 | }
|
| 148 |
|
| 149 | PrepareSpawnTask.this.loadListener.finish(LevelLoadListener.Stage.LOAD_PLAYER_CHUNKS);
|
| 150 | return PrepareSpawnTask.this.new Ready(this.spawnLevel, spawnPosition, this.spawnAngle);
|
| 151 | }
|
| 152 | }
|
| 153 |
|
| 154 | private final class Ready implements PrepareSpawnTask.State {
|
| 155 | private final ServerLevel spawnLevel;
|
| 156 | private final Vec3 spawnPosition;
|
| 157 | private final Vec2 spawnAngle;
|
| 158 |
|
| 159 | private Ready(final ServerLevel spawnLevel, final Vec3 spawnPosition, final Vec2 spawnAngle) {
|
| 160 | this.spawnLevel = spawnLevel;
|
| 161 | this.spawnPosition = spawnPosition;
|
| 162 | this.spawnAngle = spawnAngle;
|
| 163 | }
|
| 164 |
|
| 165 | public void keepAlive() {
|
| 166 | this.spawnLevel.getChunkSource().addTicketWithRadius(TicketType.PLAYER_SPAWN, ChunkPos.containing(BlockPos.containing(this.spawnPosition)), 3);
|
| 167 | }
|
| 168 |
|
| 169 | public ServerPlayer spawn(final Connection connection, final CommonListenerCookie cookie) {
|
| 170 | ChunkPos spawnChunk = ChunkPos.containing(BlockPos.containing(this.spawnPosition));
|
| 171 | this.spawnLevel.waitForEntities(spawnChunk, 3);
|
| 172 | ServerPlayer player = new ServerPlayer(PrepareSpawnTask.this.server, this.spawnLevel, cookie.gameProfile(), cookie.clientInformation());
|
| 173 |
|
| 174 | try (ProblemReporter.ScopedCollector reporter = new ProblemReporter.ScopedCollector(player.problemPath(), PrepareSpawnTask.LOGGER)) {
|
| 175 | Optional<ValueInput> input = PrepareSpawnTask.this.server
|
| 176 | .getPlayerList()
|
| 177 | .loadPlayerData(PrepareSpawnTask.this.nameAndId)
|
| 178 | .map(tag -> TagValueInput.create(reporter, PrepareSpawnTask.this.server.registryAccess(), tag));
|
| 179 | input.ifPresent(player::load);
|
| 180 | player.snapTo(this.spawnPosition, this.spawnAngle.x, this.spawnAngle.y);
|
| 181 | PrepareSpawnTask.this.server.getPlayerList().placeNewPlayer(connection, player, cookie);
|
| 182 | input.ifPresent(tag -> {
|
| 183 | player.loadAndSpawnEnderPearls(tag);
|
| 184 | player.loadAndSpawnParentVehicle(tag);
|
| 185 | });
|
| 186 | return player;
|
| 187 | }
|
| 188 | }
|
| 189 | }
|
| 190 |
|
| 191 | private sealed interface State permits PrepareSpawnTask.Preparing, PrepareSpawnTask.Ready {
|
| 192 | }
|
| 193 | }
|
| 194 |
|