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