Hbm-s-Nuclear-Tech-GIT/src/main/java/com/hbm/tileentity/machine/TileEntityHeaterElectric.java

213 lines
4.7 KiB
Java

package com.hbm.tileentity.machine;
import com.hbm.blocks.BlockDummyable;
import com.hbm.main.MainRegistry;
import com.hbm.sound.AudioWrapper;
import com.hbm.tileentity.INBTPacketReceiver;
import com.hbm.tileentity.TileEntityLoadedBase;
import com.hbm.util.CompatEnergyControl;
import api.hbm.energymk2.IEnergyReceiverMK2;
import api.hbm.tile.IHeatSource;
import api.hbm.tile.IInfoProviderEC;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraftforge.common.util.ForgeDirection;
public class TileEntityHeaterElectric extends TileEntityLoadedBase implements IHeatSource, IEnergyReceiverMK2, INBTPacketReceiver, IInfoProviderEC {
public long power;
public int heatEnergy;
public boolean isOn;
protected int setting = 0;
private AudioWrapper audio;
@Override
public void updateEntity() {
if(!worldObj.isRemote) {
if(worldObj.getTotalWorldTime() % 20 == 0) { //doesn't have to happen constantly
ForgeDirection dir = ForgeDirection.getOrientation(this.getBlockMetadata() - BlockDummyable.offset);
this.trySubscribe(worldObj, xCoord + dir.offsetX * 3, yCoord, zCoord + dir.offsetZ * 3, dir);
}
this.heatEnergy *= 0.999;
this.tryPullHeat();
this.isOn = false;
if(setting > 0 && this.power >= this.getConsumption()) {
this.power -= this.getConsumption();
this.heatEnergy += getHeatGen();
this.isOn = true;
}
NBTTagCompound data = new NBTTagCompound();
data.setByte("s", (byte) this.setting);
data.setInteger("h", this.heatEnergy);
data.setBoolean("o", isOn);
data.setBoolean("muffled", muffled);
INBTPacketReceiver.networkPack(this, data, 25);
} else {
if(isOn) {
if(audio == null) {
audio = createAudioLoop();
audio.startSound();
} else if(!audio.isPlaying()) {
audio = rebootAudio(audio);
}
audio.updateVolume(getVolume(1F));
audio.keepAlive();
} else {
if(audio != null) {
audio.stopSound();
audio = null;
}
}
}
}
@Override
public AudioWrapper createAudioLoop() {
return MainRegistry.proxy.getLoopedSound("hbm:block.electricHum", xCoord, yCoord, zCoord, 0.25F, 7.5F, 1.0F, 20);
}
@Override
public void onChunkUnload() {
if(audio != null) {
audio.stopSound();
audio = null;
}
}
@Override
public void invalidate() {
super.invalidate();
if(audio != null) {
audio.stopSound();
audio = null;
}
}
@Override
public void networkUnpack(NBTTagCompound nbt) {
this.setting = nbt.getByte("s");
this.heatEnergy = nbt.getInteger("h");
this.isOn = nbt.getBoolean("o");
this.muffled = nbt.getBoolean("muffled");
}
@Override
public void readFromNBT(NBTTagCompound nbt) {
super.readFromNBT(nbt);
this.power = nbt.getLong("power");
this.setting = nbt.getInteger("setting");
this.heatEnergy = nbt.getInteger("heatEnergy");
}
@Override
public void writeToNBT(NBTTagCompound nbt) {
super.writeToNBT(nbt);
nbt.setLong("power", power);
nbt.setInteger("setting", setting);
nbt.setInteger("heatEnergy", heatEnergy);
}
protected void tryPullHeat() {
TileEntity con = worldObj.getTileEntity(xCoord, yCoord - 1, zCoord);
if(con instanceof IHeatSource) {
IHeatSource source = (IHeatSource) con;
this.heatEnergy += source.getHeatStored() * 0.85;
source.useUpHeat(source.getHeatStored());
}
}
public void toggleSetting() {
setting++;
if(setting > 10)
setting = 0;
}
@Override
public long getPower() {
return power;
}
public long getConsumption() {
return (long) (Math.pow(setting, 1.4D) * 200D);
}
@Override
public long getMaxPower() {
return getConsumption() * 20;
}
public int getHeatGen() {
return this.setting * 100;
}
@Override
public void setPower(long power) {
this.power = power;
}
@Override
public int getHeatStored() {
return heatEnergy;
}
@Override
public void useUpHeat(int heat) {
this.heatEnergy = Math.max(0, this.heatEnergy - heat);
}
AxisAlignedBB bb = null;
@Override
public AxisAlignedBB getRenderBoundingBox() {
if(bb == null) {
bb = AxisAlignedBB.getBoundingBox(
xCoord - 2,
yCoord,
zCoord - 2,
xCoord + 3,
yCoord + 1,
zCoord + 3
);
}
return bb;
}
@Override
@SideOnly(Side.CLIENT)
public double getMaxRenderDistanceSquared() {
return 65536.0D;
}
@Override
public void provideExtraInfo(NBTTagCompound data) {
data.setLong(CompatEnergyControl.D_CONSUMPTION_HE, getConsumption());
data.setLong(CompatEnergyControl.L_ENERGY_TU, getHeatStored());
data.setLong(CompatEnergyControl.D_OUTPUT_TU, getHeatGen());
}
}