Added Durable class, fixed some tools not breaking correctly, removed some boilerplate code

This commit is contained in:
Dylan K. Taylor 2017-10-08 15:54:31 +01:00
parent ae5aa31e7b
commit aa91183504
7 changed files with 90 additions and 39 deletions

View File

@ -2512,12 +2512,8 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
}
if($this->isSurvival()){
if($heldItem->isTool()){
if($heldItem->useOn($target) and $heldItem->getDamage() >= $heldItem->getMaxDurability()){
$this->inventory->setItemInHand(ItemFactory::get(Item::AIR, 0, 0));
}else{
$this->inventory->setItemInHand($heldItem);
}
if($heldItem->useOn($target)){
$this->inventory->setItemInHand($heldItem);
}
$this->exhaust(0.3, PlayerExhaustEvent::CAUSE_ATTACK);

View File

@ -99,12 +99,7 @@ class Bow extends Tool{
$entity->setMotion($entity->getMotion()->multiply($ev->getForce()));
if($player->isSurvival()){
$player->getInventory()->removeItem(ItemFactory::get(Item::ARROW, 0, 1));
$this->setDamage($this->getDamage() + 1);
if($this->getDamage() >= $this->getMaxDurability()){
$player->getInventory()->setItemInHand(ItemFactory::get(Item::AIR, 0, 0));
}else{
$player->getInventory()->setItemInHand($this);
}
$this->applyDamage(1);
}
if($entity instanceof Projectile){

View File

@ -0,0 +1,77 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\item;
use pocketmine\item\enchantment\Enchantment;
use pocketmine\nbt\tag\ByteTag;
use pocketmine\nbt\tag\CompoundTag;
abstract class Durable extends Item{
/**
* Returns whether this item will take damage when used.
* @return bool
*/
public function isUnbreakable() : bool{
$tag = $this->getNamedTagEntry("Unbreakable");
return $tag !== null and $tag->getValue() !== 0;
}
/**
* Sets whether the item will take damage when used.
* @param bool $value
*/
public function setUnbreakable(bool $value = true){
$this->setNamedTagEntry(new ByteTag("Unbreakable", $value ? 1 : 0));
}
/**
* Applies damage to the item.
* @param int $amount
*
* @return bool if any damage was applied to the item
*/
public function applyDamage(int $amount) : bool{
if($this->isUnbreakable() or $this->isBroken()){
return false;
}
//TODO: Unbreaking enchantment
$this->meta += $amount;
if($this->isBroken()){
$this->pop();
}
return true;
}
/**
* Returns whether the item is broken.
* @return bool
*/
public function isBroken() : bool{
return $this->meta >= $this->getMaxDurability();
}
}

View File

@ -41,14 +41,7 @@ class FlintSteel extends Tool{
$level->setBlock($block, BlockFactory::get(Block::FIRE), true);
$level->broadcastLevelSoundEvent($block->add(0.5, 0.5, 0.5), LevelSoundEventPacket::SOUND_IGNITE);
if(($player->gamemode & 0x01) === 0 and $this->useOn($block)){
if($this->getDamage() >= $this->getMaxDurability()){
$player->getInventory()->setItemInHand(Item::get(Item::AIR, 0, 0));
}else{
$this->meta++;
$player->getInventory()->setItemInHand($this);
}
}
$this->applyDamage(1);
return true;
}

View File

@ -763,6 +763,8 @@ class Item implements ItemIds, \JsonSerializable{
}
/**
* Returns the maximum amount of damage this item can take before it breaks.
*
* @return int|bool
*/
public function getMaxDurability(){

View File

@ -26,7 +26,7 @@ namespace pocketmine\item;
use pocketmine\block\Block;
use pocketmine\entity\Entity;
abstract class Tool extends Item{
abstract class Tool extends Durable{
const TIER_WOODEN = 1;
const TIER_GOLD = 2;
const TIER_STONE = 3;
@ -64,18 +64,18 @@ abstract class Tool extends Item{
$object->getToolType() === Tool::TYPE_SWORD and $this->isSword() or
$object->getToolType() === Tool::TYPE_SHEARS and $this->isShears()
){
$this->meta++;
$this->applyDamage(1);
}elseif(!$this->isShears() and $object->getBreakTime($this) > 0){
$this->meta += 2;
$this->applyDamage(2);
}
}elseif($this->isHoe()){
if(($object instanceof Block) and ($object->getId() === self::GRASS or $object->getId() === self::DIRT)){
$this->meta++;
$this->applyDamage(1);
}
}elseif(($object instanceof Entity) and !$this->isSword()){
$this->meta += 2;
$this->applyDamage(2);
}else{
$this->meta++;
$this->applyDamage(1);
}
return true;
@ -111,11 +111,6 @@ abstract class Tool extends Item{
return $levels[$type];
}
public function isUnbreakable(){
$tag = $this->getNamedTagEntry("Unbreakable");
return $tag !== null and $tag->getValue() > 0;
}
public function isTool(){
return true;
}

View File

@ -1692,9 +1692,6 @@ class Level implements ChunkManager, Metadatable{
if($item !== null){
$item->useOn($target);
if($item->isTool() and $item->getDamage() >= $item->getMaxDurability()){
$item = ItemFactory::get(Item::AIR, 0, 0);
}
}
if($player === null or $player->isSurvival()){
@ -1769,11 +1766,7 @@ class Level implements ChunkManager, Metadatable{
}
if(!$player->isSneaking() and $item->onActivate($this, $player, $blockReplace, $blockClicked, $face, $facePos)){
if($item->getCount() <= 0){
$item = ItemFactory::get(Item::AIR, 0, 0);
return true;
}
return true;
}
}else{
return false;