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
|
|
|
|
*
|
|
|
|
* © 2015-2016 Ellpeck
|
|
|
|
*/
|
|
|
|
|
|
|
|
package de.ellpeck.actuallyadditions.mod.entity;
|
|
|
|
|
2016-08-07 13:09:44 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.config.values.ConfigIntValues;
|
2016-08-06 14:07:08 +02:00
|
|
|
import net.minecraft.block.*;
|
2016-07-22 20:23:51 +02:00
|
|
|
import net.minecraft.block.state.IBlockState;
|
|
|
|
import net.minecraft.entity.Entity;
|
|
|
|
import net.minecraft.init.Blocks;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
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
|
|
|
|
|
|
|
public class EntityWorm extends Entity{
|
|
|
|
|
|
|
|
public int timer;
|
|
|
|
|
|
|
|
public EntityWorm(World world){
|
|
|
|
super(world);
|
2016-08-31 21:04:49 +02:00
|
|
|
this.setEntityBoundingBox(null);
|
2016-07-22 20:23:51 +02:00
|
|
|
}
|
|
|
|
|
2016-09-12 20:45:29 +02:00
|
|
|
public static boolean canWormify(World world, BlockPos pos, IBlockState state){
|
|
|
|
Block block = state.getBlock();
|
2016-11-07 22:52:54 +01:00
|
|
|
boolean rightBlock = block instanceof BlockFarmland || block instanceof BlockDirt || block instanceof BlockGrass;
|
2016-09-12 20:45:29 +02:00
|
|
|
if(rightBlock){
|
|
|
|
BlockPos posUp = pos.up();
|
|
|
|
IBlockState stateUp = world.getBlockState(posUp);
|
|
|
|
Block blockUp = stateUp.getBlock();
|
|
|
|
return blockUp instanceof IPlantable || blockUp instanceof BlockBush || blockUp.isReplaceable(world, posUp);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-22 20:23:51 +02:00
|
|
|
@Override
|
|
|
|
protected void entityInit(){
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void readEntityFromNBT(NBTTagCompound compound){
|
|
|
|
this.timer = compound.getInteger("Timer");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void writeEntityToNBT(NBTTagCompound compound){
|
|
|
|
compound.setInteger("Timer", this.timer);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onUpdate(){
|
|
|
|
this.onEntityUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onEntityUpdate(){
|
2016-11-26 21:32:27 +01:00
|
|
|
if(!this.world.isRemote){
|
2016-08-07 13:09:44 +02:00
|
|
|
this.timer++;
|
|
|
|
|
2016-07-22 20:23:51 +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);
|
2016-11-26 21:32:27 +01:00
|
|
|
IBlockState state = this.world.getBlockState(pos);
|
2016-07-22 20:23:51 +02:00
|
|
|
Block block = state.getBlock();
|
|
|
|
boolean isMiddlePose = x == 0 && z == 0;
|
|
|
|
|
2016-11-26 21:32:27 +01:00
|
|
|
if(canWormify(this.world, pos, state)){
|
2016-07-22 20:23:51 +02:00
|
|
|
boolean isFarmland = block instanceof BlockFarmland;
|
|
|
|
|
|
|
|
if(!isFarmland || state.getValue(BlockFarmland.MOISTURE) < 7){
|
2016-11-26 21:32:27 +01:00
|
|
|
if(isMiddlePose || this.world.rand.nextFloat() >= 0.45F){
|
2016-07-22 20:23:51 +02:00
|
|
|
IBlockState stateToModify = isFarmland ? state : Blocks.FARMLAND.getDefaultState();
|
2016-11-26 21:32:27 +01:00
|
|
|
this.world.setBlockState(pos, stateToModify.withProperty(BlockFarmland.MOISTURE, 7), 2);
|
2016-07-22 20:23:51 +02:00
|
|
|
|
|
|
|
if(!isFarmland){
|
2016-11-26 21:32:27 +01:00
|
|
|
this.world.setBlockToAir(pos.up());
|
2016-07-22 20:23:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-26 21:32:27 +01:00
|
|
|
if(isFarmland && this.world.rand.nextFloat() >= 0.95F){
|
2016-07-22 20:23:51 +02:00
|
|
|
BlockPos plant = pos.up();
|
2016-11-26 21:32:27 +01:00
|
|
|
if(!this.world.isAirBlock(plant)){
|
|
|
|
IBlockState plantState = this.world.getBlockState(plant);
|
2016-07-22 20:23:51 +02:00
|
|
|
Block plantBlock = plantState.getBlock();
|
|
|
|
|
2016-08-06 14:07:08 +02:00
|
|
|
if((plantBlock instanceof IGrowable || plantBlock instanceof IPlantable) && !(plantBlock instanceof BlockGrass)){
|
2016-11-26 21:32:27 +01:00
|
|
|
plantBlock.updateTick(this.world, plant, plantState, this.world.rand);
|
2016-07-23 13:43:43 +02:00
|
|
|
|
2016-11-26 21:32:27 +01:00
|
|
|
IBlockState newState = this.world.getBlockState(plant);
|
2016-08-06 14:07:08 +02:00
|
|
|
if(newState.getBlock().getMetaFromState(newState) != plantBlock.getMetaFromState(plantState)){
|
2016-11-26 21:32:27 +01:00
|
|
|
this.world.playEvent(2005, plant, 0);
|
2016-08-06 14:07:08 +02:00
|
|
|
}
|
2016-07-23 13:43:43 +02:00
|
|
|
}
|
2016-07-22 20:23:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(isMiddlePose){
|
|
|
|
this.setDead();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-07 13:09:44 +02:00
|
|
|
|
|
|
|
int dieTime = ConfigIntValues.WORMS_DIE_TIME.getValue();
|
|
|
|
if(dieTime > 0 && this.timer >= dieTime){
|
|
|
|
this.setDead();
|
|
|
|
}
|
2016-07-22 20:23:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|