#1333 Sound locator sometimes creates thousands of entities per frame

Sound locator entities not being rendered in some cases
This commit is contained in:
CrazyPants committed 2014-12-08 21:26:09 +08:00
1 parent 7422bcd2c2
commit 90fd5dcd02
3 files changed
+25 -61

No files matched your search

@@ -1,58 +1,30 @@
package crazypants.enderio.item.darksteel;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import org.lwjgl.opengl.GL11;
import java.util.concurrent.ArrayBlockingQueue;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityClientPlayerMP;
import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.client.renderer.EntityRenderer;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.IIcon;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.util.Vec3;
import net.minecraftforge.client.event.sound.PlaySoundEvent17;
import net.minecraftforge.client.event.sound.PlaySoundSourceEvent;
import net.minecraftforge.client.event.sound.SoundEvent;
import net.minecraftforge.client.event.sound.SoundEvent.SoundSourceEvent;
import net.minecraftforge.event.entity.PlaySoundAtEntityEvent;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.common.gameevent.TickEvent;
import cpw.mods.fml.common.gameevent.TickEvent.Phase;
import cpw.mods.fml.common.gameevent.TickEvent.RenderTickEvent;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import crazypants.enderio.config.Config;
import crazypants.render.BoundingBox;
import crazypants.render.CubeRenderer;
import crazypants.render.IconUtil;
import crazypants.render.RenderUtil;
import crazypants.util.BlockCoord;
import crazypants.util.Util;
import crazypants.vecmath.Camera;
import crazypants.vecmath.Matrix4d;
import crazypants.vecmath.Vector3d;
import crazypants.vecmath.ViewFrustum;
public class SoundDetector {
private static final int MAX_ENTITIES = 64;
public static SoundDetector instance = new SoundDetector();
private List<SoundSource> soundQueue = new ArrayList<SoundDetector.SoundSource>();
private List<SoundSource> sounds = new LinkedList<SoundSource>();
private List<SoundSource> sounds;
private ArrayBlockingQueue<SoundSource> soundQueue = new ArrayBlockingQueue<SoundSource>(MAX_ENTITIES);
private Minecraft mc = Minecraft.getMinecraft();
@@ -62,15 +34,15 @@ public class SoundDetector {
@SubscribeEvent
public void onSound(PlaySoundAtEntityEvent evt) {
if(enabled && evt.entity != null && evt.entity != Minecraft.getMinecraft().thePlayer) {
soundQueue.add(new SoundSource(evt.entity, evt.volume));
if(enabled && evt.entity != null && evt.entity != Minecraft.getMinecraft().thePlayer && soundQueue.size() < MAX_ENTITIES) {
soundQueue.offer(new SoundSource(evt.entity, evt.volume));
}
}
@SubscribeEvent
public void onSound(PlaySoundSourceEvent evt) {
if(enabled) {
soundQueue.add(new SoundSource(evt.sound.getXPosF(), evt.sound.getYPosF(), evt.sound.getZPosF(), evt.sound.getVolume()));
if(enabled && soundQueue.size() < MAX_ENTITIES) {
soundQueue.offer(new SoundSource(evt.sound.getXPosF(), evt.sound.getYPosF(), evt.sound.getZPosF(), evt.sound.getVolume()));
}
}
@@ -82,9 +54,8 @@ public class SoundDetector {
return;
}
List<SoundSource> tmp = soundQueue;
soundQueue = sounds;
sounds = tmp;
sounds = new ArrayList<SoundSource>(MAX_ENTITIES);
soundQueue.drainTo(sounds);
try {
Vector3d eye = Util.getEyePositionEio(mc.thePlayer);
@@ -95,13 +66,9 @@ public class SoundDetector {
mc.thePlayer.worldObj.spawnEntityInWorld(new SoundEntity(mc.thePlayer.worldObj, ss.pos, ss.volume));
}
}
sounds.clear();
} catch (Exception ex) {
//very small chance of getting an exception here due to non synchronized swapping of the lists
//Just catching and ignoring this is the lesser of the two evils compared to the cost of syncronizing the lists
//TODO: Maybe look at the impact of using atomic references
//Probably not necessary anymore but saftey first!
}
}
public boolean isEnabled() {
@@ -1,26 +1,18 @@
package crazypants.enderio.item.darksteel;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityClientPlayerMP;
import net.minecraft.client.renderer.OpenGlHelper;
import net.minecraft.client.renderer.entity.RenderEntity;
import net.minecraft.entity.Entity;
import org.lwjgl.opengl.GL11;
import crazypants.enderio.gui.IconEIO;
import crazypants.render.BoundingBox;
import crazypants.render.CubeRenderer;
import crazypants.render.IconUtil;
import crazypants.render.RenderUtil;
import crazypants.vecmath.Matrix4d;
import crazypants.vecmath.Vector3d;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityClientPlayerMP;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.entity.Render;
import net.minecraft.client.renderer.entity.RenderEntity;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.util.IIcon;
import net.minecraft.util.ResourceLocation;
public class SoundRenderer extends RenderEntity {
@Override
public void doRender(Entity entity, double x, double y, double z, float p_76986_8_, float p_76986_9_) {
SoundEntity se = ((SoundEntity) entity);
@@ -29,8 +21,10 @@ public class SoundRenderer extends RenderEntity {
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glDisable(GL11.GL_LIGHTING);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
//float alpha = Math.min(1, se.volume) * 0.5f;
OpenGlHelper.setLightmapTextureCoords(OpenGlHelper.lightmapTexUnit, 240, 240);
float alpha = 0.5f;
GL11.glColor4f(1, 1, 1, alpha);