Jump to content

Minecraft 1.12.2 Positioned Looping sound randomly stops


Hypherion

Recommended Posts

Hi Guys

 

I have a custom PositionedSound class in my mod with repeat set to true. The loop works, but sometimes it only plays for about 3 seconds then stops and other times it will play for 10+ seconds then stop. The sound also doesn't seamlessly loop. It's got some kind of gap in the loop, even though the loop is seamless and plays fine in Audacity

 

Below are my codes:

 

The PositionedSound class: 

 

@SideOnly(Side.CLIENT)
public class InverterLoop extends PositionedSound {

    private final TileEntityUPSInverter tileEntity;
    private final World world;
    private final BlockPos pos;
    private boolean donePlaying = false;

    public InverterLoop(TileEntityUPSInverter tile) {
        super(SoundsHandler.ENTITY_INVERTER_FAN, SoundCategory.BLOCKS);
        this.tileEntity = tile;

        this.world = tile.getWorld();
        this.pos = tile.getPos();

        this.xPosF = tile.getPos().getX();
        this.yPosF = tile.getPos().getY();
        this.zPosF = tile.getPos().getZ();


        this.attenuationType = AttenuationType.LINEAR;
        this.volume = 0.5f;
        this.pitch = 1.0f;

        this.repeat = true;
        this.repeatDelay = 0;


    }
}

 

This is the class that controls the loop:

 

@SideOnly(Side.CLIENT)
public class UPSSounds {
    private static final Map<BlockPos, PositionedSound> sounds = new HashMap<>();

    public static SoundEvent startSound;
    public static SoundEvent loopSound;
    public static SoundEvent stopSound;
    
    public static void init() {
        startSound = SoundsHandler.ENTITY_INVERTER_START;
        loopSound = SoundsHandler.ENTITY_INVERTER_FAN;
        stopSound = SoundsHandler.ENTITY_INVERTER_STOP;
    }

    private static SoundEvent registerSound(IForgeRegistry<SoundEvent> registry, ResourceLocation sound) {
        SoundEvent event = new SoundEvent(sound).setRegistryName(sound);
        registry.register(event);
        return event;
    }

    public static void stopSound(TileEntityUPSInverter tile, BlockPos pos) {

        if (sounds.containsKey(pos)) {
            PositionedSound movingSound = sounds.get(pos);
            FMLClientHandler.instance().getClient().getSoundHandler().stopSound(movingSound);
            sounds.remove(pos);
        }
    }

    private static void playSound(TileEntityUPSInverter tile, BlockPos pos, PositionedSound sound) {
        stopSound(tile, pos);
        FMLClientHandler.instance().getClient().getSoundHandler().playSound(sound);
        sounds.put(pos, sound);
    }
    
    public static void playStartup(TileEntityUPSInverter tile, BlockPos pos) {
        PositionedSound sound = new InverterStart(tile);
        playSound(tile, pos, sound);
    }

    public static void playLoop(TileEntityUPSInverter tile, BlockPos pos) {
        PositionedSound sound = new InverterLoop(tile);
        playSound(tile, pos, sound);
    }

    public static void playStop(TileEntityUPSInverter tile, BlockPos pos) {
        PositionedSound sound = new InverterStop(tile);
        playSound(tile, pos, sound);
    }

    public static boolean isStartupPlaying(World worldObj, BlockPos pos) {
        PositionedSound movingSound = sounds.get(pos);
        return movingSound instanceof InverterStart;
    }

    public static boolean isLoopPlaying(World worldObj, BlockPos pos) {
        PositionedSound movingSound = sounds.get(pos);
        return movingSound instanceof InverterLoop;
    }

    public static boolean isStopPlaying(World worldObj, BlockPos pos) {
        PositionedSound movingSound = sounds.get(pos);
        return movingSound instanceof InverterStop;
    }
}

 

And the finally this is the code that runs and stops the sounds based on the state of the tile (Called from TileEntity's Update loop (when world is remote):

 

public void handleAudio() {
        if (this.lastState != this.energy.getStte()) {
            if (this.energy.getStte() == 1) {
                if (!this.sounds.isStartupPlaying(world, this.getPos())) {
                    this.sounds.playStartup(this, this.getPos());
                }
                this.lastState = this.energy.getStte();
            } else if (this.energy.getStte() == 2) {
                if (!this.sounds.isLoopPlaying(world, this.getPos())) {
                    this.sounds.playLoop(this, this.getPos());
                }
                System.out.println("IT'S LOOPING!!!");
                this.lastState = this.energy.getStte();
            } else if (this.energy.getStte() == 4) {
                if (!this.sounds.isStopPlaying(world, this.getPos())) {
                    this.sounds.playStop(this, this.getPos());
                }
                this.lastState = this.energy.getStte();
            }
    }

 

 

I have Implemented a ITickableSound on the PositionedSound class and made it write to the debug output whenever the Update event runs. At the moment the audio stops working, the Update event also stops.

 

(Don't mind my naming methods, I just made them up to try to get this thing to work.)

 

I can confirm that handleAudio() is being called on every tick, so my guess is the issue is somewhere in the other classes.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.