I am trying to make mobs, that have a certain number of times they can do a task. I have completed the first one and it works fine, but the second one won't work.
Here is the code in the first one:
public void aiStep() {
super.aiStep();
if (!this.level.isClientSide) {
int i = Mth.floor(this.getX());
int j = Mth.floor(this.getY());
int k = Mth.floor(this.getZ());
BlockPos blockpos = new BlockPos(i, j, k);
if (!net.minecraftforge.event.ForgeEventFactory.getMobGriefingEvent(this.level, this)) {
return;
}
if(workTimes > 0) {
BlockState blockstate = ModBlocks.SLIME_LAYER.get().defaultBlockState();
for (int l = 0; l < 4; ++l) {
i = Mth.floor(this.getX() + (double) ((float) (l % 2 * 2 - 1) * 0.25F));
j = Mth.floor(this.getY());
k = Mth.floor(this.getZ() + (double) ((float) (l / 2 % 2 * 2 - 1) * 0.25F));
BlockPos blockpos1 = new BlockPos(i, j, k);
if (this.level.isEmptyBlock(blockpos1) && blockstate.canSurvive(this.level, blockpos1)) {
this.level.setBlockAndUpdate(blockpos1, blockstate);
this.level.gameEvent(GameEvent.BLOCK_PLACE, blockpos1, GameEvent.Context.of(this, blockstate));
this.workTimes--;
} else if (this.level.getBlockState(blockpos1).is(blockstate.getBlock())) {
if (this.jumping) {
int size = this.level.getBlockState(blockpos1).getValue(SlimeLayerBlock.LAYERS);
int newsize = size + (Math.round(random.nextInt(0, 18) / (10 + size)));
if (newsize>size) {
BlockState higherslime = this.level.getBlockState(blockpos1).setValue(SlimeLayerBlock.LAYERS, Math.min(newsize, 8));
this.level.setBlockAndUpdate(blockpos1, higherslime);
this.level.gameEvent(GameEvent.BLOCK_PLACE, blockpos1, GameEvent.Context.of(this, higherslime));
this.workTimes--;
}
}
}
}
}
}
The variable is the "workTimes" one.
In the second mob, this code seem to never go into the if block:
protected ParticleOptions getParticleType() {
if(workTimes > 0){return ParticleTypes.HEART;}
return ParticleTypes.ITEM_SNOWBALL;
}
I don't know why it is working in one mob but not the other.
Here is my full source code, if the code above isn't enough.