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

package com.hypixel.hytale.server.spawning.commands;

import com.hypixel.hytale.component.CommandBuffer;
import com.hypixel.hytale.component.ArchetypeChunk;
import com.hypixel.hytale.server.core.universe.world.WorldConfig;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.component.query.Query;
import com.hypixel.hytale.component.RemoveReason;
import com.hypixel.hytale.server.npc.entities.NPCEntity;
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.ArgumentType;
import com.hypixel.hytale.server.core.command.system.arguments.types.ArgTypes;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.asset.type.environment.config.Environment;
import com.hypixel.hytale.server.core.command.system.arguments.system.OptionalArg;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractWorldCommand;

public class SpawnPopulateCommand extends AbstractWorldCommand
{
    @Nonnull
    private final OptionalArg<Environment> environmentArg;
    static final /* synthetic */ boolean $assertionsDisabled;
    
    public SpawnPopulateCommand() {
        super("populate", "server.commands.spawning.populate.desc");
        this.environmentArg = this.withOptionalArg("environment", "server.commands.spawning.populate.arg.environment.desc", ArgTypes.ENVIRONMENT_ASSET);
    }
    
    @Override
    protected void execute(@Nonnull final CommandContext context, @Nonnull final World world, @Nonnull final Store<EntityStore> store) {
        final int environmentIndex = this.environmentArg.provided(context) ? Environment.getAssetMap().getIndex(this.environmentArg.get(context).getId()) : Integer.MIN_VALUE;
        store.forEachEntityParallel(NPCEntity.getComponentType(), (index, archetypeChunk, commandBuffer) -> {
            final NPCEntity npcComponent = archetypeChunk.getComponent(index, NPCEntity.getComponentType());
            if (!SpawnPopulateCommand.$assertionsDisabled && npcComponent == null) {
                throw new AssertionError();
            }
            else {
                final int npcEnvironment = npcComponent.getEnvironment();
                if (npcEnvironment >= 0 && (environmentIndex == Integer.MIN_VALUE || environmentIndex == npcEnvironment)) {
                    commandBuffer.removeEntity(archetypeChunk.getReferenceTo(index), RemoveReason.REMOVE);
                }
                return;
            }
        });
        final WorldConfig worldConfig = world.getWorldConfig();
        worldConfig.setSpawningNPC(true);
        worldConfig.markChanged();
        context.sendMessage(Message.translation("server.commands.spawning.populate.success").param("worldName", world.getName()));
    }
}
