diff --git a/src/block/Barrel.php b/src/block/Barrel.php new file mode 100644 index 000000000..277cd9299 --- /dev/null +++ b/src/block/Barrel.php @@ -0,0 +1,105 @@ +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; + } +} diff --git a/src/block/BlockFactory.php b/src/block/BlockFactory.php index 36e618068..7744d6d3c 100644 --- a/src/block/BlockFactory.php +++ b/src/block/BlockFactory.php @@ -28,6 +28,7 @@ use pocketmine\block\BlockIdentifierFlattened as BIDFlattened; 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; @@ -106,6 +107,7 @@ class BlockFactory{ $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")); @@ -508,7 +510,6 @@ class BlockFactory{ //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 diff --git a/src/block/BlockLegacyMetadata.php b/src/block/BlockLegacyMetadata.php index f9da4c53c..bbc1485fc 100644 --- a/src/block/BlockLegacyMetadata.php +++ b/src/block/BlockLegacyMetadata.php @@ -42,6 +42,8 @@ final class BlockLegacyMetadata{ 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; diff --git a/src/block/inventory/BarrelInventory.php b/src/block/inventory/BarrelInventory.php new file mode 100644 index 000000000..bb05d1c9c --- /dev/null +++ b/src/block/inventory/BarrelInventory.php @@ -0,0 +1,53 @@ +getHolder(); + $block = $holder->getWorld()->getBlock($holder); + if($block instanceof Barrel){ + $holder->getWorld()->setBlock($holder, $block->setOpen($isOpen)); + } + } +} diff --git a/src/block/tile/Barrel.php b/src/block/tile/Barrel.php new file mode 100644 index 000000000..ae8d6dfa8 --- /dev/null +++ b/src/block/tile/Barrel.php @@ -0,0 +1,78 @@ +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"; + } +} diff --git a/src/block/tile/TileFactory.php b/src/block/tile/TileFactory.php index 42b663b71..df67b1801 100644 --- a/src/block/tile/TileFactory.php +++ b/src/block/tile/TileFactory.php @@ -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"]); @@ -67,7 +68,6 @@ final class TileFactory{ $this->register(Sign::class, ["Sign", "minecraft:sign"]); $this->register(Skull::class, ["Skull", "minecraft:skull"]); - //TODO: Barrel //TODO: Beacon //TODO: Bell //TODO: BlastFurnace diff --git a/src/world/sound/BarrelCloseSound.php b/src/world/sound/BarrelCloseSound.php new file mode 100644 index 000000000..a49330511 --- /dev/null +++ b/src/world/sound/BarrelCloseSound.php @@ -0,0 +1,34 @@ +