-
Notifications
You must be signed in to change notification settings - Fork 753
/
ArmorCore.java
296 lines (253 loc) · 8.48 KB
/
ArmorCore.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package tconstruct.library.armor;
import net.minecraft.block.BlockDispenser;
import net.minecraft.dispenser.IBehaviorDispenseItem;
import net.minecraft.entity.*;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.*;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.DamageSource;
import net.minecraft.world.World;
import net.minecraftforge.common.ISpecialArmor;
import cofh.api.energy.IEnergyContainerItem;
/**
* NBTTags
* Main tag - InfiArmor
*/
public abstract class ArmorCore extends ItemArmor implements IEnergyContainerItem, ISpecialArmor
{
public static final String SET_NAME = "TinkerArmor";
public final EnumArmorPart armorPart;
private static final IBehaviorDispenseItem dispenserBehavior = new BehaviorDispenseArmorCopy();
public final int baseProtection;
//TE power constants -- TODO grab these from the items added
protected int capacity = 400000;
protected int maxReceive = 75;
protected int maxExtract = 75;
public ArmorCore(int baseProtection, EnumArmorPart part)
{
super(ArmorMaterial.CHAIN, 0, part.getPartId());
this.maxStackSize = 1;
this.setMaxDamage(100);
this.setUnlocalizedName(SET_NAME);
this.armorPart = part;
this.baseProtection = baseProtection;
BlockDispenser.dispenseBehaviorRegistry.putObject(this, dispenserBehavior);
}
public String getArmorName ()
{
return this.getClass().getSimpleName();
}
public ItemStack onItemRightClick (ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
{
int i = EntityLiving.getArmorPosition(par1ItemStack) - 1;
ItemStack itemstack1 = par3EntityPlayer.getCurrentArmor(i);
if (itemstack1 == null)
{
par3EntityPlayer.setCurrentItemOrArmor(i + 1, par1ItemStack.copy()); //Forge: Vanilla bug fix associated with fixed setCurrentItemOrArmor indexs for players.
par1ItemStack.stackSize = 0;
}
return par1ItemStack;
}
//ISpecialArmor overrides
@Override
public ArmorProperties getProperties (EntityLivingBase player, ItemStack armor, DamageSource source, double damage, int slot)
{
if (player.worldObj.isRemote)
{
return new ArmorProperties(0, 0, baseProtection);
}
NBTTagCompound tags = armor.getTagCompound();
if (tags == null)
{
return new ArmorProperties(0, damage / baseProtection, baseProtection);
}
NBTTagCompound data = tags.getCompoundTag(SET_NAME);
double amount = (data.getInteger("defense") / damage) + (data.getDouble("protection") / 100);
if (source.isUnblockable())
amount = 0;
clamp_double(amount, 0, 1);
return new ArmorProperties(0, amount, 100);
}
public static double clamp_double (double par0, double par1, double par2)
{
return par0 < par1 ? par1 : (par0 > par2 ? par2 : par0);
}
@Override
public int getArmorDisplay (EntityPlayer player, ItemStack armor, int slot)
{
if (!armor.hasTagCompound())
return this.baseProtection;
NBTTagCompound armorTag = armor.getTagCompound().getCompoundTag(SET_NAME);
double amount = armorTag.getDouble("protection") / 4;
if (amount > 0 && amount < 1)
amount = 1;
return (int) (Math.floor(amount));
}
@Override
public void damageArmor (EntityLivingBase entity, ItemStack stack, DamageSource source, int damage, int slot)
{
NBTTagCompound tags = stack.getTagCompound();
NBTTagCompound data;
if (tags == null)
{
tags = new NBTTagCompound();
stack.setTagCompound(tags);
data = new NBTTagCompound();
tags.setTag(SET_NAME, data);
data.setDouble("damageReduction", baseProtection); }
data = tags.getCompoundTag(SET_NAME);
if (tags.hasKey("Energy"))
{
int energy = tags.getInteger("Energy");
if (energy > damage)
{
energy -= damage;
tags.setInteger("Energy", energy);
return;
}
else
{
damage -= energy;
tags.setInteger("Energy", 0);
int dmg = data.getInteger("Damage");
dmg += damage;
data.setDouble("Damage", dmg);
}
}
else
{
int dmg = data.getInteger("Damage");
dmg += damage;
data.setInteger("Damage", dmg);
}
}
//TE support section -- from COFH core API reference section
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;
}
/* IEnergyContainerItem */
@Override
public int receiveEnergy (ItemStack container, int maxReceive, boolean simulate)
{
NBTTagCompound tags = container.getTagCompound();
if (tags == null || !tags.hasKey("Energy"))
return 0;
int energy = tags.getInteger("Energy");
int energyReceived = Math.min(capacity - energy, Math.min(this.maxReceive, maxReceive));
if (!simulate)
{
energy += energyReceived;
tags.setInteger("Energy", energy);
container.setItemDamage(1 + (getMaxEnergyStored(container) - energy) * (container.getMaxDamage() - 2) / getMaxEnergyStored(container));
}
return energyReceived;
}
@Override
public int extractEnergy (ItemStack container, int maxExtract, boolean simulate)
{
NBTTagCompound tags = container.getTagCompound();
if (tags == null || !tags.hasKey("Energy"))
{
return 0;
}
int energy = tags.getInteger("Energy");
int energyExtracted = Math.min(energy, Math.min(this.maxExtract, maxExtract));
if (!simulate)
{
energy -= energyExtracted;
tags.setInteger("Energy", energy);
container.setItemDamage(1 + (getMaxEnergyStored(container) - energy) * (container.getMaxDamage() - 1) / getMaxEnergyStored(container));
}
return energyExtracted;
}
@Override
public int getEnergyStored (ItemStack container)
{
NBTTagCompound tags = container.getTagCompound();
if (tags == null || !tags.hasKey("Energy"))
{
return 0;
}
return tags.getInteger("Energy");
}
@Override
public int getMaxEnergyStored (ItemStack container)
{
NBTTagCompound tags = container.getTagCompound();
if (tags == null || !tags.hasKey("Energy"))
return 0;
return capacity;
}
//end of TE support section
// Vanilla overrides
public boolean isItemTool (ItemStack par1ItemStack)
{
return false;
}
@Override
public boolean getIsRepairable (ItemStack par1ItemStack, ItemStack par2ItemStack)
{
return false;
}
public boolean isRepairable ()
{
return false;
}
public int getItemEnchantability ()
{
return 0;
}
public boolean isFull3D ()
{
return true;
}
public boolean isValidArmor (ItemStack stack, int armorType, Entity entity)
{
return this.armorPart.getPartId() == armorType;
}
/* Proper stack damage */
public int getItemMaxDamageFromStack (ItemStack stack)
{
NBTTagCompound tags = stack.getTagCompound();
if (tags == null)
{
return 0;
}
if (tags.hasKey("Energy"))
{
int energy = tags.getInteger("Energy");
if (energy > 0)
{
return this.getMaxEnergyStored(stack);
}
}
return tags.getCompoundTag(SET_NAME).getInteger("TotalDurability");
}
public int getItemMaxDamageFromStackForDisplay (ItemStack stack)
{
NBTTagCompound tags = stack.getTagCompound();
if (tags == null)
{
return 0;
}
if (tags.hasKey("Energy"))
{
int energy = tags.getInteger("Energy");
if (energy > 0)
{
return this.getMaxEnergyStored(stack) - energy;
}
}
return tags.getCompoundTag(SET_NAME).getInteger("Damage");
}
}