Skip to content

Commit

Permalink
Fix #5
Browse files Browse the repository at this point in the history
  • Loading branch information
bivashy committed Nov 9, 2022
1 parent 1f7af5a commit e74fbee
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
Expand All @@ -16,8 +15,9 @@
import org.bukkit.event.Listener;

import com.ubivashka.plasmovoice.PlasmoVoiceAddon;
import com.ubivashka.plasmovoice.event.url.URLSoundCreateEvent;
import com.ubivashka.plasmovoice.event.url.URLSoundFormatPreCreateEvent;
import com.ubivashka.plasmovoice.event.url.URLSoundPlayEvent;
import com.ubivashka.plasmovoice.event.url.URLSoundPreCreateEvent;
import com.ubivashka.plasmovoice.sound.ISound;
import com.ubivashka.plasmovoice.sound.ISoundFormat;
import com.ubivashka.plasmovoice.sound.cache.CachedSound;
Expand All @@ -33,6 +33,9 @@ public CacheListener(PlasmoVoiceAddon plugin) {

@EventHandler
public void onSoundPlay(URLSoundPlayEvent e) {
Optional<CachedSound> cachedSoundOptional = plugin.getCachedSoundHolder().findCachedSound(e.getSoundEventModel().getSource());
if (cachedSoundOptional.isPresent())
return;
ISoundFrameProvider frameProvider = e.getSoundPlaySession().getSound().getFrameProvider();
if (frameProvider.getFramesCount() <= 0)
return;
Expand All @@ -50,6 +53,12 @@ public void onSoundPlay(URLSoundPlayEvent e) {
if (frames.isEmpty())
return;
try {
Path cacheFolder = plugin.getPluginConfig().getCachingSettings().getCacheFolder().toPath();
Path cachedSoundPath = Files.createTempFile(cacheFolder, "", "");
plugin.getCachedSoundHolder()
.add(new CachedSound(url.toString(), e.getSoundFormat().getName(),
cachedSoundPath.getFileName().toString()));

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
dataOutputStream.writeUTF(e.getSoundFormat().getName());
Expand All @@ -62,10 +71,7 @@ public void onSoundPlay(URLSoundPlayEvent e) {
ex.printStackTrace();
}
});
Path cacheFolder = plugin.getPluginConfig().getCachingSettings().getCacheFolder().toPath();
Path cachedSoundPath = Files.createTempFile(cacheFolder, "", "");
Files.write(cachedSoundPath, outputStream.toByteArray());
plugin.getCachedSoundHolder().add(new CachedSound(url.toString(), cachedSoundPath.toString()));
} catch(IOException ex) {
ex.printStackTrace();
}
Expand All @@ -76,11 +82,11 @@ public void onSoundPlay(URLSoundPlayEvent e) {
}

@EventHandler
public void onUrlSoundCreate(URLSoundCreateEvent e) throws IOException {
public void onUrlSoundPreCreate(URLSoundPreCreateEvent e) throws IOException {
Optional<CachedSound> cachedSoundOptional = plugin.getCachedSoundHolder().findCachedSound(e.getSoundEventModel().getSource());
if (!cachedSoundOptional.isPresent())
return;
Path cachedSoundPath = Paths.get(cachedSoundOptional.get().getCachedFile());
Path cachedSoundPath = plugin.getPluginConfig().getCachingSettings().getCacheFolder().toPath().resolve(cachedSoundOptional.get().getCachedFile());
if (!Files.exists(cachedSoundPath)) {
plugin.getCachedSoundHolder().remove(cachedSoundOptional.get());
return;
Expand All @@ -94,10 +100,21 @@ public void onUrlSoundCreate(URLSoundCreateEvent e) throws IOException {
List<byte[]> frameList = new ArrayList<>();
for (int i = 0; i < framesCount; i++) {
int frameSize = dataInputStream.readInt();
byte[] newFrame = dataInputStream.readNBytes(frameSize);
byte[] newFrame = new byte[frameSize];
dataInputStream.read(newFrame);
frameList.add(newFrame);
}
e.getSoundEventModel().setSource(cachedSoundPath.toUri().toURL());
e.setSound(ISound.of(frameList, foundSoundFormat.get()));
}

@EventHandler
public void onUrlSoundFormatPreCreate(URLSoundFormatPreCreateEvent e) {
Optional<CachedSound> cachedSoundOptional = plugin.getCachedSoundHolder().findCachedSound(e.getSoundEventModel().getSource());
if (!cachedSoundOptional.isPresent())
return;
e.setSoundFormat(plugin.getSoundFormatHolder()
.findFirstByPredicate(soundFormat -> soundFormat.getName().equals(cachedSoundOptional.get().getSoundFormat()))
.orElse(null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,25 @@
public class CachedSound {
@SerializedName("url")
private String url;
@SerializedName("format")
private String soundFormat;
@SerializedName("file")
private String cachedFile;

public CachedSound(String url, String cachedFile) {
public CachedSound(String url, String soundFormat, String cachedFile) {
this.url = url;
this.soundFormat = soundFormat;
this.cachedFile = cachedFile;
}

public String getUrl() {
return url;
}

public String getSoundFormat() {
return soundFormat;
}

public String getCachedFile() {
return cachedFile;
}
Expand Down

0 comments on commit e74fbee

Please sign in to comment.