Advertisement
Corosus

Automated SoundLoader for Installing from Zips

Jan 3rd, 2013
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.38 KB | None | 0 0
  1. package zombiecraft.Forge;
  2.  
  3. import net.minecraft.client.Minecraft;
  4. import net.minecraft.client.audio.SoundManager;
  5. import net.minecraftforge.client.event.sound.SoundLoadEvent;
  6. import net.minecraftforge.event.ForgeSubscribe;
  7.  
  8. import java.io.File;
  9. import java.io.FileInputStream;
  10. import java.io.FileNotFoundException;
  11. import java.io.FilenameFilter;
  12. import java.io.InputStream;
  13. import java.net.URL;
  14. import java.util.zip.ZipEntry;
  15. import java.util.zip.ZipInputStream;
  16.  
  17. public class SoundLoader {
  18.    
  19.     class ModFilter implements FilenameFilter {
  20.         public boolean accept(File dir, String name) {
  21.             return (name.startsWith("ZC 3") && name.endsWith(".zip"));
  22.         }
  23.     }
  24.    
  25.     @ForgeSubscribe
  26.     public void onSound(SoundLoadEvent event) {
  27.        
  28.         try {
  29.         File modsPath = new File(Minecraft.getMinecraft().getAppDir("") + File.separator + ".minecraft" + File.separator + "mods");
  30.        
  31.         File[] files = modsPath.listFiles(new ModFilter());
  32.        
  33.         if (files != null) {
  34.             InputStream theFile = new FileInputStream(files[0]);
  35.             ZipInputStream stream = new ZipInputStream(theFile);
  36.            
  37.             ZipEntry ze = stream.getNextEntry();
  38.            
  39.             while (ze != null) {
  40.                 String path = ze.getName().substring(ze.getName().indexOf('/', ze.getName().indexOf('/')+1)+1);;
  41.                 if (ze.getName().contains("resources/sound/") && ze.getName().contains(".")) {
  42.                     registerSound(event.manager, path);
  43.                 } else if (ze.getName().contains("resources/music/") && ze.getName().contains(".")) {
  44.                     registerMusic(event.manager, path);
  45.                 } else if (ze.getName().contains("resources/streaming/") && ze.getName().contains(".")) {
  46.                     registerStreaming(event.manager, path);
  47.                 }
  48.                 ze = stream.getNextEntry();
  49.             }
  50.         } else {
  51.         System.out.println("WARNING: Couldn't find mods zip file for installing sounds.");
  52.         }
  53.            
  54.         } catch (Exception ex) {
  55.             ex.printStackTrace();
  56.         }
  57.     }
  58.    
  59.     private void registerSound(SoundManager manager, String path) {
  60.         try {
  61.             URL filePath = SoundLoader.class.getResource("/resources/sound/" + path);
  62.             if (filePath != null) {
  63.                 manager.soundPoolSounds.addSound(path, filePath);
  64.             } else {
  65.                 throw new FileNotFoundException();
  66.             }
  67.         } catch (Exception ex) {
  68.             System.out.println(String.format("Warning: unable to load sound file %s", path));
  69.         }
  70.     }
  71.    
  72.     private void registerStreaming(SoundManager manager, String path) {
  73.         try {
  74.             URL filePath = SoundLoader.class.getResource("/resources/streaming/" + path);
  75.             if (filePath != null) {
  76.                 manager.soundPoolStreaming.addSound(path, filePath);
  77.             } else {
  78.                 throw new FileNotFoundException();
  79.             }
  80.         } catch (Exception ex) {
  81.             System.out.println(String.format("Warning: unable to load streaming file %s"));
  82.         }
  83.     }
  84.    
  85.     private void registerMusic(SoundManager manager, String path) {
  86.         try {
  87.             URL filePath = SoundLoader.class.getResource("/resources/music/" + path);
  88.             if (filePath != null) {
  89.                 manager.soundPoolMusic.addSound(path, filePath);
  90.             } else {
  91.                 throw new FileNotFoundException();
  92.             }
  93.         } catch (Exception ex) {
  94.             System.out.println(String.format("Warning: unable to load music file %s"));
  95.         }
  96.     }
  97.  
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement