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:
Dylan K. Taylor 2018-05-10 19:48:51 +01:00
parent b8523cb304
commit b21572774a
13 changed files with 103 additions and 56 deletions

View File

@ -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

View File

@ -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{

View File

@ -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;

View File

@ -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;
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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.
*

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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){

View File

@ -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);