Added a BrewingStandSlot enum

This commit is contained in:
Dylan K. Taylor 2020-09-26 17:20:02 +01:00
parent e192c75019
commit ec7c5fd669
3 changed files with 207 additions and 12 deletions

View File

@ -24,40 +24,84 @@ declare(strict_types=1);
namespace pocketmine\block;
use pocketmine\block\tile\BrewingStand as TileBrewingStand;
use pocketmine\block\utils\BrewingStandSlot;
use pocketmine\item\Item;
use pocketmine\item\ToolTier;
use pocketmine\math\Vector3;
use pocketmine\player\Player;
use function array_key_exists;
class BrewingStand extends Transparent{
/** @var bool */
protected $eastSlot = false;
/** @var bool */
protected $northwestSlot = false;
/** @var bool */
protected $southwestSlot = false;
/**
* @var BrewingStandSlot[]
* @phpstan-var array<int, BrewingStandSlot>
*/
protected $slots = [];
public function __construct(BlockIdentifier $idInfo, string $name, ?BlockBreakInfo $breakInfo = null){
parent::__construct($idInfo, $name, $breakInfo ?? new BlockBreakInfo(0.5, BlockToolType::PICKAXE, ToolTier::WOOD()->getHarvestLevel()));
}
protected function writeStateToMeta() : int{
return ($this->eastSlot ? BlockLegacyMetadata::BREWING_STAND_FLAG_EAST : 0) |
($this->southwestSlot ? BlockLegacyMetadata::BREWING_STAND_FLAG_SOUTHWEST : 0) |
($this->northwestSlot ? BlockLegacyMetadata::BREWING_STAND_FLAG_NORTHWEST : 0);
$flags = 0;
foreach([
BlockLegacyMetadata::BREWING_STAND_FLAG_EAST => BrewingStandSlot::EAST(),
BlockLegacyMetadata::BREWING_STAND_FLAG_NORTHWEST => BrewingStandSlot::NORTHWEST(),
BlockLegacyMetadata::BREWING_STAND_FLAG_SOUTHWEST => BrewingStandSlot::SOUTHWEST(),
] as $flag => $slot){
$flags |= (array_key_exists($slot->id(), $this->slots) ? $flag : 0);
}
return $flags;
}
public function readStateFromData(int $id, int $stateMeta) : void{
$this->eastSlot = ($stateMeta & BlockLegacyMetadata::BREWING_STAND_FLAG_EAST) !== 0;
$this->southwestSlot = ($stateMeta & BlockLegacyMetadata::BREWING_STAND_FLAG_SOUTHWEST) !== 0;
$this->northwestSlot = ($stateMeta & BlockLegacyMetadata::BREWING_STAND_FLAG_NORTHWEST) !== 0;
$this->slots = [];
foreach([
BlockLegacyMetadata::BREWING_STAND_FLAG_EAST => BrewingStandSlot::EAST(),
BlockLegacyMetadata::BREWING_STAND_FLAG_NORTHWEST => BrewingStandSlot::NORTHWEST(),
BlockLegacyMetadata::BREWING_STAND_FLAG_SOUTHWEST => BrewingStandSlot::SOUTHWEST(),
] as $flag => $slot){
if(($stateMeta & $flag) !== 0){
$this->slots[$slot->id()] = $slot;
}
}
}
public function getStateBitmask() : int{
return 0b111;
}
public function hasSlot(BrewingStandSlot $slot) : bool{
return array_key_exists($slot->id(), $this->slots);
}
public function setSlot(BrewingStandSlot $slot, bool $occupied) : self{
if($occupied){
$this->slots[$slot->id()] = $slot;
}else{
unset($this->slots[$slot->id()]);
}
return $this;
}
/**
* @return BrewingStandSlot[]
* @phpstan-return array<int, BrewingStandSlot>
*/
public function getSlots() : array{
return $this->slots;
}
/** @param BrewingStandSlot[] $slots */
public function setSlots(array $slots) : self{
$this->slots = [];
foreach($slots as $slot){
$this->slots[$slot->id()] = $slot;
}
return $this;
}
public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null) : bool{
if($player instanceof Player){
$stand = $this->pos->getWorld()->getTile($this->pos);

View File

@ -0,0 +1,47 @@
<?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\utils;
use pocketmine\utils\EnumTrait;
/**
* This doc-block is generated automatically, do not modify it manually.
* This must be regenerated whenever registry members are added, removed or changed.
* @see \pocketmine\utils\RegistryUtils::_generateMethodAnnotations()
*
* @method static BrewingStandSlot EAST()
* @method static BrewingStandSlot NORTHWEST()
* @method static BrewingStandSlot SOUTHWEST()
*/
final class BrewingStandSlot{
use EnumTrait;
protected static function setup() : void{
self::registerAll(
new self("east"),
new self("northwest"),
new self("southwest")
);
}
}

View File

@ -0,0 +1,104 @@
<?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 PHPUnit\Framework\TestCase;
use pocketmine\block\utils\BrewingStandSlot;
class BrewingStandTest extends TestCase{
/**
* @phpstan-return \Generator<int, array{list<BrewingStandSlot>}, void, void>
*/
public function slotsProvider() : \Generator{
yield [BrewingStandSlot::getAll()];
yield [[BrewingStandSlot::EAST()]];
yield [[BrewingStandSlot::EAST(), BrewingStandSlot::NORTHWEST()]];
}
/**
*
* @dataProvider slotsProvider
*
* @param BrewingStandSlot[] $slots
* @phpstan-param list<BrewingStandSlot> $slots
*/
public function testHasAndSetSlot(array $slots) : void{
$block = VanillaBlocks::BREWING_STAND();
foreach($slots as $slot){
$block->setSlot($slot, true);
}
foreach($slots as $slot){
self::assertTrue($block->hasSlot($slot));
}
foreach($slots as $slot){
$block->setSlot($slot, false);
}
foreach($slots as $slot){
self::assertFalse($block->hasSlot($slot));
}
}
/**
* @dataProvider slotsProvider
*
* @param BrewingStandSlot[] $slots
* @phpstan-param list<BrewingStandSlot> $slots
*/
public function testGetSlots(array $slots) : void{
$block = VanillaBlocks::BREWING_STAND();
foreach($slots as $slot){
$block->setSlot($slot, true);
}
self::assertCount(count($slots), $block->getSlots());
foreach($slots as $slot){
$block->setSlot($slot, false);
}
self::assertCount(0, $block->getSlots());
}
/**
* @dataProvider slotsProvider
*
* @param BrewingStandSlot[] $slots
* @phpstan-param list<BrewingStandSlot> $slots
*/
public function testSetSlots(array $slots) : void{
$block = VanillaBlocks::BREWING_STAND();
$block->setSlots($slots);
foreach($slots as $slot){
self::assertTrue($block->hasSlot($slot));
}
$block->setSlots([]);
self::assertCount(0, $block->getSlots());
foreach($slots as $slot){
self::assertFalse($block->hasSlot($slot));
}
}
}