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

package com.hypixel.hytale.server.core.command.commands.world.chunk;

import com.hypixel.hytale.server.core.universe.world.chunk.WorldChunk;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.server.core.universe.world.storage.ChunkStore;
import com.hypixel.hytale.math.vector.Vector2i;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.math.util.ChunkUtil;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.server.core.command.system.CommandContext;
import com.hypixel.hytale.server.core.command.system.arguments.types.ArgTypes;
import com.hypixel.hytale.server.core.command.system.arguments.system.FlagArg;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.command.system.arguments.types.RelativeChunkPosition;
import com.hypixel.hytale.server.core.command.system.arguments.system.RequiredArg;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractWorldCommand;

public class ChunkLoadCommand extends AbstractWorldCommand
{
    @Nonnull
    private final RequiredArg<RelativeChunkPosition> chunkPosArg;
    @Nonnull
    private final FlagArg markDirtyArg;
    
    public ChunkLoadCommand() {
        super("load", "server.commands.chunk.load.desc");
        this.chunkPosArg = this.withRequiredArg("x z", "server.commands.chunk.load.position.desc", ArgTypes.RELATIVE_CHUNK_POSITION);
        this.markDirtyArg = this.withFlagArg("markdirty", "server.commands.chunk.load.markdirty.desc");
    }
    
    @Override
    protected void execute(@Nonnull final CommandContext context, @Nonnull final World world, @Nonnull final Store<EntityStore> store) {
        final RelativeChunkPosition chunkPosition = this.chunkPosArg.get(context);
        final Vector2i position = chunkPosition.getChunkPosition(context, store);
        final ChunkStore chunkStore = world.getChunkStore();
        final long chunkIndex = ChunkUtil.indexChunk(position.x, position.y);
        final Ref<ChunkStore> chunkRef = chunkStore.getChunkReference(chunkIndex);
        if (chunkRef != null && chunkRef.isValid()) {
            context.sendMessage(Message.translation("server.commands.chunk.load.alreadyLoaded").param("chunkX", position.x).param("chunkZ", position.y).param("worldName", world.getName()));
            return;
        }
        context.sendMessage(Message.translation("server.commands.chunk.load.loading").param("chunkX", position.x).param("chunkZ", position.y).param("worldName", world.getName()));
        world.getChunkAsync(position.x, position.y).thenAccept(worldChunk -> world.execute(() -> {
            if (this.markDirtyArg.provided(context)) {
                worldChunk.markNeedsSaving();
            }
            context.sendMessage(Message.translation("server.commands.chunk.load.loaded").param("chunkX", position.x).param("chunkZ", position.y).param("worldName", world.getName()));
        }));
    }
}
