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

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

import com.hypixel.hytale.server.core.universe.world.storage.ChunkStore;
import com.hypixel.hytale.math.vector.Vector3d;
import com.hypixel.hytale.server.core.universe.world.chunk.BlockChunk;
import com.hypixel.hytale.math.iterator.SpiralIterator;
import com.hypixel.hytale.math.util.MathUtil;
import com.hypixel.hytale.server.core.modules.entity.component.TransformComponent;
import com.hypixel.hytale.server.core.modules.entity.player.ChunkTracker;
import com.hypixel.hytale.server.core.entity.entities.Player;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import javax.annotation.Nullable;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.server.core.command.system.CommandContext;
import com.hypixel.hytale.server.core.command.system.arguments.system.FlagArg;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractTargetPlayerCommand;

public class ChunkResendCommand extends AbstractTargetPlayerCommand
{
    @Nonnull
    private static final Message MESSAGE_COMMANDS_CHUNK_RESEND_UNLOADED_ALL;
    @Nonnull
    private final FlagArg clearCacheArg;
    
    public ChunkResendCommand() {
        super("resend", "server.commands.chunk.resend.desc");
        this.clearCacheArg = this.withFlagArg("clearcache", "server.commands.chunk.resend.clearcache.desc");
    }
    
    @Override
    protected void execute(@Nonnull final CommandContext context, @Nullable final Ref<EntityStore> sourceRef, @Nonnull final Ref<EntityStore> ref, @Nonnull final PlayerRef playerRef, @Nonnull final World world, @Nonnull final Store<EntityStore> store) {
        final Player playerComponent = store.getComponent(ref, Player.getComponentType());
        assert playerComponent != null;
        final ChunkTracker chunkTrackerComponent = store.getComponent(ref, ChunkTracker.getComponentType());
        assert chunkTrackerComponent != null;
        final TransformComponent transformComponent = store.getComponent(ref, TransformComponent.getComponentType());
        assert transformComponent != null;
        final Vector3d position = transformComponent.getPosition();
        final int chunkX = MathUtil.floor(position.getX()) >> 5;
        final int chunkZ = MathUtil.floor(position.getZ()) >> 5;
        if (this.clearCacheArg.provided(context)) {
            final ChunkStore chunkStore = world.getChunkStore();
            final Store<ChunkStore> chunkStoreStore = chunkStore.getStore();
            final SpiralIterator iterator = new SpiralIterator(chunkX, chunkZ, playerComponent.getViewRadius());
            while (iterator.hasNext()) {
                final long chunkIndex = iterator.next();
                final Ref<ChunkStore> chunkRef = chunkStore.getChunkReference(chunkIndex);
                if (chunkRef != null && chunkRef.isValid()) {
                    final BlockChunk blockChunkComponent = chunkStoreStore.getComponent(chunkRef, BlockChunk.getComponentType());
                    if (blockChunkComponent == null) {
                        continue;
                    }
                    for (int y = 0; y < 10; ++y) {
                        blockChunkComponent.invalidateChunkSection(y);
                    }
                }
            }
        }
        chunkTrackerComponent.unloadAll(playerRef);
        context.sendMessage(ChunkResendCommand.MESSAGE_COMMANDS_CHUNK_RESEND_UNLOADED_ALL);
    }
    
    static {
        MESSAGE_COMMANDS_CHUNK_RESEND_UNLOADED_ALL = Message.translation("server.commands.chunk.resend.unloadedAll");
    }
}
