package twilightforest.entity.ai;

import net.minecraft.entity.LivingEntity;
import net.minecraft.block.Blocks;
import net.minecraft.inventory.EquipmentSlotType;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.event.ForgeEventFactory;
import twilightforest.entity.RedcapEntity;

public class RedcapPlantTNTGoal extends RedcapBaseGoal {

	public RedcapPlantTNTGoal(RedcapEntity entityTFRedcap) {
		super(entityTFRedcap);
	}

	@Override
	public boolean shouldExecute() {
		LivingEntity attackTarget = this.redcap.getAttackTarget();
		return attackTarget != null
				&& !redcap.heldTNT.isEmpty()
				&& redcap.getDistanceSq(attackTarget) < 25
				&& !isTargetLookingAtMe(attackTarget)
				&& ForgeEventFactory.getMobGriefingEvent(redcap.world, redcap)
				&& !isLitTNTNearby(8)
				&& findBlockTNTNearby(5) == null;
	}

	@Override
	public void startExecuting() {
		BlockPos entityPos = new BlockPos(redcap.getPosition());

		this.redcap.setItemStackToSlot(EquipmentSlotType.MAINHAND, redcap.heldTNT);

		if (this.redcap.world.isAirBlock(entityPos)) {
			redcap.heldTNT.shrink(1);
			redcap.playAmbientSound();
			redcap.world.setBlockState(entityPos, Blocks.TNT.getDefaultState());
		}
	}

	@Override
	public void resetTask() {
		this.redcap.setItemStackToSlot(EquipmentSlotType.MAINHAND, redcap.heldPick);
	}
}
