PocketMine-MP 5.19.1 git-5cc1068cd43264d3363295eb8d6901e02f467897
Slab.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\block;
25
26use pocketmine\block\utils\SlabType;
27use pocketmine\block\utils\SupportType;
35
36class Slab extends Transparent{
37 protected SlabType $slabType = SlabType::BOTTOM;
38
39 public function __construct(BlockIdentifier $idInfo, string $name, BlockTypeInfo $typeInfo){
40 parent::__construct($idInfo, $name . " Slab", $typeInfo);
41 }
42
43 protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
44 $w->enum($this->slabType);
45 }
46
47 public function isTransparent() : bool{
48 return $this->slabType !== SlabType::DOUBLE;
49 }
50
54 public function getSlabType() : SlabType{
55 return $this->slabType;
56 }
57
61 public function setSlabType(SlabType $slabType) : self{
62 $this->slabType = $slabType;
63 return $this;
64 }
65
66 public function canBePlacedAt(Block $blockReplace, Vector3 $clickVector, int $face, bool $isClickedBlock) : bool{
67 if(parent::canBePlacedAt($blockReplace, $clickVector, $face, $isClickedBlock)){
68 return true;
69 }
70
71 if($blockReplace instanceof Slab && $blockReplace->slabType !== SlabType::DOUBLE && $blockReplace->hasSameTypeId($this)){
72 if($blockReplace->slabType === SlabType::TOP){ //Trying to combine with top slab
73 return $clickVector->y <= 0.5 || (!$isClickedBlock && $face === Facing::UP);
74 }else{
75 return $clickVector->y >= 0.5 || (!$isClickedBlock && $face === Facing::DOWN);
76 }
77 }
78
79 return false;
80 }
81
82 public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{
83 if($blockReplace instanceof Slab && $blockReplace->slabType !== SlabType::DOUBLE && $blockReplace->hasSameTypeId($this) && (
84 ($blockReplace->slabType === SlabType::TOP && ($clickVector->y <= 0.5 || $face === Facing::UP)) ||
85 ($blockReplace->slabType === SlabType::BOTTOM && ($clickVector->y >= 0.5 || $face === Facing::DOWN))
86 )){
87 //Clicked in empty half of existing slab
88 $this->slabType = SlabType::DOUBLE;
89 }else{
90 $this->slabType = (($face !== Facing::UP && $clickVector->y > 0.5) || $face === Facing::DOWN) ? SlabType::TOP : SlabType::BOTTOM;
91 }
92
93 return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player);
94 }
95
99 protected function recalculateCollisionBoxes() : array{
100 if($this->slabType === SlabType::DOUBLE){
101 return [AxisAlignedBB::one()];
102 }
103 return [AxisAlignedBB::one()->trim($this->slabType === SlabType::TOP ? Facing::DOWN : Facing::UP, 0.5)];
104 }
105
106 public function getSupportType(int $facing) : SupportType{
107 if($this->slabType === SlabType::DOUBLE){
108 return SupportType::FULL;
109 }elseif(($facing === Facing::UP && $this->slabType === SlabType::TOP) || ($facing === Facing::DOWN && $this->slabType === SlabType::BOTTOM)){
110 return SupportType::FULL;
111 }
112 return SupportType::NONE;
113 }
114
115 public function getDropsForCompatibleTool(Item $item) : array{
116 return [$this->asItem()->setCount($this->slabType === SlabType::DOUBLE ? 2 : 1)];
117 }
118}
hasSameTypeId(Block $other)
Definition: Block.php:184
canBePlacedAt(Block $blockReplace, Vector3 $clickVector, int $face, bool $isClickedBlock)
Definition: Slab.php:66
describeBlockOnlyState(RuntimeDataDescriber $w)
Definition: Slab.php:43
getSupportType(int $facing)
Definition: Slab.php:106
recalculateCollisionBoxes()
Definition: Slab.php:99
getDropsForCompatibleTool(Item $item)
Definition: Slab.php:115
place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player=null)
Definition: Slab.php:82
setSlabType(SlabType $slabType)
Definition: Slab.php:61
__construct(BlockIdentifier $idInfo, string $name, BlockTypeInfo $typeInfo)
Definition: Slab.php:39