From 2d51622b1253b662d2edd19edab2f69250e9299c Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Mon, 25 Feb 2019 13:04:05 +0000 Subject: [PATCH] Implemented frosted ice --- src/pocketmine/block/BlockFactory.php | 1 + src/pocketmine/block/FrostedIce.php | 115 ++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 src/pocketmine/block/FrostedIce.php diff --git a/src/pocketmine/block/BlockFactory.php b/src/pocketmine/block/BlockFactory.php index c45fd4440..b1573b6c2 100644 --- a/src/pocketmine/block/BlockFactory.php +++ b/src/pocketmine/block/BlockFactory.php @@ -136,6 +136,7 @@ class BlockFactory{ self::register(new Flower(new BID(Block::RED_FLOWER, Flower::TYPE_RED_TULIP), "Red Tulip")); self::register(new Flower(new BID(Block::RED_FLOWER, Flower::TYPE_WHITE_TULIP), "White Tulip")); self::register(new FlowerPot(new BID(Block::FLOWER_POT_BLOCK, 0, ItemIds::FLOWER_POT, \pocketmine\tile\FlowerPot::class), "Flower Pot")); + self::register(new FrostedIce(new BID(Block::FROSTED_ICE), "Frosted Ice")); self::register(new Furnace(new BlockIdentifierFlattened(Block::FURNACE, Block::LIT_FURNACE, 0, null, \pocketmine\tile\Furnace::class), "Furnace")); self::register(new Glass(new BID(Block::GLASS), "Glass")); self::register(new GlassPane(new BID(Block::GLASS_PANE), "Glass Pane")); diff --git a/src/pocketmine/block/FrostedIce.php b/src/pocketmine/block/FrostedIce.php new file mode 100644 index 000000000..aaae97160 --- /dev/null +++ b/src/pocketmine/block/FrostedIce.php @@ -0,0 +1,115 @@ +age = BlockDataValidator::readBoundedInt("age", $stateMeta, 0, 3); + } + + protected function writeStateToMeta() : int{ + return $this->age; + } + + public function getStateBitmask() : int{ + return 0b11; + } + + public function getHardness() : float{ + return 2.5; + } + + public function onNearbyBlockChange() : void{ + if(!$this->checkAdjacentBlocks(2)){ + $this->level->useBreakOn($this); + }else{ + $this->level->scheduleDelayedBlockUpdate($this, mt_rand(20, 40)); + } + } + + public function onRandomTick() : void{ + if((!$this->checkAdjacentBlocks(4) or mt_rand(0, 2) === 0) and + max( //TODO: move this to Level + $this->level->getHighestAdjacentBlockLight($this->x, $this->y, $this->z), + $this->level->getHighestAdjacentBlockSkyLight($this->x, $this->y, $this->z) - $this->level->getSkyLightReduction() + ) >= 12 - $this->age){ + if($this->tryMelt()){ + foreach($this->getAllSides() as $block){ + if($block instanceof FrostedIce){ + $block->tryMelt(); + } + } + } + }else{ + $this->level->scheduleDelayedBlockUpdate($this, mt_rand(20, 40)); + } + } + + public function onScheduledUpdate() : void{ + $this->onRandomTick(); + } + + private function checkAdjacentBlocks(int $requirement) : bool{ + $found = 0; + for($x = -1; $x <= 1; ++$x){ + for($z = -1; $z <= 1; ++$z){ + if($x === 0 and $z === 0){ + continue; + } + if( + $this->level->getBlockAt($this->x + $x, $this->y, $this->z + $z) instanceof FrostedIce and + ++$found >= $requirement + ){ + return true; + } + } + } + return false; + } + + /** + * Updates the age of the ice, destroying it if appropriate. + * + * @return bool Whether the ice was destroyed. + */ + private function tryMelt() : bool{ + if($this->age >= 3){ + $this->level->useBreakOn($this); + return true; + } + + $this->age++; + $this->level->setBlock($this, $this); + $this->level->scheduleDelayedBlockUpdate($this, mt_rand(20, 40)); + return false; + } +}