-
-
Notifications
You must be signed in to change notification settings - Fork 216
Avoid recursion as much as possible when searching for a spawn #4258
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Yeregorix
wants to merge
2
commits into
api-17
Choose a base branch
from
fix/spawn-finder
base: api-17
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
148 changes: 148 additions & 0 deletions
148
src/mixins/java/org/spongepowered/common/mixin/core/server/level/PlayerSpawnFinderMixin.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| /* | ||
| * This file is part of Sponge, licensed under the MIT License (MIT). | ||
| * | ||
| * Copyright (c) SpongePowered <https://www.spongepowered.org> | ||
| * Copyright (c) contributors | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to deal | ||
| * in the Software without restriction, including without limitation the rights | ||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| * copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| * THE SOFTWARE. | ||
| */ | ||
| package org.spongepowered.common.mixin.core.server.level; | ||
|
|
||
| import net.minecraft.CrashReport; | ||
| import net.minecraft.CrashReportCategory; | ||
| import net.minecraft.ReportedException; | ||
| import net.minecraft.core.BlockPos; | ||
| import net.minecraft.core.SectionPos; | ||
| import net.minecraft.server.MinecraftServer; | ||
| import net.minecraft.server.level.PlayerSpawnFinder; | ||
| import net.minecraft.server.level.ServerLevel; | ||
| import net.minecraft.server.level.TicketType; | ||
| import net.minecraft.world.level.ChunkPos; | ||
| import net.minecraft.world.level.CollisionGetter; | ||
| import net.minecraft.world.phys.Vec3; | ||
| import org.spongepowered.asm.mixin.Final; | ||
| import org.spongepowered.asm.mixin.Mixin; | ||
| import org.spongepowered.asm.mixin.Overwrite; | ||
| import org.spongepowered.asm.mixin.Shadow; | ||
|
|
||
| import java.util.Optional; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.function.Supplier; | ||
|
|
||
| import javax.annotation.Nullable; | ||
|
|
||
| @Mixin(PlayerSpawnFinder.class) | ||
| public abstract class PlayerSpawnFinderMixin { | ||
|
|
||
| @Shadow @Final private CompletableFuture<Vec3> finishedFuture; | ||
| @Shadow @Final private ServerLevel level; | ||
| @Shadow @Final private BlockPos spawnSuggestion; | ||
| @Shadow @Final private int radius; | ||
| @Shadow @Final private int candidateCount; | ||
| @Shadow private int nextCandidateIndex; | ||
| @Shadow @Final private int offset; | ||
| @Shadow @Final private int coprime; | ||
|
|
||
| @Shadow private static Vec3 fixupSpawnHeight(CollisionGetter level, BlockPos pos) { return null; } | ||
| @Shadow @Nullable protected static BlockPos getOverworldRespawnPos(ServerLevel level, int x, int z) { return null; } | ||
| @Shadow private static boolean noCollisionNoLiquid(CollisionGetter level, BlockPos pos) { return false; } | ||
|
|
||
| /** | ||
| * A mix of loop and recursion. | ||
| * Loop is preferred when possible. | ||
| * | ||
| * @author Yeregorix | ||
| * @reason Fix <a href="https://bugs.mojang.com/browse/MC/issues/MC-304426">MC-304426</a>. | ||
| */ | ||
| @Overwrite | ||
| private void scheduleNext() { | ||
| int i; | ||
| while ((i = this.nextCandidateIndex++) < this.candidateCount) { | ||
| if (this.finishedFuture.isDone()) { | ||
| return; | ||
| } | ||
|
|
||
| final int candidate = (this.offset + this.coprime * i) % this.candidateCount; | ||
| final int relX = candidate % (this.radius * 2 + 1); | ||
| final int relZ = candidate / (this.radius * 2 + 1); | ||
| final int x = this.spawnSuggestion.getX() + relX - this.radius; | ||
| final int z = this.spawnSuggestion.getZ() + relZ - this.radius; | ||
|
|
||
| if (this.scheduleCandidate(x, z, i, () -> { | ||
| final BlockPos pos = getOverworldRespawnPos(this.level, x, z); | ||
| return pos != null && noCollisionNoLiquid(this.level, pos) ? Optional.of(Vec3.atBottomCenterOf(pos)) : Optional.empty(); | ||
| })) { | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| if (this.finishedFuture.isDone()) { | ||
| return; | ||
| } | ||
|
|
||
| scheduleCandidate(this.spawnSuggestion.getX(), this.spawnSuggestion.getZ(), i, () -> Optional.of(PlayerSpawnFinderMixin.fixupSpawnHeight(this.level, this.spawnSuggestion))); | ||
| } | ||
|
|
||
| private boolean scheduleCandidate(int x, int z, int index, Supplier<Optional<Vec3>> supplier) { | ||
| int chunkX = SectionPos.blockToSectionCoord(x); | ||
| int chunkZ = SectionPos.blockToSectionCoord(z); | ||
| final CompletableFuture<?> future = this.level.getChunkSource().addTicketAndLoadWithRadius(TicketType.SPAWN_SEARCH, new ChunkPos(chunkX, chunkZ), 0); | ||
| final MinecraftServer server = this.level.getServer(); | ||
|
|
||
| if (future.isDone() && server.isSameThread()) { | ||
| try { | ||
| future.get(); | ||
| supplier.get().ifPresent(this.finishedFuture::complete); | ||
| } catch (Throwable error) { | ||
| this.crash(x, z, index, error); | ||
| } | ||
| return false; // stay on loop | ||
| } | ||
|
|
||
| future.whenCompleteAsync((chunk, error) -> { | ||
| if (error == null) { | ||
| try { | ||
| Optional<Vec3> result = supplier.get(); | ||
| if (result.isPresent()) { | ||
| this.finishedFuture.complete(result.get()); | ||
| } else { | ||
| this.scheduleNext(); | ||
| } | ||
| } catch (Exception exception) { | ||
| error = exception; | ||
| } | ||
| } | ||
|
|
||
| if (error != null) { | ||
| this.crash(x, z, index, error); | ||
| } | ||
| }, server); | ||
| return true; // exit loop | ||
| } | ||
|
|
||
| private void crash(final int x, final int z, final int index, final Throwable error) { | ||
| CrashReport report = CrashReport.forThrowable(error, "Searching for spawn"); | ||
| CrashReportCategory category = report.addCategory("Spawn Lookup"); | ||
| category.setDetail("Origin", this.spawnSuggestion::toString); | ||
| category.setDetail("Radius", () -> Integer.toString(this.radius)); | ||
| category.setDetail("Candidate", () -> "[" + x + "," + z + "]"); | ||
| category.setDetail("Progress", () -> index + " out of " + this.candidateCount); | ||
| this.finishedFuture.completeExceptionally(new ReportedException(report)); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.