Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class SoundManager{
- private final String path;
- private final String domain;
- private final String name;
- private final List<SoundFolder> soundFolders=new ArrayList<SoundFolder>();
- private final Map<String,SoundEvent> soundEvents=new HashMap<String,SoundEvent>();
- public SoundManager(String name, String pathResourcePackParent, String resourcePackDomain){
- this.path=pathResourcePackParent;
- this.domain=resourcePackDomain;
- this.name=name;
- }
- public void registerSoundFolder(SoundFolder soundFolder){
- this.soundFolders.add(soundFolder);
- }
- public void registerSounds(){
- createSoundsJSON();
- for(SoundFolder folder:this.soundFolders){
- List<String> sounds=folder.getSoundList();
- for(String sound:sounds){
- if(!this.soundEvents.containsKey(folder.getFolderName()+"."+sound))
- this.soundEvents.put(folder.getFolderName()+"."+sound, registerSound(this.name+"."+folder.getFolderName()+"."+sound));
- }
- }
- }
- private final SoundEvent registerSound(String name){
- ResourceLocation sound=new ResourceLocation(this.domain,name);
- return GameRegistry.register(new SoundEvent(sound).setRegistryName(sound));
- }
- private void createSoundsJSON(){
- JsonObject soundObj=new JsonObject();
- for(SoundFolder folder:this.soundFolders){
- List<String> sounds=folder.getSoundList();
- for(String sound:sounds){
- JsonObject soundFile=new JsonObject();
- JsonArray data=new JsonArray();
- JsonObject soundDetails=new JsonObject();
- soundObj.add(this.name+"."+folder.getFolderName()+"."+sound, soundFile);
- soundFile.addProperty("category", folder.getSoundCategory());
- soundFile.add("sounds", data);
- data.add(soundDetails);
- soundDetails.addProperty("name", this.getResourcePackDomain()+":"+folder.getFolderName()+"/"+sound);
- soundDetails.addProperty("stream", folder.getIsStreaming());
- }
- }
- try (Writer file = new FileWriter(this.path+"/"+this.getResourcePackDomain()+"/sounds.json")) {
- Gson gson=new GsonBuilder().setPrettyPrinting().create();
- gson.toJson(soundObj,file);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- //GETTERS & SETTERS
- public final List<SoundFolder> getSoundFolders(){
- return this.soundFolders;
- }
- public final String getPath(){
- return this.path;
- }
- public final String getResourcePackDomain(){
- return this.domain;
- }
- public final SoundEvent getSound(String sound){
- return this.soundEvents.get(sound);
- }
- public final String getName(){
- return this.name;
- }
- }
- public class SoundFolder{
- private final String folderName;
- private final String soundCategory;
- private final boolean stream;
- private final List<String> soundList=new ArrayList<String>();
- private final SoundManager sndManager;
- public SoundFolder(String folderName, SoundManager manager, SoundCategory soundCategory, boolean stream){
- this.folderName=folderName;
- this.sndManager=manager;
- this.soundCategory=soundCategory.getName();
- this.stream=stream;
- }
- public String getFolderName(){
- return this.folderName;
- }
- public List<String> getSoundList(){
- this.listSounds(this.sndManager.getResourcePackDomain(), this.sndManager.getPath());
- return this.soundList;
- }
- public String getSoundCategory(){
- return this.soundCategory;
- }
- public boolean getIsStreaming(){
- return this.stream;
- }
- public boolean folderExists(){
- File file=new File(sndManager.getPath()+"/"+sndManager.getResourcePackDomain()+"/sounds/"+this.folderName);
- return file.exists();
- }
- private void listSounds(String domain, String path){
- if(folderExists()){
- File dirPath=new File(this.sndManager.getPath()+"/"+this.sndManager.getResourcePackDomain()+"/sounds/"+this.folderName);
- if(dirPath.exists()){
- List<File> soundFiles=(List<File>) FileUtils.listFiles(dirPath, new String[] {"ogg"}, false);
- for(File file:soundFiles){
- try {
- this.soundList.add(FilenameUtils.getBaseName(file.getCanonicalPath()));
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }else
- new File(sndManager.getPath()+"/"+sndManager.getResourcePackDomain()+"/sounds/"+this.folderName).mkdir();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment