Skip to content

Commit

Permalink
Merge pull request #18 from T0biii/dev
Browse files Browse the repository at this point in the history
Bump Version 4.0
  • Loading branch information
T0biii authored Apr 29, 2020
2 parents ba434d6 + 72be6e8 commit 22e2d87
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 71 deletions.
2 changes: 1 addition & 1 deletion JoinMusic/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>de.t0biii</groupId>
<artifactId>JoinMusic</artifactId>
<version>3.1</version>
<version>4.0</version>
<packaging>jar</packaging>
<name>JoinMusic</name>
<url>http://maven.apache.org</url>
Expand Down
58 changes: 43 additions & 15 deletions JoinMusic/src/de/t0biii/music/commands/CMD_PlayMusic.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
import de.t0biii.music.domain.Music;
import de.t0biii.music.main.Main;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

Expand All @@ -20,45 +22,71 @@ public CMD_PlayMusic(Main plugin) {

@Override
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
if (sender.hasPermission("JoinMusic.command.reload")) {
if (sender.hasPermission("JoinMusic.use")) {
if (sender instanceof Player) {
Player player = ((Player) sender).getPlayer();
String noperm = plugin.getConfig().getString("messages.no-permission");
if (args.length == 1) {
if (args[0].equalsIgnoreCase("reload")) {
try {
plugin.reloadConfig();
player.sendMessage(
ChatColor.DARK_AQUA + plugin.getConfig().getString("messages.reload"));
} catch (Exception exception) {
player.sendMessage(plugin.prefix + ChatColor.RED + "Reload failed!");
if (sender.hasPermission("JoinMusic.command.reload")) {
try {
plugin.reloadConfig();
player.sendMessage(plugin.prefix + ChatColor.DARK_AQUA
+ plugin.getConfig().getString("messages.reload"));
} catch (Exception exception) {
player.sendMessage(plugin.prefix + ChatColor.RED + "Reload failed!");
}
} else {
sender.sendMessage(plugin.prefix + ChatColor.RED + noperm);
}
} else
} else if (args[0].equalsIgnoreCase("stop")) {
if (sender.hasPermission("JoinMusic.command.stop")) {
Music.stop(player);
player.sendMessage(plugin.prefix + ChatColor.DARK_AQUA
+ plugin.getConfig().getString("messages.stop"));
} else {
sender.sendMessage(plugin.prefix + ChatColor.RED + noperm);
}
} else {
sendInstructions(player);
} else
}
} else {
sendInstructions(player);
} else
}
} else {
sender.sendMessage(plugin.prefix + "This command is not for console!");
} else {
sender.sendMessage(cmd.getPermissionMessage());
}
}
return true;
}

private void sendInstructions(CommandSender sender) {
sender.sendMessage(
ChatColor.GRAY + "======= " + ChatColor.GREEN + "JoinMusic " + ChatColor.GRAY + "=======");
ChatColor.GRAY + "======= " + ChatColor.GREEN + plugin.prefix + ChatColor.GRAY + "=======");
sender.sendMessage(ChatColor.GRAY + "/jm " + ChatColor.GREEN + "reload " + ChatColor.DARK_GRAY
+ "| " + ChatColor.GREEN + "Config Reload!");
sender.sendMessage(ChatColor.GRAY + "/jm " + ChatColor.GREEN + "stop " + ChatColor.DARK_GRAY
+ "| " + ChatColor.GREEN + "Stop Song Playing!");
sender.sendMessage(
ChatColor.GRAY + "======= " + ChatColor.GREEN + "JoinMusic " + ChatColor.GRAY + "=======");
ChatColor.GRAY + "======= " + ChatColor.GREEN + plugin.prefix + ChatColor.GRAY + "=======");
}

public static TabCompleter tabCompleter = new TabCompleter() {
@Override
public List<String> onTabComplete(CommandSender commandSender, Command command, String s,
String[] strings) {
if (strings.length == 1) {
return Collections.singletonList("reload");
ArrayList<String> list = new ArrayList<String>();
if(commandSender.hasPermission("JoinMusic.command.reload")) {
list.add("reload");
}
if(commandSender.hasPermission("JoinMusic.command.stop")) {
list.add("stop");
}
if(!commandSender.hasPermission("JoinMusic.command.stop") && !commandSender.hasPermission("JoinMusic.command.reload")) {
list.add(" ");
}
return list;
}
return Collections.singletonList(" ");
}
Expand Down
5 changes: 4 additions & 1 deletion JoinMusic/src/de/t0biii/music/domain/ConfigManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ public ConfigManager(Main plugin) {
public void loadConfig() {
this.plugin.getConfig().options().header("Plugin by itzTobi_!\nChange at your own risk");
this.plugin.getConfig().addDefault("music", "Song.nbs");
this.plugin.getConfig().addDefault("options.music.random", false);
this.plugin.getConfig().addDefault("options.music.RandomFoldername", "random");
this.plugin.getConfig().addDefault("options.update-check", true);
this.plugin.getConfig().addDefault("options.updateinfo", true);
this.plugin.getConfig().addDefault("options.printSongTitel", true);
this.plugin.getConfig().addDefault("options.delaySong", 2);
this.plugin.getConfig().addDefault("messages.reload", "Reload Erfolgreich!");
this.plugin.getConfig().addDefault("messages.reload", "The reload was successful!");
this.plugin.getConfig().addDefault("messages.stop", "The song was stopped!");
this.plugin.getConfig().addDefault("messages.no-permission",
"You don't have enough permissions");
this.plugin.getConfig().addDefault("options.metrics", true);
Expand Down
100 changes: 100 additions & 0 deletions JoinMusic/src/de/t0biii/music/domain/Music.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package de.t0biii.music.domain;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Random;
import java.util.UUID;
import org.bukkit.entity.Player;
import com.google.common.io.Files;
import com.xxmicloxx.NoteBlockAPI.model.Song;
import com.xxmicloxx.NoteBlockAPI.songplayer.RadioSongPlayer;
import com.xxmicloxx.NoteBlockAPI.songplayer.SongPlayer;
import com.xxmicloxx.NoteBlockAPI.utils.NBSDecoder;
import de.t0biii.music.main.Main;

public class Music {

// private static HashMap<String, Long> playing = new HashMap<>();
private static HashMap<UUID, SongPlayer> playingSong = new HashMap<>();

public static void play(Player player, Main plugin) {
if (player.hasPermission("JoinMusic.use") || player.isOp()) {
if (!playingSong.containsKey(player.getUniqueId())) {
playSong(player, plugin);
} else {
final SongPlayer sp = playingSong.get(player.getUniqueId());
if (!sp.isPlaying()) {
removePlayer(player);
playSong(player, plugin);
}
}
}
}

public static void stop(Player player) {
removePlayer(player);
}

private static void removePlayer(Player player) {
if (playingSong.containsKey(player.getUniqueId())) {
SongPlayer sp = playingSong.get(player.getUniqueId());
sp.destroy();
playingSong.remove(player.getUniqueId());
}
}

private static void playSong(Player player, Main plugin) {
try {
File songFile;
if(plugin.getConfig().getBoolean("options.music.random")) {
songFile = SelectRandomFileFromFolder(plugin);
}else {
songFile = new File(plugin.getDataFolder() + "/" + plugin.getConfig().getString("music"));
}
Song s = NBSDecoder.parse(songFile);
final SongPlayer sp = new RadioSongPlayer(s);
playingSong.put(player.getUniqueId(), sp);
sp.addPlayer(player);
sp.setPlaying(true);
if (plugin.getConfig().getBoolean("options.printSongTitel")) {
if(!sp.getSong().getTitle().isEmpty()) {
player.sendMessage(plugin.prefix + "§2Start Playing the Song:§a§l " + sp.getSong().getTitle());
}else {
player.sendMessage(plugin.prefix + "§2Start Playing a Song.");
}
}
} catch (IllegalArgumentException e) {
System.err.println(plugin.cprefix + "No sounds detected");
}
}

public static void createRandomFileDir(Main plugin) {
File dir = new File(plugin.getDataFolder() + "/" + plugin.getConfig().getString("options.music.RandomFoldername"));
if(!dir.exists()) {
dir.mkdirs();
File songFile = new File(plugin.getDataFolder() + "/" + plugin.getConfig().getString("music"));
File copyDir = new File(plugin.getDataFolder() + "/" + plugin.getConfig().getString("options.music.RandomFoldername") + "/" + plugin.getConfig().getString("music"));
try {
Files.copy(songFile, copyDir);
} catch (IOException e) {
}
}
}

private static File SelectRandomFileFromFolder(Main plugin){
createRandomFileDir(plugin);
File dir = new File(plugin.getDataFolder() + "/" + plugin.getConfig().getString("options.music.RandomFoldername"));
if(dir.exists()) {
File[] files = dir.listFiles();
if(files.length > 0) {
Random rand = new Random();
File file = files[rand.nextInt(files.length)];
return file;
}else {
return new File(plugin.getDataFolder() + "/" + plugin.getConfig().getString("music"));
}
}
return null;
}
}
54 changes: 3 additions & 51 deletions JoinMusic/src/de/t0biii/music/listener/HANDLER_PlayerJoin.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,21 @@
package de.t0biii.music.listener;

import java.io.File;
import java.util.HashMap;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import com.xxmicloxx.NoteBlockAPI.model.Song;
import com.xxmicloxx.NoteBlockAPI.songplayer.RadioSongPlayer;
import com.xxmicloxx.NoteBlockAPI.songplayer.SongPlayer;
import com.xxmicloxx.NoteBlockAPI.utils.NBSDecoder;
import de.t0biii.music.domain.Music;
import de.t0biii.music.domain.Updater;
import de.t0biii.music.main.Main;

public class HANDLER_PlayerJoin implements Listener {


private Main plugin;

public HANDLER_PlayerJoin(Main plugin) {
this.plugin = plugin;
}

private HashMap<String, Long> playing = new HashMap<>();


@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
final Player player = event.getPlayer();
Expand All @@ -33,27 +24,7 @@ public void onPlayerJoin(PlayerJoinEvent event) {
Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
@Override
public void run() {
try {
Song s = NBSDecoder.parse(
new File(plugin.getDataFolder() + "/" + plugin.getConfig().getString("music")));
final SongPlayer sp = new RadioSongPlayer(s);

int songLengthInSec = getTimeSeconds(sp.getSong().getLength(), sp.getSong().getSpeed());
int songLengthInMillisec = songLengthInSec * 1000;

if (player.hasPermission("JoinMusic.use") || player.isOp()) {
if (!playing.containsKey(player.getUniqueId().toString())) {
playSong(sp, player, songLengthInMillisec);
} else {
if (playing.get(player.getUniqueId().toString()) <= System.currentTimeMillis()) {
playing.remove(player.getUniqueId().toString());
playSong(sp, player, songLengthInMillisec);
}
}
}
} catch (IllegalArgumentException e) {
System.err.println(plugin.cprefix + "No sounds detected");
}
Music.play(player, plugin);
}
}, 20L * delay);

Expand All @@ -68,24 +39,5 @@ public void run() {
player.sendMessage(this.plugin.prefix + "§8Download: §7" + this.plugin.link2);
}
}

}

private void playSong(SongPlayer sp, Player p, long songlengthmilli) {
playing.put(p.getUniqueId().toString(), (System.currentTimeMillis() + songlengthmilli));
sp.addPlayer(p);
sp.setPlaying(true);
if (plugin.getConfig().getBoolean("options.printSongTitel")) {
p.sendMessage(
this.plugin.prefix + "§2Start Playing the Song:§a§l " + sp.getSong().getTitle());
}
}

private static int getTimeSeconds(short ticks, float speed) {
long milisTotal = (long) ((ticks / speed) * 1000);
int seconds = 0;
seconds = (int) Math.floor(milisTotal / 1000);
milisTotal -= seconds * 1000;
return seconds;
}
}
16 changes: 16 additions & 0 deletions JoinMusic/src/de/t0biii/music/listener/HANDLER_PlayerQuit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package de.t0biii.music.listener;

import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent;
import de.t0biii.music.domain.Music;

public class HANDLER_PlayerQuit implements Listener {

@EventHandler
public void onPlayerQuit(PlayerQuitEvent event) {
final Player player = event.getPlayer();
Music.stop(player);
}
}
5 changes: 4 additions & 1 deletion JoinMusic/src/de/t0biii/music/main/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import java.util.logging.Logger;
import de.t0biii.music.commands.CMD_PlayMusic;
import de.t0biii.music.domain.ConfigManager;
import de.t0biii.music.domain.Music;
import de.t0biii.music.domain.Updater;
import de.t0biii.music.domain.Updater.ReleaseType;
import de.t0biii.music.listener.HANDLER_PlayerJoin;
import de.t0biii.music.listener.HANDLER_PlayerQuit;
import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;
import org.bukkit.plugin.PluginManager;
Expand Down Expand Up @@ -51,9 +53,10 @@ public void onEnable() {
}

pm.registerEvents(new HANDLER_PlayerJoin(this), this);
pm.registerEvents(new HANDLER_PlayerQuit(), this);
getCommand("JoinMusic").setExecutor(new CMD_PlayMusic(this));
getCommand("JoinMusic").setTabCompleter(CMD_PlayMusic.tabCompleter);

Music.createRandomFileDir(this);
log.info(cprefix + "Enabled");
}

Expand Down
7 changes: 5 additions & 2 deletions JoinMusic/src/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ authors:
- Fruxz
depend:
- NoteBlockAPI
version: '3.1'
version: '4.0'
website: http://dev.bukkit.org/bukkit-plugins/joinmusik
main: de.t0biii.music.main.Main
database: false
Expand All @@ -16,4 +16,7 @@ commands:
- jm
- joinmusic
- joinmusik
permission: JoinMusik.command.reload
permissions:
JoinMusik.command.reload
JoinMusik.command.stop
JoinMusik.use

0 comments on commit 22e2d87

Please sign in to comment.