-
Notifications
You must be signed in to change notification settings - Fork 753
/
ModFlux.java
126 lines (102 loc) · 4.65 KB
/
ModFlux.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package tconstruct.modifiers.tools;
import cofh.api.energy.IEnergyContainerItem;
import java.util.ArrayList;
import tconstruct.library.tools.ToolCore;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
/* TE3 support */
public class ModFlux extends ModBoolean
{
public ArrayList<ItemStack> batteries = new ArrayList<ItemStack>();
public int modifiersRequired = 1; // LALALALA totally not hidden IguanaTweaks Support LALALALA
public ModFlux()
{
super(new ItemStack[0], 9, "Flux", "\u00a7e", "");
}
@Override
public boolean matches (ItemStack[] input, ItemStack tool)
{
NBTTagCompound tags = tool.getTagCompound().getCompoundTag("InfiTool");
ItemStack foundBattery = null;
// try to find the battery in the input
for(ItemStack stack : input)
for(ItemStack battery : batteries)
{
if(stack == null)
continue;
if(stack.getItem() != battery.getItem())
continue;
if(!(stack.getItem() instanceof IEnergyContainerItem))
continue;
// we don't allow multiple batteries to be added
if(foundBattery != null)
return false;
// battery found, gogogo
foundBattery = stack;
}
// no battery present
if(foundBattery == null)
return false;
// check if we already have a flux modifier
if(tags.getBoolean(key))
{
// only allow if it's an upgrade
// remark: we use the ToolCores function here instead of accessing the tag directly, to achieve backwards compatibility with tools without tags.
int a = ((IEnergyContainerItem)foundBattery.getItem()).getMaxEnergyStored(foundBattery);
int b = ((ToolCore)tool.getItem()).getMaxEnergyStored(tool);
return ((IEnergyContainerItem)foundBattery.getItem()).getMaxEnergyStored(foundBattery) > ((ToolCore)tool.getItem()).getMaxEnergyStored(tool);
}
// otherwise check if we have enough modfiers
else if(tags.getInteger("Modifiers") < modifiersRequired)
return false;
// all requirements satisfied!
return true;
}
@Override
public void modify (ItemStack[] input, ItemStack tool)
{
NBTTagCompound tags = tool.getTagCompound();
// update modifiers (only applies if it's not an upgrade)
if(!tags.hasKey(key)) {
int modifiers = tags.getCompoundTag("InfiTool").getInteger("Modifiers");
modifiers -= modifiersRequired;
tags.getCompoundTag("InfiTool").setInteger("Modifiers", modifiers);
addModifierTip(tool, "\u00a7eFlux");
}
tags.getCompoundTag("InfiTool").setBoolean(key, true);
// find the battery in the input
ItemStack inputBattery = null;
for(ItemStack stack : input)
for(ItemStack battery : batteries)
{
if(stack == null)
continue;
if(stack.getItem() != battery.getItem())
continue;
if(!(stack.getItem() instanceof IEnergyContainerItem))
continue;
// we're guaranteed to only find one battery because more are prevented above
inputBattery = stack;
}
// get the energy interface
IEnergyContainerItem energyContainer = (IEnergyContainerItem)inputBattery.getItem();
// set the charge values
int charge = energyContainer.getEnergyStored(inputBattery);
// add already present charge in the tool
if(tags.hasKey("Energy"))
charge += tags.getInteger("Energy");
int maxCharge = energyContainer.getMaxEnergyStored(inputBattery);
// simulate transferring maximum amount of POWER to obtain the maximum receive-limit of the battery ;_;
int maxExtract = energyContainer.extractEnergy(inputBattery, Integer.MAX_VALUE, true);
int maxReceive = energyContainer.receiveEnergy(inputBattery, Integer.MAX_VALUE, true);
// make sure we don't overcharge
charge = Math.min(charge, maxCharge);
tags.setInteger("Energy", charge);
tags.setInteger("EnergyMax", maxCharge);
tags.setInteger("EnergyExtractionRate", maxExtract);
tags.setInteger("EnergyReceiveRate", maxReceive);
tags.setInteger(key, 1);
ToolCore toolcore = (ToolCore) tool.getItem();
tool.setItemDamage(1 + (toolcore.getMaxEnergyStored(tool) - charge) * (tool.getMaxDamage() - 1) / toolcore.getMaxEnergyStored(tool));
}
}