Implemented coral blocks

there are some complications with coral plants due to the fact we're stuck with R12 worlds right now - and also coral fans are a major pain to implement due to how messed up the metadata is.
This commit is contained in:
Dylan K. Taylor 2020-11-16 18:05:39 +00:00
parent 55a9ce46b9
commit 1eee24f1fa
5 changed files with 178 additions and 2 deletions

View File

@ -141,6 +141,7 @@ class BlockFactory{
$this->register(new Cobweb(new BID(Ids::COBWEB), "Cobweb"));
$this->register(new CocoaBlock(new BID(Ids::COCOA), "Cocoa Block"));
$this->register(new CoralBlock(new BID(Ids::CORAL_BLOCK), "Coral Block", new BlockBreakInfo(7.0, BlockToolType::PICKAXE, ToolTier::WOOD()->getHarvestLevel())));
$this->register(new CraftingTable(new BID(Ids::CRAFTING_TABLE), "Crafting Table"));
$this->register(new DaylightSensor(new BIDFlattened(Ids::DAYLIGHT_DETECTOR, Ids::DAYLIGHT_DETECTOR_INVERTED, 0, null, TileDaylightSensor::class), "Daylight Sensor"));
$this->register(new DeadBush(new BID(Ids::DEADBUSH), "Dead Bush"));
@ -525,7 +526,6 @@ class BlockFactory{
//TODO: minecraft:composter
//TODO: minecraft:conduit
//TODO: minecraft:coral
//TODO: minecraft:coral_block
//TODO: minecraft:coral_fan
//TODO: minecraft:coral_fan_dead
//TODO: minecraft:coral_fan_hang

106
src/block/CoralBlock.php Normal file
View File

@ -0,0 +1,106 @@
<?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\CoralType;
use pocketmine\block\utils\InvalidBlockStateException;
use pocketmine\data\bedrock\CoralTypeIdMap;
use pocketmine\item\Item;
use function mt_rand;
final class CoralBlock extends Opaque{
/** @var CoralType */
private $coralType; //TODO: make this dynamic via setter
/** @var bool */
private $dead = false;
public function __construct(BlockIdentifier $idInfo, string $name, BlockBreakInfo $breakInfo){
$this->coralType = CoralType::TUBE();
parent::__construct($idInfo, $name, $breakInfo);
}
public function readStateFromData(int $id, int $stateMeta) : void{
$coralType = CoralTypeIdMap::getInstance()->fromId($stateMeta & 0x7);
if($coralType === null){
throw new InvalidBlockStateException("No such coral type");
}
$this->coralType = $coralType;
$this->dead = ($stateMeta & BlockLegacyMetadata::CORAL_BLOCK_FLAG_DEAD) !== 0;
}
protected function writeStateToMeta() : int{
return ($this->dead ? BlockLegacyMetadata::CORAL_BLOCK_FLAG_DEAD : 0) | CoralTypeIdMap::getInstance()->toId($this->coralType);
}
public function getStateBitmask() : int{
return 0b1111;
}
public function getNonPersistentStateBitmask() : int{
return 0;
}
public function getCoralType() : CoralType{ return $this->coralType; }
public function isDead() : bool{ return $this->dead; }
/** @return $this */
public function setDead(bool $dead) : self{
$this->dead = $dead;
return $this;
}
public function onNearbyBlockChange() : void{
if(!$this->dead){
$this->pos->getWorld()->scheduleDelayedBlockUpdate($this->pos, mt_rand(40, 200));
}
}
public function onScheduledUpdate() : void{
if(!$this->dead){
$world = $this->pos->getWorld();
$hasWater = false;
foreach($this->pos->sides() as $vector3){
if($world->getBlock($vector3) instanceof Water){
$hasWater = true;
break;
}
}
if(!$hasWater){
$world->setBlock($this->pos, $this->setDead(true));
}
}
}
public function getDropsForCompatibleTool(Item $item) : array{
return [$this->setDead(true)->asItem()];
}
public function isAffectedBySilkTouch() : bool{
return true;
}
}

View File

@ -127,6 +127,7 @@ use function assert;
* @method static ChemistryTable COMPOUND_CREATOR()
* @method static Concrete CONCRETE()
* @method static ConcretePowder CONCRETE_POWDER()
* @method static CoralBlock CORAL_BLOCK()
* @method static Flower CORNFLOWER()
* @method static Opaque CRACKED_STONE_BRICKS()
* @method static CraftingTable CRAFTING_TABLE()
@ -743,6 +744,7 @@ final class VanillaBlocks{
self::register("compound_creator", $factory->get(238));
self::register("concrete", $factory->get(236));
self::register("concrete_powder", $factory->get(237));
self::register("coral_block", $factory->get(387));
self::register("cornflower", $factory->get(38, 9));
self::register("cracked_stone_bricks", $factory->get(98, 2));
self::register("crafting_table", $factory->get(58));

View File

@ -0,0 +1,68 @@
<?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\data\bedrock;
use pocketmine\block\BlockLegacyMetadata;
use pocketmine\block\utils\CoralType;
use pocketmine\utils\SingletonTrait;
use function array_key_exists;
final class CoralTypeIdMap{
use SingletonTrait;
/**
* @var CoralType[]
* @phpstan-var array<int, CoralType>
*/
private $idToEnum = [];
/**
* @var int[]
* @phpstan-var array<int, int>
*/
private $enumToId = [];
public function __construct(){
$this->register(BlockLegacyMetadata::CORAL_VARIANT_TUBE, CoralType::TUBE());
$this->register(BlockLegacyMetadata::CORAL_VARIANT_BRAIN, CoralType::BRAIN());
$this->register(BlockLegacyMetadata::CORAL_VARIANT_BUBBLE, CoralType::BUBBLE());
$this->register(BlockLegacyMetadata::CORAL_VARIANT_FIRE, CoralType::FIRE());
$this->register(BlockLegacyMetadata::CORAL_VARIANT_HORN, CoralType::HORN());
}
public function register(int $id, CoralType $type) : void{
$this->idToEnum[$id] = $type;
$this->enumToId[$type->id()] = $id;
}
public function fromId(int $id) : ?CoralType{
return $this->idToEnum[$id] ?? null;
}
public function toId(CoralType $type) : int{
if(!array_key_exists($type->id(), $this->enumToId)){
throw new \InvalidArgumentException("Coral type does not have a mapped ID"); //this should never happen
}
return $this->enumToId[$type->id()];
}
}

File diff suppressed because one or more lines are too long