pocketmine\item\Item::hasEnchantments PHP Method

hasEnchantments() public method

public hasEnchantments ( ) : boolean
return boolean
    public function hasEnchantments() : bool
    {
        if (!$this->hasCompoundTag()) {
            return false;
        }
        $tag = $this->getNamedTag();
        if (isset($tag->ench)) {
            $tag = $tag->ench;
            if ($tag instanceof ListTag) {
                return true;
            }
        }
        return false;
    }

Usage Example

Example #1
0
 public function onEnchant(Player $who, Item $before, Item $after)
 {
     $result = $before->getId() == Item::BOOK ? new EnchantedBook() : $before;
     if (!$before->hasEnchantments() and $after->hasEnchantments() and $after->getId() == $result->getId() and $this->levels != null and $this->entries != null) {
         $enchantments = $after->getEnchantments();
         for ($i = 0; $i < 3; $i++) {
             if ($enchantments == $this->entries[$i]->getEnchantments()) {
                 $lapis = $this->getItem(1);
                 $level = $who->getExpLevel();
                 $exp = $who->getExperience();
                 $cost = $this->entries[$i]->getCost();
                 if ($lapis->getId() == Item::DYE and $lapis->getDamage() == Dye::BLUE and $lapis->getCount() > $i and $level >= $cost) {
                     foreach ($enchantments as $enchantment) {
                         $result->addEnchantment($enchantment);
                     }
                     $this->setItem(0, $result);
                     $lapis->setCount($lapis->getCount() - $i - 1);
                     $this->setItem(1, $lapis);
                     $who->setExperienceAndLevel($exp, $level - $cost);
                     break;
                 }
             }
         }
     }
 }