mirror of
https://github.com/GTNewHorizons/ForestryMC.git
synced 2026-11-16 18:44:45 +00:00
130 lines
2.7 KiB
Java
130 lines
2.7 KiB
Java
package forestry.energy;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
public class EnergyStorage {
|
|
|
|
protected int energy;
|
|
protected int capacity;
|
|
protected int maxReceive;
|
|
protected int maxExtract;
|
|
|
|
public EnergyStorage(int capacity, int maxReceive, int maxExtract) {
|
|
this.capacity = capacity;
|
|
this.maxReceive = maxReceive;
|
|
this.maxExtract = maxExtract;
|
|
}
|
|
|
|
public EnergyStorage readFromNBT(NBTTagCompound nbt) {
|
|
this.energy = nbt.getInteger("Energy");
|
|
|
|
if (energy > capacity) {
|
|
energy = capacity;
|
|
}
|
|
return this;
|
|
}
|
|
|
|
public NBTTagCompound writeToNBT(NBTTagCompound nbt) {
|
|
if (energy < 0) {
|
|
energy = 0;
|
|
}
|
|
nbt.setInteger("Energy", energy);
|
|
return nbt;
|
|
}
|
|
|
|
public EnergyStorage setCapacity(int capacity) {
|
|
this.capacity = capacity;
|
|
|
|
if (energy > capacity) {
|
|
energy = capacity;
|
|
}
|
|
return this;
|
|
}
|
|
|
|
public EnergyStorage setMaxTransfer(int maxTransfer) {
|
|
|
|
setMaxReceive(maxTransfer);
|
|
setMaxExtract(maxTransfer);
|
|
return this;
|
|
}
|
|
|
|
public EnergyStorage setMaxReceive(int maxReceive) {
|
|
|
|
this.maxReceive = maxReceive;
|
|
return this;
|
|
}
|
|
|
|
public EnergyStorage setMaxExtract(int maxExtract) {
|
|
this.maxExtract = maxExtract;
|
|
return this;
|
|
}
|
|
|
|
public int getMaxReceive() {
|
|
return maxReceive;
|
|
}
|
|
|
|
public int getMaxExtract() {
|
|
return maxExtract;
|
|
}
|
|
|
|
/**
|
|
* This function is included to allow for server to client sync. Do not call this externally to the containing Tile Entity, as not all IEnergyHandlers
|
|
* are guaranteed to have it.
|
|
*
|
|
* @param energy
|
|
*/
|
|
public void setEnergyStored(int energy) {
|
|
if (energy > capacity) {
|
|
this.energy = capacity;
|
|
} else if (energy < 0) {
|
|
this.energy = 0;
|
|
} else {
|
|
this.energy = energy;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* This function is included to allow the containing tile to directly and efficiently modify the energy contained in the EnergyStorage. Do not rely on this
|
|
* externally, as not all IEnergyHandlers are guaranteed to have it.
|
|
*
|
|
* @param energy
|
|
*/
|
|
public void modifyEnergyStored(int energy) {
|
|
this.energy += energy;
|
|
|
|
if (this.energy > capacity) {
|
|
this.energy = capacity;
|
|
} else if (this.energy < 0) {
|
|
this.energy = 0;
|
|
}
|
|
}
|
|
|
|
/* IEnergyStorage */
|
|
public int receiveEnergy(long maxReceive, boolean simulate) {
|
|
int energyReceived = (int) Math.min(capacity - energy, Math.min(this.maxReceive, maxReceive));
|
|
|
|
if (!simulate) {
|
|
energy += energyReceived;
|
|
}
|
|
return energyReceived;
|
|
}
|
|
|
|
public int extractEnergy(long maxExtract, boolean simulate) {
|
|
int energyExtracted = (int) Math.min(energy, Math.min(this.maxExtract, maxExtract));
|
|
|
|
if (!simulate) {
|
|
energy -= energyExtracted;
|
|
}
|
|
|
|
return energyExtracted;
|
|
}
|
|
|
|
public int getEnergyStored() {
|
|
return energy;
|
|
}
|
|
|
|
public int getMaxEnergyStored() {
|
|
return capacity;
|
|
}
|
|
}
|