Skip to content

Commit 7bd89ec

Browse files
committed
Drain world chunk task queue on unload
1 parent 3b69ff0 commit 7bd89ec

File tree

5 files changed

+78
-1
lines changed

5 files changed

+78
-1
lines changed

src/main/resources/common.accesswidener

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ accessible class net/minecraft/network/protocol/game/ServerboundInteractPacket$A
2020
accessible class net/minecraft/network/protocol/game/ServerboundInteractPacket$ActionType
2121
accessible class net/minecraft/world/entity/monster/SpellcasterIllager$IllagerSpell
2222
accessible class net/minecraft/world/level/biome/Biome$ClimateSettings
23+
accessible class net/minecraft/server/level/ServerChunkCache$MainThreadExecutor

src/mixins/java/org/spongepowered/common/mixin/core/server/level/ServerChunkCacheMixin.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,17 @@
3333
import org.spongepowered.asm.mixin.Mixin;
3434
import org.spongepowered.asm.mixin.Shadow;
3535
import org.spongepowered.asm.mixin.injection.At;
36+
import org.spongepowered.asm.mixin.injection.Inject;
3637
import org.spongepowered.asm.mixin.injection.Redirect;
38+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
3739
import org.spongepowered.common.accessor.server.level.ChunkMapAccessor;
3840

3941
@Mixin(ServerChunkCache.class)
4042
public abstract class ServerChunkCacheMixin {
4143

4244
// @formatter:off
4345
@Shadow @Final private ServerLevel level;
46+
@Shadow @Final private ServerChunkCache.MainThreadExecutor mainThreadProcessor;
4447
// @formatter:on
4548

4649
@Redirect(method = "save", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/level/ChunkMap;saveAllChunks(Z)V"))
@@ -51,4 +54,9 @@ public abstract class ServerChunkCacheMixin {
5154
((ChunkMapAccessor) chunkManager).invoker$saveAllChunks(flush);
5255
}
5356
}
57+
58+
@Inject(method = "close", at = @At("TAIL"))
59+
private void impl$onClose(final CallbackInfo ci) {
60+
this.mainThreadProcessor.close();
61+
}
5462
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* This file is part of Sponge, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) SpongePowered <https://www.spongepowered.org>
5+
* Copyright (c) contributors
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package org.spongepowered.common.mixin.core.server.level;
26+
27+
import net.minecraft.server.level.ServerChunkCache;
28+
import net.minecraft.util.thread.BlockableEventLoop;
29+
import org.spongepowered.asm.mixin.Mixin;
30+
31+
@Mixin(ServerChunkCache.MainThreadExecutor.class)
32+
public abstract class ServerChunkCache_MainThreadExecutorMixin extends BlockableEventLoop<Runnable> {
33+
34+
private volatile boolean impl$closed;
35+
36+
protected ServerChunkCache_MainThreadExecutorMixin(final String $$0) {
37+
super($$0);
38+
}
39+
40+
@Override
41+
public void tell(final Runnable $$0) {
42+
if (this.impl$closed) {
43+
$$0.run();
44+
return;
45+
}
46+
47+
super.tell($$0);
48+
49+
if (this.impl$closed) {
50+
this.runAllTasks();
51+
}
52+
}
53+
54+
@Override
55+
public void close() {
56+
super.close();
57+
58+
this.impl$closed = true;
59+
this.runAllTasks();
60+
}
61+
}

src/mixins/java/org/spongepowered/common/mixin/core/server/level/ServerLevelMixin.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ public abstract class ServerLevelMixin extends LevelMixin implements ServerLevel
153153
private Weather impl$prevWeather;
154154
private boolean impl$isManualSave = false;
155155
private long impl$preTickTime = 0L;
156+
private boolean impl$closed = false;
156157

157158
@Inject(method = "<init>", at = @At("TAIL"))
158159
private void impl$onInit(
@@ -206,7 +207,7 @@ public abstract class ServerLevelMixin extends LevelMixin implements ServerLevel
206207

207208
@Override
208209
public boolean bridge$isLoaded() {
209-
if (((LevelBridge) this).bridge$isFake()) {
210+
if (((LevelBridge) this).bridge$isFake() || this.impl$closed) {
210211
return false;
211212
}
212213

@@ -553,4 +554,9 @@ public String toString() {
553554
//Weather, game rules and spawns are per world in Sponge.
554555
instance.broadcastAll($$0, this.shadow$dimension());
555556
}
557+
558+
@Inject(method = "close", at = @At("HEAD"))
559+
private void impl$onClose(final CallbackInfo ci) {
560+
this.impl$closed = true;
561+
}
556562
}

src/mixins/resources/mixins.sponge.core.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
"server.level.DistanceManagerMixin",
7878
"server.level.GenerationChunkHolderMixin",
7979
"server.level.ServerBossEventMixin",
80+
"server.level.ServerChunkCache_MainThreadExecutorMixin",
8081
"server.level.ServerChunkCacheMixin",
8182
"server.level.ServerEntityMixin",
8283
"server.level.ServerLevelMixin",

0 commit comments

Comments
 (0)