aboutsummaryrefslogtreecommitdiff
path: root/src/Java/cofh/energy/ItemEnergyContainer.java
blob: 3467aeaf0357fc9138688c960172593bd0188b33 (plain)
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
97
98
99
100
101
102
package cofh.energy;

import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;

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 void setMaxTransfer(int maxTransfer)
  {
    setMaxReceive(maxTransfer);
    setMaxExtract(maxTransfer);
  }
  
  public void setMaxReceive(int maxReceive)
  {
    this.maxReceive = maxReceive;
  }
  
  public void setMaxExtract(int maxExtract)
  {
    this.maxExtract = maxExtract;
  }
  
  @Override
public int receiveEnergy(ItemStack container, int maxReceive, boolean simulate)
  {
    if (container.stackTagCompound == null) {
      container.stackTagCompound = new NBTTagCompound();
    }
    int energy = container.stackTagCompound.getInteger("Energy");
    int energyReceived = Math.min(this.capacity - energy, Math.min(this.maxReceive, maxReceive));
    if (!simulate)
    {
      energy += energyReceived;
      container.stackTagCompound.setInteger("Energy", energy);
    }
    return energyReceived;
  }
  
  @Override
public int extractEnergy(ItemStack container, int maxExtract, boolean simulate)
  {
    if ((container.stackTagCompound == null) || (!container.stackTagCompound.hasKey("Energy"))) {
      return 0;
    }
    int energy = container.stackTagCompound.getInteger("Energy");
    int energyExtracted = Math.min(energy, Math.min(this.maxExtract, maxExtract));
    if (!simulate)
    {
      energy -= energyExtracted;
      container.stackTagCompound.setInteger("Energy", energy);
    }
    return energyExtracted;
  }
  
  @Override
public int getEnergyStored(ItemStack container)
  {
    if ((container.stackTagCompound == null) || (!container.stackTagCompound.hasKey("Energy"))) {
      return 0;
    }
    return container.stackTagCompound.getInteger("Energy");
  }
  
  @Override
public int getMaxEnergyStored(ItemStack container)
  {
    return this.capacity;
  }
}