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

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

import com.hypixel.hytale.component.Holder;
import com.hypixel.hytale.math.vector.Vector3d;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.component.AddReason;
import com.hypixel.hytale.server.core.entity.UUIDComponent;
import com.hypixel.hytale.server.core.modules.entity.component.TransformComponent;
import com.hypixel.hytale.server.core.entity.entities.BlockEntity;
import com.hypixel.hytale.server.core.modules.time.TimeResource;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.server.core.command.system.arguments.system.Argument;
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 com.hypixel.hytale.math.vector.Vector3f;
import com.hypixel.hytale.server.core.command.system.arguments.system.DefaultArg;
import com.hypixel.hytale.server.core.command.system.arguments.types.RelativeDoublePosition;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.command.system.arguments.system.RequiredArg;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractWorldCommand;

public class SpawnBlockCommand extends AbstractWorldCommand
{
    @Nonnull
    private final RequiredArg<String> blockArg;
    @Nonnull
    private final RequiredArg<RelativeDoublePosition> positionArg;
    @Nonnull
    private final DefaultArg<Vector3f> rotationArg;
    
    public SpawnBlockCommand() {
        super("spawnblock", "server.commands.spawnblock.desc");
        this.blockArg = this.withRequiredArg("block", "server.commands.spawnblock.arg.block.desc", ArgTypes.BLOCK_TYPE_KEY);
        this.positionArg = this.withRequiredArg("position", "server.commands.spawnblock.arg.position.desc", ArgTypes.RELATIVE_POSITION);
        this.rotationArg = this.withDefaultArg("rotation", "server.commands.spawnblock.arg.rotation.desc", ArgTypes.ROTATION, Vector3f.FORWARD, "server.commands.spawnblock.arg.rotation.desc");
    }
    
    @Override
    protected void execute(@Nonnull final CommandContext context, @Nonnull final World world, @Nonnull final Store<EntityStore> store) {
        final String blockTypeKey = context.get((Argument<?, String>)this.blockArg);
        final Vector3d position = context.get((Argument<?, RelativeDoublePosition>)this.positionArg).getRelativePosition(context, world, store);
        final Vector3f rotation = this.rotationArg.get(context);
        final TimeResource timeResource = world.getEntityStore().getStore().getResource(TimeResource.getResourceType());
        final Holder<EntityStore> blockEntityHolder = BlockEntity.assembleDefaultBlockEntity(timeResource, blockTypeKey, position);
        final TransformComponent transformComponent = blockEntityHolder.ensureAndGetComponent(TransformComponent.getComponentType());
        transformComponent.setPosition(position);
        transformComponent.setRotation(rotation);
        final UUIDComponent uuidComponent = blockEntityHolder.getComponent(UUIDComponent.getComponentType());
        final String entityIdString = (uuidComponent == null) ? "None" : uuidComponent.getUuid().toString();
        world.getEntityStore().getStore().addEntity(blockEntityHolder, AddReason.SPAWN);
        context.sendMessage(Message.translation("server.commands.spawnblock.success").param("id", entityIdString));
    }
}
