Have engines use EnergyManager

This commit is contained in:
mezz committed 2014-09-22 00:36:05 -07:00
1 parent 6cc67e2df5
commit 436a61978a
6 files changed
+87 -56

No files matched your search

@@ -3,7 +3,9 @@ package forestry.energy;
import cofh.api.energy.EnergyStorage;
import cofh.api.energy.IEnergyHandler;
import forestry.core.GameMode;
import forestry.core.utils.BlockUtil;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.util.ForgeDirection;
public class EnergyManager implements IEnergyHandler {
@@ -74,6 +76,15 @@ public class EnergyManager implements IEnergyHandler {
return nbt;
}
/* Packets */
public int toPacketInt() {
return energyStorage.getEnergyStored();
}
public void fromPacketInt(int packetInt) {
energyStorage.setEnergyStored(packetInt);
}
/* IEnergyHandler */
@Override
public int receiveEnergy(ForgeDirection from, int maxReceive, boolean simulate) {
@@ -94,6 +105,10 @@ public class EnergyManager implements IEnergyHandler {
return energyStorage.getEnergyStored();
}
public int getTotalEnergyStored() {
return energyStorage.getEnergyStored();
}
@Override
public int getMaxEnergyStored(ForgeDirection from) {
return energyStorage.getMaxEnergyStored();
@@ -114,4 +129,51 @@ public class EnergyManager implements IEnergyHandler {
energyStorage.modifyEnergyStored(-energyPerWork);
return true;
}
/**
* Sends as much energy as it can to the tile at orientation
* @param orientation
* @param tile
* @return amount sent
*/
public int sendEnergy(ForgeDirection orientation, TileEntity tile) {
return sendEnergy(orientation, tile, Integer.MAX_VALUE);
}
/**
* Sends amount of energy to the tile at orientation
* @param orientation
* @param tile
* @param amount
* @return amount sent
*/
public int sendEnergy(ForgeDirection orientation, TileEntity tile, int amount) {
int sent = 0;
if (BlockUtil.isRFTile(orientation.getOpposite(), tile)) {
IEnergyHandler receptor = (IEnergyHandler) tile;
int extractable = extractEnergy(orientation, amount, true);
if (extractable > 0) {
sent = receptor.receiveEnergy(orientation.getOpposite(), extractable, false);
extractEnergy(orientation, sent, false);
}
}
return sent;
}
/**
* Drains an amount of energy, due to decay from lack of work or other factors
* @param amount
*/
public void drainEnergy(int amount) {
energyStorage.modifyEnergyStored(-amount);
}
/**
* Creates an amount of energy, generated by engines
* @param amount
*/
public void generateEnergy(int amount) {
energyStorage.modifyEnergyStored(amount);
}
}