aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorbowser0000 <bowser0000@gmail.com>2021-09-02 16:24:39 -0400
committerbowser0000 <bowser0000@gmail.com>2021-09-02 16:24:39 -0400
commitaa78d52025faa74568ccdb3d299c0eb51f29aa90 (patch)
treec638387431c1131cfbcd1134b1faf1bd8584537c /src/main/java
parent948058741199d41b1e5e76970da28d45e166c244 (diff)
downloadSkyblockMod-aa78d52025faa74568ccdb3d299c0eb51f29aa90.tar.gz
SkyblockMod-aa78d52025faa74568ccdb3d299c0eb51f29aa90.tar.bz2
SkyblockMod-aa78d52025faa74568ccdb3d299c0eb51f29aa90.zip
Reduce memory issues with custom music
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/me/Danker/commands/CustomMusicCommand.java9
-rw-r--r--src/main/java/me/Danker/features/CustomMusic.java52
2 files changed, 30 insertions, 31 deletions
diff --git a/src/main/java/me/Danker/commands/CustomMusicCommand.java b/src/main/java/me/Danker/commands/CustomMusicCommand.java
index c07ab45..d21d08e 100644
--- a/src/main/java/me/Danker/commands/CustomMusicCommand.java
+++ b/src/main/java/me/Danker/commands/CustomMusicCommand.java
@@ -63,13 +63,8 @@ public class CustomMusicCommand extends CommandBase {
player.addChatMessage(new ChatComponentText(DankersSkyblockMod.MAIN_COLOUR + "Stopped custom music."));
break;
case "reload":
- try {
- CustomMusic.init(DankersSkyblockMod.configDirectory);
- player.addChatMessage(new ChatComponentText(DankersSkyblockMod.MAIN_COLOUR + "Reloaded custom music."));
- } catch (IOException | LineUnavailableException | UnsupportedAudioFileException e) {
- player.addChatMessage(new ChatComponentText(DankersSkyblockMod.ERROR_COLOUR + "An error occurred while trying to reload music."));
- e.printStackTrace();
- }
+ CustomMusic.init(DankersSkyblockMod.configDirectory);
+ player.addChatMessage(new ChatComponentText(DankersSkyblockMod.MAIN_COLOUR + "Reloaded custom music."));
break;
case "volume":
if (arg1.length < 3) {
diff --git a/src/main/java/me/Danker/features/CustomMusic.java b/src/main/java/me/Danker/features/CustomMusic.java
index 7309462..1f609cc 100644
--- a/src/main/java/me/Danker/features/CustomMusic.java
+++ b/src/main/java/me/Danker/features/CustomMusic.java
@@ -65,7 +65,7 @@ public class CustomMusic {
}
@SubscribeEvent(priority = EventPriority.LOW)
- public void onTick(TickEvent.ClientTickEvent event) {
+ public void onTick(TickEvent.ClientTickEvent event) throws UnsupportedAudioFileException, LineUnavailableException, IOException {
if (event.phase != TickEvent.Phase.START) return;
Minecraft mc = Minecraft.getMinecraft();
@@ -135,7 +135,7 @@ public class CustomMusic {
}
@SubscribeEvent(receiveCanceled = true)
- public void onChat(ClientChatReceivedEvent event) {
+ public void onChat(ClientChatReceivedEvent event) throws UnsupportedAudioFileException, LineUnavailableException, IOException {
String message = StringUtils.stripControlCodes(event.message.getUnformattedText());
if (ToggleCommand.dungeonMusic && Utils.inDungeons) {
@@ -165,7 +165,7 @@ public class CustomMusic {
}
}
- public static void init(String configDirectory) throws IOException, LineUnavailableException, UnsupportedAudioFileException {
+ public static void init(String configDirectory) {
if (configDirectory == null) return;
File directory = new File(configDirectory + "/dsmmusic");
if (!directory.exists()) directory.mkdir();
@@ -210,33 +210,30 @@ public class CustomMusic {
public static class Song {
public Clip music;
- private final List<Clip> playlist = new ArrayList<>();
+ private final List<File> playlist = new ArrayList<>();
+ private int volume;
- public Song(File directory, String songName, int volume) throws IOException, UnsupportedAudioFileException, LineUnavailableException {
+ public Song(File directory, String songName, int volume) {
File[] files = directory.listFiles();
if (files != null) {
for (File file : files) {
if (!file.isDirectory() && file.getName().matches(songName + "\\d*(?:\\.wav)?\\.wav")) { // .wav.wav moment
- Clip music = AudioSystem.getClip();
- AudioInputStream ais = AudioSystem.getAudioInputStream(file);
- music.open(ais);
- playlist.add(music);
+ playlist.add(file);
System.out.println("Added " + file.getName() + " to " + songName + " playlist.");
}
}
}
- setVolume(volume);
-
- if (playlist.size() > 0) {
- music = playlist.get(0);
- }
+ this.volume = volume;
}
- public void start() {
- if (music != null && !music.isRunning()) {
+ public void start() throws UnsupportedAudioFileException, LineUnavailableException, IOException {
+
+ if (music == null) music = AudioSystem.getClip();
+ if (!music.isRunning()) {
reset();
shuffle();
+ setVolume(volume);
cancelNotes = true;
music.setMicrosecondPosition(0);
music.start();
@@ -245,14 +242,23 @@ public class CustomMusic {
public void stop() {
cancelNotes = false;
- if (music != null) music.stop();
+ if (music != null) {
+ music.stop();
+ if (music.isOpen()) music.close();
+ }
}
- public void shuffle() {
- if (playlist.size() > 0) music = playlist.get(new Random().nextInt(playlist.size()));
+ public void shuffle() throws LineUnavailableException, UnsupportedAudioFileException, IOException {
+ if (playlist.size() > 0) {
+ File file = playlist.get(new Random().nextInt(playlist.size()));
+ music = AudioSystem.getClip();
+ AudioInputStream ais = AudioSystem.getAudioInputStream(file);
+ music.open(ais);
+ }
}
public boolean setVolume(int volume) {
+ this.volume = volume;
if (playlist.size() < 1) return false;
if (volume <= 0 || volume > 100) {
EntityPlayer player = Minecraft.getMinecraft().thePlayer;
@@ -261,11 +267,9 @@ public class CustomMusic {
}
float decibels = (float) (20 * Math.log(volume / 100.0));
- for (Clip music : playlist) {
- FloatControl control = (FloatControl) music.getControl(FloatControl.Type.MASTER_GAIN);
- if (decibels <= control.getMinimum() || decibels >= control.getMaximum()) return false;
- control.setValue(decibels);
- }
+ FloatControl control = (FloatControl) music.getControl(FloatControl.Type.MASTER_GAIN);
+ if (decibels <= control.getMinimum() || decibels >= control.getMaximum()) return false;
+ control.setValue(decibels);
return true;
}