mirror of
https://github.com/GTNewHorizons/ForestryMC.git
synced 2026-11-18 11:19:28 +00:00
Move ForestryAPI into the Forestry project instead of as a submodule Include the new RF API Remove code for mods that have not yet ported to 1.8.9 (add it back later) Bump version to 5.0.0
163 lines
3.1 KiB
Java
163 lines
3.1 KiB
Java
package cofh.api.energy;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
/**
|
|
* Reference implementation of {@link IEnergyStorage}. Use/extend this or implement your own.
|
|
*
|
|
* @author King Lemming
|
|
*
|
|
*/
|
|
public class EnergyStorage implements IEnergyStorage {
|
|
|
|
protected int energy;
|
|
protected int capacity;
|
|
protected int maxReceive;
|
|
protected int maxExtract;
|
|
|
|
public EnergyStorage(int capacity) {
|
|
|
|
this(capacity, capacity, capacity);
|
|
}
|
|
|
|
public EnergyStorage(int capacity, int maxTransfer) {
|
|
|
|
this(capacity, maxTransfer, maxTransfer);
|
|
}
|
|
|
|
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) {
|
|
|
|
this.energy = energy;
|
|
|
|
if (this.energy > capacity) {
|
|
this.energy = capacity;
|
|
} else if (this.energy < 0) {
|
|
this.energy = 0;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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 */
|
|
@Override
|
|
public int receiveEnergy(int maxReceive, boolean simulate) {
|
|
|
|
int energyReceived = Math.min(capacity - energy, Math.min(this.maxReceive, maxReceive));
|
|
|
|
if (!simulate) {
|
|
energy += energyReceived;
|
|
}
|
|
return energyReceived;
|
|
}
|
|
|
|
@Override
|
|
public int extractEnergy(int maxExtract, boolean simulate) {
|
|
|
|
int energyExtracted = Math.min(energy, Math.min(this.maxExtract, maxExtract));
|
|
|
|
if (!simulate) {
|
|
energy -= energyExtracted;
|
|
}
|
|
return energyExtracted;
|
|
}
|
|
|
|
@Override
|
|
public int getEnergyStored() {
|
|
|
|
return energy;
|
|
}
|
|
|
|
@Override
|
|
public int getMaxEnergyStored() {
|
|
|
|
return capacity;
|
|
}
|
|
|
|
}
|