mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-08 04:38:35 +00:00
Clean up ChestInventory handling
longer term I want to rip this crap out completely, but right now this provides minor maintainability benefits, while also making it slightly less nasty to deal with other containers which animate their blocks, such as barrels and shulker boxes.
This commit is contained in:
parent
b534ae050e
commit
1cf3a500f8
56
src/block/inventory/AnimatedBlockInventory.php
Normal file
56
src/block/inventory/AnimatedBlockInventory.php
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
<?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\inventory;
|
||||||
|
|
||||||
|
use pocketmine\player\Player;
|
||||||
|
use pocketmine\world\sound\Sound;
|
||||||
|
use function count;
|
||||||
|
|
||||||
|
abstract class AnimatedBlockInventory extends BlockInventory{
|
||||||
|
|
||||||
|
abstract protected function getOpenSound() : Sound;
|
||||||
|
|
||||||
|
abstract protected function getCloseSound() : Sound;
|
||||||
|
|
||||||
|
public function onOpen(Player $who) : void{
|
||||||
|
parent::onOpen($who);
|
||||||
|
|
||||||
|
if(count($this->getViewers()) === 1 and $this->getHolder()->isValid()){
|
||||||
|
//TODO: this crap really shouldn't be managed by the inventory
|
||||||
|
$this->animateBlock(true);
|
||||||
|
$this->getHolder()->getWorld()->addSound($this->getHolder()->add(0.5, 0.5, 0.5), $this->getOpenSound());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract protected function animateBlock(bool $isOpen) : void;
|
||||||
|
|
||||||
|
public function onClose(Player $who) : void{
|
||||||
|
if(count($this->getViewers()) === 1 and $this->getHolder()->isValid()){
|
||||||
|
//TODO: this crap really shouldn't be managed by the inventory
|
||||||
|
$this->animateBlock(false);
|
||||||
|
$this->getHolder()->getWorld()->addSound($this->getHolder()->add(0.5, 0.5, 0.5), $this->getCloseSound());
|
||||||
|
}
|
||||||
|
parent::onClose($who);
|
||||||
|
}
|
||||||
|
}
|
@ -24,14 +24,12 @@ declare(strict_types=1);
|
|||||||
namespace pocketmine\block\inventory;
|
namespace pocketmine\block\inventory;
|
||||||
|
|
||||||
use pocketmine\network\mcpe\protocol\BlockEventPacket;
|
use pocketmine\network\mcpe\protocol\BlockEventPacket;
|
||||||
use pocketmine\player\Player;
|
|
||||||
use pocketmine\world\Position;
|
use pocketmine\world\Position;
|
||||||
use pocketmine\world\sound\ChestCloseSound;
|
use pocketmine\world\sound\ChestCloseSound;
|
||||||
use pocketmine\world\sound\ChestOpenSound;
|
use pocketmine\world\sound\ChestOpenSound;
|
||||||
use pocketmine\world\sound\Sound;
|
use pocketmine\world\sound\Sound;
|
||||||
use function count;
|
|
||||||
|
|
||||||
class ChestInventory extends BlockInventory{
|
class ChestInventory extends AnimatedBlockInventory{
|
||||||
|
|
||||||
public function __construct(Position $holder){
|
public function __construct(Position $holder){
|
||||||
parent::__construct($holder, 27);
|
parent::__construct($holder, 27);
|
||||||
@ -45,26 +43,7 @@ class ChestInventory extends BlockInventory{
|
|||||||
return new ChestCloseSound();
|
return new ChestCloseSound();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onOpen(Player $who) : void{
|
protected function animateBlock(bool $isOpen) : void{
|
||||||
parent::onOpen($who);
|
|
||||||
|
|
||||||
if(count($this->getViewers()) === 1 and $this->getHolder()->isValid()){
|
|
||||||
//TODO: this crap really shouldn't be managed by the inventory
|
|
||||||
$this->broadcastBlockEventPacket(true);
|
|
||||||
$this->getHolder()->getWorld()->addSound($this->getHolder()->add(0.5, 0.5, 0.5), $this->getOpenSound());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function onClose(Player $who) : void{
|
|
||||||
if(count($this->getViewers()) === 1 and $this->getHolder()->isValid()){
|
|
||||||
//TODO: this crap really shouldn't be managed by the inventory
|
|
||||||
$this->broadcastBlockEventPacket(false);
|
|
||||||
$this->getHolder()->getWorld()->addSound($this->getHolder()->add(0.5, 0.5, 0.5), $this->getCloseSound());
|
|
||||||
}
|
|
||||||
parent::onClose($who);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function broadcastBlockEventPacket(bool $isOpen) : void{
|
|
||||||
$holder = $this->getHolder();
|
$holder = $this->getHolder();
|
||||||
|
|
||||||
//event ID is always 1 for a chest
|
//event ID is always 1 for a chest
|
||||||
|
@ -26,9 +26,7 @@ namespace pocketmine\block\inventory;
|
|||||||
use pocketmine\inventory\BaseInventory;
|
use pocketmine\inventory\BaseInventory;
|
||||||
use pocketmine\inventory\InventoryHolder;
|
use pocketmine\inventory\InventoryHolder;
|
||||||
use pocketmine\item\Item;
|
use pocketmine\item\Item;
|
||||||
use pocketmine\player\Player;
|
|
||||||
use pocketmine\world\Position;
|
use pocketmine\world\Position;
|
||||||
use function count;
|
|
||||||
|
|
||||||
class DoubleChestInventory extends ChestInventory implements InventoryHolder{
|
class DoubleChestInventory extends ChestInventory implements InventoryHolder{
|
||||||
/** @var ChestInventory */
|
/** @var ChestInventory */
|
||||||
@ -74,19 +72,9 @@ class DoubleChestInventory extends ChestInventory implements InventoryHolder{
|
|||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onOpen(Player $who) : void{
|
protected function animateBlock(bool $isOpen) : void{
|
||||||
parent::onOpen($who);
|
$this->left->animateBlock($isOpen);
|
||||||
|
$this->right->animateBlock($isOpen);
|
||||||
if(count($this->getViewers()) === 1 and $this->right->getHolder()->isValid()){
|
|
||||||
$this->right->broadcastBlockEventPacket(true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public function onClose(Player $who) : void{
|
|
||||||
if(count($this->getViewers()) === 1 and $this->right->getHolder()->isValid()){
|
|
||||||
$this->right->broadcastBlockEventPacket(false);
|
|
||||||
}
|
|
||||||
parent::onClose($who);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getLeftSide() : ChestInventory{
|
public function getLeftSide() : ChestInventory{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user