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