Added Block->getDropsForCompatibleTool(), removed lots of boilerplate code from subclasses

The function name is a little long-winded, but that can always be refactored later if needed. This provides a way for blocks requiring specific tools to override drops with non-standard stuff without needing to worry about what tool type was used.

It's also possible that passing the Item used here is actually entirely redundant, but again that can be fixed later.
This commit is contained in:
Dylan K. Taylor 2017-12-12 20:02:50 +00:00
parent da3640357c
commit b9b50dd5dc
11 changed files with 56 additions and 85 deletions

View File

@ -110,13 +110,9 @@ class Anvil extends Fallable{
return $this->getLevel()->setBlock($blockReplace, $this, true, true);
}
public function getDrops(Item $item) : array{
if($this->isCompatibleWithTool($item)){
return [
ItemFactory::get($this->getItemId(), $this->getDamage() & 0x0c, 1)
];
}
return [];
public function getDropsForCompatibleTool(Item $item) : array{
return [
ItemFactory::get($this->getItemId(), $this->getDamage() & 0x0c, 1)
];
}
}

View File

@ -434,14 +434,25 @@ class Block extends Position implements BlockIds, Metadatable{
*/
public function getDrops(Item $item) : array{
if($this->isCompatibleWithTool($item)){
return [
ItemFactory::get($this->getItemId(), $this->getVariant(), 1)
];
return $this->getDropsForCompatibleTool($item);
}
return [];
}
/**
* Returns an array of Items to be dropped when the block is broken using the correct tool type.
*
* @param Item $item
*
* @return Item[]
*/
public function getDropsForCompatibleTool(Item $item) : array{
return [
ItemFactory::get($this->getItemId(), $this->getVariant(), 1)
];
}
/**
* Returns the item that players will equip when middle-clicking on this block.
* @return Item

View File

@ -51,14 +51,10 @@ class CoalOre extends Solid{
return "Coal Ore";
}
public function getDrops(Item $item) : array{
if($this->isCompatibleWithTool($item)){
return [
ItemFactory::get(Item::COAL, 0, 1)
];
}
return [];
public function getDropsForCompatibleTool(Item $item) : array{
return [
ItemFactory::get(Item::COAL, 0, 1)
];
}
}

View File

@ -51,13 +51,9 @@ class DiamondOre extends Solid{
return TieredTool::TIER_IRON;
}
public function getDrops(Item $item) : array{
if($this->isCompatibleWithTool($item)){
return [
ItemFactory::get(Item::DIAMOND, 0, 1)
];
}
return [];
public function getDropsForCompatibleTool(Item $item) : array{
return [
ItemFactory::get(Item::DIAMOND, 0, 1)
];
}
}

View File

@ -51,13 +51,9 @@ class EmeraldOre extends Solid{
return 3;
}
public function getDrops(Item $item) : array{
if($this->isCompatibleWithTool($item)){
return [
public function getDropsForCompatibleTool(Item $item) : array{
return [
ItemFactory::get(Item::EMERALD, 0, 1)
];
}
return [];
];
}
}

View File

@ -101,14 +101,10 @@ class EnderChest extends Chest{
return true;
}
public function getDrops(Item $item) : array{
if($this->isCompatibleWithTool($item)){
return [
ItemFactory::get(Item::OBSIDIAN, 0, 8)
];
}
return [];
public function getDropsForCompatibleTool(Item $item) : array{
return [
ItemFactory::get(Item::OBSIDIAN, 0, 8)
];
}
public function getFuelTime() : int{

View File

@ -51,14 +51,10 @@ class LapisOre extends Solid{
return "Lapis Lazuli Ore";
}
public function getDrops(Item $item) : array{
if($this->isCompatibleWithTool($item)){
return [
ItemFactory::get(Item::DYE, 4, mt_rand(4, 8))
];
}
return [];
public function getDropsForCompatibleTool(Item $item) : array{
return [
ItemFactory::get(Item::DYE, 4, mt_rand(4, 8))
];
}
}

View File

@ -51,14 +51,10 @@ class NetherQuartzOre extends Solid{
return TieredTool::TIER_WOODEN;
}
public function getDrops(Item $item) : array{
if($this->isCompatibleWithTool($item)){
return [
ItemFactory::get(Item::QUARTZ, 0, 1)
];
}
return [];
public function getDropsForCompatibleTool(Item $item) : array{
return [
ItemFactory::get(Item::QUARTZ, 0, 1)
];
}
}

View File

@ -55,15 +55,11 @@ class NetherReactor extends Solid{
return 3;
}
public function getDrops(Item $item) : array{
if($this->isCompatibleWithTool($item)){
return [
ItemFactory::get(Item::IRON_INGOT, 0, 6),
ItemFactory::get(Item::DIAMOND, 0, 3)
];
}
return [];
public function getDropsForCompatibleTool(Item $item) : array{
return [
ItemFactory::get(Item::IRON_INGOT, 0, 6),
ItemFactory::get(Item::DIAMOND, 0, 3)
];
}
}

View File

@ -68,13 +68,9 @@ class RedstoneOre extends Solid{
return TieredTool::TIER_IRON;
}
public function getDrops(Item $item) : array{
if($this->isCompatibleWithTool($item)){
return [
ItemFactory::get(Item::REDSTONE_DUST, 0, mt_rand(4, 5))
];
}
return [];
public function getDropsForCompatibleTool(Item $item) : array{
return [
ItemFactory::get(Item::REDSTONE_DUST, 0, mt_rand(4, 5))
];
}
}

View File

@ -67,18 +67,14 @@ class Stone extends Solid{
return $names[$this->getVariant()] ?? "Unknown";
}
public function getDrops(Item $item) : array{
if($this->isCompatibleWithTool($item)){
if($this->getDamage() === self::NORMAL){
return [
ItemFactory::get(Item::COBBLESTONE, $this->getDamage(), 1)
];
}
return parent::getDrops($item);
public function getDropsForCompatibleTool(Item $item) : array{
if($this->getDamage() === self::NORMAL){
return [
ItemFactory::get(Item::COBBLESTONE, $this->getDamage(), 1)
];
}
return [];
return parent::getDropsForCompatibleTool($item);
}
}