PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
Loading...
Searching...
No Matches
block/Anvil.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
28use pocketmine\block\utils\FallableTrait;
29use pocketmine\block\utils\HorizontalFacingTrait;
30use pocketmine\block\utils\SupportType;
42use function round;
43
44class Anvil extends Transparent implements Fallable{
45 use FallableTrait;
46 use HorizontalFacingTrait;
47
48 public const UNDAMAGED = 0;
49 public const SLIGHTLY_DAMAGED = 1;
50 public const VERY_DAMAGED = 2;
51
52 private int $damage = self::UNDAMAGED;
53
54 public function describeBlockItemState(RuntimeDataDescriber $w) : void{
55 $w->boundedIntAuto(self::UNDAMAGED, self::VERY_DAMAGED, $this->damage);
56 }
57
58 protected function describeBlockOnlyState(RuntimeDataDescriber $w) : void{
59 $w->horizontalFacing($this->facing);
60 }
61
62 public function getDamage() : int{ return $this->damage; }
63
65 public function setDamage(int $damage) : self{
66 if($damage < self::UNDAMAGED || $damage > self::VERY_DAMAGED){
67 throw new \InvalidArgumentException("Damage must be in range " . self::UNDAMAGED . " ... " . self::VERY_DAMAGED);
68 }
69 $this->damage = $damage;
70 return $this;
71 }
72
76 protected function recalculateCollisionBoxes() : array{
77 return [AxisAlignedBB::one()->squash(Facing::axis(Facing::rotateY($this->facing, false)), 1 / 8)];
78 }
79
80 public function getSupportType(int $facing) : SupportType{
81 return SupportType::NONE;
82 }
83
84 public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
85 if($player instanceof Player){
86 $player->setCurrentWindow(new AnvilInventory($this->position));
87 }
88
89 return true;
90 }
91
92 public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{
93 if($player !== null){
94 $this->facing = Facing::rotateY($player->getHorizontalFacing(), false);
95 }
96 return parent::place($tx, $item, $blockReplace, $blockClicked, $face, $clickVector, $player);
97 }
98
99 public function onHitGround(FallingBlock $blockEntity) : bool{
100 if(Utils::getRandomFloat() < 0.05 + (round($blockEntity->getFallDistance()) - 1) * 0.05){
101 if($this->damage !== self::VERY_DAMAGED){
102 $this->damage = $this->damage + 1;
103 }else{
104 return false;
105 }
106 }
107 return true;
108 }
109
110 public function getFallDamagePerBlock() : float{
111 return 2.0;
112 }
113
114 public function getMaxFallDamage() : float{
115 return 40.0;
116 }
117
118 public function getLandSound() : ?Sound{
119 return new AnvilFallSound();
120 }
121}
place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player=null)
describeBlockOnlyState(RuntimeDataDescriber $w)
onHitGround(FallingBlock $blockEntity)
onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player=null, array &$returnedItems=[])
describeBlockItemState(RuntimeDataDescriber $w)
getSupportType(int $facing)