PocketMine-MP 5.21.2 git-b2aa6396c3cc2cafdd815eacc360e1ad89599899
Loading...
Searching...
No Matches
Candle.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\CandleTrait;
27use pocketmine\block\utils\SupportType;
37
38class Candle extends Transparent{
39 use CandleTrait {
40 describeBlockOnlyState as encodeLitState;
41 getLightLevel as getBaseLightLevel;
42 }
43
44 public const MIN_COUNT = 1;
45 public const MAX_COUNT = 4;
46
47 private int $count = self::MIN_COUNT;
48
49 protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
50 $this->encodeLitState($w);
51 $w->boundedIntAuto(self::MIN_COUNT, self::MAX_COUNT, $this->count);
52 }
53
54 public function getCount() : int{ return $this->count; }
55
57 public function setCount(int $count) : self{
58 if($count < self::MIN_COUNT || $count > self::MAX_COUNT){
59 throw new \InvalidArgumentException("Count must be in range " . self::MIN_COUNT . " ... " . self::MAX_COUNT);
60 }
61 $this->count = $count;
62 return $this;
63 }
64
65 public function getLightLevel() : int{
66 return $this->getBaseLightLevel() * $this->count;
67 }
68
69 protected function recalculateCollisionBoxes() : array{
70 return [
71 (match($this->count){
72 1 => AxisAlignedBB::one()
73 ->squash(Axis::X, 7 / 16)
74 ->squash(Axis::Z, 7 / 16),
75 2 => AxisAlignedBB::one()
76 ->squash(Axis::X, 5 / 16)
77 ->trim(Facing::NORTH, 7 / 16) //0.3 thick on the Z axis
78 ->trim(Facing::SOUTH, 6 / 16),
79 3 => AxisAlignedBB::one()
80 ->trim(Facing::WEST, 5 / 16)
81 ->trim(Facing::EAST, 6 / 16)
82 ->trim(Facing::NORTH, 6 / 16)
83 ->trim(Facing::SOUTH, 5 / 16),
84 4 => AxisAlignedBB::one()
85 ->squash(Axis::X, 5 / 16)
86 ->trim(Facing::NORTH, 5 / 16)
87 ->trim(Facing::SOUTH, 6 / 16),
88 default => throw new AssumptionFailedError("Unreachable")
89 })->trim(Facing::UP, 10 / 16)
90 ];
91 }
92
93 public function getSupportType(int $facing) : SupportType{
94 return SupportType::NONE;
95 }
96
97 protected function getCandleIfCompatibleType(Block $block) : ?Candle{
98 return $block instanceof Candle && $block->hasSameTypeId($this) ? $block : null;
99 }
100
101 public function canBePlacedAt(Block $blockReplace, Vector3 $clickVector, int $face, bool $isClickedBlock) : bool{
102 $candle = $this->getCandleIfCompatibleType($blockReplace);
103 return $candle !== null ? $candle->count < self::MAX_COUNT : parent::canBePlacedAt($blockReplace, $clickVector, $face, $isClickedBlock);
104 }
105
106 public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{
107 if(!$blockReplace->getAdjacentSupportType(Facing::DOWN)->hasCenterSupport()){
108 return false;
109 }
110 $existing = $this->getCandleIfCompatibleType($blockReplace);
111 if($existing !== null){
112 if($existing->count >= self::MAX_COUNT){
113 return false;
114 }
115
116 $this->count = $existing->count + 1;
117 $this->lit = $existing->lit;
118 }
119 return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player);
120 }
121
122 public function getDropsForCompatibleTool(Item $item) : array{
123 return [$this->asItem()->setCount($this->count)];
124 }
125}
getDropsForCompatibleTool(Item $item)
Definition Candle.php:122
setCount(int $count)
Definition Candle.php:57
describeBlockOnlyState(RuntimeDataDescriber $w)
Definition Candle.php:49
canBePlacedAt(Block $blockReplace, Vector3 $clickVector, int $face, bool $isClickedBlock)
Definition Candle.php:101
getSupportType(int $facing)
Definition Candle.php:93
place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player=null)
Definition Candle.php:106
boundedIntAuto(int $min, int $max, int &$value)