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

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

import com.hypixel.hytale.server.core.command.system.arguments.system.Argument;
import com.hypixel.hytale.server.core.entity.entities.Player;
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.server.core.Message;
import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.server.core.modules.entity.damage.DamageSystems;
import com.hypixel.hytale.server.core.modules.entity.damage.DamageCause;
import com.hypixel.hytale.server.core.modules.entity.damage.Damage;
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.server.core.permissions.HytalePermissions;
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.FlagArg;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.core.command.system.arguments.system.OptionalArg;
import com.hypixel.hytale.server.core.command.system.basecommands.AbstractPlayerCommand;

public class DamageCommand extends AbstractPlayerCommand
{
    @Nonnull
    private final OptionalArg<Double> amountArg;
    @Nonnull
    private final FlagArg silentArg;
    
    public DamageCommand() {
        super("damage", "server.commands.damage.desc");
        this.amountArg = this.withOptionalArg("amount", "server.commands.damage.arg.amount.desc", ArgTypes.DOUBLE);
        this.silentArg = this.withFlagArg("silent", "server.commands.damage.arg.silent.desc");
        this.addAliases("hurt");
        this.requirePermission(HytalePermissions.fromCommand("damage.self"));
        this.addUsageVariant(new DamageOtherCommand());
    }
    
    @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 double amount = this.amountArg.provided(context) ? this.amountArg.get(context) : 1.0;
        final boolean silent = ((Argument<Arg, Boolean>)this.silentArg).get(context);
        final Damage.CommandSource damageSource = new Damage.CommandSource(context.sender(), this.getName());
        final Damage damage = new Damage(damageSource, DamageCause.COMMAND, (float)amount);
        DamageSystems.executeDamage(ref, store, damage);
        if (silent) {
            return;
        }
        final String damageFmt = String.format("%.1f", amount);
        context.sendMessage(Message.translation("server.commands.damage.dealt.self").param("damage", damageFmt));
        if (world.getGameplayConfig().getCombatConfig().isPlayerIncomingDamageDisabled()) {
            context.sendMessage(Message.translation("server.commands.damage.disabled").color("#ffc800"));
        }
    }
    
    private static class DamageOtherCommand extends CommandBase
    {
        private static final Message MESSAGE_COMMANDS_ERRORS_PLAYER_NOT_IN_WORLD;
        @Nonnull
        private final RequiredArg<PlayerRef> playerArg;
        @Nonnull
        private final OptionalArg<Double> amountArg;
        @Nonnull
        private final FlagArg silentArg;
        static final /* synthetic */ boolean $assertionsDisabled;
        
        DamageOtherCommand() {
            super("server.commands.damage.other.desc");
            this.playerArg = this.withRequiredArg("player", "server.commands.argtype.player.desc", ArgTypes.PLAYER_REF);
            this.amountArg = this.withOptionalArg("amount", "server.commands.damage.arg.amount.desc", ArgTypes.DOUBLE);
            this.silentArg = this.withFlagArg("silent", "server.commands.damage.arg.silent.desc");
            this.requirePermission(HytalePermissions.fromCommand("damage.other"));
        }
        
        @Override
        protected void executeSync(@Nonnull final CommandContext context) {
            final PlayerRef targetPlayerRef = this.playerArg.get(context);
            final Ref<EntityStore> ref = targetPlayerRef.getReference();
            if (ref == null || !ref.isValid()) {
                context.sendMessage(DamageOtherCommand.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(DamageOtherCommand.MESSAGE_COMMANDS_ERRORS_PLAYER_NOT_IN_WORLD);
                }
                else {
                    final PlayerRef playerRefComponent = store.getComponent(ref, PlayerRef.getComponentType());
                    if (!DamageOtherCommand.$assertionsDisabled && playerRefComponent == null) {
                        throw new AssertionError();
                    }
                    else {
                        final double amount = this.amountArg.provided(context) ? this.amountArg.get(context) : 1.0;
                        final boolean silent = ((Argument<Arg, Boolean>)this.silentArg).get(context);
                        final Damage.CommandSource damageSource = new Damage.CommandSource(context.sender(), "damage");
                        final Damage damage = new Damage(damageSource, DamageCause.COMMAND, (float)amount);
                        DamageSystems.executeDamage(ref, store, damage);
                        if (!silent) {
                            final String damageFmt = String.format("%.1f", amount);
                            context.sendMessage(Message.translation("server.commands.damage.dealt").param("damage", damageFmt).param("victim", playerRefComponent.getUsername()));
                            if (world.getGameplayConfig().getCombatConfig().isPlayerIncomingDamageDisabled()) {
                                context.sendMessage(Message.translation("server.commands.damage.disabled").color("#ffc800"));
                            }
                        }
                    }
                }
            });
        }
        
        static {
            MESSAGE_COMMANDS_ERRORS_PLAYER_NOT_IN_WORLD = Message.translation("server.commands.errors.playerNotInWorld");
        }
    }
}
