Recoding PlayerManager

This commit is contained in:
Navoei
2025-01-30 16:12:54 -06:00
parent 906c480896
commit f423ab29c3
2 changed files with 43 additions and 22 deletions

View File

@@ -52,7 +52,8 @@ repositories {
} }
// You need this maven repository if you want to use the paper dependency // You need this maven repository if you want to use the paper dependency
maven { maven {
url = uri("https://papermc.io/repo/repository/maven-public/") name = "papermc"
url = uri("https://repo.papermc.io/repository/maven-public/")
} }
maven { url "https://repo.dmulloy2.net/repository/public/" } maven { url "https://repo.dmulloy2.net/repository/public/" }
maven { url = "https://repo.codemc.org/repository/maven-public/" } maven { url = "https://repo.codemc.org/repository/maven-public/" }

View File

@@ -21,6 +21,7 @@ import javax.annotation.Nullable;
import javax.sound.sampled.*; import javax.sound.sampled.*;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
@@ -65,8 +66,7 @@ public class PlayerManager {
AtomicBoolean stopped = new AtomicBoolean(); AtomicBoolean stopped = new AtomicBoolean();
AtomicReference<de.maxhenkel.voicechat.api.audiochannel.AudioPlayer> player = new AtomicReference<>(); AtomicReference<de.maxhenkel.voicechat.api.audiochannel.AudioPlayer> player = new AtomicReference<>();
PlayerReference playerReference = new PlayerReference(() -> {
playerMap.put(id, new PlayerReference(() -> {
synchronized (stopped) { synchronized (stopped) {
stopped.set(true); stopped.set(true);
de.maxhenkel.voicechat.api.audiochannel.AudioPlayer audioPlayer = player.get(); de.maxhenkel.voicechat.api.audiochannel.AudioPlayer audioPlayer = player.get();
@@ -74,7 +74,9 @@ public class PlayerManager {
audioPlayer.stopPlaying(); audioPlayer.stopPlaying();
} }
} }
}, player, soundFilePath)); }, player, soundFilePath);
playerMap.put(id, playerReference);
executorService.execute(() -> { executorService.execute(() -> {
de.maxhenkel.voicechat.api.audiochannel.AudioPlayer audioPlayer = playChannel(api, audioChannel, block, soundFilePath, playersInRange); de.maxhenkel.voicechat.api.audiochannel.AudioPlayer audioPlayer = playChannel(api, audioChannel, block, soundFilePath, playersInRange);
@@ -83,8 +85,10 @@ public class PlayerManager {
return; return;
} }
audioPlayer.setOnStopped(() -> { audioPlayer.setOnStopped(() -> {
plugin.getServer().getRegionScheduler().run(plugin, block.getLocation(), scheduledTask -> HopperManager.instance().discToHopper(block)); //plugin.getServer().getRegionScheduler().run(plugin, block.getLocation(), scheduledTask -> HopperManager.instance().discToHopper(block));
playerMap.remove(id); if (playerMap.containsValue(playerReference)) {
playerMap.remove(id);
}
}); });
synchronized (stopped) { synchronized (stopped) {
if (!stopped.get()) { if (!stopped.get()) {
@@ -99,21 +103,22 @@ public class PlayerManager {
@Nullable @Nullable
private de.maxhenkel.voicechat.api.audiochannel.AudioPlayer playChannel(VoicechatServerApi api, AudioChannel audioChannel, Block block, Path soundFilePath, Collection<ServerPlayer> playersInRange) { private de.maxhenkel.voicechat.api.audiochannel.AudioPlayer playChannel(VoicechatServerApi api, AudioChannel audioChannel, Block block, Path soundFilePath, Collection<ServerPlayer> playersInRange) {
try { //short[] audio = readSoundFile(soundFilePath);
short[] audio = readSoundFile(soundFilePath); AudioPlayer audioPlayer = api.createAudioPlayer(audioChannel, api.createEncoder(), () -> {
AudioPlayer audioPlayer = api.createAudioPlayer(audioChannel, api.createEncoder(), audio); try {
audioPlayer.startPlaying(); return readSoundFile(soundFilePath);
return audioPlayer; } catch (Exception e) {
} catch (Exception e) { plugin.getLogger().info("Error Occurred At: " + block.getLocation());
e.printStackTrace(); for (ServerPlayer serverPlayer : playersInRange) {
plugin.getLogger().info("Error Occurred At: " + block.getLocation()); Player bukkitPlayer = (Player) serverPlayer.getPlayer();
for (ServerPlayer serverPlayer : playersInRange) { TextComponent textComponent = Component.text("An error has occurred while trying to play this disc.").color(NamedTextColor.RED);
Player bukkitPlayer = (Player) serverPlayer.getPlayer(); bukkitPlayer.sendMessage(textComponent);
TextComponent textComponent = Component.text("An error has occurred while trying to play this disc.").color(NamedTextColor.RED); }
bukkitPlayer.sendMessage(textComponent); return null;
} }
return null; });
} audioPlayer.startPlaying();
return audioPlayer;
} }
private static short[] readSoundFile(Path file) throws UnsupportedAudioFileException, IOException { private static short[] readSoundFile(Path file) throws UnsupportedAudioFileException, IOException {
@@ -144,7 +149,23 @@ public class PlayerManager {
assert finalInputStream != null; assert finalInputStream != null;
return adjustVolume(finalInputStream.readAllBytes(), CustomDiscs.getInstance().musicDiscVolume); //byte[] audioPacket = inputStreamToPackets(finalInputStream);
return adjustVolume(finalInputStream.readNBytes(1920), CustomDiscs.getInstance().musicDiscVolume);
}
private static byte[] inputStreamToPackets(AudioInputStream inputStream) throws IOException {
int FRAME_SIZE_BYTES = 1920;
byte[] buffer = new byte[FRAME_SIZE_BYTES]; // Buffer to hold 960 bytes of audio data
int bytesRead = inputStream.read(buffer);
// If fewer than 960 bytes are read, pad with zeros
if (bytesRead < FRAME_SIZE_BYTES) {
for (int i = bytesRead; i < FRAME_SIZE_BYTES; i++) {
buffer[i] = 0; // Pad with zero
}
}
System.out.println(Arrays.toString(buffer));
return buffer;
} }
private static byte[] adjustVolume(byte[] audioSamples, double volume) { private static byte[] adjustVolume(byte[] audioSamples, double volume) {
@@ -181,7 +202,6 @@ public class PlayerManager {
if (player != null) { if (player != null) {
player.onStop.stop(); player.onStop.stop();
} }
playerMap.remove(id);
} }
public static float getLengthSeconds(Path file) throws UnsupportedAudioFileException, IOException { public static float getLengthSeconds(Path file) throws UnsupportedAudioFileException, IOException {