Skip to content

Commit f98e11c

Browse files
committed
Rework bunny reproduce AI
1 parent b489547 commit f98e11c

File tree

2 files changed

+98
-65
lines changed

2 files changed

+98
-65
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package drzhark.mocreatures.entity.ai;
2+
3+
import drzhark.mocreatures.MoCTools;
4+
import drzhark.mocreatures.entity.passive.MoCEntityBunny;
5+
import net.minecraft.entity.Entity;
6+
import net.minecraft.entity.ai.EntityAIBase;
7+
import net.minecraft.init.SoundEvents;
8+
import net.minecraft.world.World;
9+
10+
import java.util.List;
11+
12+
public class EntityAIBunnyReproduce extends EntityAIBase {
13+
private final MoCEntityBunny bunny;
14+
15+
public EntityAIBunnyReproduce(MoCEntityBunny bunny) {
16+
this.bunny = bunny;
17+
}
18+
19+
@Override
20+
public boolean shouldExecute() {
21+
return bunny.getIsTamed() && bunny.getIsAdult() && bunny.getHasEaten() && bunny.getRidingEntity() == null;
22+
}
23+
24+
@Override
25+
public void updateTask() {
26+
if (bunny.bunnyReproduceTickerA < 1023) {
27+
bunny.bunnyReproduceTickerA++;
28+
} else if (bunny.bunnyReproduceTickerB < 127) {
29+
bunny.bunnyReproduceTickerB++;
30+
} else {
31+
World world = bunny.world;
32+
List<Entity> nearbyEntities = world.getEntitiesWithinAABBExcludingEntity(bunny, bunny.getEntityBoundingBox().grow(4.0D));
33+
34+
for (Entity entity : nearbyEntities) {
35+
if (!(entity instanceof MoCEntityBunny) || entity == bunny) {
36+
continue;
37+
}
38+
39+
MoCEntityBunny otherBunny = (MoCEntityBunny) entity;
40+
if (otherBunny.getRidingEntity() != null || otherBunny.bunnyReproduceTickerA < 1023 || !otherBunny.getIsAdult() || !otherBunny.getHasEaten()) {
41+
continue;
42+
}
43+
44+
bunny.getNavigator().tryMoveToEntityLiving(otherBunny, 1.0D);
45+
46+
MoCEntityBunny babyBunny = new MoCEntityBunny(world);
47+
babyBunny.setPosition(bunny.posX, bunny.posY, bunny.posZ);
48+
babyBunny.setAdult(false);
49+
50+
int babyType = bunny.getType();
51+
if (bunny.getRNG().nextInt(2) == 0) {
52+
babyType = otherBunny.getType();
53+
}
54+
55+
babyBunny.setType(babyType);
56+
world.spawnEntity(babyBunny);
57+
MoCTools.playCustomSound(bunny, SoundEvents.ENTITY_CHICKEN_EGG);
58+
59+
resetReproduction(bunny);
60+
resetReproduction(otherBunny);
61+
break;
62+
}
63+
}
64+
}
65+
66+
private void resetReproduction(MoCEntityBunny bunny) {
67+
bunny.setHasEaten(false);
68+
bunny.bunnyReproduceTickerA = bunny.getRNG().nextInt(64);
69+
bunny.bunnyReproduceTickerB = 0;
70+
}
71+
}

src/main/java/drzhark/mocreatures/entity/passive/MoCEntityBunny.java

Lines changed: 27 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import drzhark.mocreatures.entity.tameable.MoCEntityTameableAnimal;
1010
import drzhark.mocreatures.init.MoCLootTables;
1111
import drzhark.mocreatures.init.MoCSoundEvents;
12-
import net.minecraft.entity.Entity;
1312
import net.minecraft.entity.IEntityLivingData;
1413
import net.minecraft.entity.SharedMonsterAttributes;
1514
import net.minecraft.entity.ai.EntityAISwimming;
@@ -34,25 +33,24 @@
3433
import net.minecraftforge.common.BiomeDictionary.Type;
3534

3635
import javax.annotation.Nullable;
37-
import java.util.List;
3836

3937
public class MoCEntityBunny extends MoCEntityTameableAnimal {
4038

4139
private static final DataParameter<Boolean> HAS_EATEN = EntityDataManager.createKey(MoCEntityBunny.class, DataSerializers.BOOLEAN);
42-
private int bunnyReproduceTickerA;
43-
private int bunnyReproduceTickerB;
40+
public int bunnyReproduceTickerA;
41+
public int bunnyReproduceTickerB;
4442
private int jumpTimer;
4543

4644
public MoCEntityBunny(World world) {
4745
super(world);
4846
setAdult(true);
4947
setTamed(false);
50-
setAge(50 + this.rand.nextInt(15));
51-
if (this.rand.nextInt(4) == 0) {
48+
setAge(50 + getRNG().nextInt(15));
49+
if (getRNG().nextInt(4) == 0) {
5250
setAdult(false);
5351
}
5452
setSize(0.5F, 0.5F);
55-
this.bunnyReproduceTickerA = this.rand.nextInt(64);
53+
this.bunnyReproduceTickerA = getRNG().nextInt(64);
5654
this.bunnyReproduceTickerB = 0;
5755
}
5856

@@ -63,8 +61,9 @@ protected void initEntityAI() {
6361
this.tasks.addTask(2, new EntityAIPanicMoC(this, 1.0D));
6462
this.tasks.addTask(3, new EntityAIFleeFromPlayer(this, 1.0D, 4D));
6563
this.tasks.addTask(4, new EntityAIFollowAdult(this, 1.0D));
66-
this.tasks.addTask(5, new EntityAIWanderMoC2(this, 0.8D));
67-
this.tasks.addTask(6, new EntityAIWatchClosest(this, EntityPlayer.class, 6.0F));
64+
this.tasks.addTask(5, new EntityAIBunnyReproduce(this));
65+
this.tasks.addTask(6, new EntityAIWanderMoC2(this, 0.8D));
66+
this.tasks.addTask(7, new EntityAIWatchClosest(this, EntityPlayer.class, 6.0F));
6867
}
6968

7069
@Override
@@ -78,13 +77,13 @@ protected void applyEntityAttributes() {
7877
@Override
7978
protected void entityInit() {
8079
super.entityInit();
81-
this.dataManager.register(HAS_EATEN, Boolean.FALSE);
80+
this.dataManager.register(HAS_EATEN, false);
8281
}
8382

8483
@Override
85-
public IEntityLivingData onInitialSpawn(DifficultyInstance difficulty, IEntityLivingData par1EntityLivingData) {
84+
public IEntityLivingData onInitialSpawn(DifficultyInstance difficulty, IEntityLivingData entityLivingData) {
8685
if (this.world.provider.getDimension() == MoCreatures.proxy.wyvernDimension) this.enablePersistence();
87-
return super.onInitialSpawn(difficulty, par1EntityLivingData);
86+
return super.onInitialSpawn(difficulty, entityLivingData);
8887
}
8988

9089
public boolean getHasEaten() {
@@ -100,7 +99,7 @@ public void selectType() {
10099
checkSpawningBiome();
101100

102101
if (getType() == 0) {
103-
setType(this.rand.nextInt(5) + 1);
102+
setType(getRNG().nextInt(5) + 1);
104103
}
105104

106105
}
@@ -174,21 +173,21 @@ public boolean processInteract(EntityPlayer player, EnumHand hand) {
174173
return tameResult;
175174
}
176175

177-
final ItemStack stack = player.getHeldItem(hand);
178-
if (!stack.isEmpty() && (stack.getItem() == Items.GOLDEN_CARROT) && !getHasEaten()) {
179-
if (!player.capabilities.isCreativeMode) stack.shrink(1);
180-
setHasEaten(true);
181-
MoCTools.playCustomSound(this, MoCSoundEvents.ENTITY_GENERIC_EAT);
182-
return true;
183-
}
184-
if (this.getRidingEntity() == null) {
185-
if (this.startRidingPlayer(player)) {
186-
this.rotationYaw = player.rotationYaw;
176+
final ItemStack stack = player.getHeldItemMainhand();
177+
if (!stack.isEmpty()) {
178+
if (stack.getItem() == Items.CARROT && !getHasEaten()) {
179+
if (!player.capabilities.isCreativeMode) stack.shrink(1);
180+
setHasEaten(true);
181+
MoCTools.playCustomSound(this, MoCSoundEvents.ENTITY_GENERIC_EAT);
187182
if (!getIsTamed() && !this.world.isRemote) {
188183
MoCTools.tameWithName(player, this);
189184
}
185+
return true;
186+
}
187+
} else if (getRidingEntity() == null) {
188+
if (startRidingPlayer(player)) {
189+
this.rotationYaw = player.rotationYaw;
190190
}
191-
192191
return true;
193192
}
194193

@@ -202,54 +201,15 @@ public void onUpdate() {
202201
if (this.getRidingEntity() != null) {
203202
this.rotationYaw = this.getRidingEntity().rotationYaw;
204203
}
205-
if (!this.world.isRemote) {
206204

205+
if (!this.world.isRemote) {
207206
if (--this.jumpTimer <= 0 && this.onGround && ((this.motionX > 0.05D) || (this.motionZ > 0.05D) || (this.motionX < -0.05D) || (this.motionZ < -0.05D))) {
208207
this.motionY = 0.3D;
209208
this.jumpTimer = 15;
210209
}
211-
212-
if (!getIsTamed() || !getIsAdult() || !getHasEaten() || (this.getRidingEntity() != null)) {
213-
return;
214-
}
215-
if (this.bunnyReproduceTickerA < 1023) {
216-
this.bunnyReproduceTickerA++;
217-
} else if (this.bunnyReproduceTickerB < 127) {
218-
this.bunnyReproduceTickerB++;
219-
} else {
220-
List<Entity> list1 = this.world.getEntitiesWithinAABBExcludingEntity(this, getEntityBoundingBox().grow(4.0D));
221-
for (Entity entity1 : list1) {
222-
if (!(entity1 instanceof MoCEntityBunny) || (entity1 == this)) {
223-
continue;
224-
}
225-
MoCEntityBunny entitybunny = (MoCEntityBunny) entity1;
226-
if ((entitybunny.getRidingEntity() != null) || (entitybunny.bunnyReproduceTickerA < 1023) || !entitybunny.getIsAdult() || !entitybunny.getHasEaten()) {
227-
continue;
228-
}
229-
MoCEntityBunny entitybunny1 = new MoCEntityBunny(this.world);
230-
entitybunny1.setPosition(this.posX, this.posY, this.posZ);
231-
entitybunny1.setAdult(false);
232-
int babytype = this.getType();
233-
if (this.rand.nextInt(2) == 0) {
234-
babytype = entitybunny.getType();
235-
}
236-
entitybunny1.setType(babytype);
237-
this.world.spawnEntity(entitybunny1);
238-
MoCTools.playCustomSound(this, SoundEvents.ENTITY_CHICKEN_EGG);
239-
proceed();
240-
entitybunny.proceed();
241-
break;
242-
}
243-
}
244210
}
245211
}
246212

247-
public void proceed() {
248-
setHasEaten(false);
249-
this.bunnyReproduceTickerB = 0;
250-
this.bunnyReproduceTickerA = this.rand.nextInt(64);
251-
}
252-
253213
@Override
254214
public int nameYOffset() {
255215
return -40;
@@ -300,5 +260,7 @@ public float getEyeHeight() {
300260
}
301261

302262
@Override
303-
public boolean isReadyToFollowOwnerPlayer() { return !this.isMovementCeased(); }
263+
public boolean isReadyToFollowOwnerPlayer() {
264+
return !this.isMovementCeased();
265+
}
304266
}

0 commit comments

Comments
 (0)