Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Sound is not reproduced [1.16.5]


Recommended Posts

Hi!
I was making a custom ambulance, and when i press the key "b", it should play the siren sound effect, but it doesn't.
Can someone help?

This is Where i Call the Sound:

public class AmbulanceEntity extends VehicleEntity {

    private boolean sirenActivate;

    public AmbulanceEntity(EntityType<? extends AmbulanceEntity> type, World worldIn) {
        super(type, worldIn);
        this.setMaxSpeed(20F);
        this.sirenActivate = false;
    }

    public boolean isSirenOn() {
        return this.sirenActivate;
    }

    public void updateSiren() {
        this.sirenActivate = !this.sirenActivate;
        if (this.sirenActivate) {
            AmbulanceSirenSound sound = new AmbulanceSirenSound(this);
            System.out.println("Add Sound");
            Minecraft.getInstance().getSoundHandler().play(sound);
        }
        PacketHandler.instance.sendToServer(new MessageAmbulanceSirenChange(this.sirenActivate));
    }

    public void setSirenActivate(boolean sirenActivate) {
        this.sirenActivate = sirenActivate;
    }
}

The Ambulance Sound Class:

public class AmbulanceSirenSound extends TickableSound {

    private AmbulanceEntity refEntity;

    public AmbulanceSirenSound(AmbulanceEntity entity) {
        super(ModSounds.AMBULANCE_SIREN.get(), SoundCategory.MUSIC);
        this.refEntity = entity;
        this.repeat = true;
        this.repeatDelay = 0;
        this.volume = 0.8F;
        this.pitch = 0.85F;
    }

    @Override
    public void tick() {
        if (this.refEntity == null || Minecraft.getInstance().player == null) {
            this.func_239509_o_();
            return;
        }
        if (refEntity.isSirenOn() && this.refEntity.getPassengers().size() > 0) {
            PlayerEntity localPlayer = Minecraft.getInstance().player;
            this.x = (float) (this.refEntity.getPosX() + (localPlayer.getPosX() - this.refEntity.getPosX()) * 0.65);
            this.y = (float) (this.refEntity.getPosY() + (localPlayer.getPosY() - this.refEntity.getPosY()) * 0.65);
            this.z = (float) (this.refEntity.getPosZ() + (localPlayer.getPosZ() - this.refEntity.getPosZ()) * 0.65);
            System.out.println("Sound Tick");
        } else {
            //refEntity.sirenOff();
            this.func_239509_o_();
        }
    }
}


 

Link to post
Share on other sites
10 hours ago, Yurim64 said:

Minecraft.getInstance().getSoundHandler().play(sound);

You cannot do this in a common class, you will crash servers.

Also, where do you call updateSiren?

Link to post
Share on other sites

dove dovrei riprodurre il suono? e come?

I call the update Siren here, in my Client Event Handler:

@SubscribeEvent
    public void onKeyInput(InputEvent.KeyInputEvent event) {
        if (Minecraft.getInstance().player == null)
            return;

        if (ClientProxy.KEY_CYCLE_SEATS.isPressed() && event.getAction() == GLFW.GLFW_PRESS) {
            if (Minecraft.getInstance().player.getRidingEntity() instanceof VehicleEntity) {
                PacketHandler.instance.sendToServer(new MessageCycleSeats());
            }
        }

        if (ClientProxy.KEY_AMBULANCE_SIREN.isPressed() && event.getAction() == GLFW.GLFW_PRESS) {
            Entity entity = Minecraft.getInstance().player.getRidingEntity();
            if (entity instanceof AmbulanceEntity) {
                ((AmbulanceEntity) entity).updateSiren();
            }
        }
    }

 

Link to post
Share on other sites

KeyInputEvent is for raw keyboard input. Use ClientTickEvent to check key bindings.

Have you used the debugger? Is your sound created and started? Are there any errors?

Link to post
Share on other sites
Just now, Yurim64 said:

Now i try to use ClientTickEvent and see if something changes

It won't.

1 minute ago, Yurim64 said:

There are no errors, the sound is loaded correctly.

Please post a Git repo of your mod.

Link to post
Share on other sites

Ok, thanks for the help but I found the error.

It's a stupid one..

I continually send messages to the server to update the sound, then it keeps turning on / off. I changed some lines of code and now I can reproduce the sound correctly.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.