-
Notifications
You must be signed in to change notification settings - Fork 497
/
RFBattery.java
96 lines (75 loc) · 2.19 KB
/
RFBattery.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package buildcraft.core.lib;
import net.minecraft.nbt.NBTTagCompound;
import cofh.api.energy.IEnergyStorage;
public class RFBattery implements IEnergyStorage {
private int energy, maxEnergy, maxReceive, maxExtract;
public RFBattery(int maxEnergy, int maxReceive, int maxExtract) {
this.maxEnergy = maxEnergy;
this.maxReceive = maxReceive;
this.maxExtract = maxExtract;
}
public void readFromNBT(NBTTagCompound tag) {
if (tag.hasKey("energy") && tag.hasKey("maxEnergy") && tag.hasKey("maxReceive") && tag.hasKey("maxExtract")) {
this.energy = tag.getInteger("energy");
this.maxEnergy = tag.getInteger("maxEnergy");
this.maxReceive = tag.getInteger("maxReceive");
this.maxExtract = tag.getInteger("maxExtract");
}
}
public void writeToNBT(NBTTagCompound tag) {
tag.setInteger("energy", this.energy);
tag.setInteger("maxEnergy", this.maxEnergy);
tag.setInteger("maxReceive", this.maxReceive);
tag.setInteger("maxExtract", this.maxExtract);
}
public int addEnergy(int minReceive, int maxReceive, boolean simulate) {
int amountReceived = Math.min(maxReceive, maxEnergy - energy);
if (amountReceived < minReceive) {
return 0;
}
if (!simulate) {
energy += amountReceived;
}
return amountReceived;
}
public int useEnergy(int minExtract, int maxExtract, boolean simulate) {
int amountExtracted = Math.min(maxExtract, energy);
if (amountExtracted < minExtract) {
return 0;
}
if (!simulate) {
energy -= amountExtracted;
}
return amountExtracted;
}
@Override
public int receiveEnergy(int maxReceive, boolean simulate) {
return addEnergy(0, Math.min(maxReceive, this.maxReceive), simulate);
}
@Override
public int extractEnergy(int maxExtract, boolean simulate) {
return useEnergy(0, Math.min(maxExtract, this.maxExtract), simulate);
}
@Override
public int getEnergyStored() {
return energy;
}
@Override
public int getMaxEnergyStored() {
return maxEnergy;
}
public int getMaxEnergyReceive() {
return maxReceive;
}
public int getMaxEnergyExtract() {
return maxExtract;
}
public void setEnergy(int iEnergy) {
energy = iEnergy;
if (energy < 0) {
energy = 0;
} else if (energy > maxEnergy) {
energy = maxEnergy;
}
}
}