PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ZMusicBox/ZMusicBox.php

https://gitlab.com/Skull3x/ZMusicBox
PHP | 239 lines | 219 code | 20 blank | 0 comment | 29 complexity | b2f54daf4606c7e2fa5a99b2a0531ece MD5 | raw file
  1. <?php
  2. namespace ZMusicBox;
  3. use pocketmine\command\Command;
  4. use pocketmine\command\CommandExecutor;
  5. use pocketmine\command\CommandSender;
  6. use pocketmine\plugin\PluginBase;
  7. use pocketmine\event\Listener;
  8. use pocketmine\level;
  9. use pocketmine\Server;
  10. use pocketmine\scheduler\PluginTask;
  11. use pocketmine\utils\Config;
  12. use pocketmine\utils\TextFormat;
  13. use pocketmine\network\protocol\BlockEventPacket;
  14. use pocketmine\Player;
  15. use pocketmine\math\AxisAlignedBB;
  16. use pocketmine\math\Vector3;
  17. use pocketmine\math\Math;
  18. use pocketmine\level\format\Chunk;
  19. use pocketmine\level\format\FullChunk;
  20. use pocketmine\utils\BinaryStream;
  21. use pocketmine\utils\Binary;
  22. use ZMusicBox\NoteBoxAPI;
  23. class ZMusicBox extends PluginBase implements Listener{
  24. public $song;
  25. public $MusicPlayer;
  26. public $name;
  27. public function onEnable(){
  28. $this->getLogger()->info("ZMusicBox is loading!");
  29. $this->getServer()->getPluginManager()->registerEvents($this, $this);
  30. if(!is_dir($this->getPluginDir())){
  31. @mkdir($this->getServer()->getDataPath()."plugins/songs");
  32. }
  33. $this->getServer()->getPluginManager()->registerEvents($this,$this);
  34. if(!$this->CheckMusic()){
  35. $this->getLogger()->info("§bPlease put in nbs files!!!");
  36. }else{
  37. $this->StartNewTask();
  38. }
  39. $this->getLogger()->info("ZMusicBox loaded!!!!!");
  40. }
  41. public function onCommand(CommandSender $sender, Command $cmd, $label, array $args) {
  42. switch($cmd->getName()) {
  43. case "music":
  44. if(isset($args[0])){
  45. switch($args[0]){
  46. case "next":
  47. $this->StartNewTask();
  48. $sender->sendMessage(TextFormat::GREEN."Switched to next song");
  49. return true;
  50. break;
  51. case "stop":
  52. if($sender->isOp()){
  53. $this->getServer()->getScheduler()->cancelTasks($this);
  54. $sender->sendMessage(TextFormat::GREEN."Song Stopped");
  55. }else{
  56. $sender->sendMessage(TextFormat::RED."No Permission");
  57. }
  58. return true;
  59. break;
  60. case "start":
  61. if($sender->isOp()){
  62. $this->StartNewTask();
  63. $sender->sendMessage(TextFormat::GREEN."Song Started");
  64. }else{
  65. $sender->sendMessage(TextFormat::RED."No Permission");
  66. }
  67. return true;
  68. break;
  69. }
  70. }else{
  71. $sender->sendMessage(TextFormat::RED."Usage:/music <start|stop|next>");
  72. }
  73. break;
  74. }
  75. }
  76. public function CheckMusic(){
  77. if($this->getDirCount($this->getPluginDir()) > 0 and $this->RandomFile($this->getPluginDir(),"nbs")){
  78. return true;
  79. }
  80. return false;
  81. }
  82. public function getDirCount($PATH){
  83. $num = sizeof(scandir($PATH));
  84. $num = ($num>2)?$num-2:0;
  85. return $num;
  86. }
  87. public function getPluginDir(){
  88. return $this->getServer()->getDataPath()."plugins/songs/";
  89. }
  90. public function getRandomMusic(){
  91. $dir = $this->RandomFile($this->getPluginDir(),"nbs");
  92. if($dir){
  93. $api = new NoteBoxAPI($this,$dir);
  94. return $api;
  95. }
  96. return false;
  97. }
  98. Public function RandomFile($folder='', $extensions='.*'){
  99. $folder = trim($folder);
  100. $folder = ($folder == '') ? './' : $folder;
  101. if (!is_dir($folder)){
  102. return false;
  103. }
  104. $files = array();
  105. if ($dir = @opendir($folder)){
  106. while($file = readdir($dir)){
  107. if (!preg_match('/^\.+$/', $file) and
  108. preg_match('/\.('.$extensions.')$/', $file)){
  109. $files[] = $file;
  110. }
  111. }
  112. closedir($dir);
  113. }else{
  114. return false;
  115. }
  116. if (count($files) == 0){
  117. return false;
  118. }
  119. mt_srand((double)microtime()*1000000);
  120. $rand = mt_rand(0, count($files)-1);
  121. if (!isset($files[$rand])){
  122. return false;
  123. }
  124. if(function_exists("iconv")){
  125. $rname = iconv('gbk','UTF-8',$files[$rand]);
  126. }else{
  127. $rname = $files[$rand];
  128. }
  129. $this->name = str_replace('.nbs', '', $rname);
  130. return $folder . $files[$rand];
  131. }
  132. public function getNearbyNoteBlock($x,$y,$z,$world){
  133. $nearby = [];
  134. $minX = $x - 5;
  135. $maxX = $x + 5;
  136. $minY = $y - 5;
  137. $maxY = $y + 5;
  138. $minZ = $z - 2;
  139. $maxZ = $z + 2;
  140. for($x = $minX; $x <= $maxX; ++$x){
  141. for($y = $minY; $y <= $maxY; ++$y){
  142. for($z = $minZ; $z <= $maxZ; ++$z){
  143. $v3 = new Vector3($x, $y, $z);
  144. $block = $world->getBlock($v3);
  145. if($block->getID() == 25){
  146. $nearby[] = $block;
  147. }
  148. }
  149. }
  150. }
  151. return $nearby;
  152. }
  153. public function getFullBlock($x, $y, $z, $level){
  154. return $level->getChunk($x >> 4, $z >> 4, false)->getFullBlock($x & 0x0f, $y & 0x7f, $z & 0x0f);
  155. }
  156. public function Play($sound,$type = 0,$blo = 0){
  157. if(is_numeric($sound) and $sound > 0){
  158. foreach($this->getServer()->getOnlinePlayers() as $p){
  159. $noteblock = $this->getNearbyNoteBlock($p->x,$p->y,$p->z,$p->getLevel());
  160. $noteblock1 = $noteblock;
  161. if(!empty($noteblock)){
  162. if($this->song->name != ""){
  163. $p->sendPopup("§b|->§6Now Playing: §a".$this->song->name."§b<-|");
  164. }else{
  165. $p->sendPopup("§b|->§6Now Playing: §a".$this->name."§b<-|");
  166. }
  167. $i = 0;
  168. while ($i < $blo){
  169. if(current($noteblock)){
  170. next($noteblock);
  171. $i ++;
  172. }else{
  173. $noteblock = $noteblock1;
  174. $i ++;
  175. }
  176. }
  177. $block = current($noteblock);
  178. if($block){
  179. $pk = new BlockEventPacket();
  180. $pk->x = $block->x;
  181. $pk->y = $block->y;
  182. $pk->z = $block->z;
  183. $pk->case1 = $type;
  184. $pk->case2 = $sound;
  185. $p->dataPacket($pk);
  186. }
  187. }
  188. }
  189. }
  190. }
  191. public function onDisable(){
  192. $this->getLogger()->info("ZMusicBox Unload Success!");
  193. }
  194. public function StartNewTask(){
  195. $this->song = $this->getRandomMusic();
  196. $this->getServer()->getScheduler()->cancelTasks($this);
  197. $this->MusicPlayer = new MusicPlayer($this);
  198. $this->getServer()->getScheduler()->scheduleRepeatingTask($this->MusicPlayer, 3000 / $this->song->speed );
  199. }
  200. }
  201. class MusicPlayer extends PluginTask{
  202. public function __construct(ZMusicBox $plugin){
  203. parent::__construct($plugin);
  204. $this->plugin = $plugin;
  205. }
  206. public function onRun($CT){
  207. if(isset($this->plugin->song->sounds[$this->plugin->song->tick])){
  208. $i = 0;
  209. foreach($this->plugin->song->sounds[$this->plugin->song->tick] as $data){
  210. $this->plugin->Play($data[0],$data[1],$i);
  211. $i++;
  212. }
  213. }
  214. $this->plugin->song->tick++;
  215. if($this->plugin->song->tick > $this->plugin->song->length){
  216. $this->plugin->StartNewTask();
  217. }
  218. }
  219. }