Search in sources :

Example 1 with SerializationBehavior

use of org.spongepowered.api.world.SerializationBehavior in project SpongeCommon by SpongePowered.

the class ServerChunkCacheMixin method impl$useSerializationBehaviorWhenSaving.

// @formatter:on
@Redirect(method = "save", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/level/ChunkMap;saveAllChunks(Z)V"))
private void impl$useSerializationBehaviorWhenSaving(final ChunkMap chunkManager, final boolean flush) {
    final ServerLevel world = this.level;
    final SerializationBehavior serializationBehavior = ((ServerWorldProperties) world.getLevelData()).serializationBehavior();
    if (serializationBehavior == SerializationBehavior.AUTOMATIC || serializationBehavior == SerializationBehavior.MANUAL) {
        ((ChunkMapAccessor) chunkManager).invoker$saveAllChunks(flush);
    }
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) ServerWorldProperties(org.spongepowered.api.world.server.storage.ServerWorldProperties) ChunkMapAccessor(org.spongepowered.common.accessor.server.level.ChunkMapAccessor) SerializationBehavior(org.spongepowered.api.world.SerializationBehavior) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 2 with SerializationBehavior

use of org.spongepowered.api.world.SerializationBehavior in project SpongeCommon by SpongePowered.

the class ServerLevelMixin method save.

/**
 * @author zidane - December 17th, 2020 - Minecraft 1.16.4
 * @reason Honor our serialization behavior in performing saves
 */
@Overwrite
public void save(@Nullable final ProgressListener progress, final boolean flush, final boolean skipSave) {
    final Cause currentCause = Sponge.server().causeStackManager().currentCause();
    if (Sponge.eventManager().post(SpongeEventFactory.createSaveWorldEventPre(currentCause, ((ServerWorld) this)))) {
        // cancelled save
        return;
    }
    final PrimaryLevelData levelData = (PrimaryLevelData) this.shadow$getLevelData();
    final ServerChunkCache chunkProvider = ((ServerLevel) (Object) this).getChunkSource();
    if (!skipSave) {
        final SerializationBehavior behavior = ((PrimaryLevelDataBridge) levelData).bridge$serializationBehavior().orElse(SerializationBehavior.AUTOMATIC);
        if (progress != null) {
            progress.progressStartNoAbort(new TranslatableComponent("menu.savingLevel"));
        }
        // We always save the metadata unless it is NONE
        if (behavior != SerializationBehavior.NONE) {
            this.shadow$saveLevelData();
            // Sponge Start - We do per-world WorldInfo/WorldBorders/BossBars
            levelData.setWorldBorder(this.getWorldBorder().createSettings());
            levelData.setCustomBossEvents(((ServerLevelBridge) this).bridge$getBossBarManager().save());
            ((ServerLevelBridge) this).bridge$getLevelSave().saveDataTag(SpongeCommon.server().registryAccess(), (PrimaryLevelData) this.shadow$getLevelData(), this.shadow$dimension() == Level.OVERWORLD ? SpongeCommon.server().getPlayerList().getSingleplayerData() : null);
        // Sponge End
        }
        if (progress != null) {
            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) {
            chunkProvider.save(flush);
        }
        Sponge.eventManager().post(SpongeEventFactory.createSaveWorldEventPost(currentCause, ((ServerWorld) this)));
    }
    this.impl$isManualSave = false;
}
Also used : PrimaryLevelData(net.minecraft.world.level.storage.PrimaryLevelData) ServerLevel(net.minecraft.server.level.ServerLevel) TranslatableComponent(net.minecraft.network.chat.TranslatableComponent) Cause(org.spongepowered.api.event.Cause) SerializationBehavior(org.spongepowered.api.world.SerializationBehavior) ServerChunkCache(net.minecraft.server.level.ServerChunkCache) ServerLevelBridge(org.spongepowered.common.bridge.server.level.ServerLevelBridge) PlatformServerLevelBridge(org.spongepowered.common.bridge.world.level.PlatformServerLevelBridge) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Example 3 with SerializationBehavior

use of org.spongepowered.api.world.SerializationBehavior in project SpongeCommon by SpongePowered.

the class ChunkMapMixin method impl$useSerializationBehaviorForChunkSave.

@Redirect(method = "save", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/chunk/storage/ChunkSerializer;write(Lnet/minecraft/server/level/ServerLevel;" + "Lnet/minecraft/world/level/chunk/ChunkAccess;)Lnet/minecraft/nbt/CompoundTag;"))
private CompoundTag impl$useSerializationBehaviorForChunkSave(final ServerLevel worldIn, final ChunkAccess chunkIn) {
    final PrimaryLevelDataBridge infoBridge = (PrimaryLevelDataBridge) this.level.getLevelData();
    final SerializationBehavior serializationBehavior = infoBridge.bridge$serializationBehavior().orElse(SerializationBehavior.AUTOMATIC);
    if (serializationBehavior == SerializationBehavior.AUTOMATIC || serializationBehavior == SerializationBehavior.MANUAL) {
        return ChunkSerializer.write(worldIn, chunkIn);
    }
    return null;
}
Also used : SerializationBehavior(org.spongepowered.api.world.SerializationBehavior) PrimaryLevelDataBridge(org.spongepowered.common.bridge.world.level.storage.PrimaryLevelDataBridge) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 4 with SerializationBehavior

use of org.spongepowered.api.world.SerializationBehavior in project SpongeCommon by SpongePowered.

the class ChunkMapMixin method impl$useSerializationBehaviorForPOI.

@Redirect(method = "save", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/ai/village/poi/PoiManager;flush(Lnet/minecraft/world/level/ChunkPos;)V"))
private void impl$useSerializationBehaviorForPOI(final PoiManager pointOfInterestManager, final ChunkPos p_219112_1_) {
    final PrimaryLevelDataBridge infoBridge = (PrimaryLevelDataBridge) this.level.getLevelData();
    final SerializationBehavior serializationBehavior = infoBridge.bridge$serializationBehavior().orElse(SerializationBehavior.AUTOMATIC);
    if (serializationBehavior == SerializationBehavior.AUTOMATIC || serializationBehavior == SerializationBehavior.MANUAL) {
        pointOfInterestManager.flush(p_219112_1_);
    }
}
Also used : SerializationBehavior(org.spongepowered.api.world.SerializationBehavior) PrimaryLevelDataBridge(org.spongepowered.common.bridge.world.level.storage.PrimaryLevelDataBridge) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 5 with SerializationBehavior

use of org.spongepowered.api.world.SerializationBehavior in project SpongeCommon by SpongePowered.

the class MinecraftServerMixin method saveAllChunks.

/**
 * @author Zidane - November, 24th 2020 - Minecraft 1.15
 * @reason To allow per-world auto-save tick intervals or disable auto-saving entirely
 */
@Overwrite
public boolean saveAllChunks(final boolean suppressLog, final boolean flush, final boolean isForced) {
    boolean var0 = false;
    for (final ServerLevel world : this.shadow$getAllLevels()) {
        final SerializationBehavior serializationBehavior = ((PrimaryLevelDataBridge) world.getLevelData()).bridge$serializationBehavior().orElse(SerializationBehavior.AUTOMATIC);
        final InheritableConfigHandle<WorldConfig> adapter = ((PrimaryLevelDataBridge) world.getLevelData()).bridge$configAdapter();
        // Sponge start - use our own config
        boolean log = adapter.get().world.logAutoSave;
        // by a command, save our configs
        if (!this.shadow$isRunning() || this.tickCount % 6000 == 0 || isForced) {
            ((PrimaryLevelDataBridge) world.getLevelData()).bridge$configAdapter().save();
        }
        final boolean canSaveAtAll = serializationBehavior != SerializationBehavior.NONE;
        // This world is set to not save of any time, no reason to check the auto-save/etc, skip it
        if (!canSaveAtAll) {
            continue;
        }
        // Only run auto-save skipping if the server is still running and the save is not forced
        if (this.bridge$performAutosaveChecks() && !isForced) {
            final int autoSaveInterval = adapter.get().world.autoSaveInterval;
            // Do not process properties or chunks if the world is not set to do so unless the server is shutting down
            if (autoSaveInterval <= 0 || serializationBehavior != SerializationBehavior.AUTOMATIC) {
                continue;
            }
            // Now check the interval vs the tick counter and skip it
            if (this.tickCount % autoSaveInterval != 0) {
                continue;
            }
        }
        if (log) {
            LOGGER.info("Saving chunks for level '{}'/{}", world, world.dimension().location());
        }
        world.save((ProgressListener) null, flush, world.noSave && !isForced);
        var0 = true;
    }
    // Save the usercache.json file every 10 minutes or if forced to
    if (isForced || this.tickCount % 6000 == 0) {
        // We want to save the username cache json, as we normally bypass it.
        ((GameProfileCacheBridge) this.profileCache).bridge$setCanSave(true);
        this.profileCache.save();
        ((GameProfileCacheBridge) this.profileCache).bridge$setCanSave(false);
    }
    return var0;
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) GameProfileCacheBridge(org.spongepowered.common.bridge.server.players.GameProfileCacheBridge) WorldConfig(org.spongepowered.common.config.inheritable.WorldConfig) SerializationBehavior(org.spongepowered.api.world.SerializationBehavior) PrimaryLevelDataBridge(org.spongepowered.common.bridge.world.level.storage.PrimaryLevelDataBridge) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Aggregations

SerializationBehavior (org.spongepowered.api.world.SerializationBehavior)6 ServerLevel (net.minecraft.server.level.ServerLevel)3 Redirect (org.spongepowered.asm.mixin.injection.Redirect)3 PrimaryLevelDataBridge (org.spongepowered.common.bridge.world.level.storage.PrimaryLevelDataBridge)3 Overwrite (org.spongepowered.asm.mixin.Overwrite)2 Inject (com.google.inject.Inject)1 TypeToken (io.leangen.geantyref.TypeToken)1 List (java.util.List)1 Map (java.util.Map)1 NoSuchElementException (java.util.NoSuchElementException)1 Random (java.util.Random)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 Identity (net.kyori.adventure.identity.Identity)1 Component (net.kyori.adventure.text.Component)1 NamedTextColor (net.kyori.adventure.text.format.NamedTextColor)1 Title (net.kyori.adventure.title.Title)1 TranslatableComponent (net.minecraft.network.chat.TranslatableComponent)1 ServerChunkCache (net.minecraft.server.level.ServerChunkCache)1 PrimaryLevelData (net.minecraft.world.level.storage.PrimaryLevelData)1