-
-
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 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
160 changes: 160 additions & 0 deletions
160
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,160 @@ | ||
| /* | ||
| * 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 $$0, BlockPos $$1) { | ||
| throw new IllegalStateException(); | ||
| } | ||
|
|
||
| @Shadow | ||
| @Nullable | ||
| protected static BlockPos getOverworldRespawnPos(ServerLevel $$0, int $$1, int $$2) { | ||
| throw new IllegalStateException(); | ||
| } | ||
|
|
||
| @Shadow | ||
| private static boolean noCollisionNoLiquid(CollisionGetter $$0, BlockPos $$1) { | ||
| throw new IllegalStateException(); | ||
| } | ||
|
|
||
| /** | ||
| * 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 | ||
|
Yeregorix marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| 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.