diff --git a/src/pocketmine/Player.php b/src/pocketmine/Player.php index 3be196b634..4099cb7495 100644 --- a/src/pocketmine/Player.php +++ b/src/pocketmine/Player.php @@ -2527,14 +2527,12 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{ } } - if($this->isSurvival()){ - if($heldItem->useOn($target)){ - $this->inventory->setItemInHand($heldItem); - } - - $this->exhaust(0.3, PlayerExhaustEvent::CAUSE_ATTACK); + if($heldItem->onAttackEntity($target) and $this->isSurvival()){ //always fire the hook, even if we are survival + $this->inventory->setItemInHand($heldItem); } + $this->exhaust(0.3, PlayerExhaustEvent::CAUSE_ATTACK); + return true; default: break; //unknown diff --git a/src/pocketmine/block/Dirt.php b/src/pocketmine/block/Dirt.php index 3b438e8045..96f8da27c4 100644 --- a/src/pocketmine/block/Dirt.php +++ b/src/pocketmine/block/Dirt.php @@ -23,6 +23,7 @@ declare(strict_types=1); namespace pocketmine\block; +use pocketmine\item\Hoe; use pocketmine\item\Item; use pocketmine\Player; @@ -50,8 +51,8 @@ class Dirt extends Solid{ } public function onActivate(Item $item, Player $player = null) : bool{ - if($item->isHoe()){ - $item->useOn($this); + if($item instanceof Hoe){ + $item->applyDamage(1); if($this->meta === 1){ $this->getLevel()->setBlock($this, BlockFactory::get(Block::DIRT), true); }else{ diff --git a/src/pocketmine/block/Grass.php b/src/pocketmine/block/Grass.php index bbe40ec8fe..a2887e0fdf 100644 --- a/src/pocketmine/block/Grass.php +++ b/src/pocketmine/block/Grass.php @@ -24,8 +24,10 @@ declare(strict_types=1); namespace pocketmine\block; use pocketmine\event\block\BlockSpreadEvent; +use pocketmine\item\Hoe; use pocketmine\item\Item; use pocketmine\item\ItemFactory; +use pocketmine\item\Shovel; use pocketmine\level\generator\object\TallGrass as TallGrassObject; use pocketmine\math\Vector3; use pocketmine\Player; @@ -98,13 +100,13 @@ class Grass extends Solid{ TallGrassObject::growGrass($this->getLevel(), $this, new Random(mt_rand()), 8, 2); return true; - }elseif($item->isHoe()){ - $item->useOn($this); + }elseif($item instanceof Hoe){ + $item->applyDamage(1); $this->getLevel()->setBlock($this, BlockFactory::get(Block::FARMLAND)); return true; - }elseif($item->isShovel() and $this->getSide(Vector3::SIDE_UP)->getId() === Block::AIR){ - $item->useOn($this); + }elseif($item instanceof Shovel and $this->getSide(Vector3::SIDE_UP)->getId() === Block::AIR){ + $item->applyDamage(1); $this->getLevel()->setBlock($this, BlockFactory::get(Block::GRASS_PATH)); return true; diff --git a/src/pocketmine/block/TNT.php b/src/pocketmine/block/TNT.php index f3cdd0b3f5..7666fd91f9 100644 --- a/src/pocketmine/block/TNT.php +++ b/src/pocketmine/block/TNT.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace pocketmine\block; use pocketmine\entity\Entity; +use pocketmine\item\FlintSteel; use pocketmine\item\Item; use pocketmine\math\Vector3; use pocketmine\Player; @@ -46,8 +47,8 @@ class TNT extends Solid{ } public function onActivate(Item $item, Player $player = null) : bool{ - if($item->getId() === Item::FLINT_STEEL){ - $item->useOn($this); + if($item instanceof FlintSteel){ + $item->applyDamage(1); $this->ignite(); return true; } diff --git a/src/pocketmine/item/Axe.php b/src/pocketmine/item/Axe.php index 4eb0d559b7..0250554c59 100644 --- a/src/pocketmine/item/Axe.php +++ b/src/pocketmine/item/Axe.php @@ -23,7 +23,9 @@ declare(strict_types=1); namespace pocketmine\item; +use pocketmine\block\Block; use pocketmine\block\BlockToolType; +use pocketmine\entity\Entity; class Axe extends TieredTool{ @@ -42,4 +44,15 @@ class Axe extends TieredTool{ public function getAttackPoints() : int{ return self::getBaseDamageFromTier($this->tier) - 1; } + + public function onDestroyBlock(Block $block) : bool{ + if($block->getHardness() > 0){ + return $this->applyDamage(1); + } + return false; + } + + public function onAttackEntity(Entity $victim) : bool{ + return $this->applyDamage(2); + } } diff --git a/src/pocketmine/item/Hoe.php b/src/pocketmine/item/Hoe.php index af726a795b..a3da55b43c 100644 --- a/src/pocketmine/item/Hoe.php +++ b/src/pocketmine/item/Hoe.php @@ -23,10 +23,15 @@ declare(strict_types=1); namespace pocketmine\item; +use pocketmine\entity\Entity; + class Hoe extends TieredTool{ public function isHoe(){ return $this->tier; } + public function onAttackEntity(Entity $victim) : bool{ + return $this->applyDamage(1); + } } diff --git a/src/pocketmine/item/Item.php b/src/pocketmine/item/Item.php index f3500fd6fe..52c98ce04d 100644 --- a/src/pocketmine/item/Item.php +++ b/src/pocketmine/item/Item.php @@ -715,15 +715,6 @@ class Item implements ItemIds, \JsonSerializable{ return 0; } - /** - * @param Entity|Block $object - * - * @return bool - */ - public function useOn($object){ - return false; - } - /** * Returns what type of block-breaking tool this is. Blocks requiring the same tool type as the item will break * faster (except for blocks requiring no tool, which break at the same speed regardless of the tool used) @@ -814,6 +805,28 @@ class Item implements ItemIds, \JsonSerializable{ return false; } + /** + * Called when this item is used to destroy a block. Usually used to update durability. + * + * @param Block $block + * + * @return bool + */ + public function onDestroyBlock(Block $block) : bool{ + return false; + } + + /** + * Called when this item is used to attack an entity. Usually used to update durability. + * + * @param Entity $victim + * + * @return bool + */ + public function onAttackEntity(Entity $victim) : bool{ + return false; + } + /** * Returns the number of ticks a player must wait before activating this item again. * diff --git a/src/pocketmine/item/Pickaxe.php b/src/pocketmine/item/Pickaxe.php index de0ecd03e0..02b4e9ecaf 100644 --- a/src/pocketmine/item/Pickaxe.php +++ b/src/pocketmine/item/Pickaxe.php @@ -23,7 +23,9 @@ declare(strict_types=1); namespace pocketmine\item; +use pocketmine\block\Block; use pocketmine\block\BlockToolType; +use pocketmine\entity\Entity; class Pickaxe extends TieredTool{ @@ -42,4 +44,15 @@ class Pickaxe extends TieredTool{ public function getAttackPoints() : int{ return self::getBaseDamageFromTier($this->tier) - 2; } + + public function onDestroyBlock(Block $block) : bool{ + if($block->getHardness() > 0){ + return $this->applyDamage(1); + } + return false; + } + + public function onAttackEntity(Entity $victim) : bool{ + return $this->applyDamage(2); + } } diff --git a/src/pocketmine/item/Shears.php b/src/pocketmine/item/Shears.php index 3a32b3ab84..05045974dc 100644 --- a/src/pocketmine/item/Shears.php +++ b/src/pocketmine/item/Shears.php @@ -23,6 +23,7 @@ declare(strict_types=1); namespace pocketmine\item; +use pocketmine\block\Block; use pocketmine\block\BlockToolType; class Shears extends Tool{ @@ -49,4 +50,11 @@ class Shears extends Tool{ protected function getBaseMiningEfficiency() : float{ return 15; } + + public function onDestroyBlock(Block $block) : bool{ + if($block->getHardness() === 0 or $block->isCompatibleWithTool($this)){ + return $this->applyDamage(1); + } + return false; + } } diff --git a/src/pocketmine/item/Shovel.php b/src/pocketmine/item/Shovel.php index 2e60f3f6d1..076b4b8544 100644 --- a/src/pocketmine/item/Shovel.php +++ b/src/pocketmine/item/Shovel.php @@ -23,7 +23,9 @@ declare(strict_types=1); namespace pocketmine\item; +use pocketmine\block\Block; use pocketmine\block\BlockToolType; +use pocketmine\entity\Entity; class Shovel extends TieredTool{ @@ -42,4 +44,15 @@ class Shovel extends TieredTool{ public function getAttackPoints() : int{ return self::getBaseDamageFromTier($this->tier) - 3; } + + public function onDestroyBlock(Block $block) : bool{ + if($block->getHardness() > 0){ + return $this->applyDamage(1); + } + return false; + } + + public function onAttackEntity(Entity $victim) : bool{ + return $this->applyDamage(2); + } } diff --git a/src/pocketmine/item/Sword.php b/src/pocketmine/item/Sword.php index 013f56abe1..b1789d7543 100644 --- a/src/pocketmine/item/Sword.php +++ b/src/pocketmine/item/Sword.php @@ -25,6 +25,7 @@ namespace pocketmine\item; use pocketmine\block\Block; use pocketmine\block\BlockToolType; +use pocketmine\entity\Entity; class Sword extends TieredTool{ @@ -51,4 +52,15 @@ class Sword extends TieredTool{ protected function getBaseMiningEfficiency() : float{ return 10; } + + public function onDestroyBlock(Block $block) : bool{ + if($block->getHardness() > 0){ + return $this->applyDamage(2); + } + return false; + } + + public function onAttackEntity(Entity $victim) : bool{ + return $this->applyDamage(1); + } } diff --git a/src/pocketmine/item/Tool.php b/src/pocketmine/item/Tool.php index 5e5d151e02..b0f3b1be0c 100644 --- a/src/pocketmine/item/Tool.php +++ b/src/pocketmine/item/Tool.php @@ -24,7 +24,6 @@ declare(strict_types=1); namespace pocketmine\item; use pocketmine\block\Block; -use pocketmine\entity\Entity; use pocketmine\item\enchantment\Enchantment; abstract class Tool extends Durable{ @@ -33,37 +32,6 @@ abstract class Tool extends Durable{ return 1; } - /** - * TODO: Move this to each item - * - * @param Entity|Block $object - * - * @return bool - */ - public function useOn($object){ - if($this->isUnbreakable()){ - return true; - } - - if($object instanceof Block){ - if(($object->getToolType() & $this->getBlockToolType()) !== 0){ - $this->applyDamage(1); - }elseif(!$this->isShears() and $object->getBreakTime($this) > 0){ - $this->applyDamage(2); - } - }elseif($this->isHoe()){ - if(($object instanceof Block) and ($object->getId() === self::GRASS or $object->getId() === self::DIRT)){ - $this->applyDamage(1); - } - }elseif(($object instanceof Entity) and !$this->isSword()){ - $this->applyDamage(2); - }else{ - $this->applyDamage(1); - } - - return true; - } - public function getMiningEfficiency(Block $block) : float{ $efficiency = 1; if(($block->getToolType() & $this->getBlockToolType()) !== 0){ diff --git a/src/pocketmine/level/Level.php b/src/pocketmine/level/Level.php index a0a45f9542..7dceba83f8 100644 --- a/src/pocketmine/level/Level.php +++ b/src/pocketmine/level/Level.php @@ -1718,7 +1718,7 @@ class Level implements ChunkManager, Metadatable{ $this->destroyBlockInternal($t, $item, $player, $createParticles); } - $item->useOn($target); + $item->onDestroyBlock($target); if(!empty($drops)){ $dropPos = $target->add(0.5, 0.5, 0.5);