PocketMine-MP 5.21.2 git-a6534ecbbbcf369264567d27e5ed70f7f5be9816
Loading...
Searching...
No Matches
MultiAnySupportTrait.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\utils;
25
32use function array_key_first;
33use function count;
34
39trait MultiAnySupportTrait{
40 use MultiAnyFacingTrait;
41
47 abstract protected function getInitialPlaceFaces(Block $blockReplace) : array;
48
49 public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{
50 $this->faces = $this->getInitialPlaceFaces($blockReplace);
51 $availableFaces = $this->getAvailableFaces();
52
53 if(count($availableFaces) === 0){
54 return false;
55 }
56
57 $opposite = Facing::opposite($face);
58 $placedFace = isset($availableFaces[$opposite]) ? $opposite : array_key_first($availableFaces);
59 $this->faces[$placedFace] = $placedFace;
60
61 return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player);
62 }
63
64 public function onNearbyBlockChange() : void{
65 $changed = false;
66
67 foreach($this->faces as $face){
68 if($this->getAdjacentSupportType($face) !== SupportType::FULL){
69 unset($this->faces[$face]);
70 $changed = true;
71 }
72 }
73
74 if($changed){
75 $world = $this->position->getWorld();
76 if(count($this->faces) === 0){
77 $world->useBreakOn($this->position);
78 }else{
79 $world->setBlock($this->position, $this);
80 }
81 }
82 }
83
87 private function getAvailableFaces() : array{
88 $faces = [];
89 foreach(Facing::ALL as $face){
90 if(!$this->hasFace($face) && $this->getAdjacentSupportType($face) === SupportType::FULL){
91 $faces[$face] = $face;
92 }
93 }
94 return $faces;
95 }
96}