setIngredient() public method

public setIngredient ( Item $item )
$item pocketmine\item\Item
 public function onUpdate()
 {
     if ($this->closed === true) {
         return false;
     }
     $this->timings->startTiming();
     $ret = false;
     $ingredient = $this->inventory->getIngredient();
     $potions = $this->inventory->getPotions();
     $canBrew = false;
     foreach ($potions as $pot) {
         if ($pot->getId() === Item::POTION) {
             $canBrew = true;
         }
     }
     if ($this->namedtag["BrewTime"] <= self::MAX_BREW_TIME and $canBrew and $ingredient->getCount() > 0) {
         if (!$this->checkIngredient($ingredient)) {
             $canBrew = false;
         }
     } else {
         $canBrew = false;
     }
     if ($canBrew) {
         $this->namedtag->BrewTime = new ShortTag("BrewTime", $this->namedtag["BrewTime"] - 1);
         if ($this->namedtag["BrewTime"] <= 0) {
             //20 seconds
             foreach ($this->inventory->getPotions() as $slot => $potion) {
                 $recipe = Server::getInstance()->getCraftingManager()->matchBrewingRecipe($ingredient, $potion);
                 if ($recipe instanceof BrewingRecipe) {
                     $this->inventory->setPotion($slot, $recipe->getResult());
                 } elseif ($ingredient->getId() === Item::GUNPOWDER && $potion->getId() === Item::POTION) {
                     $this->inventory->setPotion($slot, new SplashPotion($potion->getDamage()));
                 }
             }
             $ingredient->count--;
             $this->inventory->setIngredient($ingredient);
             $this->namedtag->BrewTime = new ShortTag("BrewTime", $this->namedtag["BrewTime"] + 400);
         }
         foreach ($this->getInventory()->getViewers() as $player) {
             $windowId = $player->getWindowId($this->getInventory());
             if ($windowId > 0) {
                 $pk = new ContainerSetDataPacket();
                 $pk->windowid = $windowId;
                 $pk->property = 0;
                 //Brewing
                 $pk->value = floor($this->namedtag["BrewTime"]);
                 $player->dataPacket($pk);
             }
         }
         $ret = true;
     } else {
         $this->namedtag->BrewTime = new ShortTag("BrewTime", self::MAX_BREW_TIME);
         $ret = false;
     }
     $this->lastUpdate = microtime(true);
     $this->timings->stopTiming();
     return $ret;
 }
Esempio n. 2
0
 public function onUpdate()
 {
     if ($this->closed === true) {
         return false;
     }
     $this->timings->startTiming();
     $ret = false;
     $ingredient = $this->inventory->getIngredient();
     $canBrew = false;
     for ($i = 1; $i <= 3; $i++) {
         if ($this->inventory->getItem($i)->getId() == Item::POTION) {
             $canBrew = true;
             break;
         }
     }
     if ($this->namedtag["CookTime"] <= self::MAX_BREW_TIME and $canBrew and $ingredient->getCount() > 0) {
         //if(!$this->checkIngredient($ingredient)) $canBrew = false;
         //TODO: check ingredients
     } else {
         $canBrew = false;
     }
     if ($canBrew) {
         $this->namedtag->CookTime = new ShortTag("CookTime", $this->namedtag["CookTime"] - 1);
         if ($this->namedtag["CookTime"] <= 0) {
             for ($i = 1; $i <= 3; $i++) {
                 $potion = $this->inventory->getItem($i);
                 $recipe = Server::getInstance()->getCraftingManager()->matchBrewingRecipe($ingredient, $potion);
                 if ($recipe != null) {
                     $this->inventory->setItem($i, $recipe->getResult());
                 }
             }
             $ingredient->count--;
             if ($ingredient->getCount() <= 0) {
                 $ingredient = Item::get(Item::AIR);
             }
             $this->inventory->setIngredient($ingredient);
         }
         foreach ($this->getInventory()->getViewers() as $player) {
             $windowId = $player->getWindowId($this->getInventory());
             if ($windowId > 0) {
                 $pk = new ContainerSetDataPacket();
                 $pk->windowid = $windowId;
                 $pk->property = 0;
                 //Brew
                 $pk->value = $this->namedtag["CookTime"];
                 $player->dataPacket($pk);
             }
         }
         $ret = true;
     } else {
         $this->namedtag->CookTime = new ShortTag("CookTime", self::MAX_BREW_TIME);
     }
     $this->lastUpdate = microtime(true);
     $this->timings->stopTiming();
     return $ret;
 }