Skip to content

Commit

Permalink
Improve Load Sounds tweak
Browse files Browse the repository at this point in the history
Volume control via config
Fixes #564
  • Loading branch information
ACGaming committed Oct 17, 2024
1 parent 11a1316 commit 3d14ddc
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1792,19 +1792,27 @@ public static class LoadSoundsCategory
public EnumSoundModes utLoadSoundMode = EnumSoundModes.NOTHING;

@Config.Name("[2] Minecraft Loaded Sounds")
@Config.Comment({"Sounds to play when Minecraft is loaded", "Syntax: eventname;pitch"})
@Config.Comment
({
"Sounds to play when Minecraft is loaded",
"Syntax: eventname;pitch;volume"
})
public String[] utLoadSoundMC = new String[]
{
"entity.experience_orb.pickup;1.0",
"entity.player.levelup;1.0"
"entity.experience_orb.pickup;1.0;1.0",
"entity.player.levelup;1.0;1.0"
};

@Config.Name("[3] World Loaded Sounds")
@Config.Comment({"Sounds to play when the world is loaded", "Syntax: eventname;pitch"})
@Config.Comment
({
"Sounds to play when the world is loaded",
"Syntax: eventname;pitch;volume"
})
public String[] utLoadSoundWorld = new String[]
{
"entity.experience_orb.pickup;1.0",
"entity.player.levelup;1.0"
"entity.experience_orb.pickup;1.0;1.0",
"entity.player.levelup;1.0;1.0"
};

public enum EnumSoundModes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
import java.util.Random;

import net.minecraft.client.Minecraft;
import net.minecraft.client.audio.ISound;
import net.minecraft.client.audio.PositionedSoundRecord;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.client.gui.GuiMainMenu;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvent;
import net.minecraftforge.client.event.GuiOpenEvent;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
Expand All @@ -31,6 +34,8 @@ public class UTLoadSound
private static final List<SoundEvent> soundListWorld = new ArrayList<>();
private static final List<Float> pitchListMC = new ArrayList<>();
private static final List<Float> pitchListWorld = new ArrayList<>();
private static final List<Float> volumeListMC = new ArrayList<>();
private static final List<Float> volumeListWorld = new ArrayList<>();
private static boolean playedMenu = false;
private static boolean playedWorld = false;

Expand All @@ -40,36 +45,63 @@ public static void initLists()
soundListWorld.clear();
pitchListMC.clear();
pitchListWorld.clear();
volumeListMC.clear();
volumeListWorld.clear();

for (String sound : UTConfigTweaks.MISC.LOAD_SOUNDS.utLoadSoundMC)
{
String[] soundEntries = sound.split(";");
SoundEvent soundEvent = SoundEvent.REGISTRY.getObject(new ResourceLocation(soundEntries[0]));
if (soundEvent != null)
{
Float pitch = new Float(soundEntries[1]);
Float pitch = 1.0F;
Float volume = 1.0F;
try
{
pitch = new Float(soundEntries[1]);
volume = new Float(soundEntries[2]);
}
catch (Exception e)
{
UniversalTweaks.LOGGER.warn("Unable to parse sound properties for {}", new ResourceLocation(soundEntries[0]));
}
soundListMC.add(soundEvent);
pitchListMC.add(pitch);
volumeListMC.add(volume);
}
else
{
UniversalTweaks.LOGGER.warn("Unable to find sound: {}", new ResourceLocation(soundEntries[0]));
}
}

for (String sound : UTConfigTweaks.MISC.LOAD_SOUNDS.utLoadSoundWorld)
{
String[] soundEntries = sound.split(";");
SoundEvent soundEvent = SoundEvent.REGISTRY.getObject(new ResourceLocation(soundEntries[0]));
if (soundEvent != null)
{
Float pitch = new Float(soundEntries[1]);
Float pitch = 1.0F;
Float volume = 1.0F;
try
{
pitch = new Float(soundEntries[1]);
volume = new Float(soundEntries[2]);
}
catch (Exception e)
{
UniversalTweaks.LOGGER.warn("Unable to parse sound properties for {}", new ResourceLocation(soundEntries[0]));
}
soundListWorld.add(soundEvent);
pitchListWorld.add(pitch);
volumeListWorld.add(volume);
}
else
{
UniversalTweaks.LOGGER.warn("Unable to find sound: {}", new ResourceLocation(soundEntries[0]));
}
}

UniversalTweaks.LOGGER.info("Load Sound lists initialized");
}

Expand All @@ -79,7 +111,8 @@ public static void playRandomSoundMC()
int randomIndex = random.nextInt(soundListMC.size());
SoundEvent soundEvent = soundListMC.get(randomIndex);
Float pitch = pitchListMC.get(randomIndex);
Minecraft.getMinecraft().getSoundHandler().playSound(PositionedSoundRecord.getMasterRecord(soundEvent, pitch));
Float volume = volumeListMC.get(randomIndex);
Minecraft.getMinecraft().getSoundHandler().playSound(new PositionedSoundRecord(soundEvent.getSoundName(), SoundCategory.MASTER, volume, pitch, false, 0, ISound.AttenuationType.NONE, 0F, 0F, 0F));
}

public static void playRandomSoundWorld()
Expand All @@ -88,7 +121,9 @@ public static void playRandomSoundWorld()
int randomIndex = random.nextInt(soundListWorld.size());
SoundEvent soundEvent = soundListWorld.get(randomIndex);
Float pitch = pitchListWorld.get(randomIndex);
Minecraft.getMinecraft().getSoundHandler().playSound(PositionedSoundRecord.getMasterRecord(soundEvent, pitch));
Float volume = volumeListWorld.get(randomIndex);
EntityPlayer player = Minecraft.getMinecraft().player;
Minecraft.getMinecraft().getSoundHandler().playSound(new PositionedSoundRecord(soundEvent.getSoundName(), SoundCategory.MASTER, volume, pitch, false, 0, ISound.AttenuationType.NONE, (float) player.posX, (float) player.posY, (float) player.posZ));
}

@SubscribeEvent(priority = EventPriority.LOWEST)
Expand Down

0 comments on commit 3d14ddc

Please sign in to comment.