/
EntityRabbitTFC.java
554 lines (471 loc) · 15.6 KB
/
EntityRabbitTFC.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
/*
* Work under Copyright. Licensed under the EUPL.
* See the project README.md and LICENSE.txt for more information.
*/
package net.dries007.tfc.objects.entity.animal;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import net.minecraft.block.Block;
import net.minecraft.block.BlockCarrot;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.IEntityLivingData;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.ai.*;
import net.minecraft.entity.monster.EntityMob;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.datasync.DataParameter;
import net.minecraft.network.datasync.DataSerializers;
import net.minecraft.network.datasync.EntityDataManager;
import net.minecraft.pathfinding.Path;
import net.minecraft.util.*;
import net.minecraft.util.datafix.DataFixer;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.DifficultyInstance;
import net.minecraft.world.World;
import net.minecraft.world.biome.Biome;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.oredict.OreDictionary;
import net.dries007.tfc.Constants;
import net.dries007.tfc.objects.LootTablesTFC;
import net.dries007.tfc.util.OreDictionaryHelper;
import net.dries007.tfc.util.calendar.CalendarTFC;
@ParametersAreNonnullByDefault
public class EntityRabbitTFC extends EntityAnimalMammal implements IAnimalTFC
{
private static final int DAYS_TO_ADULTHOOD = 240;
private static final int DAYS_TO_FULL_GESTATION = 30;
private static final DataParameter<Integer> RABBIT_TYPE = EntityDataManager.createKey(EntityRabbitTFC.class, DataSerializers.VARINT);
public static void registerFixesRabbit(DataFixer fixer)
{
EntityLiving.registerFixesMob(fixer, EntityRabbitTFC.class);
}
private static int getRandomGrowth()
{
int lifeTimeDays = Constants.RNG.nextInt(DAYS_TO_ADULTHOOD * 4);
return (int) (CalendarTFC.PLAYER_TIME.getTotalDays() - lifeTimeDays);
}
private int jumpTicks;
private int jumpDuration;
private boolean wasOnGround;
private int currentMoveTypeDuration;
@SuppressWarnings("unused")
public EntityRabbitTFC(World worldIn)
{
this(worldIn, Gender.fromBool(Constants.RNG.nextBoolean()), getRandomGrowth());
}
public EntityRabbitTFC(World worldIn, Gender gender, int birthDay)
{
super(worldIn, gender, birthDay);
this.setSize(0.4F, 0.5F);
this.jumpHelper = new EntityRabbitTFC.RabbitJumpHelper(this);
this.moveHelper = new EntityRabbitTFC.RabbitMoveHelper(this);
this.setMovementSpeed(0.0D);
}
@Override
public boolean isValidSpawnConditions(Biome biome, float temperature, float rainfall)
{
return temperature > -10 && rainfall > 150;
}
@Override
public void updateAITasks()
{
super.updateAITasks();
if (this.currentMoveTypeDuration > 0)
{
--this.currentMoveTypeDuration;
}
if (this.onGround)
{
if (!this.wasOnGround)
{
this.setJumping(false);
this.checkLandingDelay();
}
EntityRabbitTFC.RabbitJumpHelper entityrabbit$rabbitjumphelper = (EntityRabbitTFC.RabbitJumpHelper) this.jumpHelper;
if (!entityrabbit$rabbitjumphelper.getIsJumping())
{
if (this.moveHelper.isUpdating() && this.currentMoveTypeDuration == 0)
{
Path path = this.navigator.getPath();
Vec3d vec3d = new Vec3d(this.moveHelper.getX(), this.moveHelper.getY(), this.moveHelper.getZ());
if (path != null && path.getCurrentPathIndex() < path.getCurrentPathLength())
{
vec3d = path.getPosition(this);
}
this.calculateRotationYaw(vec3d.x, vec3d.z);
this.startJumping();
}
}
else if (!entityrabbit$rabbitjumphelper.canJump())
{
this.enableJumpControl();
}
}
this.wasOnGround = this.onGround;
}
@SideOnly(Side.CLIENT)
public void handleStatusUpdate(byte id)
{
if (id == 1)
{
this.createRunningParticles();
this.jumpDuration = 10;
this.jumpTicks = 0;
}
else
{
super.handleStatusUpdate(id);
}
}
public int getRabbitType()
{
return this.dataManager.get(RABBIT_TYPE).intValue();
}
public void setRabbitType(int rabbitTypeId)
{
this.dataManager.set(RABBIT_TYPE, Integer.valueOf(rabbitTypeId));
}
@SideOnly(Side.CLIENT)
public float getJumpCompletion(float p_175521_1_)
{
return this.jumpDuration == 0 ? 0.0F : ((float) this.jumpTicks + p_175521_1_) / (float) this.jumpDuration;
}
public void setMovementSpeed(double newSpeed)
{
this.getNavigator().setSpeed(newSpeed);
this.moveHelper.setMoveTo(this.moveHelper.getX(), this.moveHelper.getY(), this.moveHelper.getZ(), newSpeed);
}
public void startJumping()
{
this.setJumping(true);
this.jumpDuration = 10;
this.jumpTicks = 0;
}
public void onLivingUpdate()
{
super.onLivingUpdate();
if (this.jumpTicks != this.jumpDuration)
{
++this.jumpTicks;
}
else if (this.jumpDuration != 0)
{
this.jumpTicks = 0;
this.jumpDuration = 0;
this.setJumping(false);
}
}
public void writeEntityToNBT(NBTTagCompound compound)
{
super.writeEntityToNBT(compound);
compound.setInteger("RabbitType", this.getRabbitType());
}
public void readEntityFromNBT(NBTTagCompound compound)
{
super.readEntityFromNBT(compound);
this.setRabbitType(compound.getInteger("RabbitType"));
}
@Override
public void birthChildren()
{
int numberOfChilds = 5 + rand.nextInt(5); // 5-10
for (int i = 0; i < numberOfChilds; i++)
{
EntityRabbitTFC baby = new EntityRabbitTFC(this.world, Gender.fromBool(Constants.RNG.nextBoolean()), (int) CalendarTFC.PLAYER_TIME.getTotalDays());
baby.setLocationAndAngles(this.posX, this.posY, this.posZ, 0.0F, 0.0F);
this.world.spawnEntity(baby);
}
}
@Override
public long gestationDays()
{
return DAYS_TO_FULL_GESTATION;
}
public SoundCategory getSoundCategory()
{
return SoundCategory.NEUTRAL;
}
@Override
public boolean isBreedingItem(ItemStack stack)
{
return OreDictionaryHelper.doesStackMatchOre(stack, "carrot");
}
@Override
protected void entityInit()
{
super.entityInit();
this.dataManager.register(RABBIT_TYPE, Integer.valueOf(0));
}
@Override
public float getPercentToAdulthood()
{
if (this.getAge() != Age.CHILD)
return 1;
double value = (CalendarTFC.PLAYER_TIME.getTotalDays() - this.getBirthDay()) / (double) DAYS_TO_ADULTHOOD;
if (value > 1f)
value = 1f;
if (value < 0f)
value = 0;
return (float) value;
}
@Override
public Age getAge()
{
return CalendarTFC.PLAYER_TIME.getTotalDays() >= this.getBirthDay() + DAYS_TO_ADULTHOOD ? Age.ADULT : Age.CHILD;
}
@Override
@SuppressWarnings("unchecked")
protected void initEntityAI()
{
this.tasks.addTask(1, new EntityAISwimming(this));
this.tasks.addTask(1, new EntityRabbitTFC.AIPanic(this, 2.2D));
this.tasks.addTask(2, new EntityAIMate(this, 1.2D));
for (ItemStack is : OreDictionary.getOres("carrot"))
{
Item item = is.getItem();
this.tasks.addTask(3, new EntityAITempt(this, 1.4D, item, false));
}
this.tasks.addTask(4, new EntityAIAvoidEntity(this, EntityPlayer.class, 8.0F, 2.0D, 2.0D));
this.tasks.addTask(4, new EntityAIAvoidEntity(this, EntityMob.class, 4.0F, 2.0D, 2.0D));
this.tasks.addTask(6, new EntityAIWanderAvoidWater(this, 0.6D));
this.tasks.addTask(11, new EntityAIWatchClosest(this, EntityPlayer.class, 10.0F));
}
@Override
protected void applyEntityAttributes()
{
super.applyEntityAttributes();
this.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).setBaseValue(3.0D);
this.getEntityAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).setBaseValue(0.30000001192092896D);
}
@Override
protected SoundEvent getAmbientSound()
{
return SoundEvents.ENTITY_RABBIT_AMBIENT;
}
@Nullable
protected ResourceLocation getLootTable()
{
return LootTablesTFC.ANIMALS_RABBIT;
}
@Nullable
public IEntityLivingData onInitialSpawn(DifficultyInstance difficulty, @Nullable IEntityLivingData livingdata)
{
livingdata = super.onInitialSpawn(difficulty, livingdata);
int i = this.getRandomRabbitType();
if (livingdata instanceof EntityRabbitTFC.RabbitTypeData)
{
i = ((EntityRabbitTFC.RabbitTypeData) livingdata).typeData;
}
else
{
livingdata = new EntityRabbitTFC.RabbitTypeData(i);
}
this.setRabbitType(i);
return livingdata;
}
protected SoundEvent getJumpSound()
{
return SoundEvents.ENTITY_RABBIT_JUMP;
}
@Override
protected SoundEvent getHurtSound(DamageSource damageSourceIn)
{
return SoundEvents.ENTITY_RABBIT_HURT;
}
@Override
protected SoundEvent getDeathSound()
{
return SoundEvents.ENTITY_RABBIT_DEATH;
}
protected float getJumpUpwardsMotion()
{
if (!this.collidedHorizontally && (!this.moveHelper.isUpdating() || this.moveHelper.getY() <= this.posY + 0.5D))
{
Path path = this.navigator.getPath();
if (path != null && path.getCurrentPathIndex() < path.getCurrentPathLength())
{
Vec3d vec3d = path.getPosition(this);
if (vec3d.y > this.posY + 0.5D)
{
return 0.5F;
}
}
return this.moveHelper.getSpeed() <= 0.6D ? 0.2F : 0.3F;
}
else
{
return 0.5F;
}
}
protected void jump()
{
super.jump();
double d0 = this.moveHelper.getSpeed();
if (d0 > 0.0D)
{
double d1 = this.motionX * this.motionX + this.motionZ * this.motionZ;
if (d1 < 0.010000000000000002D)
{
this.moveRelative(0.0F, 0.0F, 1.0F, 0.1F);
}
}
if (!this.world.isRemote)
{
this.world.setEntityState(this, (byte) 1);
}
}
public void setJumping(boolean jumping)
{
super.setJumping(jumping);
if (jumping)
{
this.playSound(this.getJumpSound(), this.getSoundVolume(), ((this.rand.nextFloat() - this.rand.nextFloat()) * 0.2F + 1.0F) * 0.8F);
}
}
protected void createEatingParticles()
{
BlockCarrot blockcarrot = (BlockCarrot) Blocks.CARROTS;
IBlockState iblockstate = blockcarrot.withAge(blockcarrot.getMaxAge());
this.world.spawnParticle(EnumParticleTypes.BLOCK_DUST,
this.posX + (double) (this.rand.nextFloat() * this.width * 2.0F) - (double) this.width,
this.posY + 0.5D + (double) (this.rand.nextFloat() * this.height),
this.posZ + (double) (this.rand.nextFloat() * this.width * 2.0F) - (double) this.width, 0.0D, 0.0D,
0.0D, Block.getStateId(iblockstate));
}
private void calculateRotationYaw(double x, double z)
{
this.rotationYaw = (float) (MathHelper.atan2(z - this.posZ, x - this.posX) * (180D / Math.PI)) - 90.0F;
}
private void enableJumpControl()
{
((EntityRabbitTFC.RabbitJumpHelper) this.jumpHelper).setCanJump(true);
}
private void disableJumpControl()
{
((EntityRabbitTFC.RabbitJumpHelper) this.jumpHelper).setCanJump(false);
}
private void updateMoveTypeDuration()
{
if (this.moveHelper.getSpeed() < 2.2D)
{
this.currentMoveTypeDuration = 10;
}
else
{
this.currentMoveTypeDuration = 1;
}
}
private void checkLandingDelay()
{
this.updateMoveTypeDuration();
this.disableJumpControl();
}
private int getRandomRabbitType()
{
Biome biome = this.world.getBiome(new BlockPos(this));
int i = this.rand.nextInt(100);
if (biome.isSnowyBiome())
{
return i < 5 ? 7 : (i < 10 ? 6 : (i < 80 ? 1 : 3));
}
else
{
return i < 50 ? 0 : (i < 90 ? 5 : (i < 95 ? 2 : 4));
}
}
static class RabbitMoveHelper extends EntityMoveHelper
{
private final EntityRabbitTFC rabbit;
private double nextJumpSpeed;
public RabbitMoveHelper(EntityRabbitTFC rabbit)
{
super(rabbit);
this.rabbit = rabbit;
}
public void setMoveTo(double x, double y, double z, double speedIn)
{
if (this.rabbit.isInWater())
{
speedIn = 1.5D;
}
super.setMoveTo(x, y, z, speedIn);
if (speedIn > 0.0D)
{
this.nextJumpSpeed = speedIn;
}
}
public void onUpdateMoveHelper()
{
if (this.rabbit.onGround && !this.rabbit.isJumping && !((EntityRabbitTFC.RabbitJumpHelper) this.rabbit.jumpHelper).getIsJumping())
{
this.rabbit.setMovementSpeed(0.0D);
}
else if (this.isUpdating())
{
this.rabbit.setMovementSpeed(this.nextJumpSpeed);
}
super.onUpdateMoveHelper();
}
}
public static class RabbitTypeData implements IEntityLivingData
{
public int typeData;
public RabbitTypeData(int type)
{
this.typeData = type;
}
}
static class AIPanic extends EntityAIPanic
{
private final EntityRabbitTFC rabbit;
public AIPanic(EntityRabbitTFC rabbit, double speedIn)
{
super(rabbit, speedIn);
this.rabbit = rabbit;
}
public void updateTask()
{
super.updateTask();
this.rabbit.setMovementSpeed(this.speed);
}
}
public class RabbitJumpHelper extends EntityJumpHelper
{
private final EntityRabbitTFC rabbit;
private boolean canJump;
public RabbitJumpHelper(EntityRabbitTFC rabbit)
{
super(rabbit);
this.rabbit = rabbit;
}
public boolean getIsJumping()
{
return this.isJumping;
}
public boolean canJump()
{
return this.canJump;
}
public void setCanJump(boolean canJumpIn)
{
this.canJump = canJumpIn;
}
public void doJump()
{
if (this.isJumping)
{
this.rabbit.startJumping();
this.isJumping = false;
}
}
}
}