Advertisement
Corosus

Untitled

Sep 12th, 2011
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.33 KB | None | 0 0
  1. package net.minecraft.src;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.lang.reflect.Field;
  6. import java.util.Random;
  7. import net.minecraft.client.Minecraft;
  8. import net.minecraft.src.CodecMus;
  9. import net.minecraft.src.EntityLiving;
  10. import net.minecraft.src.GameSettings;
  11. import net.minecraft.src.MathHelper;
  12. import net.minecraft.src.SoundPool;
  13. import net.minecraft.src.SoundPoolEntry;
  14. import net.minecraft.src.mod_PathingActivated;
  15. import paulscode.sound.SoundSystem;
  16. import paulscode.sound.SoundSystemConfig;
  17. import paulscode.sound.codecs.CodecIBXM;
  18. import paulscode.sound.codecs.CodecJOrbis;
  19. import paulscode.sound.codecs.CodecWav;
  20. import paulscode.sound.libraries.LibraryLWJGLOpenAL;
  21.  
  22. public class SoundManager {
  23.  
  24.    private static SoundSystem sndSystem;
  25.    private SoundPool soundPoolSounds = new SoundPool();
  26.    private SoundPool soundPoolStreaming = new SoundPool();
  27.    private SoundPool soundPoolMusic = new SoundPool();
  28.    private SoundPool cave = new SoundPool();
  29.    private int field_587_e = 0;
  30.    private GameSettings options;
  31.    private static boolean loaded = false;
  32.    private Random rand = new Random();
  33.    private Minecraft mc;
  34.    private static final int MUSINTERVAL = 6000;
  35.    private int ticksBeforeMusic;
  36.    
  37.    public long lastSndPosUpdate = 0L;
  38.    public long soundUpdateCount = 0L;
  39.  
  40.  
  41.    public SoundManager() {
  42.       this.ticksBeforeMusic = this.rand.nextInt(6000);
  43.    }
  44.  
  45.    public void loadSoundSettings(GameSettings var1) {
  46.       this.soundPoolStreaming.field_1657_b = false;
  47.       this.options = var1;
  48.       if(!loaded && (var1 == null || var1.soundVolume != 0.0F || var1.musicVolume != 0.0F)) {
  49.          this.tryToSetLibraryAndCodecs();
  50.       }
  51.  
  52.       loadModAudio("minecraft/resources/mod/sound", this.soundPoolSounds);
  53.       loadModAudio("minecraft/resources/mod/streaming", this.soundPoolStreaming);
  54.       loadModAudio("minecraft/resources/mod/music", this.soundPoolMusic);
  55.       loadModAudio("minecraft/resources/mod/cavemusic", this.cave);
  56.  
  57.       try {
  58.          Field var2 = Minecraft.class.getDeclaredFields()[1];
  59.          var2.setAccessible(true);
  60.          this.mc = (Minecraft)var2.get((Object)null);
  61.       } catch (Throwable var3) {
  62.          ;
  63.       }
  64.  
  65.    }
  66.  
  67.    private static void loadModAudio(String var0, SoundPool var1) {
  68.       File var2 = Minecraft.getAppDir(var0);
  69.  
  70.       try {
  71.          walkFolder(var2, var2, var1);
  72.       } catch (IOException var4) {
  73.          var4.printStackTrace();
  74.       }
  75.  
  76.    }
  77.  
  78.    private static void walkFolder(File var0, File var1, SoundPool var2) throws IOException {
  79.       if(var1.exists() || var1.mkdirs()) {
  80.          File[] var3 = var1.listFiles();
  81.          if(var3 != null && var3.length > 0) {
  82.             for(int var4 = 0; var4 < var3.length; ++var4) {
  83.                if(!var3[var4].getName().startsWith(".")) {
  84.                   if(var3[var4].isDirectory()) {
  85.                      walkFolder(var0, var3[var4], var2);
  86.                   } else if(var3[var4].isFile()) {
  87.                      String var5 = var3[var4].getPath().substring(var0.getPath().length() + 1).replace('\\', '/');
  88.                      var2.addSound(var5, var3[var4]);
  89.                   }
  90.                }
  91.             }
  92.          }
  93.       }
  94.  
  95.    }
  96.  
  97.    private void tryToSetLibraryAndCodecs() {
  98.       try {
  99.          float var1 = this.options.soundVolume;
  100.          float var2 = this.options.musicVolume;
  101.          this.options.soundVolume = 0.0F;
  102.          this.options.musicVolume = 0.0F;
  103.          this.options.saveOptions();
  104.          SoundSystemConfig.addLibrary(LibraryLWJGLOpenAL.class);
  105.          SoundSystemConfig.setCodec("ogg", CodecJOrbis.class);
  106.          SoundSystemConfig.setCodec("mus", CodecMus.class);
  107.          SoundSystemConfig.setCodec("wav", CodecWav.class);
  108.  
  109.          try {
  110.             if(Class.forName("paulscode.sound.codecs.CodecIBXM") != null) {
  111.                SoundSystemConfig.setCodec("xm", CodecIBXM.class);
  112.                SoundSystemConfig.setCodec("s3m", CodecIBXM.class);
  113.                SoundSystemConfig.setCodec("mod", CodecIBXM.class);
  114.             }
  115.          } catch (ClassNotFoundException var4) {
  116.             ;
  117.          }
  118.  
  119.          sndSystem = new SoundSystem();
  120.          this.options.soundVolume = var1;
  121.          this.options.musicVolume = var2;
  122.          this.options.saveOptions();
  123.       } catch (Throwable var5) {
  124.          var5.printStackTrace();
  125.          System.err.println("error linking with the LibraryJavaSound plug-in");
  126.       }
  127.  
  128.       loaded = true;
  129.    }
  130.  
  131.    public void onSoundOptionsChanged() {
  132.       if(!loaded && (this.options.soundVolume != 0.0F || this.options.musicVolume != 0.0F)) {
  133.          this.tryToSetLibraryAndCodecs();
  134.       }
  135.  
  136.       if(sndSystem != null && loaded) {
  137.          if(this.options.musicVolume == 0.0F) {
  138.             sndSystem.stop("BgMusic");
  139.          } else {
  140.             sndSystem.setVolume("BgMusic", this.options.musicVolume);
  141.          }
  142.       }
  143.  
  144.    }
  145.  
  146.    public void closeMinecraft() {
  147.       if(loaded) {
  148.          sndSystem.cleanup();
  149.       }
  150.  
  151.    }
  152.  
  153.    public void addSound(String var1, File var2) {
  154.       this.soundPoolSounds.addSound(var1, var2);
  155.    }
  156.  
  157.    public void addStreaming(String var1, File var2) {
  158.       this.soundPoolStreaming.addSound(var1, var2);
  159.    }
  160.  
  161.    public void addMusic(String var1, File var2) {
  162.       this.soundPoolMusic.addSound(var1, var2);
  163.    }
  164.  
  165.    public void playRandomMusicIfReady() {
  166.       if(loaded && this.options.musicVolume != 0.0F && sndSystem != null && !sndSystem.playing("BgMusic") && !sndSystem.playing("streaming")) {
  167.          if(this.ticksBeforeMusic > 0) {
  168.             --this.ticksBeforeMusic;
  169.             return;
  170.          }
  171.  
  172.          SoundPoolEntry var1 = null;
  173.          if(this.mc != null && this.mc.thePlayer != null && !this.mc.thePlayer.worldObj.canBlockSeeTheSky(MathHelper.floor_double(this.mc.thePlayer.posX), MathHelper.floor_double(this.mc.thePlayer.posY), MathHelper.floor_double(this.mc.thePlayer.posZ))) {
  174.             var1 = this.cave.getRandomSound();
  175.          } else {
  176.             var1 = this.soundPoolMusic.getRandomSound();
  177.          }
  178.  
  179.          if(var1 != null) {
  180.             this.ticksBeforeMusic = this.rand.nextInt(6000) + 6000;
  181.             sndSystem.backgroundMusic("BgMusic", var1.soundUrl, var1.soundName, false);
  182.             sndSystem.setVolume("BgMusic", this.options.musicVolume);
  183.             sndSystem.play("BgMusic");
  184.          }
  185.       }
  186.  
  187.    }
  188.  
  189.    public void func_338_a(EntityLiving var1, float var2) {
  190.        
  191.        
  192.        
  193.       if(loaded && this.options.soundVolume != 0.0F && sndSystem != null && var1 != null) {
  194.          
  195.          if (System.currentTimeMillis() < this.lastSndPosUpdate) {
  196.             return;
  197.          }
  198.          this.lastSndPosUpdate = System.currentTimeMillis() + 1000L;
  199.          
  200.          float var3 = var1.prevRotationYaw + (var1.rotationYaw - var1.prevRotationYaw) * var2;
  201.          double var4 = var1.prevPosX + (var1.posX - var1.prevPosX) * (double)var2;
  202.          double var6 = var1.prevPosY + (var1.posY - var1.prevPosY) * (double)var2;
  203.          double var8 = var1.prevPosZ + (var1.posZ - var1.prevPosZ) * (double)var2;
  204.          float var10 = MathHelper.cos(-var3 * 0.01745329F - 3.141593F);
  205.          float var11 = MathHelper.sin(-var3 * 0.01745329F - 3.141593F);
  206.          float var12 = -var11;
  207.          float var13 = 0.0F;
  208.          float var14 = -var10;
  209.          float var15 = 0.0F;
  210.          float var16 = 1.0F;
  211.          float var17 = 0.0F;
  212.          sndSystem.setListenerPosition((float)var4, (float)var6, (float)var8);
  213.          sndSystem.setListenerOrientation(var12, var13, var14, var15, var16, var17);
  214.       }
  215.  
  216.    }
  217.  
  218.    public void playStreaming(String var1, float var2, float var3, float var4, float var5, float var6) {
  219.       if(loaded && this.options.soundVolume != 0.0F && sndSystem != null) {
  220.          String var7 = "streaming";
  221.          if(sndSystem.playing("streaming")) {
  222.             sndSystem.stop("streaming");
  223.          }
  224.  
  225.          if(var1 != null) {
  226.             SoundPoolEntry var8 = this.soundPoolStreaming.getRandomSoundFromSoundPool(var1);
  227.             if(var8 != null && var5 > 0.0F) {
  228.                if(sndSystem.playing("BgMusic")) {
  229.                   sndSystem.stop("BgMusic");
  230.                }
  231.  
  232.                float var9 = 16.0F;
  233.                sndSystem.newStreamingSource(true, var7, var8.soundUrl, var8.soundName, false, var2, var3, var4, 2, var9 * 4.0F);
  234.                sndSystem.setVolume(var7, 0.5F * this.options.soundVolume);
  235.                sndSystem.play(var7);
  236.             }
  237.          }
  238.       }
  239.  
  240.    }
  241.  
  242.    public void playSound(String var1, float var2, float var3, float var4, float var5, float var6) {
  243.        //System.out.println(sndSystem);
  244.        try {
  245.            mod_PathingActivated.spawnSoundTrace(var1, var2, var3, var4, var5, var6);
  246.        } catch (Exception ex) {
  247.            
  248.        }
  249.       boolean nothing = false;
  250.       if(loaded && this.options.soundVolume != 0.0F && sndSystem != null) {
  251.          SoundPoolEntry var7 = this.soundPoolSounds.getRandomSoundFromSoundPool(var1);
  252.          if(var7 != null && var5 > 0.0F) {
  253.             this.field_587_e = (this.field_587_e + 1) % 256;
  254.             String var8 = "sound_" + this.field_587_e;
  255.             float var9 = 16.0F;
  256.             if(var5 > 1.0F) {
  257.                var9 *= var5;
  258.             }
  259.  
  260.             sndSystem.newSource(var5 > 1.0F, var8, var7.soundUrl, var7.soundName, false, var2, var3, var4, 2, var9);
  261.             sndSystem.setPitch(var8, var6);
  262.             if(var5 > 1.0F) {
  263.                var5 = 1.0F;
  264.             }
  265.  
  266.             sndSystem.setVolume(var8, var5 * this.options.soundVolume);
  267.             sndSystem.play(var8);
  268.          }
  269.       }
  270.  
  271.    }
  272.  
  273.    public void playSoundFX(String var1, float var2, float var3) {
  274.       if(loaded && this.options.soundVolume != 0.0F && sndSystem != null) {
  275.          SoundPoolEntry var4 = this.soundPoolSounds.getRandomSoundFromSoundPool(var1);
  276.          if(var4 != null) {
  277.             this.field_587_e = (this.field_587_e + 1) % 256;
  278.             String var5 = "sound_" + this.field_587_e;
  279.             sndSystem.newSource(false, var5, var4.soundUrl, var4.soundName, false, 0.0F, 0.0F, 0.0F, 0, 0.0F);
  280.             if(var2 > 1.0F) {
  281.                var2 = 1.0F;
  282.             }
  283.  
  284.             var2 *= 0.25F;
  285.             sndSystem.setPitch(var5, var3);
  286.             sndSystem.setVolume(var5, var2 * this.options.soundVolume);
  287.             sndSystem.play(var5);
  288.          }
  289.       }
  290.  
  291.    }
  292.  
  293. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement