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
114 lines
2.6 KiB
Java
114 lines
2.6 KiB
Java
package cofh.api.energy;
|
|
|
|
import net.minecraft.item.Item;
|
|
import net.minecraft.item.ItemStack;
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
/**
|
|
* Reference implementation of {@link IEnergyContainerItem}. Use/extend this or implement your own.
|
|
*
|
|
* @author King Lemming
|
|
*
|
|
*/
|
|
public class ItemEnergyContainer extends Item implements IEnergyContainerItem {
|
|
|
|
protected int capacity;
|
|
protected int maxReceive;
|
|
protected int maxExtract;
|
|
|
|
public ItemEnergyContainer() {
|
|
|
|
}
|
|
|
|
public ItemEnergyContainer(int capacity) {
|
|
|
|
this(capacity, capacity, capacity);
|
|
}
|
|
|
|
public ItemEnergyContainer(int capacity, int maxTransfer) {
|
|
|
|
this(capacity, maxTransfer, maxTransfer);
|
|
}
|
|
|
|
public ItemEnergyContainer(int capacity, int maxReceive, int maxExtract) {
|
|
|
|
this.capacity = capacity;
|
|
this.maxReceive = maxReceive;
|
|
this.maxExtract = maxExtract;
|
|
}
|
|
|
|
public ItemEnergyContainer setCapacity(int capacity) {
|
|
|
|
this.capacity = capacity;
|
|
return this;
|
|
}
|
|
|
|
public ItemEnergyContainer setMaxTransfer(int maxTransfer) {
|
|
|
|
setMaxReceive(maxTransfer);
|
|
setMaxExtract(maxTransfer);
|
|
return this;
|
|
}
|
|
|
|
public ItemEnergyContainer setMaxReceive(int maxReceive) {
|
|
|
|
this.maxReceive = maxReceive;
|
|
return this;
|
|
}
|
|
|
|
public ItemEnergyContainer setMaxExtract(int maxExtract) {
|
|
|
|
this.maxExtract = maxExtract;
|
|
return this;
|
|
}
|
|
|
|
/* IEnergyContainerItem */
|
|
@Override
|
|
public int receiveEnergy(ItemStack container, int maxReceive, boolean simulate) {
|
|
|
|
if (!container.hasTagCompound()) {
|
|
container.setTagCompound(new NBTTagCompound());
|
|
}
|
|
int energy = container.getTagCompound().getInteger("Energy");
|
|
int energyReceived = Math.min(capacity - energy, Math.min(this.maxReceive, maxReceive));
|
|
|
|
if (!simulate) {
|
|
energy += energyReceived;
|
|
container.getTagCompound().setInteger("Energy", energy);
|
|
}
|
|
return energyReceived;
|
|
}
|
|
|
|
@Override
|
|
public int extractEnergy(ItemStack container, int maxExtract, boolean simulate) {
|
|
|
|
if (container.getTagCompound() == null || !container.getTagCompound().hasKey("Energy")) {
|
|
return 0;
|
|
}
|
|
int energy = container.getTagCompound().getInteger("Energy");
|
|
int energyExtracted = Math.min(energy, Math.min(this.maxExtract, maxExtract));
|
|
|
|
if (!simulate) {
|
|
energy -= energyExtracted;
|
|
container.getTagCompound().setInteger("Energy", energy);
|
|
}
|
|
return energyExtracted;
|
|
}
|
|
|
|
@Override
|
|
public int getEnergyStored(ItemStack container) {
|
|
|
|
if (container.getTagCompound() == null || !container.getTagCompound().hasKey("Energy")) {
|
|
return 0;
|
|
}
|
|
return container.getTagCompound().getInteger("Energy");
|
|
}
|
|
|
|
@Override
|
|
public int getMaxEnergyStored(ItemStack container) {
|
|
|
|
return capacity;
|
|
}
|
|
|
|
}
|