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
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ private void playBoxTimer(UUID UUID, Song Song, Timer Timer) {

if(np.getSound() != null) {

float v = np.isVariableVolume() ? (float) ((pl.get(P) - playSettings.getRange()) * playSettings.getFixedVolume() / (double) -playSettings.getRange()) : np.getVolume();
float v = 1f;
// TODO: FIX THIS
// float v = np.isVariableVolume() ? (float) ((pl.get(P) - playSettings.getRange()) * playSettings.getFixedVolume() / (double) -playSettings.getRange()) : np.getVolume();

Location location = np.getDistance() == 0 ? P.getLocation() : GPM.getMusicUtil().getSteroNoteUtil().convertToStero(P.getLocation(), np.getDistance());

Expand Down
102 changes: 99 additions & 3 deletions core/src/main/java/dev/geco/gmusic/manager/NBSManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ public void convertFile(File NBSFile) {
List<String> gnbsContent = new ArrayList<>();
List<Byte> gnbsInstruments = new ArrayList<>();

// Get the volume and direction of each layer in the song
List<Byte> layerVolumes = new ArrayList<>();
List<Integer> layerDirections = new ArrayList<>();
getLayerInfo(NBSFile, header, layerVolumes, layerDirections);

int layer = -1;

while(true) {

short jt = readShort(dataInput);
Expand All @@ -65,18 +72,30 @@ public void convertFile(File NBSFile) {
while(true) {

short jl = readShort(dataInput);
if(jl == 0) break;
if(jl == 0) {
layer = -1;
break;
}
layer = layer + jl;
byte i = dataInput.readByte();
byte k = dataInput.readByte();
int p = 100;
int v = 100;

if(version >= 4) {
dataInput.readByte();
v = dataInput.readByte();
p = 200 - dataInput.readUnsignedByte();
readShort(dataInput);
}

String contentPart = i + "::#" + (k - 33) + (p == 100 ? "" : ":" + p);
// Combine the layer volume with the noteblock volume
// If the layer panning is not center, combine the layer & noteblock direction
v = (layerVolumes.get(layer) * v) / 100;
if (layerDirections.get(layer) != 100) {
p = (layerDirections.get(layer) + p) / 2;
}

String contentPart = i + ":" + v + ":#" + (k - 33) + (p == 100 ? "" : ":" + p);

content.append(content.toString().endsWith("!") ? contentPart : "_" + contentPart);

Expand Down Expand Up @@ -165,4 +184,81 @@ private String readString(DataInputStream DataInput) throws IOException {
return builder.toString();
}

private void getLayerInfo(File NBSFile, Short numLayers,
List<Byte> layerVolumes, List<Integer> layerDirections) {
try {
DataInputStream dataInput = new DataInputStream(Files.newInputStream(NBSFile.toPath()));

// Skip through header section, we don't care about this
short type = readShort(dataInput);
int version = 0;
if(type == 0) {
version = dataInput.readByte();
dataInput.readByte();
if(version >= 3) readShort(dataInput);
}
readShort(dataInput);
readString(dataInput);
readString(dataInput);
readString(dataInput);
readString(dataInput);
readShort(dataInput);
dataInput.readBoolean();
dataInput.readByte();
dataInput.readByte();
readInt(dataInput);
readInt(dataInput);
readInt(dataInput);
readInt(dataInput);
readInt(dataInput);
readString(dataInput);
if(version >= 4) {
dataInput.readByte();
dataInput.readByte();
readShort(dataInput);
}

// Skip through note blocks section, we don't care about this either
while(true) {

short jt = readShort(dataInput);
if(jt == 0) break;

while(true) {

short jl = readShort(dataInput);
if(jl == 0) {
break;
}
dataInput.readByte();
dataInput.readByte();

if(version >= 4) {
dataInput.readByte();
dataInput.readUnsignedByte();
readShort(dataInput);
}
}
}

// Get volume and direction of each layer
// This is the bit we actually care about
for(int layer = 0; layer < numLayers; layer++) {
readString(dataInput);
if(version >= 4) dataInput.readByte();

byte layerVolume = dataInput.readByte();
layerVolumes.add(layerVolume);

int layerDirection = 100;
if(version >= 2) {
layerDirection = 200 - dataInput.readUnsignedByte();
}
layerDirections.add(layerDirection);
}

} catch (Throwable e) { e.printStackTrace(); }

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void run() {

if(np.getSound() != null) {

float volume = np.isVariableVolume() ? playSettings.getFixedVolume() : np.getVolume();
float volume = playSettings.getFixedVolume() * np.getVolume();

Location location = np.getDistance() == 0 ? Player.getLocation() : GPM.getMusicUtil().getSteroNoteUtil().convertToStero(Player.getLocation(), np.getDistance());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private void playTimer(Song S, Timer T) {

PlaySettings ps1 = GPM.getPlaySettingsManager().getPlaySettings(P.getUniqueId());

float v = np.isVariableVolume() ? ps1.getFixedVolume() : np.getVolume();
float v = ps1.getFixedVolume() * np.getVolume();

Location L = np.getDistance() == 0 ? P.getLocation() : GPM.getMusicUtil().getSteroNoteUtil().convertToStero(P.getLocation(), np.getDistance());

Expand Down
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
Loading