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