mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-14 15:35:31 +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:
parent
b8523cb304
commit
b21572774a
@ -2527,14 +2527,12 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if($this->isSurvival()){
|
if($heldItem->onAttackEntity($target) and $this->isSurvival()){ //always fire the hook, even if we are survival
|
||||||
if($heldItem->useOn($target)){
|
$this->inventory->setItemInHand($heldItem);
|
||||||
$this->inventory->setItemInHand($heldItem);
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->exhaust(0.3, PlayerExhaustEvent::CAUSE_ATTACK);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->exhaust(0.3, PlayerExhaustEvent::CAUSE_ATTACK);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
default:
|
default:
|
||||||
break; //unknown
|
break; //unknown
|
||||||
|
@ -23,6 +23,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace pocketmine\block;
|
namespace pocketmine\block;
|
||||||
|
|
||||||
|
use pocketmine\item\Hoe;
|
||||||
use pocketmine\item\Item;
|
use pocketmine\item\Item;
|
||||||
use pocketmine\Player;
|
use pocketmine\Player;
|
||||||
|
|
||||||
@ -50,8 +51,8 @@ class Dirt extends Solid{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function onActivate(Item $item, Player $player = null) : bool{
|
public function onActivate(Item $item, Player $player = null) : bool{
|
||||||
if($item->isHoe()){
|
if($item instanceof Hoe){
|
||||||
$item->useOn($this);
|
$item->applyDamage(1);
|
||||||
if($this->meta === 1){
|
if($this->meta === 1){
|
||||||
$this->getLevel()->setBlock($this, BlockFactory::get(Block::DIRT), true);
|
$this->getLevel()->setBlock($this, BlockFactory::get(Block::DIRT), true);
|
||||||
}else{
|
}else{
|
||||||
|
@ -24,8 +24,10 @@ declare(strict_types=1);
|
|||||||
namespace pocketmine\block;
|
namespace pocketmine\block;
|
||||||
|
|
||||||
use pocketmine\event\block\BlockSpreadEvent;
|
use pocketmine\event\block\BlockSpreadEvent;
|
||||||
|
use pocketmine\item\Hoe;
|
||||||
use pocketmine\item\Item;
|
use pocketmine\item\Item;
|
||||||
use pocketmine\item\ItemFactory;
|
use pocketmine\item\ItemFactory;
|
||||||
|
use pocketmine\item\Shovel;
|
||||||
use pocketmine\level\generator\object\TallGrass as TallGrassObject;
|
use pocketmine\level\generator\object\TallGrass as TallGrassObject;
|
||||||
use pocketmine\math\Vector3;
|
use pocketmine\math\Vector3;
|
||||||
use pocketmine\Player;
|
use pocketmine\Player;
|
||||||
@ -98,13 +100,13 @@ class Grass extends Solid{
|
|||||||
TallGrassObject::growGrass($this->getLevel(), $this, new Random(mt_rand()), 8, 2);
|
TallGrassObject::growGrass($this->getLevel(), $this, new Random(mt_rand()), 8, 2);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}elseif($item->isHoe()){
|
}elseif($item instanceof Hoe){
|
||||||
$item->useOn($this);
|
$item->applyDamage(1);
|
||||||
$this->getLevel()->setBlock($this, BlockFactory::get(Block::FARMLAND));
|
$this->getLevel()->setBlock($this, BlockFactory::get(Block::FARMLAND));
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}elseif($item->isShovel() and $this->getSide(Vector3::SIDE_UP)->getId() === Block::AIR){
|
}elseif($item instanceof Shovel and $this->getSide(Vector3::SIDE_UP)->getId() === Block::AIR){
|
||||||
$item->useOn($this);
|
$item->applyDamage(1);
|
||||||
$this->getLevel()->setBlock($this, BlockFactory::get(Block::GRASS_PATH));
|
$this->getLevel()->setBlock($this, BlockFactory::get(Block::GRASS_PATH));
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -24,6 +24,7 @@ declare(strict_types=1);
|
|||||||
namespace pocketmine\block;
|
namespace pocketmine\block;
|
||||||
|
|
||||||
use pocketmine\entity\Entity;
|
use pocketmine\entity\Entity;
|
||||||
|
use pocketmine\item\FlintSteel;
|
||||||
use pocketmine\item\Item;
|
use pocketmine\item\Item;
|
||||||
use pocketmine\math\Vector3;
|
use pocketmine\math\Vector3;
|
||||||
use pocketmine\Player;
|
use pocketmine\Player;
|
||||||
@ -46,8 +47,8 @@ class TNT extends Solid{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function onActivate(Item $item, Player $player = null) : bool{
|
public function onActivate(Item $item, Player $player = null) : bool{
|
||||||
if($item->getId() === Item::FLINT_STEEL){
|
if($item instanceof FlintSteel){
|
||||||
$item->useOn($this);
|
$item->applyDamage(1);
|
||||||
$this->ignite();
|
$this->ignite();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,9 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace pocketmine\item;
|
namespace pocketmine\item;
|
||||||
|
|
||||||
|
use pocketmine\block\Block;
|
||||||
use pocketmine\block\BlockToolType;
|
use pocketmine\block\BlockToolType;
|
||||||
|
use pocketmine\entity\Entity;
|
||||||
|
|
||||||
class Axe extends TieredTool{
|
class Axe extends TieredTool{
|
||||||
|
|
||||||
@ -42,4 +44,15 @@ class Axe extends TieredTool{
|
|||||||
public function getAttackPoints() : int{
|
public function getAttackPoints() : int{
|
||||||
return self::getBaseDamageFromTier($this->tier) - 1;
|
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;
|
namespace pocketmine\item;
|
||||||
|
|
||||||
|
use pocketmine\entity\Entity;
|
||||||
|
|
||||||
class Hoe extends TieredTool{
|
class Hoe extends TieredTool{
|
||||||
|
|
||||||
public function isHoe(){
|
public function isHoe(){
|
||||||
return $this->tier;
|
return $this->tier;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function onAttackEntity(Entity $victim) : bool{
|
||||||
|
return $this->applyDamage(1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -715,15 +715,6 @@ class Item implements ItemIds, \JsonSerializable{
|
|||||||
return 0;
|
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
|
* 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)
|
* 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;
|
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.
|
* 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;
|
namespace pocketmine\item;
|
||||||
|
|
||||||
|
use pocketmine\block\Block;
|
||||||
use pocketmine\block\BlockToolType;
|
use pocketmine\block\BlockToolType;
|
||||||
|
use pocketmine\entity\Entity;
|
||||||
|
|
||||||
class Pickaxe extends TieredTool{
|
class Pickaxe extends TieredTool{
|
||||||
|
|
||||||
@ -42,4 +44,15 @@ class Pickaxe extends TieredTool{
|
|||||||
public function getAttackPoints() : int{
|
public function getAttackPoints() : int{
|
||||||
return self::getBaseDamageFromTier($this->tier) - 2;
|
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;
|
namespace pocketmine\item;
|
||||||
|
|
||||||
|
use pocketmine\block\Block;
|
||||||
use pocketmine\block\BlockToolType;
|
use pocketmine\block\BlockToolType;
|
||||||
|
|
||||||
class Shears extends Tool{
|
class Shears extends Tool{
|
||||||
@ -49,4 +50,11 @@ class Shears extends Tool{
|
|||||||
protected function getBaseMiningEfficiency() : float{
|
protected function getBaseMiningEfficiency() : float{
|
||||||
return 15;
|
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;
|
namespace pocketmine\item;
|
||||||
|
|
||||||
|
use pocketmine\block\Block;
|
||||||
use pocketmine\block\BlockToolType;
|
use pocketmine\block\BlockToolType;
|
||||||
|
use pocketmine\entity\Entity;
|
||||||
|
|
||||||
class Shovel extends TieredTool{
|
class Shovel extends TieredTool{
|
||||||
|
|
||||||
@ -42,4 +44,15 @@ class Shovel extends TieredTool{
|
|||||||
public function getAttackPoints() : int{
|
public function getAttackPoints() : int{
|
||||||
return self::getBaseDamageFromTier($this->tier) - 3;
|
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\Block;
|
||||||
use pocketmine\block\BlockToolType;
|
use pocketmine\block\BlockToolType;
|
||||||
|
use pocketmine\entity\Entity;
|
||||||
|
|
||||||
class Sword extends TieredTool{
|
class Sword extends TieredTool{
|
||||||
|
|
||||||
@ -51,4 +52,15 @@ class Sword extends TieredTool{
|
|||||||
protected function getBaseMiningEfficiency() : float{
|
protected function getBaseMiningEfficiency() : float{
|
||||||
return 10;
|
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;
|
namespace pocketmine\item;
|
||||||
|
|
||||||
use pocketmine\block\Block;
|
use pocketmine\block\Block;
|
||||||
use pocketmine\entity\Entity;
|
|
||||||
use pocketmine\item\enchantment\Enchantment;
|
use pocketmine\item\enchantment\Enchantment;
|
||||||
|
|
||||||
abstract class Tool extends Durable{
|
abstract class Tool extends Durable{
|
||||||
@ -33,37 +32,6 @@ abstract class Tool extends Durable{
|
|||||||
return 1;
|
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{
|
public function getMiningEfficiency(Block $block) : float{
|
||||||
$efficiency = 1;
|
$efficiency = 1;
|
||||||
if(($block->getToolType() & $this->getBlockToolType()) !== 0){
|
if(($block->getToolType() & $this->getBlockToolType()) !== 0){
|
||||||
|
@ -1718,7 +1718,7 @@ class Level implements ChunkManager, Metadatable{
|
|||||||
$this->destroyBlockInternal($t, $item, $player, $createParticles);
|
$this->destroyBlockInternal($t, $item, $player, $createParticles);
|
||||||
}
|
}
|
||||||
|
|
||||||
$item->useOn($target);
|
$item->onDestroyBlock($target);
|
||||||
|
|
||||||
if(!empty($drops)){
|
if(!empty($drops)){
|
||||||
$dropPos = $target->add(0.5, 0.5, 0.5);
|
$dropPos = $target->add(0.5, 0.5, 0.5);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user