diff --git a/src/block/TallGrass.php b/src/block/TallGrass.php index 459751c4e..b56169dc8 100644 --- a/src/block/TallGrass.php +++ b/src/block/TallGrass.php @@ -25,14 +25,53 @@ namespace pocketmine\block; use pocketmine\block\utils\StaticSupportTrait; use pocketmine\block\utils\TallGrassTrait; +use pocketmine\item\Fertilizer; +use pocketmine\item\Item; use pocketmine\math\Facing; +use pocketmine\math\Vector3; +use pocketmine\player\Player; class TallGrass extends Flowable{ use TallGrassTrait; use StaticSupportTrait; + /** @phpstan-var \Closure() : DoublePlant|null */ + private ?\Closure $doublePlantVariant; + + /** + * @phpstan-param \Closure() : DoublePlant|null $doublePlantVariant + */ + public function __construct(BlockIdentifier $idInfo, string $name, BlockTypeInfo $typeInfo, ?\Closure $doublePlantVariant = null){ + parent::__construct($idInfo, $name, $typeInfo); + $this->doublePlantVariant = $doublePlantVariant; + } + private function canBeSupportedAt(Block $block) : bool{ $supportBlock = $block->getSide(Facing::DOWN); return $supportBlock->hasTypeTag(BlockTypeTags::DIRT) || $supportBlock->hasTypeTag(BlockTypeTags::MUD); } + + public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{ + $world = $this->position->getWorld(); + $upPos = $this->position->getSide(Facing::UP); + if(!$world->isInWorld($upPos->getFloorX(), $upPos->getFloorY(), $upPos->getFloorZ()) || $this->getSide(Facing::UP)->getTypeId() !== BlockTypeIds::AIR){ + return false; + } + + if($item instanceof Fertilizer && ($doubleVariant = $this->getDoublePlantVariant()) !== null){ + $bottom = (clone $doubleVariant)->setTop(false); + $top = (clone $doubleVariant)->setTop(true); + $world->setBlock($this->position, $bottom); + $world->setBlock($this->position->getSide(Facing::UP), $top); + $item->pop(); + + return true; + } + + return false; + } + + private function getDoublePlantVariant() : ?DoublePlant{ + return $this->doublePlantVariant !== null ? ($this->doublePlantVariant)() : null; + } } diff --git a/src/block/VanillaBlocks.php b/src/block/VanillaBlocks.php index fe3bbc254..026729c5c 100644 --- a/src/block/VanillaBlocks.php +++ b/src/block/VanillaBlocks.php @@ -1230,8 +1230,8 @@ final class VanillaBlocks{ self::register("sugarcane", fn(BID $id) => new Sugarcane($id, "Sugarcane", new Info(BreakInfo::instant()))); self::register("sweet_berry_bush", fn(BID $id) => new SweetBerryBush($id, "Sweet Berry Bush", new Info(BreakInfo::instant()))); self::register("tnt", fn(BID $id) => new TNT($id, "TNT", new Info(BreakInfo::instant()))); - self::register("fern", fn(BID $id) => new TallGrass($id, "Fern", new Info(BreakInfo::instant(ToolType::SHEARS, 1), [Tags::POTTABLE_PLANTS]))); - self::register("tall_grass", fn(BID $id) => new TallGrass($id, "Tall Grass", new Info(BreakInfo::instant(ToolType::SHEARS, 1)))); + self::register("fern", fn(BID $id) => new TallGrass($id, "Fern", new Info(BreakInfo::instant(ToolType::SHEARS, 1), [Tags::POTTABLE_PLANTS]), fn() => VanillaBlocks::LARGE_FERN())); + self::register("tall_grass", fn(BID $id) => new TallGrass($id, "Tall Grass", new Info(BreakInfo::instant(ToolType::SHEARS, 1)), fn() => VanillaBlocks::DOUBLE_TALLGRASS())); self::register("blue_torch", fn(BID $id) => new Torch($id, "Blue Torch", new Info(BreakInfo::instant()))); self::register("copper_torch", fn(BID $id) => new Torch($id, "Copper Torch", new Info(BreakInfo::instant())));