mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-09 19:24:12 +00:00
this is a reduced version compared to the original, due to the difficulty of getting rid of Facing values internally.
96 lines
3.0 KiB
PHP
96 lines
3.0 KiB
PHP
<?php
|
|
|
|
/*
|
|
*
|
|
* ____ _ _ __ __ _ __ __ ____
|
|
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
|
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
|
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
|
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* @author PocketMine Team
|
|
* @link http://www.pocketmine.net/
|
|
*
|
|
*
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace pocketmine\block;
|
|
|
|
use pocketmine\block\utils\AnyFacing;
|
|
use pocketmine\block\utils\AnyFacingTrait;
|
|
use pocketmine\data\runtime\RuntimeDataDescriber;
|
|
use pocketmine\item\Item;
|
|
use pocketmine\math\Facing;
|
|
use pocketmine\math\Vector3;
|
|
use pocketmine\player\Player;
|
|
use pocketmine\world\BlockTransaction;
|
|
use pocketmine\world\sound\RedstonePowerOffSound;
|
|
use pocketmine\world\sound\RedstonePowerOnSound;
|
|
|
|
abstract class Button extends Flowable implements AnyFacing{
|
|
use AnyFacingTrait;
|
|
|
|
protected bool $pressed = false;
|
|
|
|
protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
|
|
$w->enum($this->facing);
|
|
$w->bool($this->pressed);
|
|
}
|
|
|
|
public function isPressed() : bool{ return $this->pressed; }
|
|
|
|
/** @return $this */
|
|
public function setPressed(bool $pressed) : self{
|
|
$this->pressed = $pressed;
|
|
return $this;
|
|
}
|
|
|
|
public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, Facing $face, Vector3 $clickVector, ?Player $player = null) : bool{
|
|
if($this->canBeSupportedAt($blockReplace, $face)){
|
|
$this->facing = $face;
|
|
return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
abstract protected function getActivationTime() : int;
|
|
|
|
public function onInteract(Item $item, Facing $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
|
|
if(!$this->pressed){
|
|
$this->pressed = true;
|
|
$world = $this->position->getWorld();
|
|
$world->setBlock($this->position, $this);
|
|
$world->scheduleDelayedBlockUpdate($this->position, $this->getActivationTime());
|
|
$world->addSound($this->position->add(0.5, 0.5, 0.5), new RedstonePowerOnSound());
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public function onScheduledUpdate() : void{
|
|
if($this->pressed){
|
|
$this->pressed = false;
|
|
$world = $this->position->getWorld();
|
|
$world->setBlock($this->position, $this);
|
|
$world->addSound($this->position->add(0.5, 0.5, 0.5), new RedstonePowerOffSound());
|
|
}
|
|
}
|
|
|
|
public function onNearbyBlockChange() : void{
|
|
if(!$this->canBeSupportedAt($this, $this->facing)){
|
|
$this->position->getWorld()->useBreakOn($this->position);
|
|
}
|
|
}
|
|
|
|
private function canBeSupportedAt(Block $block, Facing $face) : bool{
|
|
return $block->getAdjacentSupportType(Facing::opposite($face))->hasCenterSupport();
|
|
}
|
|
}
|