PocketMine-MP 5.16.1 git-585aee9386a787c95e73dd0a05ffca8329606b68
Wall.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\SupportType;
27use pocketmine\block\utils\WallConnectionType;
32
36class Wall extends Transparent{
37
42 protected array $connections = [];
43 protected bool $post = false;
44
45 protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
46 $w->wallConnections($this->connections);
47 $w->bool($this->post);
48 }
49
54 public function getConnections() : array{ return $this->connections; }
55
56 public function getConnection(int $face) : ?WallConnectionType{
57 return $this->connections[$face] ?? null;
58 }
59
65 public function setConnections(array $connections) : self{
66 $this->connections = $connections;
67 return $this;
68 }
69
71 public function setConnection(int $face, ?WallConnectionType $type) : self{
72 if($face !== Facing::NORTH && $face !== Facing::SOUTH && $face !== Facing::WEST && $face !== Facing::EAST){
73 throw new \InvalidArgumentException("Facing can only be north, east, south or west");
74 }
75 if($type !== null){
76 $this->connections[$face] = $type;
77 }else{
78 unset($this->connections[$face]);
79 }
80 return $this;
81 }
82
83 public function isPost() : bool{ return $this->post; }
84
86 public function setPost(bool $post) : self{
87 $this->post = $post;
88 return $this;
89 }
90
91 public function onNearbyBlockChange() : void{
92 if($this->recalculateConnections()){
93 $this->position->getWorld()->setBlock($this->position, $this);
94 }
95 }
96
97 protected function recalculateConnections() : bool{
98 $changed = 0;
99
100 //TODO: implement tall/short connections - right now we only support short as per pre-1.16
101
102 foreach(Facing::HORIZONTAL as $facing){
103 $block = $this->getSide($facing);
104 if($block instanceof static || $block instanceof FenceGate || $block instanceof Thin || $block->getSupportType(Facing::opposite($facing)) === SupportType::FULL){
105 if(!isset($this->connections[$facing])){
106 $this->connections[$facing] = WallConnectionType::SHORT;
107 $changed++;
108 }
109 }elseif(isset($this->connections[$facing])){
110 unset($this->connections[$facing]);
111 $changed++;
112 }
113 }
114
115 $up = $this->getSide(Facing::UP)->getTypeId() !== BlockTypeIds::AIR;
116 if($up !== $this->post){
117 $this->post = $up;
118 $changed++;
119 }
120
121 return $changed > 0;
122 }
123
124 protected function recalculateCollisionBoxes() : array{
125 //walls don't have any special collision boxes like fences do
126
127 $north = isset($this->connections[Facing::NORTH]);
128 $south = isset($this->connections[Facing::SOUTH]);
129 $west = isset($this->connections[Facing::WEST]);
130 $east = isset($this->connections[Facing::EAST]);
131
132 $inset = 0.25;
133 if(
134 !$this->post && //if there is a block on top, it stays as a post
135 (
136 ($north && $south && !$west && !$east) ||
137 (!$north && !$south && $west && $east)
138 )
139 ){
140 //If connected to two sides on the same axis but not any others, AND there is not a block on top, there is no post and the wall is thinner
141 $inset = 0.3125;
142 }
143
144 return [
145 AxisAlignedBB::one()
146 ->extend(Facing::UP, 0.5)
147 ->trim(Facing::NORTH, $north ? 0 : $inset)
148 ->trim(Facing::SOUTH, $south ? 0 : $inset)
149 ->trim(Facing::WEST, $west ? 0 : $inset)
150 ->trim(Facing::EAST, $east ? 0 : $inset)
151 ];
152 }
153
154 public function getSupportType(int $facing) : SupportType{
155 return Facing::axis($facing) === Axis::Y ? SupportType::CENTER : SupportType::NONE;
156 }
157}
describeBlockOnlyState(RuntimeDataDescriber $w)
Definition: Wall.php:45
getSupportType(int $facing)
Definition: Wall.php:154
setConnections(array $connections)
Definition: Wall.php:65
setPost(bool $post)
Definition: Wall.php:86
setConnection(int $face, ?WallConnectionType $type)
Definition: Wall.php:71