PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Source/src/PlayNoteBlockSong/PlayNoteBlockSong.php

https://gitlab.com/Skull3x/PlayNoteBlockSong
PHP | 194 lines | 184 code | 10 blank | 0 comment | 13 complexity | 56a1e31fe72ec4b7f279e82771b690ca MD5 | raw file
  1. <?php
  2. namespace PlayNoteBlockSong;
  3. use pocketmine\plugin\PluginBase;
  4. use pocketmine\command\CommandSender;
  5. use pocketmine\command\Command;
  6. use pocketmine\level\Position;
  7. use pocketmine\utils\TextFormat as Color;
  8. use pocketmine\event\TranslationContainer as Translation;
  9. use AddNoteBlock\block\NoteBlock;
  10. use PlayNoteBlockSong\task\LoadSongAsyncTask;
  11. use PlayNoteBlockSong\task\PlaySongTask;
  12. class PlayNoteBlockSong extends PluginBase{
  13. const SONG = 0;
  14. const NAME = 1;
  15. private $songs = [], $index = 0, $song, $play = false;
  16. public function onEnable(){
  17. $this->getServer()->getScheduler()->scheduleAsyncTask(new LoadSongAsyncTask());
  18. $this->getServer()->getScheduler()->scheduleRepeatingTask(new PlaySongTask($this), 2);
  19. }
  20. public function onCommand(CommandSender $sender, Command $cmd, $label, array $sub){
  21. $ik = $this->isKorean();
  22. if(!isset($sub[0]) || $sub[0] == ""){
  23. return false;
  24. }
  25. switch(strtolower($sub[0])){
  26. case "play":
  27. case "p":
  28. if(!$sender->hasPermission("playnoteblocksong.cmd.play")){
  29. $r = new Translation(Color::RED . "%commands.generic.permission");
  30. }elseif($this->play){
  31. $r = Color::RED . "[PlayNBS] " . ($ik ? "이미 재생중입니다." : "Already playing");
  32. }elseif(count($this->songs) <= 0){
  33. $r = Color::RED . "[PlayNBS] " . ($ik ? "당신은 음악이 하나도 없습니다." : "You don't have any song");
  34. }else{
  35. if(!$this->song instanceof SongPlayer){
  36. $this->song = clone $this->songs[$this->index][self::SONG];
  37. }
  38. $this->play = true;
  39. $r = Color::YELLOW . "[PlayNBS] " . ($ik ? "음악을 재생합니다. : " : "Play the song : ") . $this->songs[$this->index][self::NAME];
  40. }
  41. break;
  42. case "stop":
  43. case "s":
  44. if(!$sender->hasPermission("playnoteblocksong.cmd.stop")){
  45. $r = new Translation(Color::RED . "%commands.generic.permission");
  46. }elseif(!$this->play){
  47. $r = Color::RED . "[PlayNBS] " . ($ik ? "음악이 재생중이 아닙니다." : "Shong is not playing");
  48. }else{
  49. $this->play = false;
  50. $r = Color::YELLOW . "[PlayNBS] " . ($ik ? "음악을 중지합니다." : "Stop the song");
  51. }
  52. break;
  53. case "next":
  54. case "n":
  55. if(!$sender->hasPermission("playnoteblocksong.cmd.next")){
  56. $r = new Translation(Color::RED . "%commands.generic.permission");
  57. }elseif(count($this->songs) <= 0){
  58. $r = Color::RED . "[PlayNBS] " . ($ik ? "당신은 음악이 하나도 없습니다." : "You don't have any song");
  59. }else{
  60. if(!isset($this->songs[$this->index + 1])){
  61. $this->index = 0;
  62. }else{
  63. $this->index++;
  64. }
  65. $this->song = clone $this->songs[$this->index][self::SONG];
  66. $this->getLogger()->notice(Color::AQUA . "Play next song : " . $this->songs[$this->index][self::NAME]);
  67. $r = Color::YELLOW . "[PlayNBS] " . ($ik ? "다음 음악을 재생합니다. : " : "Play next song : ") . $this->songs[$this->index][self::NAME];
  68. }
  69. break;
  70. case "prev":
  71. case "pr":
  72. if(!$sender->hasPermission("playnoteblocksong.cmd.prev")){
  73. $r = new Translation(Color::RED . "%commands.generic.permission");
  74. }elseif(count($this->songs) <= 0){
  75. $r = Color::RED . "[PlayNBS] " . ($ik ? "당신은 음악이 하나도 없습니다." : "You don't have any song");
  76. }else{
  77. if(!isset($this->songs[$this->index - 1])){
  78. $this->index = 0;
  79. }else{
  80. $this->index--;
  81. }
  82. $this->song = clone $this->songs[$this->index][self::SONG];
  83. $this->getLogger()->notice(Color::AQUA . "Play prev song : " . $this->songs[$this->index][self::NAME]);
  84. $r = Color::YELLOW . "[PlayNBS] " . ($ik ? "이전 음악을 재생합니다. : " : "Play prev song : ") . $this->songs[$this->index][self::NAME];
  85. }
  86. break;
  87. case "shuffle":
  88. case "sh":
  89. if(!$sender->hasPermission("playnoteblocksong.cmd.shuffle")){
  90. $r = new Translation(Color::RED . "%commands.generic.permission");
  91. }elseif(count($this->songs) <= 0){
  92. $r = Color::RED . "[PlayNBS] " . ($ik ? "당신은 음악이 하나도 없습니다." : "You don't have any song");
  93. }else{
  94. shuffle($this->songs);
  95. $this->index = 0;
  96. $this->song = clone $this->songs[$this->index][self::SONG];
  97. $this->getLogger()->notice(Color::AQUA . "Song list is Shuffled. Now song : " . $this->songs[$this->index][self::NAME]);
  98. $r = Color::YELLOW . "[PlayNBS] " . ($ik ? "음악 목록이 뒤섞였습니다. 다음 음악 : " : "Song list is Shuffled. Now song : ") . $this->songs[$this->index][self::NAME];
  99. }
  100. break;
  101. case "list":
  102. case "l":
  103. if(!$sender->hasPermission("playnoteblocksong.cmd.list")){
  104. $r = new Translation(Color::RED . "%commands.generic.permission");
  105. }elseif(count($this->songs) <= 0){
  106. $r = Color::RED . "[PlayNBS] " . ($ik ? "당신은 음악이 하나도 없습니다." : "You don't have any song");
  107. }else{
  108. $lists = array_chunk($this->songs, 5);
  109. $r = Color::YELLOW . "[PlayNBS] " . ($ik ? "음악 목록 (페이지: " : "Song list (Page: ") . ($page = min(isset($sub[1]) && is_numeric($sub[1]) && isset($lists[$sub[1] - 1]) ? $sub[1] : 1, count($lists))). "/" . count($lists) . ") (" . count($this->songs) . ")";
  110. if(isset($lists[--$page])){
  111. foreach($lists[$page] as $key => $songData){
  112. $r .= "\n" . Color::GOLD . " [" . (($page * 5 + $key) + 1) . "] " . $songData[self::NAME];
  113. }
  114. }
  115. }
  116. break;
  117. case "reload":
  118. case "r":
  119. if(!$sender->hasPermission("playnoteblocksong.cmd.reload")){
  120. $r = new Translation(Color::RED . "%commands.generic.permission");
  121. }else{
  122. $this->loadSong();
  123. $r = Color::YELLOW . "[PlayNBS] " . ($ik ? "음악을 다시 로드했습니다." : "Reloaded songs.");
  124. }
  125. break;
  126. default:
  127. return false;
  128. break;
  129. }
  130. if(isset($r)){
  131. $sender->sendMessage($r);
  132. }
  133. return true;
  134. }
  135. public function loadSong(){
  136. $this->songs = [];
  137. $logger = $this->getLogger();
  138. @mkdir($folder = $this->getDataFolder());
  139. $opendir = opendir($folder);
  140. $logger->notice(Color::AQUA . "Load song...");
  141. while(($file = readdir($opendir)) !== false){
  142. if(($pos = stripos($file, ".nbs")) !== false){
  143. $this->songs[] = [new SongPlayer($this, $folder . $file), $name = substr($file, 0, $pos)];
  144. $logger->notice(Color::AQUA . "$name is loaded");
  145. }
  146. }
  147. if(count($this->songs) >= 1){
  148. $logger->notice(Color::AQUA . "Load complete");
  149. }else{
  150. $logger->notice(Color::DARK_RED . "You don't have song");
  151. $logger->notice(Color::DARK_RED . "Please put in the song to $folder");
  152. }
  153. }
  154. public function playSong(){
  155. if($this->play){
  156. if($this->song === null || $this->song->isStop()){
  157. if(!isset($this->songs[$this->index + 1])){
  158. $this->index = 0;
  159. }else{
  160. $this->index++;
  161. }
  162. $this->song = clone $this->songs[$this->index][self::SONG];
  163. $this->getLogger()->notice(Color::AQUA . "Play next song : " . $this->songs[$this->index][self::NAME]);
  164. }
  165. $this->song->onRun();
  166. }
  167. }
  168. public function sendSound($pitch, $type = NoteBlock::PIANO_OR_HARP){
  169. foreach($this->getServer()->getOnlinePlayers() as $player){
  170. NoteBlock::runNoteBlockSound(new Position($player->x, $player->y + 1, $player->z, $player->level), $pitch, $type, $player);
  171. }
  172. }
  173. public function getPlaySongName(){
  174. if(!isset($this->songs[$this->index][self::NAME])){
  175. return null;
  176. }else{
  177. return $this->songs[$this->index][self::NAME];
  178. }
  179. }
  180. public function isKorean(){
  181. return $this->getServer()->getLanguage()->getName() == "\"한국어\"";
  182. }
  183. }