mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-06 01:46:04 +00:00
Tool: cleanup durability handling, closes #379
long overdue... this isn't quite as extensible as the original api3/blocks system was, but this is primarily intended to replace Item->useOn(). If plugins want to use it it can be extended later on.
This commit is contained in:
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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.
|
||||
*
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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){
|
||||
|
Reference in New Issue
Block a user