Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix GUI not playing music #6

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
29 changes: 18 additions & 11 deletions core/src/main/java/dev/geco/gmusic/manager/SongManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@

import java.io.*;
import java.util.*;
import java.util.stream.Collectors;

import net.kyori.adventure.text.Component;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't use paper only code. The plugin has to work on spigot & bukkit as well.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how to get rid of the errors on strings vs. components since you're using both spigot and paper as a dependency - in my IntelliJ environment, paper seems to take priority over compile time. I have buildtools for 1.21 installed.

import org.bukkit.NamespacedKey;
import org.bukkit.inventory.*;
import org.bukkit.inventory.meta.*;

import dev.geco.gmusic.GMusicMain;
import dev.geco.gmusic.objects.*;
import org.bukkit.persistence.PersistentDataType;

public class SongManager {

private final GMusicMain GPM;

public SongManager(GMusicMain GPluginMain) { GPM = GPluginMain; }

private final List<Song> songs = new ArrayList<>();
private List<Song> songs = new ArrayList<>();

private final HashMap<UUID, MusicGUI> musicGUIs = new HashMap<>();

Expand Down Expand Up @@ -55,28 +59,31 @@ public void loadSongs() {

File songsDir = new File(GPM.getDataFolder(), "songs");

Arrays.asList(Objects.requireNonNull(songsDir.listFiles())).parallelStream().forEach(file -> {
songs = Arrays.asList(Objects.requireNonNull(songsDir.listFiles())).parallelStream().map(file -> {

int pos = file.getName().lastIndexOf(".");
if(pos <= 0 || !file.getName().substring(pos + 1).equalsIgnoreCase("gnbs")) return;
if(pos <= 0 || !file.getName().substring(pos + 1).equalsIgnoreCase("gnbs")) return null;

Song song = new Song(file);

if(song.getNoteAmount() == 0) return;
if(song.getNoteAmount() == 0) return null;

List<String> description = new ArrayList<>();
for(String descriptionRow : song.getDescription()) description.add(GPM.getMManager().getMessage(descriptionRow));
ArrayList<Component> description = new ArrayList<>();
for(String descriptionRow : song.getDescription()) description.add(Component.text(GPM.getMManager().getMessage(descriptionRow)));

ItemStack itemStack = new ItemStack(song.getMaterial());
ItemMeta itemMeta = itemStack.getItemMeta();
itemMeta.setDisplayName(GPM.getMManager().getMessage("Items.disc-title", "%Title%", song.getTitle(), "%Author%", song.getAuthor().isEmpty() ? GPM.getMManager().getMessage("MusicGUI.disc-empty-author") : song.getAuthor(), "%OAuthor%", song.getOriginalAuthor().isEmpty() ? GPM.getMManager().getMessage("MusicGUI.disc-empty-oauthor") : song.getOriginalAuthor()));
itemMeta.setLocalizedName(GPM.NAME + "_D_" + song.getId());
itemMeta.setLore(description);
itemMeta.displayName(Component.text(GPM.getMManager().getMessage("Items.disc-title", "%Title%", song.getTitle(), "%Author%", song.getAuthor().isEmpty() ? GPM.getMManager().getMessage("MusicGUI.disc-empty-author") : song.getAuthor(), "%OAuthor%", song.getOriginalAuthor().isEmpty() ? GPM.getMManager().getMessage("MusicGUI.disc-empty-oauthor") : song.getOriginalAuthor())));
NamespacedKey localizedNameKey = new NamespacedKey(GPM, "LocalizedName");
itemMeta.getPersistentDataContainer().set(localizedNameKey, PersistentDataType.STRING, GPM.NAME + "_D_" + song.getId());
itemMeta.lore(description);
itemMeta.addItemFlags(ItemFlag.values());
itemStack.setItemMeta(itemMeta);

songs.add(song);
});
return song;
}).collect(Collectors.toList());

songs.sort(Comparator.comparing(Song::getTitle, String.CASE_INSENSITIVE_ORDER));
}

public void putMusicGUI(UUID UUID, MusicGUI MusicGUI) { getMusicGUIs().put(UUID, MusicGUI); }
Expand Down
96 changes: 50 additions & 46 deletions core/src/main/java/dev/geco/gmusic/objects/MusicGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.*;

import net.kyori.adventure.text.Component;
import org.bukkit.persistence.PersistentDataType;
import org.jetbrains.annotations.*;

import org.bukkit.*;
Expand Down Expand Up @@ -30,6 +32,8 @@ public class MusicGUI {
private static final long RANGE_STEPS = 1;
private static final long SHIFT_RANGE_STEPS = 10;

NamespacedKey localizedNameKey = new NamespacedKey(GPM, "LocalizedName");

public MusicGUI(UUID UUID, MenuType Type) {

uuid = UUID;
Expand Down Expand Up @@ -57,9 +61,9 @@ public void ICliE(InventoryClickEvent Event) {
ClickType click = Event.getClick();
if(GPM.getSVManager().getInventoryFromView(Event.getView(), "getTopInventory").equals(Event.getClickedInventory())) {
ItemStack itemStack = Event.getCurrentItem();
if(itemStack != null && itemStack.hasItemMeta() && itemStack.getItemMeta().hasLocalizedName()) {
if(itemStack != null && !(itemStack.getPersistentDataContainer().isEmpty())) {

String clickName = itemStack.getItemMeta().getLocalizedName();
String clickName = itemStack.getPersistentDataContainer().get(localizedNameKey, PersistentDataType.STRING);
if(clickName.startsWith("!")) setPage(Integer.parseInt(clickName.replace("!", "")));
else if(clickName.startsWith(".")) setOptionsBar();
else if(clickName.startsWith("+")) {
Expand Down Expand Up @@ -288,16 +292,16 @@ public void setDefaultBar() {
if(playSettings.getPlayList() != 2) {
lp = new ItemStack(Material.BARRIER);
lpm = lp.getItemMeta();
lpm.setDisplayName(GPM.getMManager().getMessage("MusicGUI.music-stop"));
lpm.setLocalizedName("/s");
lpm.displayName(Component.text(GPM.getMManager().getMessage("MusicGUI.music-stop")));
lpm.getPersistentDataContainer().set(localizedNameKey, PersistentDataType.STRING, "/s");
lp.setItemMeta(lpm);
inventory.setItem(46, lp);

if(!GPM.getCManager().G_DISABLE_RANDOM_SONG) {
lp = new ItemStack(Material.ENDER_PEARL);
lpm = lp.getItemMeta();
lpm.setDisplayName(GPM.getMManager().getMessage("MusicGUI.music-random"));
lpm.setLocalizedName("/q");
lpm.displayName(Component.text(GPM.getMManager().getMessage("MusicGUI.music-random")));
lpm.getPersistentDataContainer().set(localizedNameKey, PersistentDataType.STRING, "/q");
lp.setItemMeta(lpm);
inventory.setItem(47, lp);
}
Expand All @@ -306,17 +310,17 @@ public void setDefaultBar() {
if(!GPM.getCManager().G_DISABLE_PLAYLIST) {
lp = new ItemStack(Material.ENDER_CHEST);
lpm = lp.getItemMeta();
lpm.setDisplayName(GPM.getMManager().getMessage("MusicGUI.music-playlist"));
lpm.setLocalizedName(",");
lpm.displayName(Component.text(GPM.getMManager().getMessage("MusicGUI.music-playlist")));
lpm.getPersistentDataContainer().set(localizedNameKey, PersistentDataType.STRING, ",");
lp.setItemMeta(lpm);
inventory.setItem(49, lp);
}

if(!GPM.getCManager().G_DISABLE_OPTIONS) {
lp = new ItemStack(Material.HOPPER);
lpm = lp.getItemMeta();
lpm.setDisplayName(GPM.getMManager().getMessage("MusicGUI.music-options"));
lpm.setLocalizedName(".");
lpm.displayName(Component.text(GPM.getMManager().getMessage("MusicGUI.music-options")));
lpm.getPersistentDataContainer().set(localizedNameKey, PersistentDataType.STRING, ".");
lp.setItemMeta(lpm);
inventory.setItem(50, lp);
}
Expand All @@ -334,11 +338,11 @@ public void setPauseResumeBar() {
ItemStack lp = new ItemStack(Material.END_CRYSTAL);
ItemMeta lpm = lp.getItemMeta();
if(songSettings.isPaused()) {
lpm.setDisplayName(GPM.getMManager().getMessage("MusicGUI.music-resume"));
lpm.setLocalizedName("/r");
lpm.displayName(Component.text(GPM.getMManager().getMessage("MusicGUI.music-resume")));
lpm.getPersistentDataContainer().set(localizedNameKey, PersistentDataType.STRING, "/r");
} else {
lpm.setDisplayName(GPM.getMManager().getMessage("MusicGUI.music-pause"));
lpm.setLocalizedName("/p");
lpm.displayName(Component.text(GPM.getMManager().getMessage("MusicGUI.music-pause")));
lpm.getPersistentDataContainer().set(localizedNameKey, PersistentDataType.STRING, "/p");
}
lp.setItemMeta(lpm);
inventory.setItem(45, lp);
Expand All @@ -353,49 +357,49 @@ public void setOptionsBar() {

ItemStack lp = new ItemStack(Material.CHEST);
ItemMeta lpm = lp.getItemMeta();
lpm.setDisplayName(GPM.getMManager().getMessage("MusicGUI.music-back"));
lpm.setLocalizedName("-");
lpm.displayName(Component.text(GPM.getMManager().getMessage("MusicGUI.music-back")));
lpm.getPersistentDataContainer().set(localizedNameKey, PersistentDataType.STRING, "-");
lp.setItemMeta(lpm);
inventory.setItem(45, lp);
lp = new ItemStack(Material.MAGMA_CREAM);
lpm = lp.getItemMeta();
lpm.setDisplayName(GPM.getMManager().getMessage("MusicGUI.music-options-volume", "%Volume%", "" + playSettings.getVolume()));
lpm.setLocalizedName("+v");
lpm.displayName(Component.text(GPM.getMManager().getMessage("MusicGUI.music-options-volume", "%Volume%", "" + playSettings.getVolume())));
lpm.getPersistentDataContainer().set(localizedNameKey, PersistentDataType.STRING, "+v");
lp.setItemMeta(lpm);
inventory.setItem(46, lp);
lp = new ItemStack(Material.FIREWORK_ROCKET);
lpm = lp.getItemMeta();
lpm.setDisplayName(GPM.getMManager().getMessage("MusicGUI.music-options-particle", "%Particle%", GPM.getMManager().getMessage(playSettings.isShowingParticles() ? "MusicGUI.music-options-true" : "MusicGUI.music-options-false")));
lpm.setLocalizedName("+e");
lpm.displayName(Component.text(GPM.getMManager().getMessage("MusicGUI.music-options-particle", "%Particle%", GPM.getMManager().getMessage(playSettings.isShowingParticles() ? "MusicGUI.music-options-true" : "MusicGUI.music-options-false"))));
lpm.getPersistentDataContainer().set(localizedNameKey, PersistentDataType.STRING, "+e");
lp.setItemMeta(lpm);
inventory.setItem(47, lp);

if(playSettings.getPlayList() != 2) {
lp = new ItemStack(Material.DIAMOND);
lpm = lp.getItemMeta();
lpm.setDisplayName(GPM.getMManager().getMessage("MusicGUI.music-options-join", "%Join%", GPM.getMManager().getMessage(playSettings.isPlayOnJoin() ? "MusicGUI.music-options-true" : "MusicGUI.music-options-false")));
lpm.setLocalizedName("+j");
lpm.displayName(Component.text(GPM.getMManager().getMessage("MusicGUI.music-options-join", "%Join%", GPM.getMManager().getMessage(playSettings.isPlayOnJoin() ? "MusicGUI.music-options-true" : "MusicGUI.music-options-false"))));
lpm.getPersistentDataContainer().set(localizedNameKey, PersistentDataType.STRING, "+j");
lp.setItemMeta(lpm);
inventory.setItem(48, lp);
lp = new ItemStack(Material.BLAZE_POWDER);
lpm = lp.getItemMeta();
lpm.setDisplayName(GPM.getMManager().getMessage(playSettings.getPlayMode() == 0 ? "MusicGUI.music-options-playmode-once" : playSettings.getPlayMode() == 1 ? "MusicGUI.music-options-playmode-shuffle" : "MusicGUI.music-options-playmode-repeat"));
lpm.setLocalizedName("+s");
lpm.displayName(Component.text(GPM.getMManager().getMessage(playSettings.getPlayMode() == 0 ? "MusicGUI.music-options-playmode-once" : playSettings.getPlayMode() == 1 ? "MusicGUI.music-options-playmode-shuffle" : "MusicGUI.music-options-playmode-repeat")));
lpm.getPersistentDataContainer().set(localizedNameKey, PersistentDataType.STRING, "+s");
lp.setItemMeta(lpm);
inventory.setItem(49, lp);
lp = new ItemStack(Material.TOTEM_OF_UNDYING);
lpm = lp.getItemMeta();
lpm.setDisplayName(GPM.getMManager().getMessage("MusicGUI.music-options-reverse", "%Reverse%", GPM.getMManager().getMessage(playSettings.isReverseMode() ? "MusicGUI.music-options-true" : "MusicGUI.music-options-false")));
lpm.setLocalizedName("+q");
lpm.displayName(Component.text(GPM.getMManager().getMessage("MusicGUI.music-options-reverse", "%Reverse%", GPM.getMManager().getMessage(playSettings.isReverseMode() ? "MusicGUI.music-options-true" : "MusicGUI.music-options-false"))));
lpm.getPersistentDataContainer().set(localizedNameKey, PersistentDataType.STRING, "+q");
lp.setItemMeta(lpm);
inventory.setItem(50, lp);
}

if(type == MenuType.JUKEBOX || type == MenuType.FULLJUKEBOX) {
lp = new ItemStack(Material.REDSTONE);
lpm = lp.getItemMeta();
lpm.setDisplayName(GPM.getMManager().getMessage("MusicGUI.music-options-range", "%Range%", "" + playSettings.getRange()));
lpm.setLocalizedName("+r");
lpm.displayName(Component.text(GPM.getMManager().getMessage("MusicGUI.music-options-range", "%Range%", "" + playSettings.getRange())));
lpm.getPersistentDataContainer().set(localizedNameKey, PersistentDataType.STRING, "+r");
lp.setItemMeta(lpm);
inventory.setItem(51, lp);
}
Expand All @@ -409,24 +413,24 @@ public void setPlaylistBar() {

ItemStack lp = new ItemStack(Material.CHEST);
ItemMeta lpm = lp.getItemMeta();
lpm.setDisplayName(GPM.getMManager().getMessage("MusicGUI.music-back"));
lpm.setLocalizedName("-");
lpm.displayName(Component.text(GPM.getMManager().getMessage("MusicGUI.music-back")));
lpm.getPersistentDataContainer().set(localizedNameKey, PersistentDataType.STRING, "-");
lp.setItemMeta(lpm);
inventory.setItem(45, lp);

if(playSettings.getPlayList() != 2) {
lp = new ItemStack(Material.FEATHER);
lpm = lp.getItemMeta();
lpm.setDisplayName(GPM.getMManager().getMessage("MusicGUI.music-playlist-skip"));
lpm.setLocalizedName("/l");
lpm.displayName(Component.text(GPM.getMManager().getMessage("MusicGUI.music-playlist-skip")));
lpm.getPersistentDataContainer().set(localizedNameKey, PersistentDataType.STRING, "/l");
lp.setItemMeta(lpm);
inventory.setItem(47, lp);
}

lp = new ItemStack(Material.NOTE_BLOCK);
lpm = lp.getItemMeta();
lpm.setDisplayName(GPM.getMManager().getMessage(playSettings.getPlayList() == 0 ? "MusicGUI.music-playlist-type-default" : playSettings.getPlayList() == 1 ? "MusicGUI.music-playlist-type-favorites" : "MusicGUI.music-playlist-type-radio"));
lpm.setLocalizedName("%");
lpm.displayName(Component.text(GPM.getMManager().getMessage(playSettings.getPlayList() == 0 ? "MusicGUI.music-playlist-type-default" : playSettings.getPlayList() == 1 ? "MusicGUI.music-playlist-type-favorites" : "MusicGUI.music-playlist-type-radio")));
lpm.getPersistentDataContainer().set(localizedNameKey, PersistentDataType.STRING, "%");
lp.setItemMeta(lpm);
inventory.setItem(49, lp);
}
Expand All @@ -448,12 +452,12 @@ public void setPage(int Page) {
Song s1 = songs.get(z);
ItemStack is = new ItemStack(s1.getMaterial());
ItemMeta im = is.getItemMeta();
im.setDisplayName(GPM.getMManager().getMessage("MusicGUI.disc-title", "%Title%", s1.getTitle(), "%Author%", s1.getAuthor().isEmpty() ? GPM.getMManager().getMessage("MusicGUI.disc-empty-author") : s1.getAuthor(), "%OAuthor%", s1.getOriginalAuthor().isEmpty() ? GPM.getMManager().getMessage("MusicGUI.disc-empty-oauthor") : s1.getOriginalAuthor()));
List<String> dl = new ArrayList<>();
for(String d : s1.getDescription()) dl.add(GPM.getMManager().toFormattedMessage("&6" + d));
if(playSettings.getFavorites().contains(s1)) dl.add(GPM.getMManager().getMessage("MusicGUI.disc-favorite"));
im.setLore(dl);
im.setLocalizedName("=" + s1.getId());
im.displayName(Component.text(GPM.getMManager().getMessage("MusicGUI.disc-title", "%Title%", s1.getTitle(), "%Author%", s1.getAuthor().isEmpty() ? GPM.getMManager().getMessage("MusicGUI.disc-empty-author") : s1.getAuthor(), "%OAuthor%", s1.getOriginalAuthor().isEmpty() ? GPM.getMManager().getMessage("MusicGUI.disc-empty-oauthor") : s1.getOriginalAuthor())));
ArrayList<Component> dl = new ArrayList<>();
for(String d : s1.getDescription()) dl.add(Component.text(GPM.getMManager().toFormattedMessage("&6" + d)));
if(playSettings.getFavorites().contains(s1)) dl.add(Component.text(GPM.getMManager().getMessage("MusicGUI.disc-favorite")));
im.lore(dl);
im.getPersistentDataContainer().set(localizedNameKey, PersistentDataType.STRING, "=" + s1.getId());
im.addItemFlags(ItemFlag.values());
is.setItemMeta(im);
inventory.setItem(z % 45, is);
Expand All @@ -463,29 +467,29 @@ public void setPage(int Page) {
if(page > 1) {
ItemStack lp = new ItemStack(Material.ARROW);
ItemMeta lpm = lp.getItemMeta();
lpm.setDisplayName(GPM.getMManager().getMessage("MusicGUI.last-page"));
lpm.setLocalizedName("!" + (page - 1));
lpm.displayName(Component.text(GPM.getMManager().getMessage("MusicGUI.last-page")));
lpm.getPersistentDataContainer().set(localizedNameKey, PersistentDataType.STRING, "!" + (page - 1));
lp.setItemMeta(lpm);
inventory.setItem(52, lp);
} else {
ItemStack lp = new ItemStack(Material.BLACK_STAINED_GLASS_PANE);
ItemMeta lpm = lp.getItemMeta();
lpm.setDisplayName(" ");
lpm.displayName(Component.text(" "));
lp.setItemMeta(lpm);
inventory.setItem(52, lp);
}

if(page < getMaxPageSize(songs)) {
ItemStack lp = new ItemStack(Material.ARROW);
ItemMeta lpm = lp.getItemMeta();
lpm.setDisplayName(GPM.getMManager().getMessage("MusicGUI.next-page"));
lpm.setLocalizedName("!" + (page + 1));
lpm.displayName(Component.text(GPM.getMManager().getMessage("MusicGUI.next-page")));
lpm.getPersistentDataContainer().set(localizedNameKey, PersistentDataType.STRING, "!" + (page + 1));
lp.setItemMeta(lpm);
inventory.setItem(53, lp);
} else {
ItemStack lp = new ItemStack(Material.BLACK_STAINED_GLASS_PANE);
ItemMeta lpm = lp.getItemMeta();
lpm.setDisplayName(" ");
lpm.displayName(Component.text(" "));
lp.setItemMeta(lpm);
inventory.setItem(53, lp);
}
Expand Down
55 changes: 54 additions & 1 deletion src/resources/lang/en_us.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,57 @@ Messages:
actionbar-playing: "&aNow playing:&6 %SongTitle%&7 (&6%Song%&7)"
actionbar-stop: "&aSong stopped"
actionbar-pause: "&aSong paused"
actionbar-resume: "&aSong resumed"
actionbar-resume: "&aSong resumed"



MusicGUI:

title: "&cJukebox"

music-pause: "&6Pause"
music-resume: "&6Resume"
music-stop: "&6Stop"
music-random: "&6Random Song"
music-back: "&6Back"

music-playlist: "&cPlaylist"
music-playlist-skip: "&6Skip Song"
music-playlist-type-default: "&aMode:&6 Default"
music-playlist-type-favorites: "&aMode:&6 Favorites"
music-playlist-type-radio: "&aMode:&6 Radio"

music-options: "&cOptions"
music-options-volume: "&aVolume:&b %Volume%%"
music-options-join: "&aWelcomemusic:&6 %Join%"
music-options-playmode-once: "&aMode:&6 Once"
music-options-playmode-shuffle: "&aMode:&6 Shuffle"
music-options-playmode-repeat: "&aMode:&6 Repeat"
music-options-particle: "&aParticle:&6 %Particle%"
music-options-reverse: "&aReverse:&6 %Reverse%"
music-options-range: "&aRange:&b %Range%"

music-options-true: "Yes"
music-options-false: "No"

music-search-none: "&cSearch"
music-search: "&cSearch:&6 %Search%"
music-search-menu-title: "&cSearch for a Song"
music-search-menu-field: "Search"

last-page: "&6Previous Page"
next-page: "&6Next Page"

disc-title: "&b%Title%&7 -&6 %OAuthor%"
disc-empty-author: "Unknown"
disc-empty-oauthor: "Unknown"
disc-favorite: "&cFavorite"



Items:

jukebox-title: "&cJukeBox"
jukebox-description: "&aJukeBox, which allows\n&aplaying special Music!"

disc-title: "&b%Title%&7 -&6 %OAuthor%"