※New Tools' durability system

1).Axes, pickaxes, and shovels:
Breaking a block that breaks instantly counts as 0 uses.
Breaking other blocks counts as 1 use.
Tilling a grass block using a shovel to make grass path counts as 1 use.
Hitting a mob (hostile, neutral or farm animal) counts as 2 uses.
2).Hoes:
Tilling dirt or grass counts as 1 use.
Hitting a mob counts as 1 use.
Breaking a block counts as 0 uses.
3).Shears:
Shearing a sheep counts as 1 use.
Breaking cobweb, leaves, tall grass, tripwire, wool and vines counts as 1 use.
Breaking other blocks or hitting a mob count as 0 uses.
4).Swords:
Breaking a block that breaks instantly counts as 0 uses.
Breaking other blocks counts as 2 uses.
Hitting a mob counts as 1 use.
5).Flint and steel
Breaking a block or hitting a mob count as 0 uses.
Using it to light a Nether portal counts as 1 use.
Using it to set blocks on fire counts as 1 use.
From minecraft WIKI http://minecraft.gamepedia.com/Item_durability
※Update for Unbreaking
All kind of tools
This commit is contained in:
dog194 2016-04-19 12:31:32 +08:00
commit 9147026830
5 changed files with 57 additions and 30 deletions

View file

@ -51,7 +51,7 @@ class Dirt extends Solid{
public function onActivate(Item $item, Player $player = null){
if($item->isHoe()){
$item->useOn($this);
$item->useOn($this, 2);
$this->getLevel()->setBlock($this, Block::get(Item::FARMLAND, 0), true);
return true;

View file

@ -97,12 +97,12 @@ class Grass extends Solid{
return true;
}elseif($item->isHoe()){
$item->useOn($this);
$item->useOn($this, 2);
$this->getLevel()->setBlock($this, new Farmland());
return true;
}elseif($item->isShovel() and $this->getSide(1)->getId() === Block::AIR){
$item->useOn($this);
$item->useOn($this, 2);
$this->getLevel()->setBlock($this, new GrassPath());
return true;

View file

@ -113,7 +113,7 @@ class TNT extends Solid implements ElectricalAppliance{
if($item->getId() === Item::FLINT_STEEL){
$this->prime();
$this->getLevel()->setBlock($this, new Air(), true);
$item->useOn($this);
$item->useOn($this, 2);
return true;
}

View file

@ -78,6 +78,10 @@ class FlintSteel extends Tool{
$level->setBlock(new Vector3($px, $py, $tz), new Block(90, 0));
}
}
if (($player->gamemode & 0x01) === 0) {
$this->useOn($block, 2);
$player->getInventory()->setItemInHand($this);
}
return true;
}
}
@ -117,6 +121,10 @@ class FlintSteel extends Tool{
$level->setBlock(new Vector3($tx, $py, $pz), new Block(90, 0));
}
}
if (($player->gamemode & 0x01) === 0) {
$this->useOn($block, 2);
$player->getInventory()->setItemInHand($this);
}
return true;
}
}
@ -134,13 +142,9 @@ class FlintSteel extends Tool{
// return true;
}
if(($player->gamemode & 0x01) === 0 and $this->useOn($block)){
if($this->getDamage() >= $this->getMaxDurability()){
$player->getInventory()->setItemInHand(new Item(Item::AIR, 0, 0));
}else{
$this->meta++;
$player->getInventory()->setItemInHand($this);
}
if (($player->gamemode & 0x01) === 0) {
$this->useOn($block, 2);//耐久跟报废分别写在 tool 跟 level 了
$player->getInventory()->setItemInHand($this);
}
return true;

View file

@ -25,6 +25,7 @@ namespace pocketmine\item;
use pocketmine\block\Block;
use pocketmine\entity\Entity;
use pocketmine\nbt\tag\ByteTag;
use pocketmine\item\enchantment\enchantment;
abstract class Tool extends Item{
const TIER_WOODEN = 1;
@ -52,36 +53,58 @@ abstract class Tool extends Item{
* TODO: Move this to each item
*
* @param Entity|Block $object
* @param 1 for break|2 for Touch $type
*
* @return bool
*/
public function useOn($object){
public function useOn($object, $type = 1)
{
if($this->isUnbreakable()){
return true;
}
if($object instanceof Block){
if(
$object->getToolType() === Tool::TYPE_PICKAXE and $this->isPickaxe() or
$object->getToolType() === Tool::TYPE_SHOVEL and $this->isShovel() or
$object->getToolType() === Tool::TYPE_AXE and $this->isAxe() or
$object->getToolType() === Tool::TYPE_SWORD and $this->isSword() or
$object->getToolType() === Tool::TYPE_SHEARS and $this->isShears()
){
$this->meta++;
}elseif(!$this->isShears() and $object->getBreakTime($this) > 0){
$this->meta += 2;
}
}elseif($this->isHoe()){
if(($object instanceof Block) and ($object->getId() === self::GRASS or $object->getId() === self::DIRT)){
$this->meta++;
}
}elseif(($object instanceof Entity) and !$this->isSword()){
$this->meta += 2;
}else{
$this->meta++;
$unbreakingl = $this->getEnchantmentLevel(Enchantment::TYPE_MINING_DURABILITY);
$unbreakingl = $unbreakingl > 3 ? 3 : $unbreakingl;
if (mt_rand(1, $unbreakingl + 1) !== 1) {
return true;
}
if ($type === 1) {
if ($object instanceof Entity) {
if ($this->isHoe() !== false or $this->isSword() !== false) {
//Hoe and Sword
$this->meta++;
return true;
} elseif ($this->isPickaxe() !== false or $this->isAxe() !== false or $this->isShovel() !== false) {
//Pickaxe Axe and Shovel
$this->meta += 2;
return true;
}
return true;//Other tool do not lost durability white hitting
} elseif ($object instanceof Block) {
if ($this->isShears() !== false) {
if ($object->getToolType() === Tool::TYPE_SHEARS) {//This should be checked in each block
$this->meta++;
}
return true;
} elseif ($object->getHardness() > 0) {//Sword Pickaxe Axe and Shovel
if ($this->isSword() !== false) {
$this->meta += 2;
return true;
} elseif ($this->isPickaxe() !== false or $this->isAxe() !== false or $this->isShovel() !== false) {
$this->meta += 1;
return true;
}
}
}
} elseif ($type === 2) {//For Touch. only trigger when OnActivate return true
if ($this->isHoe() !== false or $this->id === self::FLINT_STEEL or $this->isShovel() !== false) {
echo $this;
$this->meta++;
echo $this;
return true;
}
}
return true;
}