PocketMine-MP 5.21.2 git-b2aa6396c3cc2cafdd815eacc360e1ad89599899
Loading...
Searching...
No Matches
PaintingItem.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\item;
25
35use function array_rand;
36use function count;
37
38class PaintingItem extends Item{
39
40 public function onInteractBlock(Player $player, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, array &$returnedItems) : ItemUseResult{
41 if(Facing::axis($face) === Axis::Y){
42 return ItemUseResult::NONE;
43 }
44
45 $motives = [];
46
47 $totalDimension = 0;
48 foreach(PaintingMotive::getAll() as $motive){
49 $currentTotalDimension = $motive->getHeight() + $motive->getWidth();
50 if($currentTotalDimension < $totalDimension){
51 continue;
52 }
53
54 if(Painting::canFit($player->getWorld(), $blockReplace->getPosition(), $face, true, $motive)){
55 if($currentTotalDimension > $totalDimension){
56 $totalDimension = $currentTotalDimension;
57 /*
58 * This drops all motive possibilities smaller than this
59 * We use the total of height + width to allow equal chance of horizontal/vertical paintings
60 * when there is an L-shape of space available.
61 */
62 $motives = [];
63 }
64
65 $motives[] = $motive;
66 }
67 }
68
69 if(count($motives) === 0){ //No space available
70 return ItemUseResult::NONE;
71 }
72
74 $motive = $motives[array_rand($motives)];
75
76 $replacePos = $blockReplace->getPosition();
77 $clickedPos = $blockClicked->getPosition();
78
79 $entity = new Painting(Location::fromObject($replacePos, $replacePos->getWorld()), $clickedPos, $face, $motive);
80 $this->pop();
81 $entity->spawnToAll();
82
83 $player->getWorld()->addSound($replacePos->add(0.5, 0.5, 0.5), new PaintingPlaceSound());
84 return ItemUseResult::SUCCESS;
85 }
86}
pop(int $count=1)
Definition Item.php:430
onInteractBlock(Player $player, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, array &$returnedItems)