mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-12 09:37:29 +00:00
Prevent Music Overlapping Memory Leak
This commit is contained in:
1 parent
3507cb85c5
commit
6f5fd25a24
2 files changed
+24
-26
No files matched your search
@@ -1,28 +1,25 @@
|
||||
package noppes.npcs.client.controllers;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.audio.ISound;
|
||||
import net.minecraft.client.audio.PositionedSoundRecord;
|
||||
import net.minecraft.client.audio.SoundCategory;
|
||||
import net.minecraft.client.audio.SoundHandler;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
|
||||
|
||||
public class MusicController {
|
||||
//public TreeSet<String> sounds = new TreeSet<String>();
|
||||
//public TreeSet<String> music = new TreeSet<String>();
|
||||
|
||||
|
||||
public static MusicController Instance;
|
||||
public PositionedSoundRecord playing;
|
||||
public ResourceLocation playingResource;
|
||||
public Entity playingEntity;
|
||||
|
||||
//private SoundPoolEntry musicPool;
|
||||
|
||||
public MusicController(){
|
||||
Instance = this;
|
||||
}
|
||||
|
||||
|
||||
public void stopMusic(){
|
||||
SoundHandler handler = Minecraft.getMinecraft().getSoundHandler();
|
||||
if(playing != null)
|
||||
@@ -32,9 +29,9 @@ public class MusicController {
|
||||
playingEntity = null;
|
||||
playing = null;
|
||||
}
|
||||
|
||||
|
||||
public void playStreaming(String music, Entity entity){
|
||||
if(isPlaying(music)){
|
||||
if(isPlaying(music)){
|
||||
return;
|
||||
}
|
||||
stopMusic();
|
||||
@@ -57,7 +54,7 @@ public class MusicController {
|
||||
playing = PositionedSoundRecord.func_147673_a(playingResource);
|
||||
handler.playSound(playing);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public boolean isPlaying(String music) {
|
||||
ResourceLocation resource = new ResourceLocation(music);
|
||||
@@ -66,7 +63,7 @@ public class MusicController {
|
||||
}
|
||||
return Minecraft.getMinecraft().getSoundHandler().isSoundPlaying(playing);
|
||||
}
|
||||
|
||||
|
||||
public void playSound(String music, float x, float y, float z) {
|
||||
Minecraft.getMinecraft().theWorld.playSound(x, y, z, music, 1, 1, false);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package noppes.npcs.roles;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
@@ -14,12 +15,12 @@ import java.util.List;
|
||||
public class JobBard extends JobInterface{
|
||||
public int minRange = 2;
|
||||
public int maxRange = 64;
|
||||
|
||||
|
||||
public boolean isStreamer = true;
|
||||
public boolean hasOffRange = true;
|
||||
|
||||
public String song = "";
|
||||
|
||||
|
||||
private EnumBardInstrument instrument = EnumBardInstrument.Banjo;
|
||||
|
||||
public JobBard(EntityNPCInterface npc) {
|
||||
@@ -38,7 +39,7 @@ public class JobBard extends JobInterface{
|
||||
nbttagcompound.setInteger("BardInstrument", instrument.ordinal());
|
||||
nbttagcompound.setBoolean("BardStreamer", isStreamer);
|
||||
nbttagcompound.setBoolean("BardHasOff", hasOffRange);
|
||||
|
||||
|
||||
return nbttagcompound;
|
||||
}
|
||||
|
||||
@@ -51,7 +52,7 @@ public class JobBard extends JobInterface{
|
||||
isStreamer = nbttagcompound.getBoolean("BardStreamer");
|
||||
hasOffRange = nbttagcompound.getBoolean("BardHasOff");
|
||||
}
|
||||
|
||||
|
||||
public void setInstrument(int i) {
|
||||
if(CustomItems.banjo == null)
|
||||
return;
|
||||
@@ -87,16 +88,11 @@ public class JobBard extends JobInterface{
|
||||
public EnumBardInstrument getInstrument(){
|
||||
return instrument;
|
||||
}
|
||||
private long ticks = 0;
|
||||
public void onLivingUpdate() {
|
||||
if(!npc.isRemote())
|
||||
if(!npc.isRemote() || song.isEmpty())
|
||||
return;
|
||||
ticks++;
|
||||
if(ticks % 10 != 0 )
|
||||
return;
|
||||
if(song.isEmpty())
|
||||
return;
|
||||
if(!MusicController.Instance.isPlaying(song)){
|
||||
|
||||
if(!MusicController.Instance.isPlaying(song)){
|
||||
List<EntityPlayer> list = npc.worldObj.getEntitiesWithinAABB(EntityPlayer.class, npc.boundingBox.expand(minRange, minRange/2, minRange));
|
||||
if(!list.contains(CustomNpcs.proxy.getPlayer()))
|
||||
return;
|
||||
@@ -104,18 +100,23 @@ public class JobBard extends JobInterface{
|
||||
MusicController.Instance.playStreaming(song, npc);
|
||||
else
|
||||
MusicController.Instance.playMusic(song, npc);
|
||||
}else if(MusicController.Instance.playingEntity != npc){
|
||||
}
|
||||
else if(MusicController.Instance.playingEntity != npc){
|
||||
EntityPlayer player = CustomNpcs.proxy.getPlayer();
|
||||
if(npc.getDistanceSqToEntity(player) < MusicController.Instance.playingEntity.getDistanceSqToEntity(player)){
|
||||
MusicController.Instance.playingEntity = npc;
|
||||
}
|
||||
|
||||
}else if(hasOffRange){
|
||||
|
||||
}
|
||||
else if(hasOffRange){
|
||||
List<EntityPlayer> list = npc.worldObj.getEntitiesWithinAABB(EntityPlayer.class, npc.boundingBox.expand(maxRange, maxRange/2, maxRange));
|
||||
if(!list.contains(CustomNpcs.proxy.getPlayer()))
|
||||
MusicController.Instance.stopMusic();
|
||||
}
|
||||
|
||||
|
||||
if(MusicController.Instance.isPlaying(song)) {
|
||||
Minecraft.getMinecraft().mcMusicTicker.field_147676_d = 12000;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in new issue
Block a user