Flatten double slabs into Slab pseudo-variant

This commit is contained in:
Dylan K. Taylor 2019-02-19 11:14:06 +00:00
parent 18f765338c
commit f351a86653
4 changed files with 116 additions and 102 deletions

View File

@ -26,6 +26,7 @@ namespace pocketmine\block;
use pocketmine\block\utils\DyeColor;
use pocketmine\block\utils\InvalidBlockStateException;
use pocketmine\block\utils\PillarRotationTrait;
use pocketmine\block\utils\SlabType;
use pocketmine\block\utils\TreeType;
use pocketmine\item\Item;
use pocketmine\level\Position;
@ -374,7 +375,7 @@ class BlockFactory{
}
foreach($slabTypes as $type){
self::registerBlock($type);
self::registerBlock(new DoubleSlab($type->getDoubleSlabId(), $type->getId(), $type->getVariant()));
self::registerBlock((clone $type)->setSlabType(SlabType::double())); //flattening hack
}
static $wallTypes = [

View File

@ -1,79 +0,0 @@
<?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\Item;
use pocketmine\item\ItemFactory;
class DoubleSlab extends Solid{
/** @var int */
protected $singleId;
public function __construct(int $id, int $singleId, int $variant = 0){
parent::__construct($id, $variant);
$this->singleId = $singleId;
}
protected function getSingle() : Block{
return BlockFactory::get($this->singleId, $this->variant);
}
public function getHardness() : float{
return $this->getSingle()->getHardness();
}
public function getToolType() : int{
return $this->getSingle()->getToolType();
}
public function getToolHarvestLevel() : int{
return $this->getSingle()->getToolHarvestLevel();
}
public function getFlameEncouragement() : int{
return $this->getSingle()->getFlameEncouragement();
}
public function getFlammability() : int{
return $this->getSingle()->getFlammability();
}
public function getName() : string{
return "Double " . $this->getSingle()->getName();
}
public function getDropsForCompatibleTool(Item $item) : array{
return [
ItemFactory::get($this->singleId, $this->variant, 2)
];
}
public function isAffectedBySilkTouch() : bool{
return false;
}
public function getPickedItem() : Item{
return ItemFactory::get($this->singleId, $this->getVariant());
}
}

View File

@ -23,6 +23,7 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\utils\SlabType;
use pocketmine\item\Item;
use pocketmine\math\AxisAlignedBB;
use pocketmine\math\Facing;
@ -33,32 +34,57 @@ abstract class Slab extends Transparent{
/** @var int */
protected $doubleId;
/** @var bool */
protected $top = false;
/** @var SlabType */
protected $slabType;
public function __construct(int $id, int $doubleId, int $variant = 0, ?string $name = null){
parent::__construct($id, $variant, $name . " Slab");
parent::__construct($id, $variant, $name . " Slab", $id);
$this->doubleId = $doubleId;
$this->slabType = SlabType::bottom();
}
public function getId() : int{
return $this->slabType === SlabType::double() ? $this->doubleId : parent::getId();
}
protected function writeStateToMeta() : int{
return ($this->top ? 0x08 : 0);
if($this->slabType !== SlabType::double()){
return ($this->slabType === SlabType::top() ? 0x08 : 0);
}
return 0;
}
public function readStateFromMeta(int $meta) : void{
$this->top = ($meta & 0x08) !== 0;
if($this->slabType !== SlabType::double()){
$this->slabType = ($meta & 0x08) !== 0 ? SlabType::top() : SlabType::bottom();
}
}
public function getStateBitmask() : int{
return 0b1000;
}
public function getDoubleSlabId() : int{
return $this->doubleId;
public function isTransparent() : bool{
return $this->slabType !== SlabType::double();
}
protected function getDouble() : Block{
return BlockFactory::get($this->doubleId, $this->variant);
/**
* Returns the type of slab block.
*
* @return SlabType
*/
public function getSlabType() : SlabType{
return $this->slabType;
}
/**
* @param SlabType $slabType
*
* @return $this
*/
public function setSlabType(SlabType $slabType) : self{
$this->slabType = $slabType;
return $this;
}
public function canBePlacedAt(Block $blockReplace, Vector3 $clickVector, int $face, bool $isClickedBlock) : bool{
@ -66,8 +92,8 @@ abstract class Slab extends Transparent{
return true;
}
if($blockReplace instanceof Slab and $blockReplace->isSameType($this)){
if($blockReplace->top){ //Trying to combine with top slab
if($blockReplace instanceof Slab and $blockReplace->slabType !== SlabType::double() and $blockReplace->isSameType($this)){
if($blockReplace->slabType === SlabType::top()){ //Trying to combine with top slab
return $clickVector->y <= 0.5 or (!$isClickedBlock and $face === Facing::UP);
}else{
return $clickVector->y >= 0.5 or (!$isClickedBlock and $face === Facing::DOWN);
@ -80,27 +106,35 @@ abstract class Slab extends Transparent{
public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, Player $player = null) : bool{
/* note these conditions can't be merged, since one targets clicked and the other replace */
if($blockClicked instanceof Slab and $blockClicked->isSameType($this) and (
($face === Facing::DOWN and $blockClicked->top) or //Bottom face of top slab
($face === Facing::UP and !$blockClicked->top) //Top face of bottom slab
if($blockClicked instanceof Slab and $blockClicked->slabType !== SlabType::double() and $blockClicked->isSameType($this) and (
($face === Facing::DOWN and $blockClicked->slabType === SlabType::top()) or
($face === Facing::UP and $blockClicked->slabType === SlabType::bottom())
)){
return $this->level->setBlock($blockClicked, $this->getDouble());
$this->slabType = SlabType::double();
return $this->level->setBlock($blockClicked, $this);
}
if($blockReplace instanceof Slab and $blockReplace->isSameType($this) and (
($blockReplace->top and ($clickVector->y <= 0.5 or $face === Facing::UP)) or
(!$blockReplace->top and ($clickVector->y >= 0.5 or $face === Facing::DOWN))
if($blockReplace instanceof Slab and $blockReplace->slabType !== SlabType::double() and $blockReplace->isSameType($this) and (
($blockReplace->slabType === SlabType::top() and ($clickVector->y <= 0.5 or $face === Facing::UP)) or
($blockReplace->slabType === SlabType::bottom() and ($clickVector->y >= 0.5 or $face === Facing::DOWN))
)){
//Clicked in empty half of existing slab
return $this->level->setBlock($blockReplace, $this->getDouble());
$this->slabType = SlabType::double();
}else{
$this->slabType = (($face !== Facing::UP && $clickVector->y > 0.5) || $face === Facing::DOWN) ? SlabType::top() : SlabType::bottom();
}
$this->top = ($face !== Facing::UP && $clickVector->y > 0.5) || $face === Facing::DOWN;
return parent::place($item, $blockReplace, $blockClicked, $face, $clickVector, $player);
}
protected function recalculateBoundingBox() : ?AxisAlignedBB{
return AxisAlignedBB::one()->trim($this->top ? Facing::DOWN : Facing::UP, 0.5);
if($this->slabType === SlabType::double()){
return parent::recalculateBoundingBox();
}
return AxisAlignedBB::one()->trim($this->slabType === SlabType::top() ? Facing::DOWN : Facing::UP, 0.5);
}
public function getDropsForCompatibleTool(Item $item) : array{
return [$this->getItem()->setCount($this->slabType === SlabType::double() ? 2 : 1)];
}
}

View File

@ -0,0 +1,58 @@
<?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\utils;
final class SlabType{
/**
* @var string
*/
private $name;
public static function bottom() : self{
/** @var SlabType $ret */
static $ret = null;
return $ret ?? ($ret = new self("bottom"));
}
public static function top() : self{
/** @var SlabType $ret */
static $ret = null;
return $ret ?? ($ret = new self("top"));
}
public static function double() : self{
/** @var SlabType $ret */
static $ret = null;
return $ret ?? ($ret = new self("double"));
}
private function __construct(string $name){
$this->name = $name;
}
public function getName() : string{
return $this->name;
}
}