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

152 lines
3.1 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.Upgrade;
2014-08-28 00:16:59 +02:00
import mekanism.common.base.IActiveState;
import mekanism.common.base.IHasSound;
import mekanism.common.base.IUpgradeTile;
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;
2016-01-09 19:37:37 +01:00
/** The path of this machine's sound */
public String soundPath;
2014-08-28 00:16:59 +02:00
/**
* The base of all blocks that deal with electricity and make noise.
*
2016-01-09 19:37:37 +01:00
* @param sound - the sound path of this block
2014-08-28 00:16:59 +02:00
* @param name - full name of this block
* @param maxEnergy - how much energy this block can store
*/
2016-01-09 19:37:37 +01:00
public TileEntityNoisyElectricBlock(String sound, String name, double maxEnergy)
2014-08-28 00:16:59 +02:00
{
super(name, maxEnergy);
2016-01-08 18:24:27 +01:00
2016-01-09 19:37:37 +01:00
soundPath = sound;
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()
{
if(this instanceof IUpgradeTile && ((IUpgradeTile)this).getComponent().supports(Upgrade.MUFFLING))
{
return Math.max(0.001F, 1F - (float)((IUpgradeTile)this).getComponent().getUpgrades(Upgrade.MUFFLING)/(float)Upgrade.MUFFLING.getMax());
}
2014-08-28 00:16:59 +02:00
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 {
2016-01-09 19:37:37 +01:00
soundURL = HolidayManager.filterSound(new ResourceLocation("mekanism", "tile." + soundPath));
2016-01-09 16:07:22 +01:00
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
}
}