pocketmine\block\Block::getBreakTime PHP Method

getBreakTime() public method

Returns the seconds that this block takes to be broken using an specific Item
public getBreakTime ( Item $item ) : float
$item pocketmine\item\Item
return float
    public function getBreakTime(Item $item)
    {
        $base = $this->getHardness() * 1.5;
        if ($this->canBeBrokenWith($item)) {
            if ($this->getToolType() === Tool::TYPE_SHEARS and $item->isShears()) {
                $base /= 15;
            } elseif ($this->getToolType() === Tool::TYPE_PICKAXE and ($tier = $item->isPickaxe()) !== false or $this->getToolType() === Tool::TYPE_AXE and ($tier = $item->isAxe()) !== false or $this->getToolType() === Tool::TYPE_SHOVEL and ($tier = $item->isShovel()) !== false) {
                switch ($tier) {
                    case Tool::TIER_WOODEN:
                        $base /= 2;
                        break;
                    case Tool::TIER_STONE:
                        $base /= 4;
                        break;
                    case Tool::TIER_IRON:
                        $base /= 6;
                        break;
                    case Tool::TIER_DIAMOND:
                        $base /= 8;
                        break;
                    case Tool::TIER_GOLD:
                        $base /= 12;
                        break;
                }
            }
        } else {
            $base *= 3.33;
        }
        if ($item->isSword()) {
            $base *= 0.5;
        }
        return $base;
    }

Usage Example

Example #1
0
 /**
  * TODO: Move this to each item
  *
  * @param Entity|Block $object
  *
  * @return bool
  */
 public function useOn($object)
 {
     if ($this->isUnbreakable()) {
         return false;
     }
     $break = true;
     if (($ench = $this->getEnchantment(Enchantment::TYPE_MINING_DURABILITY)) != null) {
         $rnd = mt_rand(1, 100);
         if ($rnd <= 100 / ($ench->getLevel() + 1)) {
             $break = false;
         }
     }
     if ($object instanceof Block) {
         if (!$break) {
             return false;
         }
         if ($object->getToolType() === Tool::TYPE_PICKAXE and $this->isPickaxe() or $object->getToolType() === Tool::TYPE_SHOVEL and $this->isShovel() or $object->getToolType() === Tool::TYPE_AXE and $this->isAxe() or $object->getToolType() === Tool::TYPE_SWORD and $this->isSword() or $object->getToolType() === Tool::TYPE_SHEARS and $this->isShears()) {
             $this->meta++;
         } elseif (!$this->isShears() and $object->getBreakTime($this) > 0) {
             $this->meta += 2;
         }
     } elseif ($this->isHoe()) {
         if (!$break) {
             return false;
         }
         if ($object instanceof Block and ($object->getId() === self::GRASS or $object->getId() === self::DIRT)) {
             $this->meta++;
         }
     } elseif ($object instanceof Entity) {
         $return = true;
         if (!$this->isSword()) {
             if ($break) {
                 $this->meta += 2;
                 $return = false;
             }
         } else {
             if ($break) {
                 $this->meta++;
                 $return = false;
             }
             if (!$this->hasEnchantments()) {
                 return $return;
             }
             //TODO: move attacking from player class here
             //$fire = $this->getEnchantment(Enchantment::TYPE_WEAPON_FIRE_ASPECT);
             //$object->setOnFire($fire->getLevel() * 4);
         }
     }
     return true;
 }
All Usage Examples Of pocketmine\block\Block::getBreakTime