Mekanism-tilera-Edition/src/main/java/mekanism/common/tile/TileEntityNoisyElectricBlock.java

151 lines
2.8 KiB
Java
Raw Normal View History

2014-08-28 00:16:59 +02:00
package mekanism.common.tile;
import mekanism.api.MekanismConfig.client;
2014-08-28 00:16:59 +02:00
import mekanism.api.Pos3D;
2016-01-08 18:24:27 +01:00
import mekanism.client.HolidayManager;
2014-08-28 00:16:59 +02:00
import mekanism.client.sound.ISoundSource;
import mekanism.common.base.IActiveState;
import mekanism.common.base.IHasSound;
import mekanism.common.base.SoundWrapper;
2014-08-28 00:16:59 +02:00
import net.minecraft.client.audio.ISound.AttenuationType;
import net.minecraft.util.ResourceLocation;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
2014-08-28 00:16:59 +02:00
public abstract class TileEntityNoisyElectricBlock extends TileEntityElectricBlock implements IHasSound, ISoundSource, IActiveState
{
/** The ResourceLocation of the machine's sound */
public ResourceLocation soundURL;
2015-03-18 21:24:03 +01:00
/** The bundled URL of this machine's sound effect */
@SideOnly(Side.CLIENT)
public SoundWrapper sound;
2014-08-28 00:16:59 +02:00
/**
* The base of all blocks that deal with electricity and make noise.
*
* @param name - full name of this block
* @param maxEnergy - how much energy this block can store
*/
public TileEntityNoisyElectricBlock(String soundPath, String name, double maxEnergy)
{
super(name, maxEnergy);
2016-01-08 18:24:27 +01:00
2016-01-09 16:07:22 +01:00
if(worldObj.isRemote)
{
try {
registerSound(soundPath);
} catch(Throwable t) {}
}
}
@SideOnly(Side.CLIENT)
private void registerSound(String soundPath)
{
2016-01-08 18:24:27 +01:00
soundURL = HolidayManager.filterSound(new ResourceLocation("mekanism", "tile." + soundPath));
2014-08-28 00:16:59 +02:00
}
@Override
@SideOnly(Side.CLIENT)
public SoundWrapper getSound()
2014-08-28 00:16:59 +02:00
{
return sound;
}
@Override
2015-03-18 21:24:03 +01:00
@SideOnly(Side.CLIENT)
2014-08-28 00:16:59 +02:00
public boolean shouldPlaySound()
{
return getActive() && !isInvalid();
}
@Override
2015-03-18 21:24:03 +01:00
@SideOnly(Side.CLIENT)
2014-08-28 00:16:59 +02:00
public ResourceLocation getSoundLocation()
{
return soundURL;
}
@Override
2015-03-18 21:24:03 +01:00
@SideOnly(Side.CLIENT)
2014-08-28 00:16:59 +02:00
public float getVolume()
{
return 1F;
}
@Override
2015-03-18 21:24:03 +01:00
@SideOnly(Side.CLIENT)
2014-08-28 00:16:59 +02:00
public float getPitch()
{
return 1F;
}
@Override
2015-03-18 21:24:03 +01:00
@SideOnly(Side.CLIENT)
2014-08-28 00:16:59 +02:00
public Pos3D getSoundPosition()
{
return new Pos3D(xCoord+0.5, yCoord+0.5, zCoord+0.5);
}
@Override
2015-03-18 21:24:03 +01:00
@SideOnly(Side.CLIENT)
2014-08-28 00:16:59 +02:00
public boolean shouldRepeat()
{
return true;
}
@Override
2015-03-18 21:24:03 +01:00
@SideOnly(Side.CLIENT)
2014-08-28 00:16:59 +02:00
public int getRepeatDelay()
{
return 0;
}
@Override
@SideOnly(Side.CLIENT)
2014-08-28 00:16:59 +02:00
public AttenuationType getAttenuation()
{
return AttenuationType.LINEAR;
}
@Override
public void validate()
{
super.validate();
2014-09-05 19:31:10 +02:00
2015-03-18 21:24:03 +01:00
if(worldObj.isRemote)
{
2016-01-09 16:07:22 +01:00
try {
initSounds();
} catch(Throwable t) {}
2015-03-18 21:24:03 +01:00
}
2014-09-05 19:31:10 +02:00
}
@SideOnly(Side.CLIENT)
2014-09-05 19:31:10 +02:00
public void initSounds()
{
sound = new SoundWrapper(this, this);
2014-08-28 00:16:59 +02:00
}
@Override
public void onUpdate()
{
super.onUpdate();
2015-03-18 21:24:03 +01:00
if(worldObj.isRemote)
{
updateSound();
}
}
@SideOnly(Side.CLIENT)
public void updateSound()
{
if(shouldPlaySound() && getSound().canRestart() && client.enableMachineSounds)
2015-03-18 21:24:03 +01:00
{
getSound().reset();
getSound().play();
2015-03-18 21:24:03 +01:00
}
2014-08-28 00:16:59 +02:00
}
}