Skip to content

Commit

Permalink
Implemented Barrels, closes #3672
Browse files Browse the repository at this point in the history
  • Loading branch information
Aericio authored and dktapps committed Nov 16, 2020
1 parent 1cf3a50 commit b2765f3
Show file tree
Hide file tree
Showing 10 changed files with 315 additions and 3 deletions.
105 changes: 105 additions & 0 deletions src/block/Barrel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?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\tile\Barrel as TileBarrel;
use pocketmine\block\utils\AnyFacingTrait;
use pocketmine\block\utils\BlockDataSerializer;
use pocketmine\item\Item;
use pocketmine\math\Facing;
use pocketmine\math\Vector3;
use pocketmine\player\Player;
use pocketmine\world\BlockTransaction;

class Barrel extends Opaque{
use AnyFacingTrait;

/** @var bool */
protected $open = false;

public function __construct(BlockIdentifier $idInfo, string $name, ?BlockBreakInfo $breakInfo = null){
parent::__construct($idInfo, $name, $breakInfo ?? new BlockBreakInfo(2.5, BlockToolType::AXE));
}

protected function writeStateToMeta() : int{
return BlockDataSerializer::writeFacing($this->facing) | ($this->open ? BlockLegacyMetadata::BARREL_FLAG_OPEN : 0);
}

public function readStateFromData(int $id, int $stateMeta) : void{
$this->facing = BlockDataSerializer::readFacing($stateMeta & 0x07);
$this->open = ($stateMeta & BlockLegacyMetadata::BARREL_FLAG_OPEN) === BlockLegacyMetadata::BARREL_FLAG_OPEN;
}

public function getStateBitmask() : int{
return 0b1111;
}

public function isOpen() : bool{
return $this->open;
}

public function setOpen(bool $open) : Barrel{
$this->open = $open;
return $this;
}

public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{
if($player !== null){
if(abs($player->getPosition()->getX() - $this->pos->getX()) < 2 && abs($player->getPosition()->getZ() - $this->pos->getZ()) < 2){
$y = $player->getEyePos()->getY();

if($y - $this->pos->getY() > 2){
$this->facing = Facing::UP;
}elseif($this->pos->getY() - $y > 0){
$this->facing = Facing::DOWN;
}else{
$this->facing = Facing::opposite($player->getHorizontalFacing());
}
}else{
$this->facing = Facing::opposite($player->getHorizontalFacing());
}
}

return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player);
}

public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null) : bool{
if($player instanceof Player){
$barrel = $this->pos->getWorld()->getTile($this->pos);
if($barrel instanceof TileBarrel){
if(!$barrel->canOpenWith($item->getCustomName())){
return true;
}

$player->setCurrentWindow($barrel->getInventory());
}
}

return true;
}

public function getFuelTime() : int{
return 300;
}
}
3 changes: 2 additions & 1 deletion src/block/BlockFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use pocketmine\block\BlockLegacyIds as Ids;
use pocketmine\block\BlockLegacyMetadata as Meta;
use pocketmine\block\tile\Banner as TileBanner;
use pocketmine\block\tile\Barrel as TileBarrel;
use pocketmine\block\tile\Beacon as TileBeacon;
use pocketmine\block\tile\Bed as TileBed;
use pocketmine\block\tile\BrewingStand as TileBrewingStand;
Expand Down Expand Up @@ -106,6 +107,7 @@ public function __construct(){
$this->register(new BambooSapling(new BID(Ids::BAMBOO_SAPLING), "Bamboo Sapling", BlockBreakInfo::instant()));
$this->register(new FloorBanner(new BID(Ids::STANDING_BANNER, 0, ItemIds::BANNER, TileBanner::class), "Banner"));
$this->register(new WallBanner(new BID(Ids::WALL_BANNER, 0, ItemIds::BANNER, TileBanner::class), "Wall Banner"));
$this->register(new Barrel(new BID(Ids::BARREL, 0, null, TileBarrel::class), "Barrel"));
$this->register(new Transparent(new BID(Ids::BARRIER), "Barrier", BlockBreakInfo::indestructible()));
$this->register(new Beacon(new BID(Ids::BEACON, 0, null, TileBeacon::class), "Beacon", new BlockBreakInfo(3.0)));
$this->register(new Bed(new BID(Ids::BED_BLOCK, 0, ItemIds::BED, TileBed::class), "Bed Block"));
Expand Down Expand Up @@ -508,7 +510,6 @@ public function isAffectedBySilkTouch() : bool{
//region --- auto-generated TODOs for bedrock-1.11.0 ---
//TODO: minecraft:bamboo
//TODO: minecraft:bamboo_sapling
//TODO: minecraft:barrel
//TODO: minecraft:beacon
//TODO: minecraft:bell
//TODO: minecraft:blast_furnace
Expand Down
2 changes: 2 additions & 0 deletions src/block/BlockLegacyMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ private function __construct(){
public const BAMBOO_LEAF_SIZE_SHIFT = 1;
public const BAMBOO_LEAF_SIZE_MASK = 0x03;

public const BARREL_FLAG_OPEN = 0x08;

public const BED_FLAG_HEAD = 0x08;
public const BED_FLAG_OCCUPIED = 0x04;

Expand Down
53 changes: 53 additions & 0 deletions src/block/inventory/BarrelInventory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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\block\Barrel;
use pocketmine\world\Position;
use pocketmine\world\sound\BarrelCloseSound;
use pocketmine\world\sound\BarrelOpenSound;
use pocketmine\world\sound\Sound;

class BarrelInventory extends AnimatedBlockInventory{

public function __construct(Position $holder){
parent::__construct($holder, 27);
}

protected function getOpenSound() : Sound{
return new BarrelOpenSound();
}

protected function getCloseSound() : Sound{
return new BarrelCloseSound();
}

protected function animateBlock(bool $isOpen) : void{
$holder = $this->getHolder();
$block = $holder->getWorld()->getBlock($holder);
if($block instanceof Barrel){
$holder->getWorld()->setBlock($holder, $block->setOpen($isOpen));
}
}
}
78 changes: 78 additions & 0 deletions src/block/tile/Barrel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?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\tile;

use pocketmine\block\inventory\BarrelInventory;
use pocketmine\math\Vector3;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\world\World;

class Barrel extends Spawnable implements Container, Nameable{
use NameableTrait;
use ContainerTrait;

/** @var BarrelInventory */
protected $inventory;

public function __construct(World $world, Vector3 $pos){
parent::__construct($world, $pos);
$this->inventory = new BarrelInventory($this->pos);
}

public function readSaveData(CompoundTag $nbt) : void{
$this->loadName($nbt);
$this->loadItems($nbt);
}

protected function writeSaveData(CompoundTag $nbt) : void{
$this->saveName($nbt);
$this->saveItems($nbt);
}

public function close() : void{
if(!$this->closed){
$this->inventory->removeAllViewers();
$this->inventory = null;
parent::close();
}
}

/**
* @return BarrelInventory
*/
public function getInventory(){
return $this->inventory;
}

/**
* @return BarrelInventory
*/
public function getRealInventory(){
return $this->inventory;
}

public function getDefaultName() : string{
return "Barrel";
}
}
2 changes: 1 addition & 1 deletion src/block/tile/TileFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ final class TileFactory{
private $saveNames = [];

public function __construct(){
$this->register(Barrel::class, ["Barrel", "minecraft:barrel"]);
$this->register(Banner::class, ["Banner", "minecraft:banner"]);
$this->register(Beacon::class, ["Beacon", "minecraft:beacon"]);
$this->register(Bed::class, ["Bed", "minecraft:bed"]);
Expand All @@ -67,7 +68,6 @@ public function __construct(){
$this->register(Sign::class, ["Sign", "minecraft:sign"]);
$this->register(Skull::class, ["Skull", "minecraft:skull"]);

//TODO: Barrel
//TODO: Beacon
//TODO: Bell
//TODO: BlastFurnace
Expand Down
34 changes: 34 additions & 0 deletions src/world/sound/BarrelCloseSound.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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\world\sound;

use pocketmine\math\Vector3;
use pocketmine\network\mcpe\protocol\LevelSoundEventPacket;

class BarrelCloseSound implements Sound{

public function encode(?Vector3 $pos) : array{
return [LevelSoundEventPacket::create(LevelSoundEventPacket::SOUND_BLOCK_BARREL_CLOSE, $pos)];
}
}
34 changes: 34 additions & 0 deletions src/world/sound/BarrelOpenSound.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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\world\sound;

use pocketmine\math\Vector3;
use pocketmine\network\mcpe\protocol\LevelSoundEventPacket;

class BarrelOpenSound implements Sound{

public function encode(?Vector3 $pos) : array{
return [LevelSoundEventPacket::create(LevelSoundEventPacket::SOUND_BLOCK_BARREL_OPEN, $pos)];
}
}
5 changes: 5 additions & 0 deletions tests/phpstan/configs/gc-hacks.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
parameters:
ignoreErrors:
-
message: "#^Property pocketmine\\\\block\\\\tile\\\\Barrel\\:\\:\\$inventory \\(pocketmine\\\\block\\\\inventory\\\\BarrelInventory\\) does not accept null\\.$#"
count: 1
path: ../../../src/block/tile/Barrel.php

-
message: "#^Property pocketmine\\\\block\\\\tile\\\\BrewingStand\\:\\:\\$inventory \\(pocketmine\\\\block\\\\inventory\\\\BrewingStandInventory\\) does not accept null\\.$#"
count: 1
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/block/block_factory_consistency_check.json

Large diffs are not rendered by default.

0 comments on commit b2765f3

Please sign in to comment.