Files
CustomNPC-Plus/src/main/java/noppes/npcs/client/controllers/MusicController.java
T

115 lines
3.6 KiB
Java
Raw Normal View History

2018-09-13 12:03:28 -04:00
package noppes.npcs.client.controllers;
import net.minecraft.client.Minecraft;
2024-05-14 17:52:19 -05:00
import net.minecraft.client.audio.ISound;
2018-09-13 12:03:28 -04:00
import net.minecraft.client.audio.SoundHandler;
import net.minecraft.entity.Entity;
import net.minecraft.util.ResourceLocation;
public class MusicController {
public static MusicController Instance;
2024-05-15 03:35:39 -05:00
public ScriptClientSound playingSound;
2024-05-14 18:05:25 -05:00
private Entity entity;
private int offRange;
2018-09-13 12:03:28 -04:00
public MusicController(){
Instance = this;
}
2024-05-15 03:35:39 -05:00
public void onUpdate() {
if (this.playingSound != null
&& !Minecraft.getMinecraft().getSoundHandler().isSoundPlaying(this.playingSound)) {
Entity playingEntity = this.playingSound.getEntity();
String sound = this.playingSound.sound;
this.playingSound.stopSound();
this.stopMusic();
if (playingEntity == Minecraft.getMinecraft().thePlayer) {
this.playMusicBackground(sound, this.entity, this.offRange);
} else {
this.playMusicJukebox(sound, this.entity, this.offRange);
}
}
}
2018-09-13 12:03:28 -04:00
public void stopMusic(){
2024-05-14 17:52:19 -05:00
SoundHandler soundHandler = Minecraft.getMinecraft().getSoundHandler();
soundHandler.stopSounds();
2024-05-14 17:59:25 -05:00
if (this.isPlaying()) {
2024-05-15 03:35:39 -05:00
this.playingSound.stopSound();
this.playingSound = null;
2024-05-14 17:59:25 -05:00
}
2018-09-13 12:03:28 -04:00
}
2024-02-20 20:30:18 -05:00
public void playMusicJukebox(String music, Entity entity, int offRange){
2024-05-14 17:52:19 -05:00
if (this.isPlaying(music)) {
2018-09-13 12:03:28 -04:00
return;
}
this.stopMusic();
2024-05-14 17:52:19 -05:00
ScriptClientSound clientSound = new ScriptClientSound(music);
2024-05-15 03:35:39 -05:00
clientSound.setEntity(entity);
clientSound.setVolume(Math.max(1, offRange/16.0F));
2024-05-14 17:52:19 -05:00
clientSound.setAttenuationType(ISound.AttenuationType.LINEAR);
clientSound.setRepeat(true);
2024-05-15 03:35:39 -05:00
this.playingSound = clientSound;
2024-05-14 18:05:25 -05:00
this.entity = entity;
this.offRange = offRange;
2024-05-14 17:52:19 -05:00
SoundHandler soundHandler = Minecraft.getMinecraft().getSoundHandler();
soundHandler.playSound(clientSound);
2018-09-13 12:03:28 -04:00
}
public void playMusicBackground(String music, Entity entity, int offRange) {
2024-05-14 17:52:19 -05:00
if (this.isPlaying(music)) {
return;
}
this.stopMusic();
2018-09-13 12:03:28 -04:00
2024-05-14 17:52:19 -05:00
ScriptClientSound clientSound = new ScriptClientSound(music);
clientSound.setEntity(Minecraft.getMinecraft().thePlayer);
clientSound.setRepeat(true);
2024-05-15 03:35:39 -05:00
this.playingSound = clientSound;
2024-05-14 18:05:25 -05:00
this.entity = entity;
this.offRange = offRange;
2024-05-14 18:05:25 -05:00
2024-05-14 17:52:19 -05:00
SoundHandler soundHandler = Minecraft.getMinecraft().getSoundHandler();
soundHandler.playSound(clientSound);
2018-09-13 12:03:28 -04:00
}
2024-02-20 20:30:18 -05:00
2018-09-13 12:03:28 -04:00
public boolean isPlaying(String music) {
2024-05-15 03:35:39 -05:00
if (this.playingSound != null && this.playingSound.sound.equals(music)) {
2024-05-14 17:52:19 -05:00
ResourceLocation resource = new ResourceLocation(music);
2024-05-15 03:35:39 -05:00
if (!this.playingSound.getPositionedSoundLocation().equals(resource)) {
2024-05-14 17:52:19 -05:00
return false;
}
2024-05-15 03:35:39 -05:00
return Minecraft.getMinecraft().getSoundHandler().isSoundPlaying(this.playingSound);
2024-05-14 17:52:19 -05:00
} else {
return false;
}
2018-09-13 12:03:28 -04:00
}
2024-02-20 20:30:18 -05:00
2024-05-14 17:52:19 -05:00
public boolean isPlaying() {
2024-05-15 03:35:39 -05:00
return this.playingSound != null;
2024-05-14 17:52:19 -05:00
}
2024-05-14 18:05:25 -05:00
public Entity getEntity() {
return this.entity;
}
public int getOffRange() {
return this.offRange;
}
public double getDistance() {
Entity player = Minecraft.getMinecraft().thePlayer;
if (this.entity != null && this.entity.dimension == player.dimension) {
return player.getDistanceToEntity(this.entity);
}
return Double.MAX_VALUE;
}
2018-09-13 12:03:28 -04:00
public void playSound(String music, float x, float y, float z) {
Minecraft.getMinecraft().theWorld.playSound(x, y, z, music, 1, 1, false);
}
}