ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/tile/TileEntityCoalGenerator.java

161 lines
5.9 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
* This file ("TileEntityCoalGenerator.java") is part of the Actually Additions Mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
2016-01-03 16:05:51 +01:00
* http://ellpeck.de/actaddlicense/
2015-08-29 14:33:25 +02:00
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2016-01-03 16:05:51 +01:00
* © 2016 Ellpeck
2015-08-29 14:33:25 +02:00
*/
2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.tile;
2015-05-20 22:39:43 +02:00
import cofh.api.energy.EnergyStorage;
import cofh.api.energy.IEnergyProvider;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
2015-05-20 22:39:43 +02:00
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntityFurnace;
import net.minecraftforge.common.util.ForgeDirection;
2016-01-07 18:20:59 +01:00
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
2015-05-20 22:39:43 +02:00
public class TileEntityCoalGenerator extends TileEntityInventoryBase implements IEnergyProvider, IEnergySaver{
2015-05-20 22:39:43 +02:00
2015-12-01 19:48:09 +01:00
public static final int PRODUCE = 30;
public EnergyStorage storage = new EnergyStorage(60000);
2015-05-20 22:39:43 +02:00
public int maxBurnTime;
public int currentBurnTime;
2015-10-03 10:16:18 +02:00
private int lastEnergy;
private int lastBurnTime;
private int lastCurrentBurnTime;
2015-05-20 22:39:43 +02:00
public TileEntityCoalGenerator(){
super(1, "coalGenerator");
}
2015-12-01 19:48:09 +01:00
@SideOnly(Side.CLIENT)
public int getEnergyScaled(int i){
return this.storage.getEnergyStored()*i/this.storage.getMaxEnergyStored();
}
@SideOnly(Side.CLIENT)
public int getBurningScaled(int i){
return this.currentBurnTime*i/this.maxBurnTime;
}
2015-05-20 22:39:43 +02:00
@Override
@SuppressWarnings("unchecked")
public void updateEntity(){
super.updateEntity();
2015-05-20 22:39:43 +02:00
if(!worldObj.isRemote){
2015-06-21 02:28:49 +02:00
boolean flag = this.currentBurnTime > 0;
2015-05-20 22:39:43 +02:00
if(this.currentBurnTime > 0){
this.currentBurnTime--;
this.storage.receiveEnergy(PRODUCE, false);
2015-05-20 22:39:43 +02:00
}
if(this.currentBurnTime <= 0 && this.slots[0] != null && TileEntityFurnace.getItemBurnTime(this.slots[0]) > 0 && this.storage.getEnergyStored() < this.storage.getMaxEnergyStored()){
int burnTime = TileEntityFurnace.getItemBurnTime(this.slots[0]);
2015-12-16 00:08:18 +01:00
this.maxBurnTime = burnTime;
this.currentBurnTime = burnTime;
this.slots[0].stackSize--;
if(this.slots[0].stackSize == 0){
this.slots[0] = this.slots[0].getItem().getContainerItem(this.slots[0]);
2015-05-20 22:39:43 +02:00
}
}
if(this.getEnergyStored(ForgeDirection.UNKNOWN) > 0){
WorldUtil.pushEnergy(worldObj, xCoord, yCoord, zCoord, ForgeDirection.UP, storage);
WorldUtil.pushEnergy(worldObj, xCoord, yCoord, zCoord, ForgeDirection.DOWN, storage);
WorldUtil.pushEnergy(worldObj, xCoord, yCoord, zCoord, ForgeDirection.NORTH, storage);
WorldUtil.pushEnergy(worldObj, xCoord, yCoord, zCoord, ForgeDirection.EAST, storage);
WorldUtil.pushEnergy(worldObj, xCoord, yCoord, zCoord, ForgeDirection.SOUTH, storage);
WorldUtil.pushEnergy(worldObj, xCoord, yCoord, zCoord, ForgeDirection.WEST, storage);
}
2015-06-21 02:28:49 +02:00
if(flag != this.currentBurnTime > 0){
this.markDirty();
2015-06-21 02:28:49 +02:00
int meta = worldObj.getBlockMetadata(xCoord, yCoord, zCoord);
if(meta == 1){
if(!(this.currentBurnTime <= 0 && this.slots[0] != null && TileEntityFurnace.getItemBurnTime(this.slots[0]) > 0 && this.storage.getEnergyStored() < this.storage.getMaxEnergyStored())){
2015-06-21 02:28:49 +02:00
worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, 0, 2);
2015-10-02 16:48:01 +02:00
}
}
else{
worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, 1, 2);
2015-06-21 02:28:49 +02:00
}
}
if((this.storage.getEnergyStored() != this.lastEnergy || this.currentBurnTime != this.lastCurrentBurnTime || this.lastBurnTime != this.maxBurnTime) && this.sendUpdateWithInterval()){
this.lastEnergy = this.storage.getEnergyStored();
this.lastCurrentBurnTime = this.currentBurnTime;
this.lastBurnTime = this.currentBurnTime;
}
2015-05-20 22:39:43 +02:00
}
}
@Override
2015-12-19 10:30:39 +01:00
public void writeSyncableNBT(NBTTagCompound compound, boolean sync){
compound.setInteger("BurnTime", this.currentBurnTime);
compound.setInteger("MaxBurnTime", this.maxBurnTime);
this.storage.writeToNBT(compound);
super.writeSyncableNBT(compound, sync);
}
@Override
public void readSyncableNBT(NBTTagCompound compound, boolean sync){
this.currentBurnTime = compound.getInteger("BurnTime");
this.maxBurnTime = compound.getInteger("MaxBurnTime");
this.storage.readFromNBT(compound);
super.readSyncableNBT(compound, sync);
2015-05-20 22:39:43 +02:00
}
@Override
public boolean canInsertItem(int slot, ItemStack stack, int side){
return this.isItemValidForSlot(slot, stack);
}
2015-12-19 10:30:39 +01:00
@Override
public boolean isItemValidForSlot(int i, ItemStack stack){
return TileEntityFurnace.getItemBurnTime(stack) > 0;
}
2015-05-20 22:39:43 +02:00
@Override
public boolean canExtractItem(int slot, ItemStack stack, int side){
return false;
}
@Override
public int extractEnergy(ForgeDirection from, int maxReceive, boolean simulate){
return this.storage.extractEnergy(maxReceive, simulate);
2015-05-20 22:39:43 +02:00
}
@Override
public int getEnergyStored(ForgeDirection from){
return this.storage.getEnergyStored();
}
@Override
public int getMaxEnergyStored(ForgeDirection from){
return this.storage.getMaxEnergyStored();
}
@Override
public boolean canConnectEnergy(ForgeDirection from){
return true;
}
@Override
public int getEnergy(){
return this.storage.getEnergyStored();
}
@Override
public void setEnergy(int energy){
this.storage.setEnergyStored(energy);
}
2015-05-20 22:39:43 +02:00
}