Skip to content

Commit

Permalink
Fixes to serialization behavior
Browse files Browse the repository at this point in the history
We were marking world unload as manual save, it should be explicitly asked for.

The documentation of ServerWorld#save states that it should return false on SerializationBehavior.NONE.
However still call the events to be consistent with how it works for other sources.

Make sure we set the isManualSave to false in case the save event is cancelled. Otherwise the next call would be errously marked as manual save.

Fixed that we did not do automatic saves on saves marked as manual, we should always on automatic behaviour.
  • Loading branch information
aromaa committed Jan 21, 2023
1 parent 9c89669 commit 3a371bb
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,6 @@ private void unloadWorld0(final ServerLevel world) throws IOException {
world.getChunkSource().removeRegionTicket(SpongeWorldManager.SPAWN_CHUNKS, new ChunkPos(spawnPoint), 11, registryKey.location());

((PrimaryLevelDataBridge) world.getLevelData()).bridge$configAdapter().save();
((ServerLevelBridge) world).bridge$setManualSave(true);

try {
world.save(null, true, world.noSave);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import org.spongepowered.api.scheduler.ScheduledUpdateList;
import org.spongepowered.api.util.Ticks;
import org.spongepowered.api.world.BlockChangeFlag;
import org.spongepowered.api.world.SerializationBehavior;
import org.spongepowered.api.world.border.WorldBorder;
import org.spongepowered.api.world.chunk.WorldChunk;
import org.spongepowered.api.world.generation.ChunkGenerator;
Expand All @@ -74,6 +75,7 @@
import org.spongepowered.common.accessor.world.entity.raid.RaidsAccessor;
import org.spongepowered.common.bridge.server.level.ServerLevelBridge;
import org.spongepowered.common.bridge.world.level.border.WorldBorderBridge;
import org.spongepowered.common.bridge.world.level.storage.PrimaryLevelDataBridge;
import org.spongepowered.common.data.holder.SpongeLocationBaseDataHolder;
import org.spongepowered.common.mixin.api.minecraft.world.level.LevelMixin_API;
import org.spongepowered.common.util.VecHelper;
Expand Down Expand Up @@ -206,9 +208,10 @@ public Path directory() {

@Override
public boolean save() throws IOException {
final SerializationBehavior behavior = ((PrimaryLevelDataBridge) this.serverLevelData).bridge$serializationBehavior().orElse(SerializationBehavior.AUTOMATIC);
((ServerLevelBridge) this).bridge$setManualSave(true);
this.shadow$save(null, false, false);
return true;
return !behavior.equals(SerializationBehavior.NONE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,10 @@ public abstract class ServerLevelMixin extends LevelMixin implements ServerLevel
@Overwrite
public void save(@Nullable final ProgressListener progress, final boolean flush, final boolean skipSave) {

final boolean isManualSave = this.impl$isManualSave;

this.impl$isManualSave = false;

final Cause currentCause = Sponge.server().causeStackManager().currentCause();

if (Sponge.eventManager().post(SpongeEventFactory.createSaveWorldEventPre(currentCause, ((ServerWorld) this)))) {
Expand Down Expand Up @@ -346,17 +350,12 @@ public void save(@Nullable final ProgressListener progress, final boolean flush,
progress.progressStage(new TranslatableComponent("menu.savingChunks"));
}

final boolean canAutomaticallySave = !this.impl$isManualSave && behavior == SerializationBehavior.AUTOMATIC;
final boolean canManuallySave = this.impl$isManualSave && behavior == SerializationBehavior.MANUAL;

if (canAutomaticallySave || canManuallySave) {
if (behavior == SerializationBehavior.AUTOMATIC || (isManualSave && behavior == SerializationBehavior.MANUAL)) {
chunkProvider.save(flush);
}

Sponge.eventManager().post(SpongeEventFactory.createSaveWorldEventPost(currentCause, ((ServerWorld) this)));
}

this.impl$isManualSave = false;
}

@Inject(method = "tick",
Expand Down

0 comments on commit 3a371bb

Please sign in to comment.