Skip to content
This repository was archived by the owner on Oct 29, 2023. It is now read-only.

Commit

Permalink
🎵 List all songs and execute play command after click event
Browse files Browse the repository at this point in the history
  • Loading branch information
ProfessorSam committed May 23, 2022
1 parent 877466f commit 98fd92f
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 5 deletions.
29 changes: 27 additions & 2 deletions src/main/java/de/petropia/nbsDemo/NbsDemo.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,42 @@
package de.petropia.nbsDemo;

import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;

import de.petropia.nbsDemo.commands.SongCommand;
import de.petropia.nbsDemo.song.SongList;

public class NbsDemo extends JavaPlugin {

private static JavaPlugin instance;
private static NbsDemo instance;
private SongList songList;

@Override
public void onEnable() {
if(!isNoteBlockApiPresent()) {
getLogger().warning("Bitte füge das NoteBlockAPI plugin in den Pluginsordner hinzu, damit dieses Plugin ordnungsgemäß funktioniert!");
getServer().getPluginManager().disablePlugin(this);
return;
}
instance = this;
songList = new SongList(getDataFolder().toPath().toString() + "/Songs");
getCommand("songs").setExecutor(new SongCommand());
}

private boolean isNoteBlockApiPresent() {
return Bukkit.getServer().getPluginManager().getPlugin("NoteBlockAPI") != null;
}

public static JavaPlugin getInstance() {
public static NbsDemo getInstance() {
return instance;
}

public SongList getSongList() {
return songList;
}

public void setSongList(SongList songList) {
this.songList = songList;
}

}
58 changes: 55 additions & 3 deletions src/main/java/de/petropia/nbsDemo/commands/SongCommand.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package de.petropia.nbsDemo.commands;

import java.util.ArrayList;
import java.util.List;
import java.io.File;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import com.xxmicloxx.NoteBlockAPI.model.Song;
import com.xxmicloxx.NoteBlockAPI.songplayer.RadioSongPlayer;

import de.petropia.nbsDemo.NbsDemo;
import de.petropia.nbsDemo.song.SongList;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;

public class SongCommand implements CommandExecutor {

Expand All @@ -23,7 +30,8 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
return true;
}

Player player = (Player) sender;
final Player player = (Player) sender;
final SongList songlist = NbsDemo.getInstance().getSongList();

//Permission check and error message if fails
if(!player.hasPermission("nbsdemo.song")) {
Expand All @@ -32,11 +40,55 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
return true;
}

//Check if arguments are present and if the first is play, than the song is played from the 2. argument
if(args.length != 0) {
if(args.length == 2 && args[0].equalsIgnoreCase("play")) {
playSong(Integer.parseInt(args[1]), player);
}
return false;
}

player.sendMessage(Component.text("Folgende Songs sind verfügbar: ").color(NamedTextColor.GOLD));

//Check if Songs are present
if(songlist.getSongs().size() == 0) {
player.sendMessage(Component.text("Keine Songs verfügbar").color(NamedTextColor.RED).decorate(TextDecoration.UNDERLINED));
return true;
}

//loop over every song and sends it title as message to player with click event and when clicked, it play the song
for(int i = 0; i < songlist.getSongs().size(); i++) {
Song song = songlist.getSongs().get(i);
Component message = Component.text()
.append(Component.text("[").color(NamedTextColor.GRAY))
.append(Component.text(i).color(NamedTextColor.GOLD))
.append(Component.text("] ").color(NamedTextColor.GRAY))
.append(Component.text(getSongName(song, i)).color(NamedTextColor.GRAY).decorate(TextDecoration.ITALIC))
.hoverEvent(HoverEvent.showText(Component.text("Klicke um " + getSongName(song, i) + " zu hören")))
.clickEvent(ClickEvent.runCommand("/songs play " + i))
.build();
player.sendMessage(message);
}

return false;
}

//return the given name in the .nbs file or, if it doesnt exist, the file name
private String getSongName(Song song, int index) {
String name = song.getTitle();
if(!name.isEmpty() || !name.isBlank()) {
return name;
}
File file = NbsDemo.getInstance().getSongList().getNbsFiles().get(index);
return file.getName().replaceAll("^.*?(([^/\\\\\\.]+))\\.[^\\.]+$", "$1");
}

//plays the song for the player. Songid is equal to the index in SongList#getSongs()
private void playSong(int songid, Player player) {
Song song = NbsDemo.getInstance().getSongList().getSongs().get(songid);
RadioSongPlayer radioSongPlayer = new RadioSongPlayer(song);
radioSongPlayer.addPlayer(player);
radioSongPlayer.setPlaying(true);
}

}

0 comments on commit 98fd92f

Please sign in to comment.