Skip to content

Instantly share code, notes, and snippets.

@sirsavary
Last active April 5, 2016 21:50
Show Gist options
  • Select an option

  • Save sirsavary/bb6116005cb75af3d52aef9a698a992b to your computer and use it in GitHub Desktop.

Select an option

Save sirsavary/bb6116005cb75af3d52aef9a698a992b to your computer and use it in GitHub Desktop.
package com.mordrum.mcore.common;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.function.Consumer;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class AmbientMusicUtility {
private static Path musicPath = Paths.get(System.getProperty("user.dir") + "/ambience_music/ambience.properties");
private static URL musicURL;
static {
try {
musicURL = new URL("http://solder.mordrum.com/mods/ambient-music/ambient-music-pokemon.zip");
} catch (MalformedURLException ignored) {
}
}
public static boolean isMusicPresent() {
return Files.exists(musicPath);
}
public static Thread downloadMusic(Consumer<String> callback) {
Runnable downloadThread = () -> {
try {
HttpURLConnection urlConnection = (HttpURLConnection) musicURL.openConnection();
// Buffered file download
ZipInputStream zipInputStream = new ZipInputStream(urlConnection.getInputStream());
ZipEntry nextEntry;
while ((nextEntry = zipInputStream.getNextEntry()) != null) {
if (nextEntry.isDirectory()) {
new File(System.getProperty("user.dir") + "/" + nextEntry.getName()).mkdirs();
continue;
}
callback.accept(nextEntry.getName());
FileOutputStream fos = new FileOutputStream(System.getProperty("user.dir") + "/" + nextEntry.getName());
final byte[] buffer = new byte[102400];
int len;
while((len = (zipInputStream.read(buffer))) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
}
zipInputStream.close();
callback.accept(null);
} catch (IOException e) {
e.printStackTrace();
}
};
Thread thread = new Thread(downloadThread);
thread.setName("Music Download Thread");
thread.start();
return thread;
}
}
@Subscribe
public void onButtonClick(UIButton.ClickEvent event) {
String buttonID = event.getComponent().getName().toLowerCase();
if (buttonID.equals("button.no")) {
Minecraft.getMinecraft().displayGuiScreen(new SomeGUI(null));
} else if (buttonID.equals("button.yes")) {
progressLabel.setVisible(true);
AmbientMusicUtility.downloadMusic((fileName) -> {
if (fileName == null) this.mc.addScheduledTask(() -> {
this.mc.displayGuiScreen(new SomeGUI(null));
});
else progressLabel.setText(ChatColor.WHITE + "Downloading... " + fileName);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment