Skip to content

Commit 631aade

Browse files
committed
new: preDowngrade hook
1 parent e081820 commit 631aade

File tree

7 files changed

+27
-30
lines changed

7 files changed

+27
-30
lines changed

c2me-rewrites-chunk-system/src/main/java/com/ishland/c2me/rewrites/chunksystem/common/statuses/Deferred.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import io.reactivex.rxjava3.core.Completable;
77
import net.minecraft.world.chunk.ChunkStatus;
88

9-
import java.util.concurrent.CompletableFuture;
10-
119
public class Deferred extends NewChunkStatus {
1210

1311
public Deferred(int ordinal) {

c2me-rewrites-chunk-system/src/main/java/com/ishland/c2me/rewrites/chunksystem/common/statuses/ReadFromDisk.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.ishland.flowsched.util.Assertions;
2121
import io.reactivex.rxjava3.annotations.NonNull;
2222
import io.reactivex.rxjava3.core.Completable;
23+
import io.reactivex.rxjava3.core.CompletableObserver;
2324
import io.reactivex.rxjava3.core.Single;
2425
import io.reactivex.rxjava3.schedulers.Schedulers;
2526
import net.minecraft.nbt.NbtElement;
@@ -120,18 +121,24 @@ protected Completable finalizeLoading(ChunkLoadingContext context, Single<ProtoC
120121
}
121122

122123
@Override
123-
public Completable downgradeFromThis(ChunkLoadingContext context, Cancellable cancellable) {
124+
public Completable preDowngradeFromThis(ChunkLoadingContext context, Cancellable cancellable) {
124125
return Completable.defer(() -> Completable.fromCompletionStage(syncWithLightEngine(context)))
125126
.observeOn(Schedulers.from(((IThreadedAnvilChunkStorage) context.tacs()).getMainThreadExecutor()))
126-
.doOnEvent(throwable -> {
127-
if (throwable != null) return;
127+
.andThen((CompletableObserver observer) -> {
128+
if (context.holder().getTargetStatus().ordinal() >= this.ordinal()) { // saving cancelled
129+
cancellable.cancel();
130+
observer.onError(new CancellationException());
131+
} else {
132+
observer.onComplete();
133+
}
134+
});
135+
}
136+
137+
@Override
138+
public Completable downgradeFromThis(ChunkLoadingContext context, Cancellable cancellable) {
139+
return Completable.defer(() -> {
128140
Assertions.assertTrue(((IThreadedAnvilChunkStorage) context.tacs()).getMainThreadExecutor().isOnThread());
129141
try (var ignored = ThreadInstrumentation.getCurrent().begin(new ChunkTaskWork(context, this, false))) {
130-
if (context.holder().getTargetStatus().ordinal() >= this.ordinal()) { // saving cancelled
131-
cancellable.cancel();
132-
throw new CancellationException();
133-
}
134-
135142
final ChunkState chunkState = context.holder().getItem().get();
136143
Chunk chunk = chunkState.chunk();
137144
if (chunk instanceof WrapperProtoChunk protoChunk) chunk = protoChunk.getWrappedChunk();
@@ -174,6 +181,8 @@ public Completable downgradeFromThis(ChunkLoadingContext context, Cancellable ca
174181
((IPOIUnloading) ((IThreadedAnvilChunkStorage) context.tacs()).getPointOfInterestStorage()).c2me$unloadPoi(context.holder().getKey());
175182

176183
context.holder().getItem().set(new ChunkState(null, null, null));
184+
185+
return Completable.complete();
177186
}
178187
})
179188
.doOnError((throwable) -> {

c2me-rewrites-chunk-system/src/main/java/com/ishland/c2me/rewrites/chunksystem/common/statuses/ReadFromDiskAsync.java

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import com.ishland.flowsched.util.Assertions;
2929
import io.reactivex.rxjava3.annotations.NonNull;
3030
import io.reactivex.rxjava3.core.Completable;
31-
import io.reactivex.rxjava3.core.CompletableSource;
3231
import io.reactivex.rxjava3.core.Single;
3332
import io.reactivex.rxjava3.schedulers.Schedulers;
3433
import it.unimi.dsi.fastutil.Pair;
@@ -50,9 +49,7 @@
5049
import org.slf4j.LoggerFactory;
5150

5251
import java.util.concurrent.CancellationException;
53-
import java.util.concurrent.CompletableFuture;
5452
import java.util.concurrent.atomic.AtomicBoolean;
55-
import java.util.function.Function;
5653

5754
public class ReadFromDiskAsync extends ReadFromDisk {
5855

@@ -140,19 +137,18 @@ protected Single<ProtoChunk> invokeAsyncLoad(ChunkLoadingContext context) {
140137
});
141138
}
142139

140+
@Override
141+
public Completable preDowngradeFromThis(ChunkLoadingContext context, Cancellable cancellable) {
142+
return super.preDowngradeFromThis(context, cancellable);
143+
}
144+
143145
@Override
144146
public Completable downgradeFromThis(ChunkLoadingContext context, Cancellable cancellable) {
145147
final AtomicBoolean loadedToWorld = new AtomicBoolean(false);
146-
return Completable.defer(() -> Completable.fromCompletionStage(syncWithLightEngine(context)))
147-
.observeOn(Schedulers.from(((IThreadedAnvilChunkStorage) context.tacs()).getMainThreadExecutor()))
148-
.andThen(Completable.defer(() -> {
148+
return Completable
149+
.defer(() -> {
149150
Assertions.assertTrue(((IThreadedAnvilChunkStorage) context.tacs()).getMainThreadExecutor().isOnThread());
150151
try (var ignored = ThreadInstrumentation.getCurrent().begin(new ChunkTaskWork(context, this, false))) {
151-
if (context.holder().getTargetStatus().ordinal() >= this.ordinal()) { // saving cancelled
152-
cancellable.cancel();
153-
return Completable.error(new CancellationException());
154-
}
155-
156152
final ChunkState chunkState = context.holder().getItem().get();
157153
Chunk chunk = chunkState.chunk();
158154
if (chunk instanceof WrapperProtoChunk protoChunk) chunk = protoChunk.getWrappedChunk();
@@ -173,10 +169,9 @@ public Completable downgradeFromThis(ChunkLoadingContext context, Cancellable ca
173169
return asyncSave(context.tacs(), chunk);
174170
}
175171
}
176-
}))
172+
})
177173
.observeOn(Schedulers.from(((IThreadedAnvilChunkStorage) context.tacs()).getMainThreadExecutor()))
178-
.doOnEvent(throwable -> {
179-
if (throwable != null) return;
174+
.doOnComplete(() -> {
180175
Assertions.assertTrue(((IThreadedAnvilChunkStorage) context.tacs()).getMainThreadExecutor().isOnThread());
181176
try (var ignored = ThreadInstrumentation.getCurrent().begin(new ChunkTaskWork(context, this, false))) {
182177
Chunk chunk = context.holder().getItem().get().chunk();

c2me-rewrites-chunk-system/src/main/java/com/ishland/c2me/rewrites/chunksystem/common/statuses/ServerAccessible.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import net.minecraft.world.chunk.WrapperProtoChunk;
2828

2929
import java.util.List;
30-
import java.util.concurrent.CompletableFuture;
3130

3231
public class ServerAccessible extends NewChunkStatus {
3332

c2me-rewrites-chunk-system/src/main/java/com/ishland/c2me/rewrites/chunksystem/common/statuses/ServerAccessibleChunkSending.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
import net.minecraft.util.math.ChunkPos;
1111
import net.minecraft.world.chunk.ChunkStatus;
1212

13-
import java.util.concurrent.CompletableFuture;
14-
1513
public class ServerAccessibleChunkSending extends NewChunkStatus {
1614

1715
private static final KeyStatusPair<ChunkPos, ChunkState, ChunkLoadingContext>[] deps;

c2me-rewrites-chunk-system/src/main/java/com/ishland/c2me/rewrites/chunksystem/common/statuses/ServerEntityTicking.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
import net.minecraft.world.chunk.ChunkStatus;
2121
import net.minecraft.world.chunk.WorldChunk;
2222

23-
import java.util.concurrent.CompletableFuture;
24-
2523
public class ServerEntityTicking extends NewChunkStatus {
2624

2725
private static final KeyStatusPair<ChunkPos, ChunkState, ChunkLoadingContext>[] deps;

0 commit comments

Comments
 (0)