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

package com.hypixel.hytale.server.core.command.system.basecommands;

import java.util.concurrent.CompletionStage;
import com.hypixel.hytale.server.core.universe.world.World;
import java.util.concurrent.Executor;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import java.util.concurrent.CompletableFuture;
import com.hypixel.hytale.server.core.command.system.CommandContext;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.Message;

public abstract class AbstractAsyncPlayerCommand extends AbstractAsyncCommand
{
    @Nonnull
    private static final Message MESSAGE_COMMANDS_ERRORS_PLAYER_NOT_IN_WORLD;
    @Nonnull
    private static final Message MESSAGE_COMMANDS_ERRORS_PLAYER_OR_ARG;
    
    public AbstractAsyncPlayerCommand(@Nonnull final String name, @Nonnull final String description) {
        super(name, description);
    }
    
    public AbstractAsyncPlayerCommand(@Nonnull final String name, @Nonnull final String description, final boolean requiresConfirmation) {
        super(name, description, requiresConfirmation);
    }
    
    public AbstractAsyncPlayerCommand(@Nonnull final String description) {
        super(description);
    }
    
    @Nonnull
    @Override
    protected final CompletableFuture<Void> executeAsync(@Nonnull final CommandContext context) {
        final Ref<EntityStore> ref = context.senderAsPlayerRef();
        if (ref == null) {
            context.sendMessage(AbstractAsyncPlayerCommand.MESSAGE_COMMANDS_ERRORS_PLAYER_OR_ARG);
            return CompletableFuture.completedFuture((Void)null);
        }
        if (!ref.isValid()) {
            context.sendMessage(AbstractAsyncPlayerCommand.MESSAGE_COMMANDS_ERRORS_PLAYER_NOT_IN_WORLD);
            return CompletableFuture.completedFuture((Void)null);
        }
        final Store<EntityStore> store = ref.getStore();
        final World world = store.getExternalData().getWorld();
        return CompletableFuture.supplyAsync(() -> {
            final PlayerRef playerRefComponent = store.getComponent(ref, PlayerRef.getComponentType());
            if (playerRefComponent == null) {
                context.sendMessage(AbstractAsyncPlayerCommand.MESSAGE_COMMANDS_ERRORS_PLAYER_NOT_IN_WORLD);
                return null;
            }
            else {
                return this.executeAsync(context, store, ref, playerRefComponent, world);
            }
        }, world).thenCompose(future -> (future != null) ? future : CompletableFuture.completedFuture((Object)null));
    }
    
    @Nonnull
    protected abstract CompletableFuture<Void> executeAsync(@Nonnull final CommandContext p0, @Nonnull final Store<EntityStore> p1, @Nonnull final Ref<EntityStore> p2, @Nonnull final PlayerRef p3, @Nonnull final World p4);
    
    static {
        MESSAGE_COMMANDS_ERRORS_PLAYER_NOT_IN_WORLD = Message.translation("server.commands.errors.playerNotInWorld");
        MESSAGE_COMMANDS_ERRORS_PLAYER_OR_ARG = Message.translation("server.commands.errors.playerOrArg").param("option", "player");
    }
}
