setCount() public method

public setCount ( integer $count )
$count integer
Example #1
4
 public function onActivate(Item $item, Player $player = null)
 {
     $tile = $this->getLevel()->getTile($this);
     if (!$tile instanceof ItemFrameTile) {
         $nbt = new CompoundTag("", [new StringTag("id", Tile::ITEM_FRAME), new IntTag("x", $this->x), new IntTag("y", $this->y), new IntTag("z", $this->z), new ByteTag("ItemRotation", 0), new FloatTag("ItemDropChance", 1.0)]);
         Tile::createTile(Tile::ITEM_FRAME, $this->getLevel()->getChunk($this->x >> 4, $this->z >> 4), $nbt);
     }
     $tile = $this->getLevel()->getTile($this);
     if (!$tile instanceof ItemFrameTile) {
         return false;
     }
     if ($tile->getItem()->getId() === 0) {
         $tile->setItem(Item::get($item->getId(), $item->getDamage(), 1));
         if ($player instanceof Player) {
             if ($player->isSurvival()) {
                 $count = $item->getCount();
                 if (--$count <= 0) {
                     $player->getInventory()->setItemInHand(Item::get(Item::AIR));
                     return true;
                 }
                 $item->setCount($count);
                 $player->getInventory()->setItemInHand($item);
             }
         }
     } else {
         $itemRot = $tile->getItemRotation();
         if ($itemRot === 7) {
             $itemRot = 0;
         } else {
             $itemRot++;
         }
         $tile->setItemRotation($itemRot);
     }
     return true;
 }
Example #2
0
 public function onActivate(Item $item, Player $player = null)
 {
     $tile = $this->getLevel()->getTile($this);
     if ($tile instanceof FlowerPotTile) {
         if ($tile->getFlowerPotItem() === Item::AIR) {
             switch ($item->getId()) {
                 case Item::TALL_GRASS:
                     if ($item->getDamage() === 1) {
                         break;
                     }
                 case Item::SAPLING:
                 case Item::DEAD_BUSH:
                 case Item::DANDELION:
                 case Item::RED_FLOWER:
                 case Item::BROWN_MUSHROOM:
                 case Item::RED_MUSHROOM:
                 case Item::CACTUS:
                     $tile->setFlowerPotData($item->getId(), $item->getDamage());
                     $this->setDamage($item->getId());
                     $this->getLevel()->setBlock($this, $this, true, false);
                     if ($player->isSurvival()) {
                         $item->setCount($item->getCount() - 1);
                         $player->getInventory()->setItemInHand($item->getCount() > 0 ? $item : Item::get(Item::AIR));
                     }
                     return true;
                     break;
             }
         }
     }
     return false;
 }
Example #3
0
 /**
  * @param Item $item
  *
  * @returns ShapelessRecipe
  *
  * @throws \InvalidArgumentException
  */
 public function addIngredient(Item $item)
 {
     if (count($this->ingredients) >= 9) {
         throw new \InvalidArgumentException("Shapeless recipes cannot have more than 9 ingredients");
     }
     $it = clone $item;
     $it->setCount(1);
     while ($item->getCount() > 0) {
         $this->ingredients[] = clone $it;
         $item->setCount($item->getCount() - 1);
     }
     return $this;
 }
Example #4
0
 protected function checkFuel(Item $fuel)
 {
     $this->server->getPluginManager()->callEvent($ev = new FurnaceBurnEvent($this, $fuel, $fuel->getFuelTime()));
     if ($ev->isCancelled()) {
         return;
     }
     $this->namedtag->MaxTime = new Short("MaxTime", $ev->getBurnTime());
     $this->namedtag->BurnTime = new Short("BurnTime", $ev->getBurnTime());
     $this->namedtag->BurnTicks = new Short("BurnTicks", 0);
     if ($this->getBlock()->getId() === Item::FURNACE) {
         $this->getLevel()->setBlock($this, Block::get(Item::BURNING_FURNACE, $this->getBlock()->getDamage()), true);
     }
     if ($this->namedtag["BurnTime"] > 0 and $ev->isBurning()) {
         $fuel->setCount($fuel->getCount() - 1);
         if ($fuel->getCount() === 0) {
             $fuel = Item::get(Item::AIR, 0, 0);
         }
         $this->inventory->setFuel($fuel);
     }
 }
Example #5
0
 public function onActivate(Item $item, Player $player = null)
 {
     $tile = $this->getLevel()->getTile($this);
     if (!$tile instanceof Cauldron) {
         return false;
     }
     switch ($item->getId()) {
         case Item::BUCKET:
             if ($item->getDamage() === 0) {
                 //empty bucket
                 if (!$this->isFull() or $tile->isCustomColor() or $tile->hasPotion()) {
                     break;
                 }
                 $bucket = clone $item;
                 $bucket->setDamage(8);
                 //water bucket
                 Server::getInstance()->getPluginManager()->callEvent($ev = new PlayerBucketFillEvent($player, $this, 0, $item, $bucket));
                 if (!$ev->isCancelled()) {
                     if ($player->isSurvival()) {
                         $player->getInventory()->setItemInHand($ev->getItem(), $player);
                     }
                     $this->meta = 0;
                     //empty
                     $this->getLevel()->setBlock($this, $this, true);
                     $tile->clearCustomColor();
                     $this->getLevel()->addSound(new SplashSound($this->add(0.5, 1, 0.5)));
                 }
             } elseif ($item->getDamage() === 8) {
                 //water bucket
                 if ($this->isFull() and !$tile->isCustomColor() and !$tile->hasPotion()) {
                     break;
                 }
                 $bucket = clone $item;
                 $bucket->setDamage(0);
                 //empty bucket
                 Server::getInstance()->getPluginManager()->callEvent($ev = new PlayerBucketEmptyEvent($player, $this, 0, $item, $bucket));
                 if (!$ev->isCancelled()) {
                     if ($player->isSurvival()) {
                         $player->getInventory()->setItemInHand($ev->getItem(), $player);
                     }
                     if ($tile->hasPotion()) {
                         //if has potion
                         $this->meta = 0;
                         //empty
                         $this->getLevel()->setBlock($this, $this, true);
                         $tile->setPotionId(0xffff);
                         //reset potion
                         $tile->clearCustomColor();
                         $this->getLevel()->addSound(new ExplodeSound($this->add(0.5, 0, 0.5)));
                     } else {
                         $this->meta = 6;
                         //fill
                         $this->getLevel()->setBlock($this, $this, true);
                         $tile->clearCustomColor();
                         $this->getLevel()->addSound(new SplashSound($this->add(0.5, 1, 0.5)));
                     }
                     $this->update();
                 }
             }
             break;
         case Item::DYE:
             if ($tile->hasPotion()) {
                 break;
             }
             $color = Color::getDyeColor($item->getDamage());
             if ($tile->isCustomColor()) {
                 $color = Color::averageColor($color, $tile->getCustomColor());
             }
             if ($player->isSurvival()) {
                 $item->setCount($item->getCount() - 1);
                 /*if($item->getCount() <= 0){
                 		$player->getInventory()->setItemInHand(Item::get(Item::AIR));
                 		}*/
             }
             $tile->setCustomColor($color);
             $this->getLevel()->addSound(new SplashSound($this->add(0.5, 1, 0.5)));
             $this->update();
             break;
         case Item::LEATHER_CAP:
         case Item::LEATHER_TUNIC:
         case Item::LEATHER_PANTS:
         case Item::LEATHER_BOOTS:
             if ($this->isEmpty()) {
                 break;
             }
             if ($tile->isCustomColor()) {
                 --$this->meta;
                 $this->getLevel()->setBlock($this, $this, true);
                 $newItem = clone $item;
                 Utils::setCustomColorToArmor($newItem, $tile->getCustomColor());
                 $player->getInventory()->setItemInHand($newItem);
                 $this->getLevel()->addSound(new SplashSound($this->add(0.5, 1, 0.5)));
                 if ($this->isEmpty()) {
                     $tile->clearCustomColor();
                 }
             } else {
                 --$this->meta;
                 $this->getLevel()->setBlock($this, $this, true);
                 $newItem = clone $item;
                 Utils::clearCustomColorToArmor($newItem);
                 $player->getInventory()->setItemInHand($newItem);
                 $this->getLevel()->addSound(new SplashSound($this->add(0.5, 1, 0.5)));
             }
             break;
         case Item::POTION:
         case Item::SPLASH_POTION:
             if (!$this->isEmpty() and ($tile->getPotionId() !== $item->getDamage() and $item->getDamage() !== Potion::WATER_BOTTLE or $item->getId() === Item::POTION and $tile->getSplashPotion() or $item->getId() === Item::SPLASH_POTION and !$tile->getSplashPotion() or $item->getDamage() === Potion::WATER_BOTTLE and $tile->hasPotion())) {
                 //long...
                 $this->meta = 0x0;
                 $this->getLevel()->setBlock($this, $this, true);
                 $tile->setPotionId(0xffff);
                 //reset
                 $tile->setSplashPotion(false);
                 if ($player->isSurvival()) {
                     $player->getInventory()->setItemInHand(Item::get(Item::GLASS_BOTTLE));
                 }
                 $this->getLevel()->addSound(new ExplodeSound($this->add(0.5, 0, 0.5)));
             } elseif ($item->getDamage() === Potion::WATER_BOTTLE) {
                 //water bottle
                 $this->meta += 2;
                 if ($this->meta > 0x6) {
                     $this->meta = 0x6;
                 }
                 $this->getLevel()->setBlock($this, $this, true);
                 if ($player->isSurvival()) {
                     $player->getInventory()->setItemInHand(Item::get(Item::GLASS_BOTTLE));
                 }
                 $tile->setPotionId(0xffff);
                 $tile->setSplashPotion(false);
                 $tile->clearCustomColor();
                 $this->getLevel()->addSound(new SplashSound($this->add(0.5, 1, 0.5)));
             } elseif (!$this->isFull()) {
                 $this->meta += 2;
                 if ($this->meta > 0x6) {
                     $this->meta = 0x6;
                 }
                 $this->getLevel()->setBlock($this, $this, true);
                 $tile->setPotionId($item->getDamage());
                 $tile->setSplashPotion($item->getId() === Item::SPLASH_POTION);
                 $tile->clearCustomColor();
                 if ($player->isSurvival()) {
                     $player->getInventory()->setItemInHand(Item::get(Item::GLASS_BOTTLE));
                 }
                 $color = Potion::getColor($item->getDamage());
                 $this->getLevel()->addSound(new SpellSound($this->add(0.5, 1, 0.5), $color[0], $color[1], $color[2]));
             }
             break;
         case Item::GLASS_BOTTLE:
             if ($this->meta < 2) {
                 break;
             }
             if ($tile->hasPotion()) {
                 $this->meta -= 2;
                 if ($this->meta < 0x0) {
                     $this->meta = 0x0;
                 }
                 $this->getLevel()->setBlock($this, $this, true);
                 if ($player->isSurvival()) {
                     $result = Item::get(Item::POTION, $tile->getPotionId());
                     if ($item->getCount() === 1) {
                         $player->getInventory()->setItemInHand($result);
                     } else {
                         $player->getInventory()->addItem($result);
                     }
                 }
                 if ($this->isEmpty()) {
                     $tile->setPotionId(0xffff);
                     //reset
                     $tile->setSplashPotion(false);
                 }
                 $color = Potion::getColor($result->getDamage());
                 $this->getLevel()->addSound(new SpellSound($this->add(0.5, 1, 0.5), $color[0], $color[1], $color[2]));
             } else {
                 $this->meta -= 2;
                 if ($this->meta < 0x0) {
                     $this->meta = 0x0;
                 }
                 $this->getLevel()->setBlock($this, $this, true);
                 if ($player->isSurvival()) {
                     $result = Item::get(Item::POTION, Potion::WATER_BOTTLE);
                     if ($item->getCount() > 1 and $player->getInventory()->canAddItem($result)) {
                         $player->getInventory()->addItem($result);
                     } else {
                         $player->getInventory()->setItemInHand($result);
                     }
                 }
                 $this->getLevel()->addSound(new GraySplashSound($this->add(0.5, 1, 0.5)));
             }
             break;
     }
     return true;
 }
Example #6
0
 /**
  * @param string $key
  * @param Item   $item
  *
  * @return $this
  * @throws \Exception
  */
 public function setIngredient($key, Item $item)
 {
     if (!array_key_exists($key, $this->shape)) {
         throw new \Exception("Symbol does not appear in the shape: " . $key);
     }
     $item->setCount(1);
     $this->fixRecipe($key, $item);
     return $this;
 }
Example #7
0
 public function addItem(Item $item, Player $player, Item $result)
 {
     if ($item->getCount() <= 1) {
         $player->getInventory()->setItemInHand($result);
     } else {
         $item->setCount($item->getCount() - 1);
         if ($player->getInventory()->canAddItem($result) === true) {
             $player->getInventory()->addItem($result);
         } else {
             $motion = $player->getDirectionVector()->multiply(0.4);
             $position = clone $player->getPosition();
             $player->getLevel()->dropItem($position->add(0, 0.5, 0), $result, $motion, 40);
         }
     }
 }
Example #8
0
 /**
  * @param Item $item
  * @return Item|null
  */
 private function condenseRecipes(Item $item)
 {
     if (isset($this->condenseShapes[0][$item->getId()])) {
         // 2x2 Shapes
         $shape = 4;
     } elseif (isset($this->condenseShapes[1][$item->getId()])) {
         // 3x3 Shapes
         $shape = 9;
     } else {
         return null;
     }
     $index = (int) sqrt($shape) - 2;
     $newId = $this->condenseShapes[$index][$item->getId()];
     $damage = 0;
     if (is_array($newId)) {
         if (!isset($newId[1][$item->getDamage()])) {
             return null;
         }
         $damage = $newId[1][$item->getDamage()];
         $newId = $newId[0];
     }
     $count = floor($item->getCount() / $shape);
     if ($count < 1) {
         return null;
     }
     $condensed = new Item($newId, $damage, $count);
     if ($condensed->getId() === Item::AIR) {
         return null;
     }
     $item->setCount($item->getCount() - $count * $shape);
     return $condensed;
 }
 public function onActivate(Item $item, Player $player = null)
 {
     $tile = $this->level->getTile($this);
     if ($tile instanceof ItemFrame) {
         if ($tile->getItem()->getId() === Item::AIR) {
             $tile->setItem(Item::get($item->getId(), $item->getDamage(), 1));
             $item->setCount($item->getCount() - 1);
         } else {
             $rot = $tile->getItemRotation() + 1;
             $tile->setItemRotation($rot > 8 ? 0 : $rot);
         }
         return true;
     }
     return false;
 }