Enchantment: Implement Silk Touch (#1912)

This commit is contained in:
Dylan K. Taylor 2018-01-14 13:37:27 +00:00 committed by GitHub
parent 96f6362117
commit 8d7c65585c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 92 additions and 2 deletions

View File

@ -207,6 +207,10 @@ class Bed extends Transparent{
return []; return [];
} }
public function isAffectedBySilkTouch() : bool{
return false;
}
public function getAffectedBlocks() : array{ public function getAffectedBlocks() : array{
if(($other = $this->getOtherHalf()) !== null){ if(($other = $this->getOtherHalf()) !== null){
return [$this, $other]; return [$this, $other];

View File

@ -27,6 +27,7 @@ declare(strict_types=1);
namespace pocketmine\block; namespace pocketmine\block;
use pocketmine\entity\Entity; use pocketmine\entity\Entity;
use pocketmine\item\enchantment\Enchantment;
use pocketmine\item\Item; use pocketmine\item\Item;
use pocketmine\item\ItemFactory; use pocketmine\item\ItemFactory;
use pocketmine\level\Level; use pocketmine\level\Level;
@ -433,6 +434,10 @@ class Block extends Position implements BlockIds, Metadatable{
*/ */
public function getDrops(Item $item) : array{ public function getDrops(Item $item) : array{
if($this->isCompatibleWithTool($item)){ if($this->isCompatibleWithTool($item)){
if($this->isAffectedBySilkTouch() and $item->hasEnchantment(Enchantment::SILK_TOUCH)){
return $this->getSilkTouchDrops($item);
}
return $this->getDropsForCompatibleTool($item); return $this->getDropsForCompatibleTool($item);
} }
@ -452,6 +457,29 @@ class Block extends Position implements BlockIds, Metadatable{
]; ];
} }
/**
* Returns an array of Items to be dropped when the block is broken using a compatible Silk Touch-enchanted tool.
*
* @param Item $item
*
* @return Item[]
*/
public function getSilkTouchDrops(Item $item) : array{
return [
ItemFactory::get($this->getItemId(), $this->getVariant())
];
}
/**
* Returns whether Silk Touch enchanted tools will cause this block to drop as itself. Since most blocks drop
* themselves anyway, this is implicitly true.
*
* @return bool
*/
public function isAffectedBySilkTouch() : bool{
return true;
}
/** /**
* Returns the item that players will equip when middle-clicking on this block. * Returns the item that players will equip when middle-clicking on this block.
* @return Item * @return Item

View File

@ -89,6 +89,10 @@ class Cake extends Transparent implements FoodSource{
return []; return [];
} }
public function isAffectedBySilkTouch() : bool{
return false;
}
public function onActivate(Item $item, Player $player = null) : bool{ public function onActivate(Item $item, Player $player = null) : bool{
if($player !== null){ if($player !== null){
$player->consumeObject($this); $player->consumeObject($this);

View File

@ -44,4 +44,8 @@ class CocoaBlock extends Transparent{
} }
//TODO //TODO
public function isAffectedBySilkTouch() : bool{
return false;
}
} }

View File

@ -95,4 +95,8 @@ abstract class Crops extends Flowable{
return false; return false;
} }
public function isAffectedBySilkTouch() : bool{
return false;
}
} }

View File

@ -278,6 +278,10 @@ abstract class Door extends Transparent{
return []; return [];
} }
public function isAffectedBySilkTouch() : bool{
return false;
}
public function getAffectedBlocks() : array{ public function getAffectedBlocks() : array{
if(($this->getDamage() & 0x08) === 0x08){ if(($this->getDamage() & 0x08) === 0x08){
$down = $this->getSide(Vector3::SIDE_DOWN); $down = $this->getSide(Vector3::SIDE_DOWN);

View File

@ -44,4 +44,7 @@ abstract class DoubleSlab extends Solid{
]; ];
} }
public function isAffectedBySilkTouch() : bool{
return false;
}
} }

View File

@ -112,4 +112,8 @@ class Farmland extends Transparent{
ItemFactory::get(Item::DIRT) ItemFactory::get(Item::DIRT)
]; ];
} }
public function isAffectedBySilkTouch() : bool{
return false;
}
} }

View File

@ -114,4 +114,7 @@ class FlowerPot extends Flowable{
return $items; return $items;
} }
public function isAffectedBySilkTouch() : bool{
return false;
}
} }

View File

@ -23,6 +23,7 @@ declare(strict_types=1);
namespace pocketmine\block; namespace pocketmine\block;
use pocketmine\item\enchantment\Enchantment;
use pocketmine\item\Item; use pocketmine\item\Item;
use pocketmine\level\Level; use pocketmine\level\Level;
use pocketmine\Player; use pocketmine\Player;
@ -56,8 +57,11 @@ class Ice extends Transparent{
} }
public function onBreak(Item $item, Player $player = null) : bool{ public function onBreak(Item $item, Player $player = null) : bool{
if(!$item->hasEnchantment(Enchantment::SILK_TOUCH)){
return $this->getLevel()->setBlock($this, BlockFactory::get(Block::WATER), true); return $this->getLevel()->setBlock($this, BlockFactory::get(Block::WATER), true);
} }
return parent::onBreak($item, $player);
}
public function ticksRandomly() : bool{ public function ticksRandomly() : bool{
return true; return true;

View File

@ -112,4 +112,8 @@ class ItemFrame extends Flowable{
return $drops; return $drops;
} }
public function isAffectedBySilkTouch() : bool{
return false;
}
} }

View File

@ -53,4 +53,8 @@ class MonsterSpawner extends Transparent{
public function getDropsForCompatibleTool(Item $item) : array{ public function getDropsForCompatibleTool(Item $item) : array{
return []; return [];
} }
public function isAffectedBySilkTouch() : bool{
return false;
}
} }

View File

@ -90,4 +90,8 @@ class NetherWartPlant extends Flowable{
ItemFactory::get($this->getItemId(), 0, ($this->getDamage() === 3 ? mt_rand(2, 4) : 1)) ItemFactory::get($this->getItemId(), 0, ($this->getDamage() === 3 ? mt_rand(2, 4) : 1))
]; ];
} }
public function isAffectedBySilkTouch() : bool{
return false;
}
} }

View File

@ -81,4 +81,8 @@ class Skull extends Flowable{
return []; return [];
} }
public function isAffectedBySilkTouch() : bool{
return false;
}
} }

View File

@ -96,4 +96,8 @@ class SnowLayer extends Flowable{
ItemFactory::get(Item::SNOWBALL) //TODO: check layer count ItemFactory::get(Item::SNOWBALL) //TODO: check layer count
]; ];
} }
public function isAffectedBySilkTouch() : bool{
return false;
}
} }

View File

@ -105,4 +105,8 @@ class StandingBanner extends Transparent{
return [$drop]; return [$drop];
} }
public function isAffectedBySilkTouch() : bool{
return false;
}
} }

View File

@ -43,4 +43,8 @@ class Tripwire extends Flowable{
ItemFactory::get(Item::STRING) ItemFactory::get(Item::STRING)
]; ];
} }
public function isAffectedBySilkTouch() : bool{
return false;
}
} }

View File

@ -95,7 +95,7 @@ class Enchantment{
self::registerEnchantment(new Enchantment(self::RESPIRATION, "%enchantment.oxygen", self::RARITY_RARE, self::SLOT_HEAD, 3)); self::registerEnchantment(new Enchantment(self::RESPIRATION, "%enchantment.oxygen", self::RARITY_RARE, self::SLOT_HEAD, 3));
self::registerEnchantment(new Enchantment(self::EFFICIENCY, "%enchantment.digging", self::RARITY_COMMON, self::SLOT_DIG | self::SLOT_SHEARS, 5)); self::registerEnchantment(new Enchantment(self::EFFICIENCY, "%enchantment.digging", self::RARITY_COMMON, self::SLOT_DIG | self::SLOT_SHEARS, 5));
self::registerEnchantment(new Enchantment(self::SILK_TOUCH, "%enchantment.untouching", self::RARITY_MYTHIC, self::SLOT_DIG | self::SLOT_SHEARS, 1));
self::registerEnchantment(new Enchantment(self::UNBREAKING, "%enchantment.durability", self::RARITY_UNCOMMON, self::SLOT_ALL, 3)); //TODO: item type flags need to be split up self::registerEnchantment(new Enchantment(self::UNBREAKING, "%enchantment.durability", self::RARITY_UNCOMMON, self::SLOT_ALL, 3)); //TODO: item type flags need to be split up
} }