Search in sources :

Example 1 with PositionedSound

use of net.minecraft.client.audio.PositionedSound in project Railcraft by Railcraft.

the class RCSoundHandler method onPlaySound.

//TODO: test, catch PlaySoundAtEntityEvent?
@SubscribeEvent
public void onPlaySound(PlaySoundEvent event) {
    ISound soundEvent = event.getSound();
    ResourceLocation soundResource = soundEvent.getSoundLocation();
    if (SoundHelper.matchesSoundResource(soundResource, "null")) {
        event.setResultSound(null);
    } else if (SoundHelper.matchesSoundResource(soundResource, "override")) {
        World world = Railcraft.getProxy().getClientWorld();
        if (world != null) {
            float x = soundEvent.getXPosF();
            float y = soundEvent.getYPosF();
            float z = soundEvent.getZPosF();
            BlockPos pos = new BlockPos(x, y, z);
            String soundPath = soundEvent.getSoundLocation().getResourcePath();
            SoundType blockSound = SoundRegistry.getBlockSound(world, pos);
            if (blockSound == null) {
                if (soundPath.contains("place")) {
                    //Play sound later to adjust for the block not being there yet.
                    event.getManager().playDelayedSound(event.getSound(), 3);
                } else if (soundPath.contains("step")) {
                    blockSound = SoundRegistry.getBlockSound(world, pos.down());
                }
            }
            if (blockSound != null) {
                SoundEvent newSound = SoundHelper.matchSoundEvent(soundResource, blockSound);
                ObfuscationReflectionHelper.setPrivateValue(PositionedSound.class, (PositionedSound) soundEvent, newSound.getSoundName(), 3);
            } else {
                event.setResultSound(null);
            }
        }
    }
}
Also used : SoundType(net.minecraft.block.SoundType) ISound(net.minecraft.client.audio.ISound) PlaySoundEvent(net.minecraftforge.client.event.sound.PlaySoundEvent) SoundEvent(net.minecraft.util.SoundEvent) PositionedSound(net.minecraft.client.audio.PositionedSound) ResourceLocation(net.minecraft.util.ResourceLocation) BlockPos(net.minecraft.util.math.BlockPos) World(net.minecraft.world.World) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent)

Aggregations

SoundType (net.minecraft.block.SoundType)1 ISound (net.minecraft.client.audio.ISound)1 PositionedSound (net.minecraft.client.audio.PositionedSound)1 ResourceLocation (net.minecraft.util.ResourceLocation)1 SoundEvent (net.minecraft.util.SoundEvent)1 BlockPos (net.minecraft.util.math.BlockPos)1 World (net.minecraft.world.World)1 PlaySoundEvent (net.minecraftforge.client.event.sound.PlaySoundEvent)1 SubscribeEvent (net.minecraftforge.fml.common.eventhandler.SubscribeEvent)1