PocketMine-MP 5.17.1 git-df4ada81e5d74a14046f27cf44a37dcee69d657e
Barrel.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\block;
25
26use pocketmine\block\tile\Barrel as TileBarrel;
27use pocketmine\block\utils\AnyFacingTrait;
34use function abs;
35
36class Barrel extends Opaque{
37 use AnyFacingTrait;
38
39 protected bool $open = false;
40
41 protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
42 $w->facing($this->facing);
43 $w->bool($this->open);
44 }
45
46 public function isOpen() : bool{
47 return $this->open;
48 }
49
51 public function setOpen(bool $open) : Barrel{
52 $this->open = $open;
53 return $this;
54 }
55
56 public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{
57 if($player !== null){
58 if(abs($player->getPosition()->x - $this->position->x) < 2 && abs($player->getPosition()->z - $this->position->z) < 2){
59 $y = $player->getEyePos()->y;
60
61 if($y - $this->position->y > 2){
62 $this->facing = Facing::UP;
63 }elseif($this->position->y - $y > 0){
64 $this->facing = Facing::DOWN;
65 }else{
66 $this->facing = Facing::opposite($player->getHorizontalFacing());
67 }
68 }else{
69 $this->facing = Facing::opposite($player->getHorizontalFacing());
70 }
71 }
72
73 return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player);
74 }
75
76 public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
77 if($player instanceof Player){
78 $barrel = $this->position->getWorld()->getTile($this->position);
79 if($barrel instanceof TileBarrel){
80 if(!$barrel->canOpenWith($item->getCustomName())){
81 return true;
82 }
83
84 $player->setCurrentWindow($barrel->getInventory());
85 }
86 }
87
88 return true;
89 }
90
91 public function getFuelTime() : int{
92 return 300;
93 }
94}
place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player=null)
Definition: Barrel.php:56
setOpen(bool $open)
Definition: Barrel.php:51
onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player=null, array &$returnedItems=[])
Definition: Barrel.php:76
describeBlockOnlyState(RuntimeDataDescriber $w)
Definition: Barrel.php:41