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

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

import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.server.npc.sensorinfo.InfoProvider;
import com.hypixel.hytale.server.npc.role.Role;
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.entity.builders.BuilderActionSetStat;
import com.hypixel.hytale.server.core.modules.entitystats.EntityStatMap;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import com.hypixel.hytale.component.ComponentType;
import com.hypixel.hytale.server.npc.corecomponents.ActionBase;

public class ActionSetStat extends ActionBase
{
    protected static final ComponentType<EntityStore, EntityStatMap> STAT_MAP_COMPONENT_TYPE;
    protected final int stat;
    protected final float value;
    protected final boolean add;
    
    public ActionSetStat(@Nonnull final BuilderActionSetStat builder, @Nonnull final BuilderSupport support) {
        super(builder);
        this.stat = builder.getStat(support);
        this.value = builder.getValue(support);
        this.add = builder.isAdd(support);
    }
    
    @Override
    public boolean canExecute(@Nonnull final Ref<EntityStore> ref, @Nonnull final Role role, final InfoProvider sensorInfo, final double dt, @Nonnull final Store<EntityStore> store) {
        return super.canExecute(ref, role, sensorInfo, dt, store) && store.getComponent(ref, ActionSetStat.STAT_MAP_COMPONENT_TYPE).get(this.stat) != null;
    }
    
    @Override
    public boolean execute(@Nonnull final Ref<EntityStore> ref, @Nonnull final Role role, final InfoProvider sensorInfo, final double dt, @Nonnull final Store<EntityStore> store) {
        super.execute(ref, role, sensorInfo, dt, store);
        final EntityStatMap entityStatMapComponent = store.getComponent(ref, ActionSetStat.STAT_MAP_COMPONENT_TYPE);
        assert entityStatMapComponent != null;
        if (this.add) {
            entityStatMapComponent.addStatValue(this.stat, this.value);
        }
        else {
            entityStatMapComponent.setStatValue(this.stat, this.value);
        }
        return true;
    }
    
    static {
        STAT_MAP_COMPONENT_TYPE = EntityStatMap.getComponentType();
    }
}
