mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-06 11:57:10 +00:00
Implemented bamboo (#3762)
This commit is contained in:
parent
62394811e3
commit
bf401421fa
235
src/block/Bamboo.php
Normal file
235
src/block/Bamboo.php
Normal file
@ -0,0 +1,235 @@
|
||||
<?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\BlockDataSerializer;
|
||||
use pocketmine\item\Bamboo as ItemBamboo;
|
||||
use pocketmine\item\Fertilizer;
|
||||
use pocketmine\item\Item;
|
||||
use pocketmine\math\AxisAlignedBB;
|
||||
use pocketmine\math\Facing;
|
||||
use pocketmine\math\Vector3;
|
||||
use pocketmine\player\Player;
|
||||
use pocketmine\world\BlockTransaction;
|
||||
use function count;
|
||||
use function gmp_add;
|
||||
use function gmp_and;
|
||||
use function gmp_intval;
|
||||
use function gmp_mul;
|
||||
use function gmp_xor;
|
||||
use function min;
|
||||
use function mt_rand;
|
||||
use const PHP_INT_MAX;
|
||||
|
||||
class Bamboo extends Transparent{
|
||||
|
||||
public const NO_LEAVES = 0;
|
||||
public const SMALL_LEAVES = 1;
|
||||
public const LARGE_LEAVES = 2;
|
||||
|
||||
/** @var bool */
|
||||
protected $thick = false; //age in PC, but this is 0/1
|
||||
/** @var bool */
|
||||
protected $ready = false;
|
||||
/** @var int */
|
||||
protected $leafSize = self::NO_LEAVES;
|
||||
|
||||
public function readStateFromData(int $id, int $stateMeta) : void{
|
||||
$this->thick = ($stateMeta & BlockLegacyMetadata::BAMBOO_FLAG_THICK) !== 0;
|
||||
$this->leafSize = BlockDataSerializer::readBoundedInt("leafSize", ($stateMeta >> BlockLegacyMetadata::BAMBOO_LEAF_SIZE_SHIFT) & BlockLegacyMetadata::BAMBOO_LEAF_SIZE_MASK, self::NO_LEAVES, self::LARGE_LEAVES);
|
||||
$this->ready = ($stateMeta & BlockLegacyMetadata::BAMBOO_FLAG_READY) !== 0;
|
||||
}
|
||||
|
||||
public function writeStateToMeta() : int{
|
||||
return ($this->thick ? BlockLegacyMetadata::BAMBOO_FLAG_THICK : 0) | ($this->leafSize << BlockLegacyMetadata::BAMBOO_LEAF_SIZE_SHIFT) | ($this->ready ? BlockLegacyMetadata::BAMBOO_FLAG_READY : 0);
|
||||
}
|
||||
|
||||
public function getStateBitmask() : int{
|
||||
return 0b1111;
|
||||
}
|
||||
|
||||
public function isThick() : bool{ return $this->thick; }
|
||||
|
||||
/** @return $this */
|
||||
public function setThick(bool $thick) : self{
|
||||
$this->thick = $thick;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function isReady() : bool{ return $this->ready; }
|
||||
|
||||
/** @return $this */
|
||||
public function setReady(bool $ready) : self{
|
||||
$this->ready = $ready;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getLeafSize() : int{ return $this->leafSize; }
|
||||
|
||||
/** @return $this */
|
||||
public function setLeafSize(int $leafSize) : self{
|
||||
$this->leafSize = $leafSize;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return AxisAlignedBB[]
|
||||
*/
|
||||
protected function recalculateCollisionBoxes() : array{
|
||||
//this places the BB at the northwest corner, not the center
|
||||
$inset = 1 - (($this->thick ? 3 : 2) / 16);
|
||||
return [AxisAlignedBB::one()->trim(Facing::SOUTH, $inset)->trim(Facing::EAST, $inset)];
|
||||
}
|
||||
|
||||
private static function getOffsetSeed(int $x, int $y, int $z) : int{
|
||||
$p1 = gmp_mul($z, 0x6ebfff5);
|
||||
$p2 = gmp_mul($x, 0x2fc20f);
|
||||
$p3 = $y;
|
||||
|
||||
$xord = gmp_xor(gmp_xor($p1, $p2), $p3);
|
||||
|
||||
$fullResult = gmp_mul(gmp_add(gmp_mul($xord, 0x285b825), 0xb), $xord);
|
||||
return gmp_intval(gmp_and($fullResult, 0xffffffff));
|
||||
}
|
||||
|
||||
private static function getMaxHeight(int $x, int $z) : int{
|
||||
return 12 + (self::getOffsetSeed($x, 0, $z) % 5);
|
||||
}
|
||||
|
||||
public function getPosOffset() : ?Vector3{
|
||||
$seed = self::getOffsetSeed($this->pos->getFloorX(), 0, $this->pos->getFloorZ());
|
||||
$retX = (($seed % 12) + 1) / 16;
|
||||
$retZ = ((($seed >> 8) % 12) + 1) / 16;
|
||||
return new Vector3($retX, 0, $retZ);
|
||||
}
|
||||
|
||||
private function canBeSupportedBy(Block $block) : bool{
|
||||
//TODO: tags would be better for this
|
||||
return
|
||||
$block instanceof Dirt ||
|
||||
$block instanceof Grass ||
|
||||
$block instanceof Gravel ||
|
||||
$block instanceof Sand ||
|
||||
$block instanceof Mycelium ||
|
||||
$block instanceof Podzol;
|
||||
}
|
||||
|
||||
private function seekToTop() : Bamboo{
|
||||
$world = $this->pos->getWorld();
|
||||
$top = $this;
|
||||
while(($next = $world->getBlock($top->pos->up())) instanceof Bamboo && $next->isSameType($this)){
|
||||
$top = $next;
|
||||
}
|
||||
return $top;
|
||||
}
|
||||
|
||||
public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null) : bool{
|
||||
if($item instanceof Fertilizer){
|
||||
$top = $this->seekToTop();
|
||||
if($top->grow(self::getMaxHeight($top->pos->getFloorX(), $top->pos->getFloorZ()), mt_rand(1, 2))){
|
||||
$item->pop();
|
||||
return true;
|
||||
}
|
||||
}elseif($item instanceof ItemBamboo){
|
||||
if($this->seekToTop()->grow(PHP_INT_MAX, 1)){
|
||||
$item->pop();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function onNearbyBlockChange() : void{
|
||||
$below = $this->pos->getWorld()->getBlock($this->pos->down());
|
||||
if(!$this->canBeSupportedBy($below) and !$below->isSameType($this)){
|
||||
$this->pos->getWorld()->useBreakOn($this->pos);
|
||||
}
|
||||
}
|
||||
|
||||
private function grow(int $maxHeight, int $growAmount) : bool{
|
||||
$world = $this->pos->getWorld();
|
||||
if(!$world->getBlock($this->pos->up())->canBeReplaced()){
|
||||
return false;
|
||||
}
|
||||
|
||||
$height = 1;
|
||||
while($world->getBlock($this->pos->subtract(0, $height, 0))->isSameType($this)){
|
||||
if(++$height >= $maxHeight){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
$newHeight = $height + $growAmount;
|
||||
|
||||
$stemBlock = (clone $this)->setReady(false)->setLeafSize(self::NO_LEAVES);
|
||||
if($newHeight >= 4 && !$stemBlock->isThick()){ //don't change it to false if height is less, because it might have been chopped
|
||||
$stemBlock = $stemBlock->setThick(true);
|
||||
}
|
||||
$smallLeavesBlock = (clone $stemBlock)->setLeafSize(self::SMALL_LEAVES);
|
||||
$bigLeavesBlock = (clone $stemBlock)->setLeafSize(self::LARGE_LEAVES);
|
||||
|
||||
$newBlocks = [];
|
||||
if($newHeight === 2){
|
||||
$newBlocks[] = $smallLeavesBlock;
|
||||
}elseif($newHeight === 3){
|
||||
$newBlocks[] = $smallLeavesBlock;
|
||||
$newBlocks[] = $smallLeavesBlock;
|
||||
}elseif($newHeight === 4){
|
||||
$newBlocks[] = $bigLeavesBlock;
|
||||
$newBlocks[] = $smallLeavesBlock;
|
||||
$newBlocks[] = $stemBlock;
|
||||
$newBlocks[] = $stemBlock;
|
||||
}elseif($newHeight > 4){
|
||||
$newBlocks[] = $bigLeavesBlock;
|
||||
$newBlocks[] = $bigLeavesBlock;
|
||||
$newBlocks[] = $smallLeavesBlock;
|
||||
for($i = 0, $max = min($growAmount, $newHeight - count($newBlocks)); $i < $max; ++$i){
|
||||
$newBlocks[] = $stemBlock; //to replace the bottom blocks that currently have leaves
|
||||
}
|
||||
}
|
||||
|
||||
$tx = new BlockTransaction($this->pos->getWorld());
|
||||
foreach($newBlocks as $idx => $newBlock){
|
||||
$tx->addBlock($this->pos->subtract(0, $idx - $growAmount, 0), $newBlock);
|
||||
}
|
||||
return $tx->apply();
|
||||
}
|
||||
|
||||
public function ticksRandomly() : bool{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function onRandomTick() : void{
|
||||
$world = $this->pos->getWorld();
|
||||
if($this->ready){
|
||||
$this->ready = false;
|
||||
if($world->getFullLight($this->pos) < 9 || !$this->grow(self::getMaxHeight($this->pos->getFloorX(), $this->pos->getFloorZ()), 1)){
|
||||
$world->setBlock($this->pos, $this);
|
||||
}
|
||||
}elseif($world->getBlock($this->pos->up())->canBeReplaced()){
|
||||
$this->ready = true;
|
||||
$world->setBlock($this->pos, $this);
|
||||
}
|
||||
}
|
||||
}
|
123
src/block/BambooSapling.php
Normal file
123
src/block/BambooSapling.php
Normal file
@ -0,0 +1,123 @@
|
||||
<?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\item\Bamboo as ItemBamboo;
|
||||
use pocketmine\item\Fertilizer;
|
||||
use pocketmine\item\Item;
|
||||
use pocketmine\math\Vector3;
|
||||
use pocketmine\player\Player;
|
||||
use pocketmine\world\BlockTransaction;
|
||||
|
||||
final class BambooSapling extends Flowable{
|
||||
|
||||
/** @var bool */
|
||||
private $ready = false;
|
||||
|
||||
public function readStateFromData(int $id, int $stateMeta) : void{
|
||||
$this->ready = ($stateMeta & BlockLegacyMetadata::SAPLING_FLAG_READY) !== 0;
|
||||
}
|
||||
|
||||
protected function writeStateToMeta() : int{
|
||||
return $this->ready ? BlockLegacyMetadata::SAPLING_FLAG_READY : 0;
|
||||
}
|
||||
|
||||
public function getStateBitmask() : int{ return 0b1000; }
|
||||
|
||||
public function isReady() : bool{ return $this->ready; }
|
||||
|
||||
/** @return $this */
|
||||
public function setReady(bool $ready) : self{
|
||||
$this->ready = $ready;
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function canBeSupportedBy(Block $block) : bool{
|
||||
//TODO: tags would be better for this
|
||||
return
|
||||
$block instanceof Dirt ||
|
||||
$block instanceof Grass ||
|
||||
$block instanceof Gravel ||
|
||||
$block instanceof Sand ||
|
||||
$block instanceof Mycelium ||
|
||||
$block instanceof Podzol;
|
||||
}
|
||||
|
||||
public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{
|
||||
if(!$this->canBeSupportedBy($blockReplace->pos->getWorld()->getBlock($blockReplace->pos->down()))){
|
||||
return false;
|
||||
}
|
||||
return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player);
|
||||
}
|
||||
|
||||
public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null) : bool{
|
||||
if($item instanceof Fertilizer || $item instanceof ItemBamboo){
|
||||
if($this->grow()){
|
||||
$item->pop();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function onNearbyBlockChange() : void{
|
||||
if(!$this->canBeSupportedBy($this->pos->getWorld()->getBlock($this->pos->down()))){
|
||||
$this->pos->getWorld()->useBreakOn($this->pos);
|
||||
}
|
||||
}
|
||||
|
||||
private function grow() : bool{
|
||||
$world = $this->pos->getWorld();
|
||||
if(!$world->getBlock($this->pos->up())->canBeReplaced()){
|
||||
return false;
|
||||
}
|
||||
|
||||
$tx = new BlockTransaction($world);
|
||||
$bamboo = VanillaBlocks::BAMBOO();
|
||||
$tx->addBlock($this->pos, $bamboo)
|
||||
->addBlock($this->pos->up(), (clone $bamboo)->setLeafSize(Bamboo::SMALL_LEAVES));
|
||||
return $tx->apply();
|
||||
}
|
||||
|
||||
public function ticksRandomly() : bool{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function onRandomTick() : void{
|
||||
$world = $this->pos->getWorld();
|
||||
if($this->ready){
|
||||
$this->ready = false;
|
||||
if($world->getFullLight($this->pos) < 9 || !$this->grow()){
|
||||
$world->setBlock($this->pos, $this);
|
||||
}
|
||||
}elseif($world->getBlock($this->pos->up())->canBeReplaced()){
|
||||
$this->ready = true;
|
||||
$world->setBlock($this->pos, $this);
|
||||
}
|
||||
}
|
||||
|
||||
public function asItem() : Item{
|
||||
return VanillaBlocks::BAMBOO()->asItem();
|
||||
}
|
||||
}
|
@ -547,14 +547,24 @@ class Block{
|
||||
final public function getCollisionBoxes() : array{
|
||||
if($this->collisionBoxes === null){
|
||||
$this->collisionBoxes = $this->recalculateCollisionBoxes();
|
||||
$extraOffset = $this->getPosOffset();
|
||||
$offset = $extraOffset !== null ? $this->pos->addVector($extraOffset) : $this->pos;
|
||||
foreach($this->collisionBoxes as $bb){
|
||||
$bb->offset($this->pos->x, $this->pos->y, $this->pos->z);
|
||||
$bb->offset($offset->x, $offset->y, $offset->z);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->collisionBoxes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an additional fractional vector to shift the block's effective position by based on the current position.
|
||||
* Used to randomize position of things like bamboo canes and tall grass.
|
||||
*/
|
||||
public function getPosOffset() : ?Vector3{
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return AxisAlignedBB[]
|
||||
*/
|
||||
|
@ -97,6 +97,8 @@ class BlockFactory{
|
||||
$this->register(new Anvil(new BID(Ids::ANVIL, Meta::ANVIL_NORMAL), "Anvil"));
|
||||
$this->register(new Anvil(new BID(Ids::ANVIL, Meta::ANVIL_SLIGHTLY_DAMAGED), "Slightly Damaged Anvil"));
|
||||
$this->register(new Anvil(new BID(Ids::ANVIL, Meta::ANVIL_VERY_DAMAGED), "Very Damaged Anvil"));
|
||||
$this->register(new Bamboo(new BID(Ids::BAMBOO), "Bamboo", new BlockBreakInfo(2.0 /* 1.0 in PC */, BlockToolType::AXE)));
|
||||
$this->register(new BambooSapling(new BID(Ids::BAMBOO_SAPLING), "Bamboo Sapling", BlockBreakInfo::instant()));
|
||||
$this->register(new Banner(new BIDFlattened(Ids::STANDING_BANNER, Ids::WALL_BANNER, 0, ItemIds::BANNER, TileBanner::class), "Banner"));
|
||||
$this->register(new Transparent(new BID(Ids::BARRIER), "Barrier", BlockBreakInfo::indestructible()));
|
||||
$this->register(new Bed(new BID(Ids::BED_BLOCK, 0, ItemIds::BED, TileBed::class), "Bed Block"));
|
||||
|
@ -24,13 +24,12 @@ declare(strict_types=1);
|
||||
namespace pocketmine\block;
|
||||
|
||||
use pocketmine\utils\CloningRegistryTrait;
|
||||
use pocketmine\utils\RegistryTrait;
|
||||
use function assert;
|
||||
|
||||
/**
|
||||
* This doc-block is generated automatically, do not modify it manually.
|
||||
* This must be regenerated whenever registry members are added, removed or changed.
|
||||
* @see RegistryTrait::_generateMethodAnnotations()
|
||||
* @see \pocketmine\utils\RegistryUtils::_generateMethodAnnotations()
|
||||
*
|
||||
* @method static WoodenButton ACACIA_BUTTON()
|
||||
* @method static WoodenDoor ACACIA_DOOR()
|
||||
@ -55,6 +54,8 @@ use function assert;
|
||||
* @method static Wall ANDESITE_WALL()
|
||||
* @method static Anvil ANVIL()
|
||||
* @method static Flower AZURE_BLUET()
|
||||
* @method static Bamboo BAMBOO()
|
||||
* @method static BambooSapling BAMBOO_SAPLING()
|
||||
* @method static Banner BANNER()
|
||||
* @method static Transparent BARRIER()
|
||||
* @method static Bed BED()
|
||||
@ -721,6 +722,8 @@ final class VanillaBlocks{
|
||||
self::register("andesite_wall", $factory->get(139, 4));
|
||||
self::register("anvil", $factory->get(145));
|
||||
self::register("azure_bluet", $factory->get(38, 3));
|
||||
self::register("bamboo", $factory->get(418));
|
||||
self::register("bamboo_sapling", $factory->get(419));
|
||||
self::register("banner", $factory->get(176));
|
||||
self::register("barrier", $factory->get(416));
|
||||
self::register("bed", $factory->get(26));
|
||||
|
38
src/item/Bamboo.php
Normal file
38
src/item/Bamboo.php
Normal file
@ -0,0 +1,38 @@
|
||||
<?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\item;
|
||||
|
||||
use pocketmine\block\Block;
|
||||
use pocketmine\block\VanillaBlocks;
|
||||
|
||||
final class Bamboo extends Item{
|
||||
|
||||
public function getFuelTime() : int{
|
||||
return 50;
|
||||
}
|
||||
|
||||
public function getBlock() : Block{
|
||||
return VanillaBlocks::BAMBOO_SAPLING();
|
||||
}
|
||||
}
|
@ -64,6 +64,7 @@ class ItemFactory{
|
||||
$this->register(new Arrow(new ItemIdentifier(ItemIds::ARROW, 0), "Arrow"));
|
||||
|
||||
$this->register(new BakedPotato(new ItemIdentifier(ItemIds::BAKED_POTATO, 0), "Baked Potato"));
|
||||
$this->register(new Bamboo(new ItemIdentifier(ItemIds::BAMBOO, 0), "Bamboo"), true);
|
||||
$this->register(new Beetroot(new ItemIdentifier(ItemIds::BEETROOT, 0), "Beetroot"));
|
||||
$this->register(new BeetrootSeeds(new ItemIdentifier(ItemIds::BEETROOT_SEEDS, 0), "Beetroot Seeds"));
|
||||
$this->register(new BeetrootSoup(new ItemIdentifier(ItemIds::BEETROOT_SOUP, 0), "Beetroot Soup"));
|
||||
|
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user