PocketMine-MP 5.33.2 git-1133d49c924b4358c79d44eeb97dcbf56cb4d1eb
Loading...
Searching...
No Matches
Jukebox.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\Jukebox as JukeboxTile;
35
36class Jukebox extends Opaque{
37
38 private ?Record $record = null;
39
40 public function getFuelTime() : int{
41 return 300;
42 }
43
44 public function onInteract(Item $item, Facing $face, Vector3 $clickVector, ?Player $player = null, array &$returnedItems = []) : bool{
45 if($player instanceof Player){
46 if($this->record !== null){
47 $this->ejectRecord();
48 }elseif($item instanceof Record){
49 $player->sendJukeboxPopup(KnownTranslationFactory::record_nowPlaying($item->getRecordType()->getTranslatableName()));
50 $this->insertRecord($item->pop());
51 }
52 }
53
54 $this->position->getWorld()->setBlock($this->position, $this);
55
56 return true;
57 }
58
59 public function getRecord() : ?Record{
60 return $this->record;
61 }
62
63 public function ejectRecord() : void{
64 if($this->record !== null){
65 $this->position->getWorld()->dropItem($this->position->add(0.5, 1, 0.5), $this->record);
66 $this->record = null;
67 $this->stopSound();
68 }
69 }
70
71 public function insertRecord(Record $record) : void{
72 if($this->record === null){
73 $this->record = $record;
74 $this->startSound();
75 }
76 }
77
78 public function startSound() : void{
79 if($this->record !== null){
80 $this->position->getWorld()->addSound($this->position, new RecordSound($this->record->getRecordType()));
81 }
82 }
83
84 public function stopSound() : void{
85 $this->position->getWorld()->addSound($this->position, new RecordStopSound());
86 }
87
88 public function onBreak(Item $item, ?Player $player = null, array &$returnedItems = []) : bool{
89 $this->stopSound();
90 return parent::onBreak($item, $player, $returnedItems);
91 }
92
93 public function getDropsForCompatibleTool(Item $item) : array{
94 $drops = parent::getDropsForCompatibleTool($item);
95 if($this->record !== null){
96 $drops[] = $this->record;
97 }
98 return $drops;
99 }
100
101 public function readStateFromWorld() : Block{
102 parent::readStateFromWorld();
103 $jukebox = $this->position->getWorld()->getTile($this->position);
104 if($jukebox instanceof JukeboxTile){
105 $this->record = $jukebox->getRecord();
106 }
107
108 return $this;
109 }
110
111 public function writeStateToWorld() : void{
112 parent::writeStateToWorld();
113 $jukebox = $this->position->getWorld()->getTile($this->position);
114 if($jukebox instanceof JukeboxTile){
115 $jukebox->setRecord($this->record);
116 }
117 }
118
119 //TODO: Jukebox has redstone effects, they are not implemented.
120}
onBreak(Item $item, ?Player $player=null, array &$returnedItems=[])
Definition Jukebox.php:88
onInteract(Item $item, Facing $face, Vector3 $clickVector, ?Player $player=null, array &$returnedItems=[])
Definition Jukebox.php:44
getDropsForCompatibleTool(Item $item)
Definition Jukebox.php:93
pop(int $count=1)
Definition Item.php:435