-
Notifications
You must be signed in to change notification settings - Fork 755
/
ProjectileBase.java
633 lines (520 loc) · 25.8 KB
/
ProjectileBase.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
package tconstruct.library.entity;
import tconstruct.library.weaponry.AmmoItem;
import tconstruct.util.Reference;
import cpw.mods.fml.common.network.ByteBufUtils;
import cpw.mods.fml.common.registry.IEntityAdditionalSpawnData;
import io.netty.buffer.ByteBuf;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.monster.EntityEnderman;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.entity.projectile.EntityArrow;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.play.server.S2BPacketChangeGameState;
import net.minecraft.util.*;
import net.minecraft.world.World;
import tconstruct.library.ActiveToolMod;
import tconstruct.library.TConstructRegistry;
import tconstruct.library.tools.AbilityHelper;
import tconstruct.library.tools.ToolCore;
import tconstruct.weaponry.entity.ArrowEntity;
import java.util.List;
// have to base this on EntityArrow, otherwise minecraft does derp things because everything is handled based on class.
public abstract class ProjectileBase extends EntityArrow implements IEntityAdditionalSpawnData {
public final static String woodSound = Reference.resource("woodHit");
public final static String stoneSound = Reference.resource("stoneHit");
public ItemStack returnStack;
public boolean bounceOnNoDamage = true;
public boolean defused = false; // if this is true it wont hit any entities anymore
public ProjectileBase(World world) {
super(world);
}
public ProjectileBase(World world, double d, double d1, double d2) {
this(world);
this.setPosition(d, d1, d2);
}
public ProjectileBase(World world, EntityPlayer player, float speed, float accuracy, ItemStack stack) {
this(world);
this.shootingEntity = player;
canBePickedUp = 1;
// stuff from the arrow
this.setLocationAndAngles(player.posX, player.posY + (double)player.getEyeHeight(), player.posZ, player.rotationYaw, player.rotationPitch);
this.posX -= MathHelper.cos(this.rotationYaw / 180.0F * (float)Math.PI) * 0.16F;
this.posY -= 0.10000000149011612D;
this.posZ -= MathHelper.sin(this.rotationYaw / 180.0F * (float)Math.PI) * 0.16F;
this.setPosition(this.posX, this.posY, this.posZ);
this.yOffset = 0.0F;
this.motionX = -MathHelper.sin(this.rotationYaw / 180.0F * (float)Math.PI) * MathHelper.cos(this.rotationPitch / 180.0F * (float)Math.PI);
this.motionZ = +MathHelper.cos(this.rotationYaw / 180.0F * (float)Math.PI) * MathHelper.cos(this.rotationPitch / 180.0F * (float)Math.PI);
this.motionY = -MathHelper.sin(this.rotationPitch / 180.0F * (float)Math.PI);
this.setThrowableHeading(this.motionX, this.motionY, this.motionZ, speed, accuracy);
// our stuff
returnStack = stack;
/*
// fix upward default angle
motionY = -MathHelper.sin((rotationPitch / 180F) * 3.141593F - 0.2f);
setArrowHeading(motionX, motionY, motionZ, speed, 0.0f);
// set better position and move it a tad so we don't hit ourselves.
this.setPosition(player.posX, player.posY + player.getEyeHeight(), player.posZ);
*/
/*
double l = Math.sqrt(motionX*motionX + motionY*motionY + motionZ*motionZ);
double vx = motionX/l;
double vy = motionY/l;
double vz = motionZ/l;
posX += vx*1.5f;
posY += vy*1.5f;
posZ += vz*1.5f;
*/
//init();
}
public ItemStack getEntityItem ()
{
return returnStack;
}
protected void playHitBlockSound(int x, int y, int z)
{
Block block = worldObj.getBlock(x,y,z);
if(block != null && block.blockMaterial == Material.wood)
worldObj.playSoundAtEntity(this, woodSound, 1.0f, 1.0f);
else {
worldObj.playSoundAtEntity(this, stoneSound, 1.0f, 1.0f);
}
if(block != null)
worldObj.playSoundAtEntity(this, block.stepSound.getBreakSound(), 0.7f, 1.0f);
}
protected void playHitEntitySound()
{
}
/**
* How deep the item enters stuff it hits. Should be bigger for bigger objects, and smaller for smaller objects.
* 1.0f is exactly halfway in.
*/
protected double getStuckDepth() { return 0.5f; }
protected void doLivingHit(EntityLivingBase entityHit)
{
float knockback = returnStack.getTagCompound().getCompoundTag("InfiTool").getFloat("Knockback");
if(shootingEntity instanceof EntityLivingBase)
knockback += EnchantmentHelper.getKnockbackModifier((EntityLivingBase) shootingEntity, entityHit);
if (!this.worldObj.isRemote)
{
entityHit.setArrowCountInEntity(entityHit.getArrowCountInEntity() + 1);
}
if (knockback > 0)
{
double horizontalSpeed = MathHelper.sqrt_double(this.motionX * this.motionX + this.motionZ * this.motionZ);
if (horizontalSpeed > 0.0F)
{
entityHit.addVelocity(this.motionX * (double) knockback * 0.6000000238418579D / horizontalSpeed, 0.1D, this.motionZ * (double) knockback * 0.6000000238418579D / (double) horizontalSpeed);
}
}
if (this.shootingEntity != null && this.shootingEntity instanceof EntityLivingBase)
{
EnchantmentHelper.func_151384_a(entityHit, this.shootingEntity);
EnchantmentHelper.func_151385_b((EntityLivingBase)this.shootingEntity, entityHit);
}
if (this.shootingEntity != null && entityHit != this.shootingEntity && entityHit instanceof EntityPlayer && this.shootingEntity instanceof EntityPlayerMP)
{
((EntityPlayerMP)this.shootingEntity).playerNetServerHandler.sendPacket(new S2BPacketChangeGameState(6, 0.0F));
}
}
public void onHitBlock(MovingObjectPosition movingobjectposition) {
this.field_145791_d = movingobjectposition.blockX;
this.field_145792_e = movingobjectposition.blockY;
this.field_145789_f = movingobjectposition.blockZ;
this.field_145790_g = this.worldObj.getBlock(this.field_145791_d, this.field_145792_e, this.field_145789_f);
this.inData = this.worldObj.getBlockMetadata(this.field_145791_d, this.field_145792_e, this.field_145789_f);
this.motionX = movingobjectposition.hitVec.xCoord - this.posX;
this.motionY = movingobjectposition.hitVec.yCoord - this.posY;
this.motionZ = movingobjectposition.hitVec.zCoord - this.posZ;
double speed = getStuckDepth() * MathHelper.sqrt_double(this.motionX * this.motionX + this.motionY * this.motionY + this.motionZ * this.motionZ);
this.posX -= this.motionX / speed * 0.05000000074505806D;
this.posY -= this.motionY / speed * 0.05000000074505806D;
this.posZ -= this.motionZ / speed * 0.05000000074505806D;
playHitBlockSound(movingobjectposition.blockX, movingobjectposition.blockY, movingobjectposition.blockZ);
this.inGround = true;
this.arrowShake = 7;
this.setIsCritical(false);
this.defused = true; // defuse it so it doesn't hit stuff anymore, being weird
if (this.field_145790_g.getMaterial() != Material.air)
{
this.field_145790_g.onEntityCollidedWithBlock(this.worldObj, this.field_145791_d, this.field_145792_e, this.field_145789_f, this);
}
}
public void onHitEntity(MovingObjectPosition movingobjectposition) {
NBTTagCompound tags = returnStack.getTagCompound().getCompoundTag("InfiTool");
float speed = MathHelper.sqrt_double(this.motionX * this.motionX + this.motionY * this.motionY + this.motionZ * this.motionZ);
// absolute distance travelled minus the current tick
float distance = speed * (this.ticksInAir-1);
// distance travelled in the current tick
float dist2 = 0;
dist2 += MathHelper.abs((float)movingobjectposition.entityHit.lastTickPosX - (float)this.lastTickPosX);
dist2 += MathHelper.abs((float)movingobjectposition.entityHit.lastTickPosY - (float)this.lastTickPosY);
dist2 += MathHelper.abs((float)movingobjectposition.entityHit.lastTickPosZ - (float)this.lastTickPosZ);
dist2 = MathHelper.sqrt_double(dist2);
distance += dist2;
if(!tags.hasKey("BaseAttack"))
{
// quickly calculate the base attack damage from the actual attack damage and quartz modifiers
// yes, this relies on quartz being the only modifier that modifiers damage, but at the point of writing this
// that is the case, and later tools should have the tag
int atk = tags.getInteger("Attack");
if(tags.hasKey("ModAttack"))
{
int bonusDmg = tags.getIntArray("ModAttack")[0]/24 + 1;
atk -= bonusDmg;
}
tags.setInteger("BaseAttack", atk);
}
float baseAttack = tags.getInteger("BaseAttack");
float totalAttack = tags.getInteger("Attack");
float damage = speed * baseAttack; // todo: potentially change this back to MathHelper.ceiling_float_int to get 1/2 heart steps back
// add quartz damage
damage += (totalAttack - baseAttack);
// this is needed so really high scaling bow&arrow combinations don't get out of hand
if(this instanceof ArrowEntity)
damage = Math.max(0, damage - totalAttack/2f);
boolean shotByPlayer = this.shootingEntity != null && this.shootingEntity instanceof EntityPlayer;
// Damage calculations and stuff. For reference see AbilityHelper.onLeftClickEntity
ToolCore ammo = (ToolCore)returnStack.getItem();
// factor in modified stuff for base damage
// basically we pass the base damage to all modifiers and take the highest one
int baseDamage = 0;
if(shotByPlayer) // prevent crashes with other things reflecting/shooting them
for(ActiveToolMod toolmod : TConstructRegistry.activeModifiers) {
int dmg = toolmod.baseAttackDamage(baseDamage, (int)damage, ammo, returnStack.getTagCompound(), tags, returnStack, (EntityPlayer)this.shootingEntity, movingobjectposition.entityHit);
if(dmg > baseDamage)
baseDamage = dmg;
}
damage += baseDamage;
// damage modification from the weapon itself
damage *= ammo.getDamageModifier();
// unlike the regular weapons, we do no gain more damage from potion of strength/weakness since we don't hit stuff directly
// stonebound decreases, jagged increases damage too
float bonusDamage = -AbilityHelper.calcStoneboundBonus(ammo, tags);
// enchantments might add too
if(shootingEntity != null && movingobjectposition.entityHit instanceof EntityLivingBase)
bonusDamage += EnchantmentHelper.getEnchantmentModifierLiving((EntityLivingBase)this.shootingEntity, (EntityLivingBase)movingobjectposition.entityHit);
damage += bonusDamage;
// ensure we still have damage
if(damage < 1)
damage = 1;
// the regular tool modifier damage stuff
int modDamage = 0;
if(shotByPlayer)
for (ActiveToolMod mod : TConstructRegistry.activeModifiers)
{
modDamage += mod.attackDamage(modDamage, (int)damage, ammo, returnStack.getTagCompound(), tags, returnStack, (EntityPlayer)this.shootingEntity, movingobjectposition.entityHit);
}
damage += modDamage;
// calculate critical damaaage
if (this.getIsCritical())
damage += (this.rand.nextFloat()/4f + Math.min(0.75f, distance/25f)) * (damage / 2f + 2f);
// and now we come to the part where we actually deal the damage!
if(!dealDamage(damage, ammo, tags, movingobjectposition.entityHit))
{
if(!bounceOnNoDamage)
this.setDead();
// bounce off if we didn't deal damage
this.motionX *= -0.10000000149011612D;
this.motionY *= -0.10000000149011612D;
this.motionZ *= -0.10000000149011612D;
this.rotationYaw += 180.0F;
this.prevRotationYaw += 180.0F;
this.ticksInAir = 0;
return;
}
// we hit it, BURNNNNNN
else
AbilityHelper.processFiery(shootingEntity, movingobjectposition.entityHit, tags);
if (movingobjectposition.entityHit instanceof EntityLivingBase)
doLivingHit((EntityLivingBase)movingobjectposition.entityHit);
playHitEntitySound();
if (!(movingobjectposition.entityHit instanceof EntityEnderman))
{
// reinforced helps for them to not break!
if(rand.nextInt(10)+1 > tags.getInteger("Reinforced"))
this.setDead();
else
{
this.motionX = 0;
this.motionY = 0;
this.motionZ = 0;
this.ticksInAir = 0;
}
}
}
// returns true if it was successful
public boolean dealDamage(float damage, ToolCore ammo, NBTTagCompound tags, Entity entityHit)
{
boolean dealtDamage = false;
// deal regular damage
DamageSource damagesource;
if (this.shootingEntity == null)
damagesource = DamageSource.causeArrowDamage(this, this);
else
damagesource = DamageSource.causeArrowDamage(this, this.shootingEntity);
dealtDamage = entityHit.attackEntityFrom(damagesource, damage);
return dealtDamage;
}
@Override
public void setVelocity(double p_70016_1_, double p_70016_3_, double p_70016_5_) {
// don't do anything, we set it ourselves at spawn
// Mojangs code has a hard cap of 3.9 speed, but our projectiles can go faster, which desyncs client and server speeds
// Packet that's causing it: S12PacketEntityVelocity
}
@Override
// this function is the same as the vanilla EntityArrow
public void onUpdate() {
// call the entity update routine
// luckily we can call this directly and take the arrow-code, since we'd have to call super.onUpdate otherwise. Which would not work.
onEntityUpdate();
// boioioiooioing
if (this.arrowShake > 0)
--this.arrowShake;
// If we don't have our rotation set correctly, infer it from our motion direction
if (this.prevRotationPitch == 0.0F && this.prevRotationYaw == 0.0F)
{
float f = MathHelper.sqrt_double(this.motionX * this.motionX + this.motionZ * this.motionZ);
this.prevRotationYaw = this.rotationYaw = (float)(Math.atan2(this.motionX, this.motionZ) * 180.0D / Math.PI);
this.prevRotationPitch = this.rotationPitch = (float)(Math.atan2(this.motionY, (double)f) * 180.0D / Math.PI);
}
// we previously hit something. Check if the block is still there.
Block block = this.worldObj.getBlock(this.field_145791_d, this.field_145792_e, this.field_145789_f);
if (block.getMaterial() != Material.air)
{
block.setBlockBoundsBasedOnState(this.worldObj, this.field_145791_d, this.field_145792_e, this.field_145789_f);
AxisAlignedBB axisalignedbb = block.getCollisionBoundingBoxFromPool(this.worldObj, this.field_145791_d, this.field_145792_e, this.field_145789_f);
// we are stuck in a block. yay.
if (axisalignedbb != null && axisalignedbb.isVecInside(Vec3.createVectorHelper(this.posX, this.posY, this.posZ)))
this.inGround = true;
}
if (this.inGround)
updateInGround();
else
updateInAir();
}
// Update while we're stuck in a block
protected void updateInGround()
{
Block block = this.worldObj.getBlock(this.field_145791_d, this.field_145792_e, this.field_145789_f);
int j = this.worldObj.getBlockMetadata(this.field_145791_d, this.field_145792_e, this.field_145789_f);
// check if it's still the same block
if (block == this.field_145790_g && j == this.inData)
{
++this.ticksInGround;
if (this.ticksInGround == 1200)
{
this.setDead();
}
}
else
{
this.inGround = false;
this.motionX *= (double)(this.rand.nextFloat() * 0.2F);
this.motionY *= (double)(this.rand.nextFloat() * 0.2F);
this.motionZ *= (double)(this.rand.nextFloat() * 0.2F);
this.ticksInGround = 0;
this.ticksInAir = 0;
}
}
// update while traveling
protected void updateInAir()
{
// tick tock
++this.ticksInAir;
// do a raytrace from old to new position
Vec3 curPos = Vec3.createVectorHelper(this.posX, this.posY, this.posZ);
Vec3 newPos = Vec3.createVectorHelper(this.posX + this.motionX, this.posY + this.motionY, this.posZ + this.motionZ);
MovingObjectPosition movingobjectposition = this.worldObj.func_147447_a(curPos, newPos, false, true, false);
// raytrace messes with the positions. get new ones!
curPos = Vec3.createVectorHelper(this.posX, this.posY, this.posZ);
// if we hit something, the collision point is our new position
if (movingobjectposition != null)
newPos = Vec3.createVectorHelper(movingobjectposition.hitVec.xCoord, movingobjectposition.hitVec.yCoord, movingobjectposition.hitVec.zCoord);
else
newPos = Vec3.createVectorHelper(this.posX + this.motionX, this.posY + this.motionY, this.posZ + this.motionZ);
if(!defused) {
// Check all the entities we on our way
Entity entity = null;
List<Entity> list = this.worldObj.getEntitiesWithinAABBExcludingEntity(this, this.boundingBox.addCoord(this.motionX, this.motionY, this.motionZ).expand(1.0D, 1.0D, 1.0D));
double distance = 0.0D;
float f1;
for (Entity ent : list) {
if (!ent.canBeCollidedWith())
continue;
// we don't shoot ourselves into the foot.
if (ent == this.shootingEntity && this.ticksInAir < 5)
continue;
f1 = 0.3F;
AxisAlignedBB axisalignedbb1 = ent.boundingBox.expand((double) f1, (double) f1, (double) f1);
MovingObjectPosition movingobjectposition1 = axisalignedbb1.calculateIntercept(curPos, newPos);
// did we actually collide with the entity, or was it just really close nearby?
if (movingobjectposition1 != null) {
// check if this entity is closer than the other one we already hit
double otherDistance = curPos.distanceTo(movingobjectposition1.hitVec);
if (otherDistance < distance || distance == 0.0D) {
entity = ent;
distance = otherDistance;
}
}
}
// if we hit something, new collision point!
if (entity != null)
movingobjectposition = new MovingObjectPosition(entity);
// did we hit a player?
if (movingobjectposition != null && movingobjectposition.entityHit != null && movingobjectposition.entityHit instanceof EntityPlayer) {
EntityPlayer entityplayer = (EntityPlayer) movingobjectposition.entityHit;
// can we attack said player?
if (entityplayer.capabilities.disableDamage || this.shootingEntity instanceof EntityPlayer && !((EntityPlayer) this.shootingEntity).canAttackPlayer(entityplayer))
movingobjectposition = null;
// this check should probably done inside of the loop for accuracy..
}
}
// time to hit the object
if (movingobjectposition != null) {
if (movingobjectposition.entityHit != null)
onHitEntity(movingobjectposition);
else
onHitBlock(movingobjectposition);
}
// crithit particles
if (this.getIsCritical())
drawCritParticles();
// MOVEMENT! yay.
doMoveUpdate();
double slowdown = 1.0d - getSlowdown();
// bubblez
if (this.isInWater())
{
for (int l = 0; l < 4; ++l)
{
float f4 = 0.25F;
this.worldObj.spawnParticle("bubble", this.posX - this.motionX * (double)f4, this.posY - this.motionY * (double)f4, this.posZ - this.motionZ * (double)f4, this.motionX, this.motionY, this.motionZ);
}
// more slowdown in water
slowdown = 1d - 20d*getSlowdown();
}
// phshshshshshs
if (this.isWet())
this.extinguish();
// minimalistic slowdown!
this.motionX *= slowdown;
this.motionY *= slowdown;
this.motionZ *= slowdown;
// gravity
this.motionY -= getGravity();
this.setPosition(this.posX, this.posY, this.posZ);
// tell blocks we collided with, that we collided with them!
this.func_145775_I();
}
public void drawCritParticles()
{
for (int i = 0; i < 4; ++i)
{
this.worldObj.spawnParticle("crit", this.posX + this.motionX * (double)i / 4.0D, this.posY + this.motionY * (double)i / 4.0D, this.posZ + this.motionZ * (double)i / 4.0D, -this.motionX, -this.motionY + 0.2D, -this.motionZ);
}
}
protected void doMoveUpdate()
{
this.posX += this.motionX;
this.posY += this.motionY;
this.posZ += this.motionZ;
double f2 = MathHelper.sqrt_double(this.motionX * this.motionX + this.motionZ * this.motionZ);
this.rotationYaw = (float)(Math.atan2(this.motionX, this.motionZ) * 180.0D / Math.PI);
this.rotationPitch = (float)(Math.atan2(this.motionY, f2) * 180.0D / Math.PI);
// normalize rotations
while (this.rotationPitch - this.prevRotationPitch < -180.0F)
this.prevRotationPitch -= 360.0F;
while (this.rotationPitch - this.prevRotationPitch >= 180.0F)
this.prevRotationPitch += 360.0F;
while (this.rotationYaw - this.prevRotationYaw < -180.0F)
this.prevRotationYaw -= 360.0F;
while (this.rotationYaw - this.prevRotationYaw >= 180.0F)
this.prevRotationYaw += 360.0F;
this.rotationPitch = this.prevRotationPitch + (this.rotationPitch - this.prevRotationPitch) * 0.2F;
this.rotationYaw = this.prevRotationYaw + (this.rotationYaw - this.prevRotationYaw) * 0.2F;
}
/**
* Factor for the slowdown. 0 = no slowdown, >0 = (1-slowdown)*speed slowdown, <0 = speedup
*/
protected double getSlowdown() { return 0.01; }
/**
* Added to the y-velocity as gravitational pull. Otherwise stuff would simply float midair.
*/
protected double getGravity() { return 0.05; }
/**
* Called by a player entity when they collide with an entity
*/
public void onCollideWithPlayer(EntityPlayer player)
{
if (!this.worldObj.isRemote && this.inGround && this.arrowShake <= 0)
{
boolean flag = this.canBePickedUp == 1 || this.canBePickedUp == 2 && player.capabilities.isCreativeMode;
if (this.canBePickedUp == 1)
{
// ammo pickup
if(this.returnStack != null && this.returnStack.getItem() instanceof AmmoItem)
{
if(!((AmmoItem) this.returnStack.getItem()).pickupAmmo(returnStack, null, player))
flag = false;
}
// regular pickup.
// Note that you can't pick up ammo items without the proper itemstack in your inventory to prevent dupes
else if(returnStack != null && !player.inventory.addItemStackToInventory(returnStack))
{
flag = false;
}
}
if (flag)
{
this.playSound("random.pop", 0.2F, ((this.rand.nextFloat() - this.rand.nextFloat()) * 0.7F + 1.0F) * 2.0F);
player.onItemPickup(this, 1);
this.setDead();
}
}
}
/** NBT stuff **/
@Override
public void writeEntityToNBT (NBTTagCompound tags)
{
super.writeEntityToNBT(tags);
tags.setTag("Throwable", this.returnStack.writeToNBT(new NBTTagCompound()));
tags.setByte("onGround", (byte) (onGround ? 1 : 0));
//tags.setBoolean("Retrieval", doNotRetrieve);
}
@Override
public void readEntityFromNBT(NBTTagCompound tags) {
super.readEntityFromNBT(tags);
this.returnStack = ItemStack.loadItemStackFromNBT(tags.getCompoundTag("Throwable"));
onGround = tags.getByte("onGround") == 1;
//doNotRetrieve = tags.getBoolean("Retrieval");
}
@Override
public void writeSpawnData(ByteBuf data) {
ByteBufUtils.writeItemStack(data, returnStack);
data.writeFloat(rotationYaw);
// shooting entity
int id = shootingEntity == null ? this.getEntityId() : shootingEntity.getEntityId();
data.writeInt(id);
// motion stuff. This has to be sent separately since MC seems to do hardcoded stuff to arrows with this
data.writeDouble(this.motionX);
data.writeDouble(this.motionY);
data.writeDouble(this.motionZ);
}
@Override
public void readSpawnData(ByteBuf data) {
returnStack = ByteBufUtils.readItemStack(data);
rotationYaw = data.readFloat();
shootingEntity = worldObj.getEntityByID(data.readInt());
this.motionX = data.readDouble();
this.motionY = data.readDouble();
this.motionZ = data.readDouble();
}
}