ActuallyAdditions/src/main/java/cofh/api/energy/ItemEnergyContainer.java

114 lines
2.6 KiB
Java
Raw Normal View History

2015-07-15 00:19:44 +02:00
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.
2015-10-03 22:13:57 +02:00
*
2015-07-15 00:19:44 +02:00
* @author King Lemming
2015-10-03 22:13:57 +02:00
*
2015-07-15 00:19:44 +02:00
*/
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;
}
2016-02-16 21:49:44 +01:00
public ItemEnergyContainer setMaxTransfer(int maxTransfer) {
2015-07-15 00:19:44 +02:00
setMaxReceive(maxTransfer);
setMaxExtract(maxTransfer);
2016-02-16 21:49:44 +01:00
return this;
2015-07-15 00:19:44 +02:00
}
2016-02-16 21:49:44 +01:00
public ItemEnergyContainer setMaxReceive(int maxReceive) {
2015-07-15 00:19:44 +02:00
this.maxReceive = maxReceive;
2016-02-16 21:49:44 +01:00
return this;
2015-07-15 00:19:44 +02:00
}
2016-02-16 21:49:44 +01:00
public ItemEnergyContainer setMaxExtract(int maxExtract) {
2015-07-15 00:19:44 +02:00
this.maxExtract = maxExtract;
2016-02-16 21:49:44 +01:00
return this;
2015-07-15 00:19:44 +02:00
}
/* IEnergyContainerItem */
@Override
public int receiveEnergy(ItemStack container, int maxReceive, boolean simulate) {
2016-02-16 21:49:44 +01:00
if (!container.hasTagCompound()) {
2016-01-07 18:20:59 +01:00
container.setTagCompound(new NBTTagCompound());
2015-07-15 00:19:44 +02:00
}
2016-01-07 18:20:59 +01:00
int energy = container.getTagCompound().getInteger("Energy");
2015-07-15 00:19:44 +02:00
int energyReceived = Math.min(capacity - energy, Math.min(this.maxReceive, maxReceive));
if (!simulate) {
energy += energyReceived;
2016-01-07 18:20:59 +01:00
container.getTagCompound().setInteger("Energy", energy);
2015-07-15 00:19:44 +02:00
}
return energyReceived;
}
@Override
public int extractEnergy(ItemStack container, int maxExtract, boolean simulate) {
2016-01-07 18:20:59 +01:00
if (container.getTagCompound() == null || !container.getTagCompound().hasKey("Energy")) {
2015-07-15 00:19:44 +02:00
return 0;
}
2016-01-07 18:20:59 +01:00
int energy = container.getTagCompound().getInteger("Energy");
2015-07-15 00:19:44 +02:00
int energyExtracted = Math.min(energy, Math.min(this.maxExtract, maxExtract));
if (!simulate) {
energy -= energyExtracted;
2016-01-07 18:20:59 +01:00
container.getTagCompound().setInteger("Energy", energy);
2015-07-15 00:19:44 +02:00
}
return energyExtracted;
}
@Override
public int getEnergyStored(ItemStack container) {
2016-01-07 18:20:59 +01:00
if (container.getTagCompound() == null || !container.getTagCompound().hasKey("Energy")) {
2015-07-15 00:19:44 +02:00
return 0;
}
2016-01-07 18:20:59 +01:00
return container.getTagCompound().getInteger("Energy");
2015-07-15 00:19:44 +02:00
}
@Override
public int getMaxEnergyStored(ItemStack container) {
return capacity;
}
}