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

package com.hypixel.hytale.server.npc.corecomponents.combat;

import com.hypixel.hytale.component.ComponentAccessor;
import com.hypixel.hytale.server.core.entity.effect.EffectControllerComponent;
import com.hypixel.hytale.server.core.asset.type.entityeffect.config.EntityEffect;
import com.hypixel.hytale.component.Store;
import javax.annotation.Nullable;
import com.hypixel.hytale.server.npc.sensorinfo.InfoProvider;
import com.hypixel.hytale.server.npc.role.Role;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.server.npc.corecomponents.builders.BuilderActionBase;
import com.hypixel.hytale.server.npc.asset.builder.BuilderSupport;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.npc.corecomponents.combat.builders.BuilderActionApplyEntityEffect;
import com.hypixel.hytale.server.npc.corecomponents.ActionBase;

public class ActionApplyEntityEffect extends ActionBase
{
    protected final int entityEffectId;
    protected final boolean useTarget;
    
    public ActionApplyEntityEffect(@Nonnull final BuilderActionApplyEntityEffect builder, @Nonnull final BuilderSupport support) {
        super(builder);
        this.entityEffectId = builder.getEntityEffect(support);
        this.useTarget = builder.isUseTarget(support);
    }
    
    @Override
    public boolean canExecute(@Nonnull final Ref<EntityStore> ref, @Nonnull final Role role, @Nullable final InfoProvider sensorInfo, final double dt, @Nonnull final Store<EntityStore> store) {
        return super.canExecute(ref, role, sensorInfo, dt, store) && (!this.useTarget || (sensorInfo != null && sensorInfo.hasPosition()));
    }
    
    @Override
    public boolean execute(@Nonnull final Ref<EntityStore> ref, @Nonnull final Role role, @Nonnull final InfoProvider sensorInfo, final double dt, @Nonnull final Store<EntityStore> store) {
        super.execute(ref, role, sensorInfo, dt, store);
        final Ref<EntityStore> targetRef = this.useTarget ? sensorInfo.getPositionProvider().getTarget() : ref;
        if (targetRef == null || !targetRef.isValid()) {
            return true;
        }
        final EntityEffect entityEffect = EntityEffect.getAssetMap().getAsset(this.entityEffectId);
        if (entityEffect != null) {
            final EffectControllerComponent effectControllerComponent = store.getComponent(targetRef, EffectControllerComponent.getComponentType());
            if (effectControllerComponent != null) {
                effectControllerComponent.addEffect(targetRef, this.entityEffectId, entityEffect, store);
            }
        }
        return true;
    }
}
