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

package com.hypixel.hytale.builtin.buildertools.commands;

import com.hypixel.hytale.server.core.command.system.arguments.system.Argument;
import com.hypixel.hytale.math.vector.Vector3i;
import com.hypixel.hytale.server.core.command.system.arguments.types.ArgTypes;
import com.hypixel.hytale.server.core.command.system.arguments.types.RelativeIntPosition;
import com.hypixel.hytale.server.core.command.system.arguments.system.RequiredArg;
import com.hypixel.hytale.math.vector.Vector3d;
import com.hypixel.hytale.server.core.universe.world.storage.ChunkStore;
import com.hypixel.hytale.builtin.buildertools.BuilderToolsPlugin;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.math.util.MathUtil;
import com.hypixel.hytale.server.core.modules.entity.component.TransformComponent;
import com.hypixel.hytale.server.core.entity.entities.Player;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.server.core.command.system.CommandContext;
import com.hypixel.hytale.server.core.command.system.AbstractCommand;
import com.hypixel.hytale.protocol.GameMode;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.command.system.arguments.system.FlagArg;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractPlayerCommand;

public class PasteCommand extends AbstractPlayerCommand
{
    @Nonnull
    private final FlagArg technicalFlag;
    
    public PasteCommand() {
        super("paste", "server.commands.paste.desc");
        this.technicalFlag = this.withFlagArg("technical", "server.commands.paste.technical.desc");
        this.setPermissionGroup(GameMode.Creative);
        this.requirePermission("hytale.editor.selection.clipboard");
        this.addUsageVariant(new PasteAtPositionCommand());
    }
    
    @Override
    protected void execute(@Nonnull final CommandContext context, @Nonnull final Store<EntityStore> store, @Nonnull final Ref<EntityStore> ref, @Nonnull final PlayerRef playerRef, @Nonnull final World world) {
        final ChunkStore chunkStore = world.getChunkStore();
        final Player playerComponent = store.getComponent(ref, Player.getComponentType());
        assert playerComponent != null;
        final TransformComponent transformComponent = store.getComponent(ref, TransformComponent.getComponentType());
        assert transformComponent != null;
        final Vector3d position = transformComponent.getPosition();
        final int x = MathUtil.floor(position.x);
        final int y = MathUtil.floor(position.y);
        final int z = MathUtil.floor(position.z);
        final boolean technical = ((Argument<Arg, Boolean>)this.technicalFlag).get(context);
        BuilderToolsPlugin.addToQueue(playerComponent, playerRef, (r, s, componentAccessor) -> s.paste(r, x, y, z, technical, componentAccessor));
    }
    
    private static class PasteAtPositionCommand extends AbstractPlayerCommand
    {
        @Nonnull
        private final RequiredArg<RelativeIntPosition> positionArg;
        @Nonnull
        private final FlagArg technicalFlag;
        
        public PasteAtPositionCommand() {
            super("server.commands.paste.desc");
            this.positionArg = this.withRequiredArg("position", "server.commands.paste.position.desc", ArgTypes.RELATIVE_BLOCK_POSITION);
            this.technicalFlag = this.withFlagArg("technical", "server.commands.paste.technical.desc");
        }
        
        @Override
        protected void execute(@Nonnull final CommandContext context, @Nonnull final Store<EntityStore> store, @Nonnull final Ref<EntityStore> ref, @Nonnull final PlayerRef playerRef, @Nonnull final World world) {
            final ChunkStore chunkStore = world.getChunkStore();
            final Player playerComponent = store.getComponent(ref, Player.getComponentType());
            assert playerComponent != null;
            final TransformComponent transformComponent = store.getComponent(ref, TransformComponent.getComponentType());
            assert transformComponent != null;
            final Vector3d position = transformComponent.getPosition();
            final RelativeIntPosition relativePos = this.positionArg.get(context);
            final Vector3i blockPos = relativePos.getBlockPosition(position, chunkStore);
            final boolean technical = ((Argument<Arg, Boolean>)this.technicalFlag).get(context);
            BuilderToolsPlugin.addToQueue(playerComponent, playerRef, (r, s, componentAccessor) -> s.paste(r, blockPos.x, blockPos.y, blockPos.z, technical, componentAccessor));
        }
    }
}
