mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-06 09:56:06 +00:00
this allows cleaning up a whole lot of abusable mess from the API, and we never properly supported overriding built-in block types anyway.
123 lines
3.6 KiB
PHP
123 lines
3.6 KiB
PHP
<?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\block;
|
|
|
|
use pocketmine\block\utils\TreeType;
|
|
use pocketmine\data\runtime\RuntimeDataDescriber;
|
|
use pocketmine\event\block\StructureGrowEvent;
|
|
use pocketmine\item\Fertilizer;
|
|
use pocketmine\item\Item;
|
|
use pocketmine\math\Facing;
|
|
use pocketmine\math\Vector3;
|
|
use pocketmine\player\Player;
|
|
use pocketmine\utils\Random;
|
|
use pocketmine\world\BlockTransaction;
|
|
use pocketmine\world\generator\object\TreeFactory;
|
|
use function mt_rand;
|
|
|
|
class Sapling extends Flowable{
|
|
protected bool $ready = false;
|
|
|
|
private TreeType $treeType;
|
|
|
|
public function __construct(BlockIdentifier $idInfo, string $name, BlockTypeInfo $typeInfo, TreeType $treeType){
|
|
parent::__construct($idInfo, $name, $typeInfo);
|
|
$this->treeType = $treeType;
|
|
}
|
|
|
|
public function describeState(RuntimeDataDescriber $w) : void{
|
|
$w->bool($this->ready);
|
|
}
|
|
|
|
public function isReady() : bool{ return $this->ready; }
|
|
|
|
/** @return $this */
|
|
public function setReady(bool $ready) : self{
|
|
$this->ready = $ready;
|
|
return $this;
|
|
}
|
|
|
|
public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{
|
|
$down = $this->getSide(Facing::DOWN);
|
|
if($down->hasTypeTag(BlockTypeTags::DIRT) || $down->hasTypeTag(BlockTypeTags::MUD)){
|
|
return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
|
|
if($item instanceof Fertilizer && $this->grow($player)){
|
|
$item->pop();
|
|
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function onNearbyBlockChange() : void{
|
|
$down = $this->getSide(Facing::DOWN);
|
|
if(!$down->hasTypeTag(BlockTypeTags::DIRT) && !$down->hasTypeTag(BlockTypeTags::MUD)){
|
|
$this->position->getWorld()->useBreakOn($this->position);
|
|
}
|
|
}
|
|
|
|
public function ticksRandomly() : bool{
|
|
return true;
|
|
}
|
|
|
|
public function onRandomTick() : void{
|
|
$world = $this->position->getWorld();
|
|
if($world->getFullLightAt($this->position->getFloorX(), $this->position->getFloorY(), $this->position->getFloorZ()) >= 8 && mt_rand(1, 7) === 1){
|
|
if($this->ready){
|
|
$this->grow(null);
|
|
}else{
|
|
$this->ready = true;
|
|
$world->setBlock($this->position, $this);
|
|
}
|
|
}
|
|
}
|
|
|
|
private function grow(?Player $player) : bool{
|
|
$random = new Random(mt_rand());
|
|
$tree = TreeFactory::get($random, $this->treeType);
|
|
$transaction = $tree?->getBlockTransaction($this->position->getWorld(), $this->position->getFloorX(), $this->position->getFloorY(), $this->position->getFloorZ(), $random);
|
|
if($transaction === null){
|
|
return false;
|
|
}
|
|
|
|
$ev = new StructureGrowEvent($this, $transaction, $player);
|
|
$ev->call();
|
|
if(!$ev->isCancelled()){
|
|
return $transaction->apply();
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public function getFuelTime() : int{
|
|
return 100;
|
|
}
|
|
}
|