Refactor Slab logic away from WoodenSlab, added StoneSlab2 and its double

This commit is contained in:
Dylan K. Taylor 2017-10-18 19:27:54 +01:00
parent f148c366f9
commit d2dc49cd9c
9 changed files with 268 additions and 147 deletions

View File

@ -250,8 +250,8 @@ class BlockFactory{
//TODO: DAYLIGHT_DETECTOR_INVERTED
//TODO: RED_SANDSTONE
//TODO: RED_SANDSTONE_STAIRS
//TODO: DOUBLE_STONE_SLAB2
//TODO: STONE_SLAB2
self::registerBlock(new DoubleStoneSlab2());
self::registerBlock(new StoneSlab2());
self::registerBlock(new FenceGate(Block::SPRUCE_FENCE_GATE, 0, "Spruce Fence Gate"));
self::registerBlock(new FenceGate(Block::BIRCH_FENCE_GATE, 0, "Birch Fence Gate"));
self::registerBlock(new FenceGate(Block::JUNGLE_FENCE_GATE, 0, "Jungle Fence Gate"));

View File

@ -0,0 +1,47 @@
<?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;
abstract class DoubleSlab extends Solid{
public function __construct(int $meta = 0){
$this->meta = $meta;
}
abstract public function getSlabId() : int;
public function getName() : string{
return "Double " . BlockFactory::get($this->getSlabId(), $this->getVariant())->getName() . " Slab";
}
public function getDrops(Item $item) : array{
return [
ItemFactory::get($this->getSlabId(), $this->getVariant(), 2)
];
}
}

View File

@ -24,15 +24,14 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\item\Item;
use pocketmine\item\ItemFactory;
use pocketmine\item\Tool;
class DoubleStoneSlab extends Solid{
class DoubleStoneSlab extends DoubleSlab{
protected $id = self::DOUBLE_STONE_SLAB;
public function __construct(int $meta = 0){
$this->meta = $meta;
public function getSlabId() : int{
return self::STONE_SLAB;
}
public function getHardness() : float{
@ -43,25 +42,9 @@ class DoubleStoneSlab extends Solid{
return Tool::TYPE_PICKAXE;
}
public function getName() : string{
static $names = [
0 => "Stone",
1 => "Sandstone",
2 => "Wooden",
3 => "Cobblestone",
4 => "Brick",
5 => "Stone Brick",
6 => "Quartz",
7 => "Nether Brick"
];
return "Double " . $names[$this->getVariant()] . " Slab";
}
public function getDrops(Item $item) : array{
if($item->isPickaxe() >= Tool::TIER_WOODEN){
return [
ItemFactory::get(Item::STONE_SLAB, $this->getVariant(), 2)
];
return parent::getDrops($item);
}
return [];

View File

@ -0,0 +1,34 @@
<?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;
class DoubleStoneSlab2 extends DoubleStoneSlab{
protected $id = self::DOUBLE_STONE_SLAB2;
public function getSlabId() : int{
return self::STONE_SLAB2;
}
}

View File

@ -23,16 +23,14 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\item\Item;
use pocketmine\item\ItemFactory;
use pocketmine\item\Tool;
class DoubleWoodenSlab extends Solid{
class DoubleWoodenSlab extends DoubleSlab{
protected $id = self::DOUBLE_WOODEN_SLAB;
public function __construct(int $meta = 0){
$this->meta = $meta;
public function getSlabId() : int{
return self::WOODEN_SLAB;
}
public function getHardness() : float{
@ -42,23 +40,4 @@ class DoubleWoodenSlab extends Solid{
public function getToolType() : int{
return Tool::TYPE_AXE;
}
public function getName() : string{
static $names = [
0 => "Oak",
1 => "Spruce",
2 => "Birch",
3 => "Jungle",
4 => "Acacia",
5 => "Dark Oak"
];
return "Double " . ($names[$this->getVariant()] ?? "") . " Wooden Slab";
}
public function getDrops(Item $item) : array{
return [
ItemFactory::get(Item::WOODEN_SLAB, $this->getVariant(), 2)
];
}
}

View File

@ -0,0 +1,125 @@
<?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\math\AxisAlignedBB;
use pocketmine\math\Vector3;
use pocketmine\Player;
abstract class Slab extends Transparent{
public function __construct(int $meta = 0){
$this->meta = $meta;
}
abstract public function getDoubleSlabId() : int;
public function canBePlacedAt(Block $blockReplace, Vector3 $clickVector) : bool{
return parent::canBePlacedAt($blockReplace, $clickVector) or
(
$blockReplace->getId() === $this->getId() and
$blockReplace->getVariant() === $this->getVariant() and
(
(($blockReplace->getDamage() & 0x08) !== 0 and ($clickVector->y <= 0.5 or $clickVector->y === 1.0)) or //top slab, fill bottom half
(($blockReplace->getDamage() & 0x08) === 0 and ($clickVector->y >= 0.5 or $clickVector->y === 0.0)) //bottom slab, fill top half
)
);
}
public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $facePos, Player $player = null) : bool{
$this->meta &= 0x07;
if($face === Vector3::SIDE_DOWN){
if($blockClicked->getId() === $this->id and ($blockClicked->getDamage() & 0x08) === 0x08 and $blockClicked->getVariant() === $this->getVariant()){
$this->getLevel()->setBlock($blockClicked, BlockFactory::get($this->getDoubleSlabId(), $this->getVariant()), true);
return true;
}elseif($blockReplace->getId() === $this->id and $blockReplace->getVariant() === $this->getVariant()){
$this->getLevel()->setBlock($blockReplace, BlockFactory::get($this->getDoubleSlabId(), $this->getVariant()), true);
return true;
}else{
$this->meta |= 0x08;
}
}elseif($face === Vector3::SIDE_UP){
if($blockClicked->getId() === $this->id and ($blockClicked->getDamage() & 0x08) === 0 and $blockClicked->getVariant() === $this->getVariant()){
$this->getLevel()->setBlock($blockClicked, BlockFactory::get($this->getDoubleSlabId(), $this->getVariant()), true);
return true;
}elseif($blockReplace->getId() === $this->id and $blockReplace->getVariant() === $this->getVariant()){
$this->getLevel()->setBlock($blockReplace, BlockFactory::get($this->getDoubleSlabId(), $this->getVariant()), true);
return true;
}
}else{ //TODO: collision
if($blockReplace->getId() === $this->id){
if($blockReplace->getVariant() === $this->meta){
$this->getLevel()->setBlock($blockReplace, BlockFactory::get($this->getDoubleSlabId(), $this->getVariant()), true);
return true;
}
return false;
}else{
if($facePos->y > 0.5){
$this->meta |= 0x08;
}
}
}
if($blockReplace->getId() === $this->id and $blockClicked->getVariant() !== $this->getVariant()){
return false;
}
$this->getLevel()->setBlock($blockReplace, $this, true, true);
return true;
}
public function getVariantBitmask() : int{
return 0x07;
}
protected function recalculateBoundingBox() : ?AxisAlignedBB{
if(($this->meta & 0x08) > 0){
return new AxisAlignedBB(
$this->x,
$this->y + 0.5,
$this->z,
$this->x + 1,
$this->y + 1,
$this->z + 1
);
}else{
return new AxisAlignedBB(
$this->x,
$this->y,
$this->z,
$this->x + 1,
$this->y + 0.5,
$this->z + 1
);
}
}
}

View File

@ -26,7 +26,7 @@ namespace pocketmine\block;
use pocketmine\item\Item;
use pocketmine\item\Tool;
class StoneSlab extends WoodenSlab{
class StoneSlab extends Slab{
const STONE = 0;
const SANDSTONE = 1;
const WOODEN = 2;
@ -38,7 +38,9 @@ class StoneSlab extends WoodenSlab{
protected $id = self::STONE_SLAB;
protected $doubleId = self::DOUBLE_STONE_SLAB;
public function getDoubleSlabId() : int{
return self::DOUBLE_STONE_SLAB;
}
public function getHardness() : float{
return 2;
@ -69,8 +71,4 @@ class StoneSlab extends WoodenSlab{
return [];
}
public function getFuelTime() : int{
return 0;
}
}

View File

@ -0,0 +1,44 @@
<?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;
class StoneSlab2 extends StoneSlab{
const TYPE_RED_SANDSTONE = 0;
const TYPE_PURPUR = 1;
protected $id = self::STONE_SLAB2;
public function getDoubleSlabId() : int{
return self::DOUBLE_STONE_SLAB2;
}
public function getName() : string{
static $names = [
self::TYPE_RED_SANDSTONE => "Red Sandstone",
self::TYPE_PURPUR => "Purpur"
];
return (($this->meta & 0x08) > 0 ? "Upper " : "") . ($names[$this->getVariant()] ?? "") . " Slab";
}
}

View File

@ -23,22 +23,20 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\item\Item;
use pocketmine\item\Tool;
use pocketmine\math\AxisAlignedBB;
use pocketmine\math\Vector3;
use pocketmine\Player;
class WoodenSlab extends Transparent{
class WoodenSlab extends Slab{
protected $id = self::WOODEN_SLAB;
protected $doubleId = self::DOUBLE_WOODEN_SLAB;
public function __construct(int $meta = 0){
$this->meta = $meta;
}
public function getDoubleSlabId() : int{
return self::DOUBLE_WOODEN_SLAB;
}
public function getHardness() : float{
return 2;
}
@ -55,97 +53,10 @@ class WoodenSlab extends Transparent{
return (($this->meta & 0x08) === 0x08 ? "Upper " : "") . ($names[$this->getVariant()] ?? "") . " Wooden Slab";
}
protected function recalculateBoundingBox() : ?AxisAlignedBB{
if(($this->meta & 0x08) > 0){
return new AxisAlignedBB(
$this->x,
$this->y + 0.5,
$this->z,
$this->x + 1,
$this->y + 1,
$this->z + 1
);
}else{
return new AxisAlignedBB(
$this->x,
$this->y,
$this->z,
$this->x + 1,
$this->y + 0.5,
$this->z + 1
);
}
}
public function canBePlacedAt(Block $blockReplace, Vector3 $clickVector) : bool{
return parent::canBePlacedAt($blockReplace, $clickVector) or
(
$blockReplace->getId() === $this->getId() and
$blockReplace->getVariant() === $this->getVariant() and
(
(($blockReplace->getDamage() & 0x08) !== 0 and ($clickVector->y <= 0.5 or $clickVector->y === 1.0)) or //top slab, fill bottom half
(($blockReplace->getDamage() & 0x08) === 0 and ($clickVector->y >= 0.5 or $clickVector->y === 0.0)) //bottom slab, fill top half
)
);
}
public function place(Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $facePos, Player $player = null) : bool{
$this->meta &= 0x07;
if($face === Vector3::SIDE_DOWN){
if($blockClicked->getId() === $this->id and ($blockClicked->getDamage() & 0x08) === 0x08 and $blockClicked->getVariant() === $this->getVariant()){
$this->getLevel()->setBlock($blockClicked, BlockFactory::get($this->doubleId, $this->getVariant()), true);
return true;
}elseif($blockReplace->getId() === $this->id and $blockReplace->getVariant() === $this->getVariant()){
$this->getLevel()->setBlock($blockReplace, BlockFactory::get($this->doubleId, $this->getVariant()), true);
return true;
}else{
$this->meta |= 0x08;
}
}elseif($face === Vector3::SIDE_UP){
if($blockClicked->getId() === $this->id and ($blockClicked->getDamage() & 0x08) === 0 and $blockClicked->getVariant() === $this->getVariant()){
$this->getLevel()->setBlock($blockClicked, BlockFactory::get($this->doubleId, $this->getVariant()), true);
return true;
}elseif($blockReplace->getId() === $this->id and $blockReplace->getVariant() === $this->getVariant()){
$this->getLevel()->setBlock($blockReplace, BlockFactory::get($this->doubleId, $this->getVariant()), true);
return true;
}
}else{ //TODO: collision
if($blockReplace->getId() === $this->id){
if($blockReplace->getVariant() === $this->meta){
$this->getLevel()->setBlock($blockReplace, BlockFactory::get($this->doubleId, $this->getVariant()), true);
return true;
}
return false;
}else{
if($facePos->y > 0.5){
$this->meta |= 0x08;
}
}
}
if($blockReplace->getId() === $this->id and $blockClicked->getVariant() !== $this->getVariant()){
return false;
}
$this->getLevel()->setBlock($blockReplace, $this, true, true);
return true;
}
public function getToolType() : int{
return Tool::TYPE_AXE;
}
public function getVariantBitmask() : int{
return 0x07;
}
public function getFuelTime() : int{
return 300;
}