forked from tedo0627/RedstoneCircuit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlockWoodenPressurePlate.php
111 lines (94 loc) · 4.25 KB
/
BlockWoodenPressurePlate.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
namespace tedo0627\redstonecircuit\block\power;
use pocketmine\block\Block;
use pocketmine\block\utils\SupportType;
use pocketmine\block\WoodenPressurePlate;
use pocketmine\entity\Entity;
use pocketmine\item\Item;
use pocketmine\math\AxisAlignedBB;
use pocketmine\math\Facing;
use pocketmine\player\Player;
use pocketmine\world\sound\RedstonePowerOffSound;
use pocketmine\world\sound\RedstonePowerOnSound;
use tedo0627\redstonecircuit\block\BlockUpdateHelper;
use tedo0627\redstonecircuit\block\ILinkRedstoneWire;
use tedo0627\redstonecircuit\block\IRedstoneComponent;
use tedo0627\redstonecircuit\block\LinkRedstoneWireTrait;
use tedo0627\redstonecircuit\block\RedstoneComponentTrait;
use tedo0627\redstonecircuit\event\BlockRedstonePowerUpdateEvent;
use tedo0627\redstonecircuit\RedstoneCircuit;
class BlockWoodenPressurePlate extends WoodenPressurePlate implements IRedstoneComponent, ILinkRedstoneWire {
use LinkRedstoneWireTrait;
use RedstoneComponentTrait;
public function onBreak(Item $item, ?Player $player = null): bool {
parent::onBreak($item, $player);
BlockUpdateHelper::updateAroundDirectionRedstone($this, Facing::DOWN);
return true;
}
public function onNearbyBlockChange(): void {
if ($this->canBeSupportedBy($this->getSide(Facing::DOWN))) return;
$this->getPosition()->getWorld()->useBreakOn($this->getPosition());
}
public function onScheduledUpdate(): void {
if (!$this->isPressed()) return;
$entities = $this->getPosition()->getWorld()->getNearbyEntities($this->getHitCollision());
if (count($entities) !== 0) {
$this->getPosition()->getWorld()->scheduleDelayedBlockUpdate($this->getPosition(), 20);
return;
}
$pressed = false;
if (RedstoneCircuit::isCallEvent()) {
$event = new BlockRedstonePowerUpdateEvent($this, false, $this->isPressed());
$event->call();
$pressed = $event->getNewPowered();
}
$this->setPressed($pressed);
$this->getPosition()->getWorld()->setBlock($this->getPosition(), $this);
$this->getPosition()->getWorld()->addSound($this->getPosition()->add(0.5, 0.5, 0.5), new RedstonePowerOffSound());
BlockUpdateHelper::updateAroundDirectionRedstone($this, Facing::DOWN);
}
public function onEntityInside(Entity $entity): bool {
if ($entity instanceof Player && $entity->isSpectator()) return true;
$entities = $this->getPosition()->getWorld()->getNearbyEntities($this->getHitCollision());
if (count($entities) <= 0) return true;
if (!$this->isPressed()) {
$pressed = true;
if (RedstoneCircuit::isCallEvent()) {
$event = new BlockRedstonePowerUpdateEvent($this, true, $this->isPressed());
$event->call();
$pressed = $event->getNewPowered();
}
$this->setPressed($pressed);
$this->getPosition()->getWorld()->setBlock($this->getPosition(), $this);
$this->getPosition()->getWorld()->addSound($this->getPosition()->add(0.5, 0.5, 0.5), new RedstonePowerOnSound());
BlockUpdateHelper::updateAroundDirectionRedstone($this, Facing::DOWN);
}
$this->getPosition()->getWorld()->scheduleDelayedBlockUpdate($this->getPosition(), 20);
return true;
}
public function hasEntityCollision(): bool {
return true;
}
protected function getHitCollision(): AxisAlignedBB {
return new AxisAlignedBB(
$this->getPosition()->getX() + 0.0625,
$this->getPosition()->getY(),
$this->getPosition()->getZ() + 0.0625,
$this->getPosition()->getX() + 0.9375,
$this->getPosition()->getY() + 0.0625,
$this->getPosition()->getZ() + 0.9375
);
}
public function getStrongPower(int $face): int {
return $this->isPressed() && $face == Facing::UP ? 15 : 0;
}
public function getWeakPower(int $face): int {
return $this->isPressed() ? 15 : 0;
}
public function isPowerSource(): bool {
return $this->isPressed();
}
private function canBeSupportedBy(Block $block): bool {
return !$block->getSupportType(Facing::UP)->equals(SupportType::NONE());
}
}