// 
// Decompiled by Procyon v0.6.0
// 

package com.hypixel.hytale.server.core.universe.system;

import com.hypixel.hytale.server.core.universe.world.WorldConfig;
import com.hypixel.hytale.server.core.universe.Universe;
import java.util.concurrent.CompletableFuture;
import com.hypixel.hytale.server.core.universe.world.World;
import javax.annotation.Nonnull;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.system.DelayedSystem;

public class WorldConfigSaveSystem extends DelayedSystem<EntityStore>
{
    public WorldConfigSaveSystem() {
        super(10.0f);
    }
    
    @Override
    public void delayedTick(final float dt, final int systemIndex, @Nonnull final Store<EntityStore> store) {
        final World world = store.getExternalData().getWorld();
        saveWorldConfigAndResources(world).join();
    }
    
    @Nonnull
    public static CompletableFuture<Void> saveWorldConfigAndResources(@Nonnull final World world) {
        final WorldConfig worldConfig = world.getWorldConfig();
        if (worldConfig.isSavingConfig() && worldConfig.consumeHasChanged()) {
            return CompletableFuture.allOf(world.getChunkStore().getStore().saveAllResources(), world.getEntityStore().getStore().saveAllResources(), Universe.get().getWorldConfigProvider().save(world.getSavePath(), world.getWorldConfig(), world));
        }
        return CompletableFuture.allOf(world.getChunkStore().getStore().saveAllResources(), world.getEntityStore().getStore().saveAllResources());
    }
}
