ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/entity/EntityWorm.java

128 lines
4.9 KiB
Java
Raw Normal View History

2016-07-22 20:23:51 +02:00
/*
* This file ("EntityWorm.java") is part of the Actually Additions mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
* http://ellpeck.de/actaddlicense
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
2016-07-22 20:23:51 +02:00
*/
package de.ellpeck.actuallyadditions.mod.entity;
import de.ellpeck.actuallyadditions.mod.config.values.ConfigIntValues;
import de.ellpeck.actuallyadditions.mod.misc.apiimpl.farmer.DefaultFarmerBehavior;
2021-02-27 16:33:00 +01:00
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.IGrowable;
2016-07-22 20:23:51 +02:00
import net.minecraft.entity.Entity;
2021-02-26 22:15:48 +01:00
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.math.AxisAlignedBB;
2016-07-22 20:23:51 +02:00
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
2016-07-22 20:59:38 +02:00
import net.minecraftforge.common.IPlantable;
2016-07-22 20:23:51 +02:00
2019-05-02 09:10:29 +02:00
public class EntityWorm extends Entity {
2016-07-22 20:23:51 +02:00
public int timer;
2019-05-02 09:10:29 +02:00
public EntityWorm(World world) {
2016-07-22 20:23:51 +02:00
super(world);
this.setEntityBoundingBox(new AxisAlignedBB(0, 0, 0, 0, 0, 0));
2016-07-22 20:23:51 +02:00
}
2021-02-26 22:15:48 +01:00
public static boolean canWormify(World world, BlockPos pos, BlockState state) {
2016-09-12 20:45:29 +02:00
Block block = state.getBlock();
boolean rightBlock = block instanceof BlockFarmland || block instanceof BlockDirt || block instanceof BlockGrass;
2019-05-02 09:10:29 +02:00
if (rightBlock) {
BlockPos posUp = pos.above();
2021-02-26 22:15:48 +01:00
BlockState stateUp = world.getBlockState(posUp);
2016-09-12 20:45:29 +02:00
Block blockUp = stateUp.getBlock();
return blockUp instanceof IPlantable || blockUp instanceof BlockBush || blockUp.canBeReplaced(world, posUp);
2019-05-02 09:10:29 +02:00
} else {
2016-09-12 20:45:29 +02:00
return false;
}
}
2016-07-22 20:23:51 +02:00
@Override
2019-05-02 09:10:29 +02:00
protected void entityInit() {
2016-07-22 20:23:51 +02:00
}
@Override
2021-02-26 22:15:48 +01:00
protected void readEntityFromNBT(CompoundNBT compound) {
2021-02-27 16:33:00 +01:00
this.timer = compound.getInt("Timer");
2016-07-22 20:23:51 +02:00
}
@Override
2021-02-26 22:15:48 +01:00
protected void writeEntityToNBT(CompoundNBT compound) {
2021-02-27 16:33:00 +01:00
compound.putInt("Timer", this.timer);
2016-07-22 20:23:51 +02:00
}
@Override
2019-05-02 09:10:29 +02:00
public void onUpdate() {
2016-07-22 20:23:51 +02:00
this.onEntityUpdate();
}
@Override
2019-05-02 09:10:29 +02:00
public void onEntityUpdate() {
if (!this.level.isClientSide) {
this.timer++;
2019-05-02 09:10:29 +02:00
if (this.timer % 50 == 0) {
for (int x = -1; x <= 1; x++) {
for (int z = -1; z <= 1; z++) {
BlockPos pos = new BlockPos(this.posX + x, this.posY, this.posZ + z);
BlockState state = this.level.getBlockState(pos);
2016-07-22 20:23:51 +02:00
Block block = state.getBlock();
boolean isMiddlePose = x == 0 && z == 0;
if (canWormify(this.level, pos, state)) {
2016-07-22 20:23:51 +02:00
boolean isFarmland = block instanceof BlockFarmland;
2019-05-02 09:10:29 +02:00
if (!isFarmland || state.getValue(BlockFarmland.MOISTURE) < 7) {
if (isMiddlePose || this.level.random.nextFloat() >= 0.45F) {
2018-03-24 00:19:39 +01:00
2021-02-26 22:15:48 +01:00
if (!isFarmland) {
DefaultFarmerBehavior.useHoeAt(this.level, pos);
2021-02-26 22:15:48 +01:00
}
state = this.level.getBlockState(pos);
2019-02-27 19:53:05 +01:00
isFarmland = state.getBlock() instanceof BlockFarmland;
2016-07-22 20:23:51 +02:00
2021-02-26 22:15:48 +01:00
if (isFarmland) {
this.level.setBlock(pos, state.withProperty(BlockFarmland.MOISTURE, 7), 2);
2021-02-26 22:15:48 +01:00
}
2016-07-22 20:23:51 +02:00
}
}
if (isFarmland && this.level.random.nextFloat() >= 0.95F) {
BlockPos plant = pos.above();
if (!this.level.isEmptyBlock(plant)) {
BlockState plantState = this.level.getBlockState(plant);
2016-07-22 20:23:51 +02:00
Block plantBlock = plantState.getBlock();
2019-05-02 09:10:29 +02:00
if ((plantBlock instanceof IGrowable || plantBlock instanceof IPlantable) && !(plantBlock instanceof BlockGrass)) {
plantBlock.updateTick(this.level, plant, plantState, this.level.random);
2016-07-23 13:43:43 +02:00
BlockState newState = this.level.getBlockState(plant);
2019-05-02 09:10:29 +02:00
if (newState != plantState) {
this.level.levelEvent(2005, plant, 0);
}
2016-07-23 13:43:43 +02:00
}
2016-07-22 20:23:51 +02:00
}
}
2019-05-02 09:10:29 +02:00
} else if (isMiddlePose) {
this.removeAfterChangingDimensions();
2016-07-22 20:23:51 +02:00
}
}
}
}
int dieTime = ConfigIntValues.WORMS_DIE_TIME.getValue();
2019-05-02 09:10:29 +02:00
if (dieTime > 0 && this.timer >= dieTime) {
this.removeAfterChangingDimensions();
}
2016-07-22 20:23:51 +02:00
}
}
}