<?php

class TaskEatTileGoal extends TaskBase
{
	public function onStart(EntityAI $ai)
	{
		$this->selfCounter = 40;
		$ai->entity->server->api->player->broadcastPacket($ai->entity->level->players, new EntityEventPacket($ai->entity->eid, EntityEventPacket::ENTITY_ANIM_10));
	}

	public function onEnd(EntityAI $ai)
	{
		
	}

	public function onUpdate(EntityAI $ai)
	{
		if (--$this->selfCounter === 4)
		{
			$id = $ai->entity->level->level->getBlockID($ai->entity->x, $ai->entity->y, $ai->entity->z);
			$idb = $ai->entity->level->level->getBlockID($ai->entity->x, $ai->entity->y - 1, $ai->entity->z);
			if($id === TALL_GRASS){
				$ai->entity->level->fastSetBlockUpdate($ai->entity->x, $ai->entity->y, $ai->entity->z, AIR, 0);
				$ai->entity->eatGrass();
				
			}elseif($idb === GRASS){
				$ai->entity->level->fastSetBlockUpdate($ai->entity->x, $ai->entity->y - 1, $ai->entity->z, DIRT, 0);
				$ai->entity->eatGrass();
			}
		}
	}

	public function canBeExecuted(EntityAI $ai)
	{
		if($ai->isStarted("TaskRandomWalk")) return false;
		if(mt_rand(0, ($ai->entity instanceof Ageable && $ai->entity->isBaby()) ? 50 : 1000) == 0){
			$idm = $ai->entity->level->level->getBlock($ai->entity->x, $ai->entity->y, $ai->entity->z);
			$idb = $ai->entity->level->level->getBlockID($ai->entity->x, $ai->entity->y - 1, $ai->entity->z);
			return ($idm[0] === TALL_GRASS && $idm[1] === 1) || $idb === GRASS;
		}
		return false;
		//this.theEntity.getRNG().nextInt(this.theEntity.isChild() ? 50 : 1000) != 0
	}

}

