Remove Tool <-> Block circular dependency in efficiency calculation

This commit is contained in:
Dylan K. Taylor 2019-03-01 18:18:56 +00:00
parent 72233a509d
commit d961b272c7
4 changed files with 6 additions and 7 deletions

View File

@ -325,7 +325,7 @@ class Block extends Position implements BlockIds, Metadatable{
$base *= 5;
}
$efficiency = $item->getMiningEfficiency($this);
$efficiency = $item->getMiningEfficiency(($this->getToolType() & $item->getBlockToolType()) !== 0);
if($efficiency <= 0){
throw new \InvalidArgumentException(get_class($item) . " has invalid mining efficiency: expected >= 0, got $efficiency");
}

View File

@ -726,7 +726,7 @@ class Item implements ItemIds, \JsonSerializable{
return 0;
}
public function getMiningEfficiency(Block $block) : float{
public function getMiningEfficiency(bool $isCorrectTool) : float{
return 1;
}

View File

@ -41,8 +41,8 @@ class Sword extends TieredTool{
return 1;
}
public function getMiningEfficiency(Block $block) : float{
return parent::getMiningEfficiency($block) * 1.5; //swords break any block 1.5x faster than hand
public function getMiningEfficiency(bool $isCorrectTool) : float{
return parent::getMiningEfficiency($isCorrectTool) * 1.5; //swords break any block 1.5x faster than hand
}
protected function getBaseMiningEfficiency() : float{

View File

@ -23,7 +23,6 @@ declare(strict_types=1);
namespace pocketmine\item;
use pocketmine\block\Block;
use pocketmine\item\enchantment\Enchantment;
abstract class Tool extends Durable{
@ -32,9 +31,9 @@ abstract class Tool extends Durable{
return 1;
}
public function getMiningEfficiency(Block $block) : float{
public function getMiningEfficiency(bool $isCorrectTool) : float{
$efficiency = 1;
if(($block->getToolType() & $this->getBlockToolType()) !== 0){
if($isCorrectTool){
$efficiency = $this->getBaseMiningEfficiency();
if(($enchantmentLevel = $this->getEnchantmentLevel(Enchantment::EFFICIENCY())) > 0){
$efficiency += ($enchantmentLevel ** 2 + 1);