Merge pull request #844 from GirafiStudios/sheep_eat_grass_ai

Fixed that sheep won't eat BoP grass Fixes #823
This commit is contained in:
Adubbz 2016-08-24 18:20:53 +10:00 committed by GitHub
commit d7ed3a742b
2 changed files with 58 additions and 36 deletions

View file

@ -1,5 +1,10 @@
package biomesoplenty.common.entities.ai;
import biomesoplenty.api.block.BOPBlocks;
import biomesoplenty.api.enums.BOPPlants;
import biomesoplenty.common.block.BlockBOPDirt;
import biomesoplenty.common.block.BlockBOPGrass;
import biomesoplenty.common.block.BlockBOPPlant;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import net.minecraft.block.Block;
@ -16,18 +21,23 @@ import net.minecraft.world.World;
public class EntityAIEatBOPGrass extends EntityAIEatGrass
{
private static final Predicate<IBlockState> IS_TALL_GRASS = BlockStateMatcher.forBlock(Blocks.TALLGRASS).where(BlockTallGrass.TYPE, Predicates.equalTo(BlockTallGrass.EnumType.GRASS));
private static final Predicate<IBlockState> IS_SHORT_GRASS = forBoPPlant(BOPPlants.SHORTGRASS);
private static final Predicate<IBlockState> IS_MEDIUM_GRASS = forBoPPlant(BOPPlants.MEDIUMGRASS);
private static final Predicate<IBlockState> IS_WHEAT_GRASS = forBoPPlant(BOPPlants.WHEATGRASS);
private static final Predicate<IBlockState> IS_DAMP_GRASS = forBoPPlant(BOPPlants.DAMPGRASS);
private EntityLiving sheep;
private World world;
int bopEatingGrassTimer;
public EntityAIEatBOPGrass(EntityLiving grassEaterEntityIn)
public EntityAIEatBOPGrass(EntityLiving entityLiving)
{
super(grassEaterEntityIn);
this.sheep = grassEaterEntityIn;
this.world = grassEaterEntityIn.worldObj;
super(entityLiving);
this.sheep = entityLiving;
this.world = entityLiving.worldObj;
this.setMutexBits(7);
}
@Override
public boolean shouldExecute()
{
if (this.sheep.getRNG().nextInt(this.sheep.isChild() ? 50 : 1000) != 0)
@ -36,11 +46,14 @@ public class EntityAIEatBOPGrass extends EntityAIEatGrass
}
else
{
BlockPos blockpos = new BlockPos(this.sheep.posX, this.sheep.posY, this.sheep.posZ);
return IS_TALL_GRASS.apply(this.world.getBlockState(blockpos)) ? true : this.world.getBlockState(blockpos.down()).getBlock() instanceof BlockGrass;
BlockPos pos = new BlockPos(this.sheep.posX, this.sheep.posY, this.sheep.posZ);
IBlockState state = this.world.getBlockState(pos);
return IS_TALL_GRASS.apply(state) || IS_SHORT_GRASS.apply(state) || IS_MEDIUM_GRASS.apply(state) || IS_WHEAT_GRASS.apply(state) || IS_DAMP_GRASS.apply(state)
|| this.world.getBlockState(pos.down()).getBlock() instanceof BlockGrass;
}
}
@Override
public void startExecuting()
{
this.bopEatingGrassTimer = 40;
@ -48,48 +61,67 @@ public class EntityAIEatBOPGrass extends EntityAIEatGrass
this.sheep.getNavigator().clearPathEntity();
}
@Override
public void resetTask()
{
this.bopEatingGrassTimer = 0;
}
@Override
public boolean continueExecuting()
{
return this.bopEatingGrassTimer > 0;
}
@Override
public int getEatingGrassTimer()
{
return this.bopEatingGrassTimer;
}
@Override
public void updateTask()
{
this.bopEatingGrassTimer = Math.max(0, this.bopEatingGrassTimer - 1);
if (this.bopEatingGrassTimer == 4)
{
BlockPos blockpos = new BlockPos(this.sheep.posX, this.sheep.posY, this.sheep.posZ);
BlockPos pos = new BlockPos(this.sheep.posX, this.sheep.posY, this.sheep.posZ);
IBlockState state = this.world.getBlockState(pos);
if (IS_TALL_GRASS.apply(this.world.getBlockState(blockpos)))
if (IS_TALL_GRASS.apply(state) || IS_SHORT_GRASS.apply(state) || IS_MEDIUM_GRASS.apply(state) || IS_WHEAT_GRASS.apply(state) || IS_DAMP_GRASS.apply(state))
{
if (this.world.getGameRules().getBoolean("mobGriefing"))
{
this.world.destroyBlock(blockpos, false);
this.world.destroyBlock(pos, false);
}
this.sheep.eatGrassBonus();
}
else
{
BlockPos blockpos1 = blockpos.down();
else {
BlockPos posDown = pos.down();
IBlockState stateDown = world.getBlockState(posDown);
if (this.world.getBlockState(blockpos1).getBlock() == Blocks.GRASS)
if (stateDown.getBlock() instanceof BlockBOPGrass)
{
if (this.world.getGameRules().getBoolean("mobGriefing"))
BlockBOPGrass grass = (BlockBOPGrass) stateDown.getBlock();
Block dirtBlock = grass.getDirtBlockState(stateDown).getBlock();
if (dirtBlock instanceof BlockBOPDirt)
{
this.world.playEvent(2001, blockpos1, Block.getIdFromBlock(Blocks.GRASS));
this.world.setBlockState(blockpos1, Blocks.DIRT.getDefaultState(), 2);
if (this.world.getGameRules().getBoolean("mobGriefing"))
{
this.world.playEvent(2001, posDown, Block.getIdFromBlock(BOPBlocks.grass));
this.world.setBlockState(posDown, grass.getDirtBlockState(stateDown), 2);
}
} else if (stateDown.getValue(BlockBOPGrass.VARIANT) == BlockBOPGrass.BOPGrassType.DAISY)
{
if (this.world.getGameRules().getBoolean("mobGriefing"))
{
this.world.playEvent(2001, posDown, Block.getIdFromBlock(BOPBlocks.grass));
this.world.setBlockState(posDown, Blocks.DIRT.getDefaultState(), 2);
}
}
this.sheep.eatGrassBonus();
@ -97,4 +129,9 @@ public class EntityAIEatBOPGrass extends EntityAIEatGrass
}
}
}
private static BlockStateMatcher forBoPPlant (BOPPlants plant)
{
return BlockStateMatcher.forBlock(BOPBlocks.plant_0).where(BlockBOPPlant.paging.getVariantProperty(BlockBOPPlant.paging.getPageNum(plant)), Predicates.equalTo(plant));
}
}

View file

@ -1,35 +1,20 @@
package biomesoplenty.common.handler;
import biomesoplenty.common.entities.ai.EntityAIEatBOPGrass;
import net.minecraft.entity.ai.EntityAIBase;
import net.minecraft.entity.passive.EntitySheep;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.relauncher.ReflectionHelper;
import java.lang.reflect.Field;
public class SheepEventHandler
{
@SubscribeEvent
public void onEntityConstructEvent(EntityJoinWorldEvent event)
public void onEntityJoin(EntityJoinWorldEvent event)
{
if (event.getEntity() instanceof EntitySheep)
{
EntitySheep sheep = (EntitySheep)event.getEntity();
Field eatGrass = ReflectionHelper.findField(EntitySheep.class, new String[]{"entityAIEatGrass", "field_146087_bs"});
try
{
eatGrass.set(sheep, new EntityAIEatBOPGrass(sheep));
}
catch (IllegalArgumentException e)
{
e.printStackTrace();
}
catch (IllegalAccessException e)
{
e.printStackTrace();
}
EntitySheep sheep = (EntitySheep) event.getEntity();
sheep.tasks.addTask(5, new EntityAIEatBOPGrass(sheep));
}
}
}
}