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

package com.hypixel.hytale.server.core.modules.prefabspawner.commands;

import com.hypixel.hytale.server.core.universe.world.meta.BlockState;
import com.hypixel.hytale.server.core.universe.world.chunk.WorldChunk;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.math.vector.Vector3i;
import com.hypixel.hytale.server.core.modules.prefabspawner.PrefabSpawnerState;
import com.hypixel.hytale.math.util.ChunkUtil;
import com.hypixel.hytale.server.core.util.TargetUtil;
import com.hypixel.hytale.server.core.command.system.exceptions.GeneralCommandException;
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.types.RelativeIntPosition;
import com.hypixel.hytale.server.core.command.system.arguments.system.OptionalArg;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractWorldCommand;

public abstract class TargetPrefabSpawnerCommand extends AbstractWorldCommand
{
    @Nonnull
    private static final Message MESSAGE_GENERAL_BLOCK_TARGET_NOT_IN_RANGE;
    @Nonnull
    private static final Message MESSAGE_COMMANDS_ERRORS_PROVIDE_POSITION;
    @Nonnull
    protected final OptionalArg<RelativeIntPosition> positionArg;
    
    public TargetPrefabSpawnerCommand(@Nonnull final String name, @Nonnull final String description) {
        super(name, description);
        this.positionArg = this.withOptionalArg("position", "server.commands.prefabspawner.position.desc", ArgTypes.RELATIVE_BLOCK_POSITION);
    }
    
    @Override
    protected void execute(@Nonnull final CommandContext context, @Nonnull final World world, @Nonnull final Store<EntityStore> store) {
        Vector3i target;
        if (this.positionArg.provided(context)) {
            final RelativeIntPosition relativePosition = this.positionArg.get(context);
            target = relativePosition.getBlockPosition(context, store);
        }
        else {
            if (!context.isPlayer()) {
                throw new GeneralCommandException(TargetPrefabSpawnerCommand.MESSAGE_COMMANDS_ERRORS_PROVIDE_POSITION);
            }
            final Ref<EntityStore> playerRef = context.senderAsPlayerRef();
            final Vector3i targetBlock = TargetUtil.getTargetBlock(playerRef, 10.0, store);
            if (targetBlock == null) {
                throw new GeneralCommandException(TargetPrefabSpawnerCommand.MESSAGE_GENERAL_BLOCK_TARGET_NOT_IN_RANGE);
            }
            target = targetBlock;
        }
        final WorldChunk chunk = world.getChunk(ChunkUtil.indexChunkFromBlock(target.x, target.z));
        final BlockState state = chunk.getState(target.x, target.y, target.z);
        if (!(state instanceof PrefabSpawnerState)) {
            context.sendMessage(Message.translation("server.commands.prefabspawner.spawnerNotFoundAtTarget").param("pos", target.toString()));
            return;
        }
        this.execute(context, chunk, (PrefabSpawnerState)state);
    }
    
    protected abstract void execute(@Nonnull final CommandContext p0, @Nonnull final WorldChunk p1, @Nonnull final PrefabSpawnerState p2);
    
    static {
        MESSAGE_GENERAL_BLOCK_TARGET_NOT_IN_RANGE = Message.translation("server.general.blockTargetNotInRange");
        MESSAGE_COMMANDS_ERRORS_PROVIDE_POSITION = Message.translation("server.commands.errors.providePosition");
    }
}
