-
Notifications
You must be signed in to change notification settings - Fork 755
/
AbilityHelper.java
637 lines (557 loc) · 24.6 KB
/
AbilityHelper.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
package tconstruct.library.tools;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import net.minecraft.block.Block;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.monster.EntityCreeper;
import net.minecraft.entity.monster.EntityGhast;
import net.minecraft.entity.monster.IMob;
import net.minecraft.entity.passive.EntityWolf;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.potion.Potion;
import net.minecraft.stats.AchievementList;
import net.minecraft.stats.StatList;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.DamageSource;
import net.minecraft.util.MathHelper;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.util.Vec3;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.util.FakePlayer;
import net.minecraftforge.event.entity.player.UseHoeEvent;
import tconstruct.library.ActiveToolMod;
import tconstruct.library.TConstructRegistry;
import tconstruct.library.util.PiercingEntityDamage;
import cpw.mods.fml.common.eventhandler.Event.Result;
public class AbilityHelper
{
public static Random random = new Random();
public static boolean necroticUHS;
/* Normal interactions */
public static boolean onBlockChanged (ItemStack stack, World world, Block block, int x, int y, int z, EntityLivingBase player, Random random)
{
if (!stack.hasTagCompound())
return false;
int reinforced = 0;
NBTTagCompound tags = stack.getTagCompound();
if (tags.getCompoundTag("InfiTool").hasKey("Unbreaking"))
reinforced = tags.getCompoundTag("InfiTool").getInteger("Unbreaking");
if (random.nextInt(10) < 10 - reinforced)
{
damageTool(stack, 1, tags, player, false);
}
return true;
}
public static boolean onLeftClickEntity (ItemStack stack, EntityLivingBase player, Entity entity, ToolCore tool)
{
return onLeftClickEntity(stack, player, entity, tool, 0);
}
public static boolean onLeftClickEntity (ItemStack stack, EntityLivingBase player, Entity entity, ToolCore tool, int baseDamage)
{
if (entity.canAttackWithItem() && stack.hasTagCompound())
{
if (!entity.hitByEntity(player)) // can't attack this entity
{
NBTTagCompound tags = stack.getTagCompound();
NBTTagCompound toolTags = stack.getTagCompound().getCompoundTag("InfiTool");
int damage = toolTags.getInteger("Attack") + baseDamage;
boolean broken = toolTags.getBoolean("Broken");
int durability = tags.getCompoundTag("InfiTool").getInteger("Damage");
float stonebound = tags.getCompoundTag("InfiTool").getFloat("Shoddy");
float stoneboundDamage = (float) Math.log(durability / 72f + 1) * -2 * stonebound;
int earlyModDamage = 0;
for (ActiveToolMod mod : TConstructRegistry.activeModifiers)
{
earlyModDamage = mod.baseAttackDamage(earlyModDamage, damage, tool, tags, toolTags, stack, player, entity);
}
damage += earlyModDamage;
if (player.isPotionActive(Potion.damageBoost))
{
damage += 3 << player.getActivePotionEffect(Potion.damageBoost).getAmplifier();
}
if (player.isPotionActive(Potion.weakness))
{
damage -= 2 << player.getActivePotionEffect(Potion.weakness).getAmplifier();
}
float knockback = 0;
float enchantDamage = 0;
if (entity instanceof EntityLivingBase)
{
enchantDamage = EnchantmentHelper.getEnchantmentModifierLiving(player, (EntityLivingBase) entity);
knockback += EnchantmentHelper.getKnockbackModifier(player, (EntityLivingBase) entity);
}
damage += stoneboundDamage;
if (damage < 1)
damage = 1;
if (player.isSprinting())
{
knockback++;
float lunge = tool.chargeAttack();
if (lunge > 1f)
{
knockback += lunge - 1.0f;
damage *= lunge;
}
}
float modKnockback = 0f;
for (ActiveToolMod mod : TConstructRegistry.activeModifiers)
{
modKnockback = mod.knockback(modKnockback, knockback, tool, tags, toolTags, stack, player, entity);
}
knockback += modKnockback;
int modDamage = 0;
for (ActiveToolMod mod : TConstructRegistry.activeModifiers)
{
modDamage = mod.attackDamage(modDamage, damage, tool, tags, toolTags, stack, player, entity);
}
damage += modDamage;
if (damage > 0 || enchantDamage > 0)
{
boolean criticalHit = player.fallDistance > 0.0F && !player.onGround && !player.isOnLadder() && !player.isInWater() && !player.isPotionActive(Potion.blindness)
&& player.ridingEntity == null && entity instanceof EntityLivingBase;
for (ActiveToolMod mod : TConstructRegistry.activeModifiers)
{
if (mod.doesCriticalHit(tool, tags, toolTags, stack, player, entity))
criticalHit = true;
}
if (criticalHit)
{
damage += random.nextInt(damage / 2 + 2);
}
damage += enchantDamage;
if (tool.getDamageModifier() != 1f)
{
damage *= tool.getDamageModifier();
}
boolean var6 = false;
int fireAspect = EnchantmentHelper.getFireAspectModifier(player);
if (entity instanceof EntityLivingBase && fireAspect > 0 && !entity.isBurning())
{
var6 = true;
entity.setFire(1);
}
if (broken)
{
if (baseDamage > 0)
damage = baseDamage;
else
damage = 1;
}
boolean causedDamage = false;
if (tool.pierceArmor() && !broken)
{
if (player instanceof EntityPlayer)
causedDamage = entity.attackEntityFrom(causePlayerPiercingDamage((EntityPlayer) player), damage);
else
causedDamage = entity.attackEntityFrom(causePiercingDamage(player), damage);
}
else
{
if (player instanceof EntityPlayer)
causedDamage = entity.attackEntityFrom(DamageSource.causePlayerDamage((EntityPlayer) player), damage);
else
causedDamage = entity.attackEntityFrom(DamageSource.causeMobDamage(player), damage);
}
if (causedDamage)
{
int reinforced = 0;
if (toolTags.hasKey("Unbreaking"))
reinforced = tags.getCompoundTag("InfiTool").getInteger("Unbreaking");
if (random.nextInt(10) < 10 - reinforced)
{
damageTool(stack, 1, tags, player, false);
}
// damageTool(stack, 1, player, false);
tool.onEntityDamaged(player.worldObj, player, entity);
if (!necroticUHS || (entity instanceof IMob && entity instanceof EntityLivingBase && ((EntityLivingBase) entity).getHealth() <= 0))
{
int drain = toolTags.getInteger("Necrotic") * 2;
if (drain > 0)
player.heal(random.nextInt(drain + 1));
}
if (knockback > 0)
{
entity.addVelocity((double) (-MathHelper.sin(player.rotationYaw * (float) Math.PI / 180.0F) * (float) knockback * 0.5F), 0.1D,
(double) (MathHelper.cos(player.rotationYaw * (float) Math.PI / 180.0F) * (float) knockback * 0.5F));
player.motionX *= 0.6D;
player.motionZ *= 0.6D;
player.setSprinting(false);
}
if (player instanceof EntityPlayer)
{
if (criticalHit)
{
((EntityPlayer) player).onCriticalHit(entity);
}
if (enchantDamage > 0)
{
((EntityPlayer) player).onEnchantmentCritical(entity);
}
if (damage >= 18)
{
((EntityPlayer) player).triggerAchievement(AchievementList.overkill);
}
}
player.setLastAttacker(entity);
if (entity instanceof EntityLivingBase)
{
DamageSource.causeThornsDamage(entity);// (((EntityLivingBase)player,
// (EntityLivingBase)
// entity);
}
}
if (entity instanceof EntityLivingBase)
{
if (entity instanceof EntityPlayer)
{
stack.hitEntity((EntityLivingBase) entity, (EntityPlayer) player);
if (entity.isEntityAlive())
{
alertPlayerWolves((EntityPlayer) player, (EntityLivingBase) entity, true);
}
((EntityPlayer) player).addStat(StatList.damageDealtStat, damage);
}
else
{
stack.getItem().hitEntity(stack, (EntityLivingBase) entity, player);
}
if ((fireAspect > 0 || toolTags.hasKey("Fiery") || toolTags.hasKey("Lava")) && causedDamage)
{
fireAspect *= 4;
if (toolTags.hasKey("Fiery"))
{
fireAspect += toolTags.getInteger("Fiery") / 5 + 1;
}
if (toolTags.getBoolean("Lava"))
{
fireAspect += 3;
}
entity.setFire(fireAspect);
}
else if (var6)
{
entity.extinguish();
}
}
if (entity instanceof EntityPlayer)
((EntityPlayer) player).addExhaustion(0.3F);
if (causedDamage)
return true;
}
}
}
return false;
}
static void alertPlayerWolves (EntityPlayer player, EntityLivingBase living, boolean par2)
{
if (!(living instanceof EntityCreeper) && !(living instanceof EntityGhast))
{
if (living instanceof EntityWolf)
{
EntityWolf var3 = (EntityWolf) living;
if (var3.isTamed() && player.getDisplayName().equals(var3.func_152113_b()))
{
return;
}
}
if (!(living instanceof EntityPlayer) || player.canAttackPlayer((EntityPlayer) living))
{
List var6 = player.worldObj.getEntitiesWithinAABB(EntityWolf.class,
AxisAlignedBB.getBoundingBox(player.posX, player.posY, player.posZ, player.posX + 1.0D, player.posY + 1.0D, player.posZ + 1.0D).expand(16.0D, 4.0D, 16.0D));
Iterator var4 = var6.iterator();
while (var4.hasNext())
{
EntityWolf var5 = (EntityWolf) var4.next();
if (var5.isTamed() && var5.getEntityToAttack() == null && player.getDisplayName().equals(var5.func_152113_b()) && (!par2 || !var5.isSitting()))
{
var5.setSitting(false);
var5.setTarget(living);
}
}
}
}
}
/* Tool specific */
public static void damageTool (ItemStack stack, int dam, EntityLivingBase entity, boolean ignoreCharge)
{
NBTTagCompound tags = stack.getTagCompound();
damageTool(stack, dam, tags, entity, ignoreCharge);
}
public static void healTool (ItemStack stack, int dam, EntityLivingBase entity, boolean ignoreCharge)
{
NBTTagCompound tags = stack.getTagCompound();
damageTool(stack, -dam, tags, entity, ignoreCharge);
}
public static void damageTool (ItemStack stack, int dam, NBTTagCompound tags, EntityLivingBase entity, boolean ignoreCharge)
{
if (entity instanceof EntityPlayer && ((EntityPlayer) entity).capabilities.isCreativeMode || tags == null)
return;
if (ignoreCharge || !damageElectricTool(stack, tags, entity))
{
boolean damagedTool = false;
for (ActiveToolMod mod : TConstructRegistry.activeModifiers)
{
if (mod.damageTool(stack, dam, entity))
damagedTool = true;
}
if (damagedTool)
return;
int damage = tags.getCompoundTag("InfiTool").getInteger("Damage");
int damageTrue = damage + dam;
int maxDamage = tags.getCompoundTag("InfiTool").getInteger("TotalDurability");
if (damageTrue <= 0)
{
tags.getCompoundTag("InfiTool").setInteger("Damage", 0);
stack.setItemDamage(0);
tags.getCompoundTag("InfiTool").setBoolean("Broken", false);
}
else if (damageTrue > maxDamage)
{
breakTool(stack, tags, entity);
stack.setItemDamage(0);
}
else
{
tags.getCompoundTag("InfiTool").setInteger("Damage", damage + dam);
int toolDamage = (damage * 100 / maxDamage) + 1;
int stackDamage = stack.getItemDamage();
if (toolDamage != stackDamage)
{
stack.setItemDamage((damage * 100 / maxDamage) + 1);
}
}
}
}
public static boolean damageElectricTool (ItemStack stack, NBTTagCompound tags, Entity entity)
{
if (tags.hasKey("Energy"))
{
NBTTagCompound toolTag = stack.getTagCompound().getCompoundTag("InfiTool");
int energy = -1;
if (tags.hasKey("Energy"))
{
energy = tags.getInteger("Energy");
}
int durability = toolTag.getInteger("Damage");
float shoddy = toolTag.getFloat("Shoddy");
float mineSpeed = toolTag.getInteger("MiningSpeed");
int heads = 1;
if (toolTag.hasKey("MiningSpeed2"))
{
mineSpeed += toolTag.getInteger("MiningSpeed2");
heads++;
}
if (toolTag.hasKey("MiningSpeedHandle"))
{
mineSpeed += toolTag.getInteger("MiningSpeedHandle");
heads++;
}
if (toolTag.hasKey("MiningSpeedExtra"))
{
mineSpeed += toolTag.getInteger("MiningSpeedExtra");
heads++;
}
float trueSpeed = mineSpeed / (heads * 100f);
float stonebound = toolTag.getFloat("Shoddy");
float bonusLog = (float) Math.log(durability / 72f + 1) * 2 * stonebound;
trueSpeed += bonusLog;
trueSpeed *= 6;
if (energy != -1)
{
if (energy < trueSpeed * 2)
{
if (energy > 0)
tags.setInteger("Energy", 0);
return false;
}
energy -= trueSpeed * 2;
ToolCore tool = (ToolCore) stack.getItem();
stack.setItemDamage(1 + (tool.getMaxEnergyStored(stack) - energy) * (stack.getMaxDamage() - 1) / tool.getMaxEnergyStored(stack));
tags.setInteger("Energy", energy);
}
return true;
}
else
{
return false;
}
}
public static void breakTool (ItemStack stack, NBTTagCompound tags, Entity entity)
{
tags.getCompoundTag("InfiTool").setBoolean("Broken", true);
if (entity != null)
entity.worldObj.playSound(entity.posX, entity.posY, entity.posZ, "random.break", 1f, 1f, true);
}
public static void repairTool (ItemStack stack, NBTTagCompound tags)
{
tags.getCompoundTag("InfiTool").setBoolean("Broken", false);
tags.getCompoundTag("InfiTool").setInteger("Damage", 0);
}
public static DamageSource causePiercingDamage (EntityLivingBase mob)
{
return new PiercingEntityDamage("mob", mob);
}
public static DamageSource causePlayerPiercingDamage (EntityPlayer player)
{
return new PiercingEntityDamage("player", player);
}
public static void knockbackEntity (EntityLivingBase living, double boost)
{
living.motionX *= boost;
// living.motionY *= boost/2;
living.motionZ *= boost;
}
public static boolean hoeGround (ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int side, Random random)
{
if (!player.canPlayerEdit(x, y, z, side, stack))
{
return false;
}
else
{
UseHoeEvent event = new UseHoeEvent(player, stack, world, x, y, z);
if (MinecraftForge.EVENT_BUS.post(event))
{
return false;
}
if (event.getResult() == Result.ALLOW)
{
damageTool(stack, 1, player, false);
return true;
}
Block block = world.getBlock(x, y, z);
if (side != 0 && world.getBlock(x, y + 1, z).isAir(world, x, y + 1, z) && (block == Blocks.grass || block == Blocks.dirt))
{
Block block1 = Blocks.farmland;
world.playSoundEffect((double)((float)x + 0.5F), (double)((float)y + 0.5F), (double)((float)z + 0.5F), block1.stepSound.getStepResourcePath(), (block1.stepSound.getVolume() + 1.0F) / 2.0F, block1.stepSound.getPitch() * 0.8F);
if (world.isRemote)
{
return true;
}
else
{
world.setBlock(x, y, z, block1);
damageTool(stack, 1, player, false);
return true;
}
}
else
{
return false;
}
}
}
public static void spawnItemAtEntity (Entity entity, ItemStack stack, int delay)
{
if (!entity.worldObj.isRemote)
{
EntityItem entityitem = new EntityItem(entity.worldObj, entity.posX + 0.5D, entity.posY + 0.5D, entity.posZ + 0.5D, stack);
entityitem.delayBeforeCanPickup = delay;
entity.worldObj.spawnEntityInWorld(entityitem);
}
}
public static void spawnItemAtPlayer (EntityPlayer player, ItemStack stack)
{
if (!player.worldObj.isRemote)
{
EntityItem entityitem = new EntityItem(player.worldObj, player.posX + 0.5D, player.posY + 0.5D, player.posZ + 0.5D, stack);
player.worldObj.spawnEntityInWorld(entityitem);
if (!(player instanceof FakePlayer))
entityitem.onCollideWithPlayer(player);
}
}
/* Ranged weapons */
public static void forceAddToInv (EntityPlayer entityplayer, ItemStack itemstack, int i, boolean flag)
{
ItemStack itemstack1 = entityplayer.inventory.getStackInSlot(i);
entityplayer.inventory.setInventorySlotContents(i, itemstack);
if (itemstack1 != null)
{
addToInv(entityplayer, itemstack1, flag);
}
}
public static boolean addToInv (EntityPlayer entityplayer, ItemStack itemstack, boolean flag)
{
return addToInv(entityplayer, itemstack, entityplayer.inventory.currentItem, flag);
}
public static boolean addToInv (EntityPlayer entityplayer, ItemStack itemstack, int i, boolean flag)
{
ItemStack itemstack1 = entityplayer.inventory.getStackInSlot(i);
boolean flag1;
if (itemstack1 == null)
{
entityplayer.inventory.setInventorySlotContents(i, itemstack);
flag1 = true;
}
else
{
flag1 = entityplayer.inventory.addItemStackToInventory(itemstack);
}
if (flag && !flag1)
{
addItemStackToWorld(entityplayer.worldObj, (float) Math.floor(entityplayer.posX), (float) Math.floor(entityplayer.posY), (float) Math.floor(entityplayer.posZ), itemstack);
return true;
}
else
{
return flag1;
}
}
public static EntityItem addItemStackToWorld (World world, float f, float f1, float f2, ItemStack itemstack)
{
return addItemStackToWorld(world, f, f1, f2, itemstack, false);
}
public static EntityItem addItemStackToWorld (World world, float f, float f1, float f2, ItemStack itemstack, boolean flag)
{
EntityItem entityitem;
if (flag)
{
entityitem = new EntityItem(world, f, f1, f2, itemstack);
}
else
{
float f3 = 0.7F;
float f4 = random.nextFloat() * f3 + (1.0F - f3) * 0.5F;
float f5 = 1.2F;
float f6 = random.nextFloat() * f3 + (1.0F - f3) * 0.5F;
entityitem = new EntityItem(world, f + f4, f1 + f5, f2 + f6, itemstack);
}
entityitem.delayBeforeCanPickup = 10;
world.spawnEntityInWorld(entityitem);
return entityitem;
}
public static MovingObjectPosition raytraceFromEntity (World world, Entity player, boolean par3, double range)
{
float f = 1.0F;
float f1 = player.prevRotationPitch + (player.rotationPitch - player.prevRotationPitch) * f;
float f2 = player.prevRotationYaw + (player.rotationYaw - player.prevRotationYaw) * f;
double d0 = player.prevPosX + (player.posX - player.prevPosX) * (double) f;
double d1 = player.prevPosY + (player.posY - player.prevPosY) * (double) f;
if (!world.isRemote && player instanceof EntityPlayer)
d1 += 1.62D;
double d2 = player.prevPosZ + (player.posZ - player.prevPosZ) * (double) f;
Vec3 vec3 = Vec3.createVectorHelper(d0, d1, d2);
float f3 = MathHelper.cos(-f2 * 0.017453292F - (float) Math.PI);
float f4 = MathHelper.sin(-f2 * 0.017453292F - (float) Math.PI);
float f5 = -MathHelper.cos(-f1 * 0.017453292F);
float f6 = MathHelper.sin(-f1 * 0.017453292F);
float f7 = f4 * f5;
float f8 = f3 * f5;
double d3 = range;
if (player instanceof EntityPlayerMP)
{
d3 = ((EntityPlayerMP) player).theItemInWorldManager.getBlockReachDistance();
}
Vec3 vec31 = vec3.addVector((double) f7 * d3, (double) f6 * d3, (double) f8 * d3);
return world.func_147447_a(vec3, vec31, par3, !par3, par3);
}
}