Implement Weeping & Twisting vines (#5396)

This commit is contained in:
ipad54 2022-11-15 18:29:42 +03:00 committed by GitHub
parent 34839da757
commit 858d3dce8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 225 additions and 2 deletions

View File

@ -704,8 +704,10 @@ final class BlockTypeIds{
public const MANGROVE_ROOTS = 10677;
public const MUDDY_MANGROVE_ROOTS = 10678;
public const FROGLIGHT = 10679;
public const TWISTING_VINES = 10680;
public const WEEPING_VINES = 10681;
public const FIRST_UNUSED_BLOCK_ID = 10680;
public const FIRST_UNUSED_BLOCK_ID = 10682;
private static int $nextDynamicId = self::FIRST_UNUSED_BLOCK_ID;

196
src/block/NetherVines.php Normal file
View File

@ -0,0 +1,196 @@
<?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\SupportType;
use pocketmine\data\runtime\RuntimeDataReader;
use pocketmine\data\runtime\RuntimeDataWriter;
use pocketmine\entity\Entity;
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\world\BlockTransaction;
use function min;
use function mt_rand;
/**
* This class is used for Weeping & Twisting vines, because they have same behaviour
*/
class NetherVines extends Flowable{
public const MAX_AGE = 25;
/** Direction the vine grows towards. */
private int $growthFace;
protected int $age = 0;
public function __construct(BlockIdentifier $idInfo, string $name, BlockTypeInfo $typeInfo, int $growthFace){
$this->growthFace = $growthFace;
parent::__construct($idInfo, $name, $typeInfo);
}
public function getRequiredStateDataBits() : int{
return 5;
}
public function describeState(RuntimeDataWriter|RuntimeDataReader $w) : void{
$w->boundedInt(5, 0, self::MAX_AGE, $this->age);
}
public function getAge() : int{
return $this->age;
}
/** @return $this */
public function setAge(int $age) : self{
if($age < 0 || $age > self::MAX_AGE){
throw new \InvalidArgumentException("Age must be in range 0-" . self::MAX_AGE);
}
$this->age = $age;
return $this;
}
public function isAffectedBySilkTouch() : bool{
return true;
}
public function ticksRandomly() : bool{
return true;
}
public function canClimb() : bool{
return true;
}
private function getSupportFace() : int{
return Facing::opposite($this->growthFace);
}
private function canBeSupportedBy(Block $block) : bool{
return $block->getSupportType($this->getSupportFace())->hasCenterSupport() || $block->isSameType($this);
}
public function onNearbyBlockChange() : void{
if(!$this->canBeSupportedBy($this->getSide($this->getSupportFace()))){
$this->position->getWorld()->useBreakOn($this->position);
}
}
/**
* Returns the block at the end of the vine structure furthest from the supporting block.
*/
private function seekToTip() : NetherVines{
$top = $this;
while(($next = $top->getSide($this->growthFace)) instanceof NetherVines && $next->isSameType($this)){
$top = $next;
}
return $top;
}
public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{
if(!$this->canBeSupportedBy($blockReplace->getSide($this->getSupportFace()))){
return false;
}
$this->age = mt_rand(0, self::MAX_AGE - 1);
return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player);
}
public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
if($item instanceof Fertilizer){
if($this->grow($player, mt_rand(1, 5))){
$item->pop();
}
return true;
}
return false;
}
public function onRandomTick() : void{
if(mt_rand(1, 10) === 1 && $this->age < self::MAX_AGE){
if($this->getSide($this->growthFace)->canBeReplaced()){
$this->grow(null);
}
}
}
private function grow(?Player $player, int $growthAmount = 1) : bool{
$top = $this->seekToTip();
$age = $top->getAge();
$pos = $top->getPosition();
$world = $pos->getWorld();
$changedBlocks = 0;
$tx = new BlockTransaction($world);
for($i = 1; $i <= $growthAmount; $i++){
$growthPos = $pos->getSide($this->growthFace, $i);
if(!$world->isInWorld($growthPos->getFloorX(), $growthPos->getFloorY(), $growthPos->getFloorZ()) || !$world->getBlock($growthPos)->canBeReplaced()){
break;
}
$tx->addBlock($growthPos, (clone $top)->setAge(min(++$age, self::MAX_AGE)));
$changedBlocks++;
}
if($changedBlocks > 0){
$ev = new StructureGrowEvent($top, $tx, $player);
$ev->call();
if($ev->isCancelled()){
return false;
}
return $tx->apply();
}
return false;
}
public function hasEntityCollision() : bool{
return true;
}
public function onEntityInside(Entity $entity) : bool{
$entity->resetFallDistance();
return false;
}
protected function recalculateCollisionBoxes() : array{
return [];
}
public function getDropsForCompatibleTool(Item $item) : array{
if(($item->getBlockToolType() & BlockToolType::SHEARS) !== 0 || mt_rand(1, 3) === 1){
return [$this->asItem()];
}
return [];
}
public function getSupportType(int $facing) : SupportType{
return SupportType::NONE();
}
}

View File

@ -58,6 +58,7 @@ use pocketmine\block\utils\WoodType;
use pocketmine\entity\projectile\Projectile;
use pocketmine\item\Item;
use pocketmine\item\ToolTier;
use pocketmine\math\Facing;
use pocketmine\math\RayTraceResult;
use pocketmine\utils\CloningRegistryTrait;
use pocketmine\world\sound\AmethystBlockChimeSound;
@ -695,6 +696,7 @@ use function mb_strtolower;
* @method static Tripwire TRIPWIRE()
* @method static TripwireHook TRIPWIRE_HOOK()
* @method static Opaque TUFF()
* @method static NetherVines TWISTING_VINES()
* @method static UnderwaterTorch UNDERWATER_TORCH()
* @method static Vine VINES()
* @method static WallBanner WALL_BANNER()
@ -715,6 +717,7 @@ use function mb_strtolower;
* @method static Opaque WARPED_WART_BLOCK()
* @method static Water WATER()
* @method static WaterCauldron WATER_CAULDRON()
* @method static NetherVines WEEPING_VINES()
* @method static WeightedPressurePlateHeavy WEIGHTED_PRESSURE_PLATE_HEAVY()
* @method static WeightedPressurePlateLight WEIGHTED_PRESSURE_PLATE_LIGHT()
* @method static Wheat WHEAT()
@ -1466,6 +1469,9 @@ final class VanillaBlocks{
self::register("crying_obsidian", new class(new BID(Ids::CRYING_OBSIDIAN), "Crying Obsidian", new Info(BreakInfo::pickaxe(35.0 /* 50 in Java */, ToolTier::DIAMOND(), 6000.0))) extends Opaque{
public function getLightLevel() : int{ return 10;}
});
self::register("twisting_vines", new NetherVines(new BID(Ids::TWISTING_VINES), "Twisting Vines", new Info(BreakInfo::instant()), Facing::UP));
self::register("weeping_vines", new NetherVines(new BID(Ids::WEEPING_VINES), "Weeping Vines", new Info(BreakInfo::instant()), Facing::DOWN));
}
private static function registerBlocksR17() : void{

View File

@ -93,6 +93,7 @@ use pocketmine\block\LitPumpkin;
use pocketmine\block\Loom;
use pocketmine\block\MelonStem;
use pocketmine\block\NetherPortal;
use pocketmine\block\NetherVines;
use pocketmine\block\NetherWartPlant;
use pocketmine\block\Potato;
use pocketmine\block\PoweredRail;
@ -1389,6 +1390,10 @@ final class BlockObjectToStateSerializer implements BlockStateSerializer{
->writeBool(StateNames::POWERED_BIT, $block->isPowered())
->writeLegacyHorizontalFacing($block->getFacing());
});
$this->map(Blocks::TWISTING_VINES(), function(NetherVines $block) : Writer{
return Writer::create(Ids::TWISTING_VINES)
->writeInt(StateNames::TWISTING_VINES_AGE, $block->getAge());
});
$this->map(Blocks::UNDERWATER_TORCH(), function(UnderwaterTorch $block) : Writer{
return Writer::create(Ids::UNDERWATER_TORCH)
->writeTorchFacing($block->getFacing());
@ -1425,6 +1430,10 @@ final class BlockObjectToStateSerializer implements BlockStateSerializer{
$this->map(Blocks::WARPED_TRAPDOOR(), fn(Trapdoor $block) => Helper::encodeTrapdoor($block, new Writer(Ids::WARPED_TRAPDOOR)));
$this->map(Blocks::WARPED_WALL_SIGN(), fn(WallSign $block) => Helper::encodeWallSign($block, new Writer(Ids::WARPED_WALL_SIGN)));
$this->map(Blocks::WATER(), fn(Water $block) => Helper::encodeLiquid($block, Ids::WATER, Ids::FLOWING_WATER));
$this->map(Blocks::WEEPING_VINES(), function(NetherVines $block) : Writer{
return Writer::create(Ids::WEEPING_VINES)
->writeInt(StateNames::WEEPING_VINES_AGE, $block->getAge());
});
$this->map(Blocks::WEIGHTED_PRESSURE_PLATE_HEAVY(), function(WeightedPressurePlateHeavy $block) : Writer{
return Writer::create(Ids::HEAVY_WEIGHTED_PRESSURE_PLATE)
->writeInt(StateNames::REDSTONE_SIGNAL, $block->getOutputSignalStrength());

View File

@ -1257,6 +1257,10 @@ final class BlockStateToObjectDeserializer implements BlockStateDeserializer{
->setFacing($in->readLegacyHorizontalFacing())
->setPowered($in->readBool(StateNames::POWERED_BIT));
});
$this->map(Ids::TWISTING_VINES, function(Reader $in) : Block{
return Blocks::TWISTING_VINES()
->setAge($in->readBoundedInt(StateNames::TWISTING_VINES_AGE, 0, 25));
});
$this->map(Ids::UNDERWATER_TORCH, function(Reader $in) : Block{
return Blocks::UNDERWATER_TORCH()
->setFacing($in->readTorchFacing());
@ -1315,6 +1319,10 @@ final class BlockStateToObjectDeserializer implements BlockStateDeserializer{
$this->map(Ids::WEATHERED_CUT_COPPER, fn() => Helper::decodeCopper(Blocks::CUT_COPPER(), CopperOxidation::WEATHERED()));
$this->mapSlab(Ids::WEATHERED_CUT_COPPER_SLAB, Ids::WEATHERED_DOUBLE_CUT_COPPER_SLAB, fn() => Helper::decodeCopper(Blocks::CUT_COPPER_SLAB(), CopperOxidation::WEATHERED()));
$this->mapStairs(Ids::WEATHERED_CUT_COPPER_STAIRS, fn() => Helper::decodeCopper(Blocks::CUT_COPPER_STAIRS(), CopperOxidation::WEATHERED()));
$this->map(Ids::WEEPING_VINES, function(Reader $in) : Block{
return Blocks::WEEPING_VINES()
->setAge($in->readBoundedInt(StateNames::WEEPING_VINES_AGE, 0, 25));
});
$this->map(Ids::WHEAT, fn(Reader $in) => Helper::decodeCrops(Blocks::WHEAT(), $in));
$this->map(Ids::WHITE_GLAZED_TERRACOTTA, fn(Reader $in) => Helper::decodeGlazedTerracotta(DyeColor::WHITE(), $in));
$this->map(Ids::WOOD, fn(Reader $in) : Block => Helper::decodeLog(match($woodType = $in->readString(StateNames::WOOD_TYPE)){

View File

@ -1054,6 +1054,7 @@ final class StringToItemParser extends StringToTParser{
$result->registerBlock("trunk", fn() => Blocks::OAK_PLANKS());
$result->registerBlock("trunk2", fn() => Blocks::ACACIA_LOG()->setStripped(false));
$result->registerBlock("tuff", fn() => Blocks::TUFF());
$result->registerBlock("twisting_vines", fn() => Blocks::TWISTING_VINES());
$result->registerBlock("underwater_torch", fn() => Blocks::UNDERWATER_TORCH());
$result->registerBlock("undyed_shulker_box", fn() => Blocks::SHULKER_BOX());
$result->registerBlock("unlit_redstone_torch", fn() => Blocks::REDSTONE_TORCH());
@ -1084,6 +1085,7 @@ final class StringToItemParser extends StringToTParser{
$result->registerBlock("water_lily", fn() => Blocks::LILY_PAD());
$result->registerBlock("waterlily", fn() => Blocks::LILY_PAD());
$result->registerBlock("web", fn() => Blocks::COBWEB());
$result->registerBlock("weeping_vines", fn() => Blocks::WEEPING_VINES());
$result->registerBlock("weighted_pressure_plate_heavy", fn() => Blocks::WEIGHTED_PRESSURE_PLATE_HEAVY());
$result->registerBlock("weighted_pressure_plate_light", fn() => Blocks::WEIGHTED_PRESSURE_PLATE_LIGHT());
$result->registerBlock("wheat_block", fn() => Blocks::WHEAT());

File diff suppressed because one or more lines are too long