Skip to content

Commit

Permalink
Update Forge and simplify mixin stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
fnuecke committed Jan 28, 2022
1 parent 7be40b0 commit f8899d3
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 43 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
org.gradle.jvmargs=-Xmx3G
org.gradle.daemon=false

forge_version=39.0.19
forge_version=39.0.63

semver=0.0.0

Expand Down
49 changes: 8 additions & 41 deletions src/main/java/li/cil/oc2/common/mixin/ChunkMapMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.nio.file.Path;
import java.util.concurrent.CompletableFuture;

/**
* Hooks into {@link ChunkMap} saving code-paths for "hard" save operations.
Expand Down Expand Up @@ -51,46 +49,15 @@ public ChunkMapMixin(final Path path, final DataFixer dataFixer, final boolean s
super(path, dataFixer, sync);
}

/**
* This is for the code-path taken when a chunk is being unloaded.
*/
@Inject(method = "lambda$scheduleUnload$11", at = {@At(value = "INVOKE", target = "Lnet/minecraft/server/level/ChunkMap;save(Lnet/minecraft/world/level/chunk/ChunkAccess;)Z")})
private void beforeSaveOnUnload(final ChunkHolder chunkHolder, final CompletableFuture<?> chunkToSave, final long chunkId, final ChunkAccess chunkAccess, final CallbackInfo ci) {
if (chunkAccess instanceof ChunkAccessExt ext) {
ext.applyAndClearLazyUnsaved();
}
}

/**
* This is for the code-path taken when saving all chunks upon server shutdown or when
* running a save command with the "flush" flag.
*/
@Inject(method = "lambda$saveAllChunks$8", at = {@At(value = "HEAD")})
private static void beforeSyncSave(final ChunkAccess chunkAccess, final CallbackInfoReturnable<Boolean> cir) {
if (chunkAccess instanceof ChunkAccessExt ext) {
ext.applyAndClearLazyUnsaved();
}
}

/**
* This is for the code-path taken when saving chunk upon pausing the game or when
* running a save command without the "flush" flag.
*/
@Inject(method = "saveAllChunks", at = {@At(value = "HEAD")})
private void beforeAsyncSave(final boolean sync, final CallbackInfo ci) {
// The sync case is handled in beforeSyncSave.
if (!sync) {
// Need to iterate this ourselves, because I can't find the hook for the save call
// inside the foreach in the method. Slightly annoying, but only happens on explicit
// save requests, so not too much of a performance worry.
visibleChunkMap.values().forEach(holder -> {
if (holder.wasAccessibleSinceLastSave()) {
final ChunkAccess chunkToSave = holder.getChunkToSave().getNow(null);
if (chunkToSave instanceof ChunkAccessExt ext) {
ext.applyAndClearLazyUnsaved();
}
private void beforeAsyncSave(final CallbackInfo ci) {
visibleChunkMap.values().forEach(holder -> {
if (holder.wasAccessibleSinceLastSave()) {
final ChunkAccess chunkToSave = holder.getChunkToSave().getNow(null);
if (chunkToSave instanceof ChunkAccessExt ext) {
ext.applyAndClearLazyUnsaved();
}
});
}
}
});
}
}
12 changes: 12 additions & 0 deletions src/main/java/li/cil/oc2/common/util/ChunkUtils.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package li.cil.oc2.common.util;

import li.cil.oc2.api.API;
import net.minecraft.core.BlockPos;
import net.minecraft.core.SectionPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.chunk.ChunkAccess;
import net.minecraftforge.event.world.ChunkEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

@Mod.EventBusSubscriber(modid = API.MOD_ID, bus = Mod.EventBusSubscriber.Bus.FORGE)
public final class ChunkUtils {
/**
* This will mark a chunk unsaved lazily, right before an attempt to save it would be made due
Expand Down Expand Up @@ -60,4 +65,11 @@ public static void setLazyUnsaved(final Level level, final BlockPos blockPos) {
setLazyUnsaved(level.getChunk(chunkX, chunkZ));
}
}

@SubscribeEvent
public static void handleChunkUnload(final ChunkEvent.Unload event) {
if (event.getChunk() instanceof ChunkAccessExt ext) {
ext.applyAndClearLazyUnsaved();
}
}
}
2 changes: 1 addition & 1 deletion src/main/resources/META-INF/mods.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ This mod uses the Terminus Font under the Open Font License. The full license ca
[[dependencies.oc2]]
modId = "forge"
mandatory = true
versionRange = "[39.0.19,)"
versionRange = "[39.0.63,)"
ordering = "NONE"
side = "BOTH"
[[dependencies.oc2]]
Expand Down

0 comments on commit f8899d3

Please sign in to comment.