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

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

import com.hypixel.hytale.server.core.entity.entities.Player;
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.server.core.command.system.arguments.system.RequiredArg;
import com.hypixel.hytale.server.core.command.system.basecommands.CommandBase;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.server.core.modules.entity.damage.DeathComponent;
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 javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.Message;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractPlayerCommand;

public class PlayerRespawnCommand extends AbstractPlayerCommand
{
    @Nonnull
    private static final Message MESSAGE_COMMANDS_PLAYER_RESPAWN_SUCCESS_SELF;
    
    public PlayerRespawnCommand() {
        super("respawn", "server.commands.player.respawn.desc");
        this.addUsageVariant(new PlayerRespawnOtherCommand());
    }
    
    @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) {
        DeathComponent.respawn(store, ref);
        context.sendMessage(PlayerRespawnCommand.MESSAGE_COMMANDS_PLAYER_RESPAWN_SUCCESS_SELF);
    }
    
    static {
        MESSAGE_COMMANDS_PLAYER_RESPAWN_SUCCESS_SELF = Message.translation("server.commands.player.respawn.success.self");
    }
    
    private static class PlayerRespawnOtherCommand extends CommandBase
    {
        @Nonnull
        private static final Message MESSAGE_COMMANDS_ERRORS_PLAYER_NOT_IN_WORLD;
        @Nonnull
        private final RequiredArg<PlayerRef> playerArg;
        
        PlayerRespawnOtherCommand() {
            super("server.commands.player.respawn.other.desc");
            this.playerArg = this.withRequiredArg("player", "server.commands.argtype.player.desc", ArgTypes.PLAYER_REF);
        }
        
        @Override
        protected void executeSync(@Nonnull final CommandContext context) {
            final PlayerRef playerRef = this.playerArg.get(context);
            final Ref<EntityStore> ref = playerRef.getReference();
            if (ref == null || !ref.isValid()) {
                context.sendMessage(PlayerRespawnOtherCommand.MESSAGE_COMMANDS_ERRORS_PLAYER_NOT_IN_WORLD);
                return;
            }
            final Store<EntityStore> store = ref.getStore();
            final World world = store.getExternalData().getWorld();
            world.execute(() -> {
                final Player playerComponent = store.getComponent(ref, Player.getComponentType());
                if (playerComponent == null) {
                    context.sendMessage(PlayerRespawnOtherCommand.MESSAGE_COMMANDS_ERRORS_PLAYER_NOT_IN_WORLD);
                }
                else {
                    DeathComponent.respawn(store, ref);
                    context.sendMessage(Message.translation("server.commands.player.respawn.success.other").param("username", playerRef.getUsername()));
                }
            });
        }
        
        static {
            MESSAGE_COMMANDS_ERRORS_PLAYER_NOT_IN_WORLD = Message.translation("server.commands.errors.playerNotInWorld");
        }
    }
}
