Skip to content

Instantly share code, notes, and snippets.

@williambl
Last active December 6, 2019 17:44
Show Gist options
  • Save williambl/e3baaa76382a6c4aa251d33919f656bd to your computer and use it in GitHub Desktop.
Save williambl/e3baaa76382a6c4aa251d33919f656bd to your computer and use it in GitHub Desktop.
MovingSound
/*
* A sound that follows an entity
* Up-to-date source: https://github.com/williambl/EssentialFeatures/blob/master/src/main/java/com/williambl/essentialfeatures/client/music/MovingSound.java
* @Author Willbl3pic
*/
public class MovingSound extends TickableSound {
private final Entity entity;
private float distance = 0.0F;
public MovingSound(Entity entityIn, SoundEvent soundIn) {
super(soundIn, SoundCategory.NEUTRAL);
this.entity = entityIn;
this.repeat = false;
this.repeatDelay = 0;
this.volume = 1.0F;
}
public void tick() {
if (!this.entity.isAlive()) {
this.donePlaying = true;
} else {
this.x = (float) this.entity.posX;
this.y = (float) this.entity.posY;
this.z = (float) this.entity.posZ;
}
}
}
/*
* Play a moving sound
* Adapted from: https://github.com/williambl/EssentialFeatures/blob/e77a8cd31f80543c9210e9bfac2431d2b92a1cdf/src/main/java/com/williambl/essentialfeatures/client/DistHelper.java
* @Author Willbl3pic
*/
public void playMovingSoundWithDisc(MusicDiscItem disc, PlayerEntity player) {
Minecraft.getInstance().getSoundHandler().play(
new MovingSound(
player,
disc.getSound()
)
);
}
/*
* Stop a moving sound
* Adapted from: https://github.com/williambl/EssentialFeatures/blob/e77a8cd31f80543c9210e9bfac2431d2b92a1cdf/src/main/java/com/williambl/essentialfeatures/client/DistHelper.java
* @Author Willbl3pic
*/
public void stopMovingSound(MusicDisc disc) {
Minecraft.getInstance().getSoundHandler().stop(disc.getSound().getName(), SoundCategory.NEUTRAL);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment