Guest User

SoundManager.class & SoundFolder.class

a guest
Sep 30th, 2016
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.19 KB | None | 0 0
  1. public class SoundManager{
  2.    
  3.     private final String path;
  4.     private final String domain;
  5.     private final String name;
  6.    
  7.     private final List<SoundFolder> soundFolders=new ArrayList<SoundFolder>();
  8.     private final Map<String,SoundEvent> soundEvents=new HashMap<String,SoundEvent>();
  9.    
  10.     public SoundManager(String name, String pathResourcePackParent, String resourcePackDomain){
  11.         this.path=pathResourcePackParent;
  12.         this.domain=resourcePackDomain;
  13.         this.name=name;
  14.     }
  15.    
  16.     public void registerSoundFolder(SoundFolder soundFolder){
  17.         this.soundFolders.add(soundFolder);
  18.     }
  19.    
  20.     public void registerSounds(){
  21.         createSoundsJSON();
  22.         for(SoundFolder folder:this.soundFolders){
  23.             List<String> sounds=folder.getSoundList();
  24.             for(String sound:sounds){
  25.                 if(!this.soundEvents.containsKey(folder.getFolderName()+"."+sound))
  26.                     this.soundEvents.put(folder.getFolderName()+"."+sound, registerSound(this.name+"."+folder.getFolderName()+"."+sound));
  27.             }
  28.         }
  29.     }
  30.    
  31.     private final SoundEvent registerSound(String name){
  32.         ResourceLocation sound=new ResourceLocation(this.domain,name);
  33.         return GameRegistry.register(new SoundEvent(sound).setRegistryName(sound));
  34.     }
  35.    
  36.     private void createSoundsJSON(){
  37.         JsonObject soundObj=new JsonObject();
  38.         for(SoundFolder folder:this.soundFolders){
  39.             List<String> sounds=folder.getSoundList();
  40.             for(String sound:sounds){
  41.                 JsonObject soundFile=new JsonObject();
  42.                 JsonArray data=new JsonArray();
  43.                 JsonObject soundDetails=new JsonObject();
  44.                
  45.                 soundObj.add(this.name+"."+folder.getFolderName()+"."+sound, soundFile);
  46.                
  47.                 soundFile.addProperty("category", folder.getSoundCategory());
  48.                 soundFile.add("sounds", data);
  49.                
  50.                 data.add(soundDetails);
  51.                
  52.                 soundDetails.addProperty("name", this.getResourcePackDomain()+":"+folder.getFolderName()+"/"+sound);
  53.                 soundDetails.addProperty("stream", folder.getIsStreaming());
  54.             }
  55.         }
  56.        
  57.         try (Writer file = new FileWriter(this.path+"/"+this.getResourcePackDomain()+"/sounds.json")) {
  58.             Gson gson=new GsonBuilder().setPrettyPrinting().create();
  59.             gson.toJson(soundObj,file);
  60.         } catch (IOException e) {
  61.             e.printStackTrace();
  62.         }
  63.     }
  64.    
  65.     //GETTERS & SETTERS
  66.    
  67.     public final List<SoundFolder> getSoundFolders(){
  68.         return this.soundFolders;
  69.     }
  70.    
  71.     public final String getPath(){
  72.         return this.path;
  73.     }
  74.    
  75.     public final String getResourcePackDomain(){
  76.         return this.domain;
  77.     }
  78.    
  79.     public final SoundEvent getSound(String sound){
  80.         return this.soundEvents.get(sound);
  81.     }
  82.    
  83.     public final String getName(){
  84.         return this.name;
  85.     }
  86. }
  87.  
  88. public class SoundFolder{
  89.    
  90.     private final String folderName;
  91.     private final String soundCategory;
  92.     private final boolean stream;
  93.     private final List<String> soundList=new ArrayList<String>();
  94.     private final SoundManager sndManager;
  95.    
  96.     public SoundFolder(String folderName, SoundManager manager, SoundCategory soundCategory, boolean stream){
  97.         this.folderName=folderName;
  98.         this.sndManager=manager;
  99.         this.soundCategory=soundCategory.getName();
  100.         this.stream=stream;
  101.     }
  102.    
  103.     public String getFolderName(){
  104.         return this.folderName;
  105.     }
  106.    
  107.     public List<String> getSoundList(){
  108.         this.listSounds(this.sndManager.getResourcePackDomain(), this.sndManager.getPath());
  109.         return this.soundList;
  110.     }
  111.    
  112.     public String getSoundCategory(){
  113.         return this.soundCategory;
  114.     }
  115.    
  116.     public boolean getIsStreaming(){
  117.         return this.stream;
  118.     }
  119.    
  120.     public boolean folderExists(){
  121.         File file=new File(sndManager.getPath()+"/"+sndManager.getResourcePackDomain()+"/sounds/"+this.folderName);
  122.         return file.exists();
  123.     }
  124.    
  125.     private void listSounds(String domain, String path){
  126.         if(folderExists()){
  127.             File dirPath=new File(this.sndManager.getPath()+"/"+this.sndManager.getResourcePackDomain()+"/sounds/"+this.folderName);
  128.             if(dirPath.exists()){
  129.                 List<File> soundFiles=(List<File>) FileUtils.listFiles(dirPath, new String[] {"ogg"}, false);
  130.                 for(File file:soundFiles){
  131.                     try {
  132.                         this.soundList.add(FilenameUtils.getBaseName(file.getCanonicalPath()));
  133.                     } catch (IOException e) {
  134.                         e.printStackTrace();
  135.                     }
  136.                 }
  137.             }
  138.         }else
  139.             new File(sndManager.getPath()+"/"+sndManager.getResourcePackDomain()+"/sounds/"+this.folderName).mkdir();
  140.     }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment