mirror of
https://github.com/Ellpeck/ActuallyAdditions.git
synced 2024-06-16 10:54:31 +02:00
be421af8e2
Ignore this commit for diffs
73 lines
2.1 KiB
Java
73 lines
2.1 KiB
Java
package de.ellpeck.actuallyadditions.common.tile;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
import net.minecraftforge.energy.EnergyStorage;
|
|
|
|
public class CustomEnergyStorage extends EnergyStorage {
|
|
|
|
public CustomEnergyStorage(int capacity, int maxReceive, int maxExtract) {
|
|
super(capacity, maxReceive, maxExtract);
|
|
}
|
|
|
|
public int extractEnergyInternal(int maxExtract, boolean simulate) {
|
|
int before = this.maxExtract;
|
|
this.maxExtract = Integer.MAX_VALUE;
|
|
|
|
int toReturn = this.extractEnergy(maxExtract, simulate);
|
|
|
|
this.maxExtract = before;
|
|
return toReturn;
|
|
}
|
|
|
|
public int receiveEnergyInternal(int maxReceive, boolean simulate) {
|
|
int before = this.maxReceive;
|
|
this.maxReceive = Integer.MAX_VALUE;
|
|
|
|
int toReturn = this.receiveEnergy(maxReceive, simulate);
|
|
|
|
this.maxReceive = before;
|
|
return toReturn;
|
|
}
|
|
|
|
public void addEnergyRaw(int energy) {
|
|
this.energy = Math.min(this.energy + energy, this.capacity);
|
|
}
|
|
|
|
@Override
|
|
public int receiveEnergy(int maxReceive, boolean simulate) {
|
|
if (!this.canReceive()) { return 0; }
|
|
int energy = this.getEnergyStored();
|
|
|
|
int energyReceived = Math.min(this.capacity - energy, Math.min(this.maxReceive, maxReceive));
|
|
if (!simulate) {
|
|
this.setEnergyStored(energy + energyReceived);
|
|
}
|
|
|
|
return energyReceived;
|
|
}
|
|
|
|
@Override
|
|
public int extractEnergy(int maxExtract, boolean simulate) {
|
|
if (!this.canExtract()) { return 0; }
|
|
int energy = this.getEnergyStored();
|
|
|
|
int energyExtracted = Math.min(energy, Math.min(this.maxExtract, maxExtract));
|
|
if (!simulate) {
|
|
this.setEnergyStored(energy - energyExtracted);
|
|
}
|
|
return energyExtracted;
|
|
}
|
|
|
|
public void readFromNBT(NBTTagCompound compound) {
|
|
this.setEnergyStored(compound.getInteger("Energy"));
|
|
}
|
|
|
|
public void writeToNBT(NBTTagCompound compound) {
|
|
compound.setInteger("Energy", this.getEnergyStored());
|
|
}
|
|
|
|
public void setEnergyStored(int energy) {
|
|
this.energy = energy;
|
|
}
|
|
}
|