Skip to content

Commit

Permalink
Implement snowlogging
Browse files Browse the repository at this point in the history
  • Loading branch information
DavyCraft648 committed Mar 2, 2024
1 parent 18654c3 commit a00fc74
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 7 deletions.
10 changes: 9 additions & 1 deletion src/block/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -1030,7 +1030,7 @@ public function setLayer(int $layer) : static{
}

public function isLayerSupported(int $layer) : bool{
return $layer === 0;
return $layer === 0 || ($this->canBeSnowlogged() && $layer === 1);
}

/**
Expand Down Expand Up @@ -1058,4 +1058,12 @@ public function canWaterlogged(Liquid $water) : bool{
public function isWaterlogged() : bool{
return $this->getBlockLayer(1) instanceof Water;
}

public function canBeSnowlogged() : bool{
return false;
}

public function isSnowlogged() : bool{
return $this instanceof SnowLayer && $this->getBlockLayer(1)->canBeSnowlogged();
}
}
4 changes: 4 additions & 0 deletions src/block/Flower.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@ public function getFlameEncouragement() : int{
public function getFlammability() : int{
return 100;
}

public function canBeSnowlogged() : bool{
return true;
}
}
4 changes: 4 additions & 0 deletions src/block/RedMushroom.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,8 @@ public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Blo

return false;
}

public function canBeSnowlogged() : bool{
return true;
}
}
12 changes: 7 additions & 5 deletions src/block/SnowLayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,13 @@ public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Blo
$this->layers = $blockReplace->layers + 1;
}
if($this->canBeSupportedAt($blockReplace)){
return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player);
parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player);
if($blockReplace->canBeSnowlogged()){
$tx->addBlock($blockReplace->position, $blockReplace->setLayer(1));
}elseif($blockReplace->isSnowlogged()){
$tx->addBlock($blockReplace->position, $blockReplace->getBlockLayer(1));
}
return true;
}

return false;
Expand All @@ -114,8 +120,4 @@ public function getDropsForCompatibleTool(Item $item) : array{
VanillaItems::SNOWBALL()->setCount(max(1, (int) floor($this->layers / 2)))
];
}

public function isLayerSupported(int $layer) : bool{
return parent::isLayerSupported($layer) || $layer === 1;
}
}
4 changes: 4 additions & 0 deletions src/block/TallGrass.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,8 @@ private function canBeSupportedAt(Block $block) : bool{
$supportBlock = $block->getSide(Facing::DOWN);
return $supportBlock->hasTypeTag(BlockTypeTags::DIRT) || $supportBlock->hasTypeTag(BlockTypeTags::MUD);
}

public function canBeSnowlogged() : bool{
return true;
}
}
19 changes: 18 additions & 1 deletion src/entity/object/FallingBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use pocketmine\block\Block;
use pocketmine\block\RuntimeBlockStateRegistry;
use pocketmine\block\SnowLayer;
use pocketmine\block\utils\Fallable;
use pocketmine\block\Water;
use pocketmine\data\bedrock\block\BlockStateDeserializeException;
Expand Down Expand Up @@ -143,14 +144,30 @@ protected function entityBaseTick(int $tickDiff = 1) : bool{

$blockResult = $blockTarget ?? $this->block;
$block = $world->getBlock($pos);
if(!$block->canBeReplaced() || !$world->isInWorld($pos->getFloorX(), $pos->getFloorY(), $pos->getFloorZ()) || ($this->onGround && abs($this->location->y - $this->location->getFloorY()) > 0.001)){
$down = $world->getBlock($pos->down());
if((!$block->canBeReplaced() || !$world->isInWorld($pos->getFloorX(), $pos->getFloorY(), $pos->getFloorZ()) || ($this->onGround && abs($this->location->y - $this->location->getFloorY()) > 0.001)) && !(($block instanceof SnowLayer || $down instanceof SnowLayer) && $blockResult instanceof SnowLayer)){
$world->dropItem($this->location, $this->block->asItem());
$world->addSound($pos->add(0.5, 0.5, 0.5), new BlockBreakSound($blockResult));
}else{
$ev = new EntityBlockChangeEvent($this, $block, $blockResult);
$ev->call();
if(!$ev->isCancelled()){
$b = $ev->getTo();
if($b instanceof SnowLayer){
$otherSnow = $down instanceof SnowLayer && $down->getLayers() < SnowLayer::MAX_LAYERS ? $down : $block;
if($block->canBeSnowlogged()){
$world->setBlockLayer($pos, $block, 1);
}elseif($otherSnow instanceof SnowLayer){
$layers = $otherSnow->getLayers() + $b->getLayers();
$pos = $otherSnow->getPosition();
if($layers > SnowLayer::MAX_LAYERS){
$world->setBlock($pos->up(), $b->setLayers($layers - SnowLayer::MAX_LAYERS));
$b->setLayers(SnowLayer::MAX_LAYERS);
}else{
$world->setBlock($pos, $b->setLayers($layers));
}
}
}
$world->setBlock($pos, $b);
if($block instanceof Water && $b->canWaterlogged($block)){
$world->setBlockLayer($pos, $block, 1);
Expand Down

0 comments on commit a00fc74

Please sign in to comment.