Fixed that sheep won't eat BoP grass

- Sheep now also eat BoPs variant of tall grass
This commit is contained in:
GirafiStudios 2016-08-23 21:12:50 +02:00
parent eee1a0358a
commit 8f95dcc9c3
2 changed files with 58 additions and 36 deletions

View file

@ -1,5 +1,10 @@
package biomesoplenty.common.entities.ai; 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.Predicate;
import com.google.common.base.Predicates; import com.google.common.base.Predicates;
import net.minecraft.block.Block; import net.minecraft.block.Block;
@ -16,18 +21,23 @@ import net.minecraft.world.World;
public class EntityAIEatBOPGrass extends EntityAIEatGrass 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_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 EntityLiving sheep;
private World world; private World world;
int bopEatingGrassTimer; int bopEatingGrassTimer;
public EntityAIEatBOPGrass(EntityLiving grassEaterEntityIn) public EntityAIEatBOPGrass(EntityLiving entityLiving)
{ {
super(grassEaterEntityIn); super(entityLiving);
this.sheep = grassEaterEntityIn; this.sheep = entityLiving;
this.world = grassEaterEntityIn.worldObj; this.world = entityLiving.worldObj;
this.setMutexBits(7); this.setMutexBits(7);
} }
@Override
public boolean shouldExecute() public boolean shouldExecute()
{ {
if (this.sheep.getRNG().nextInt(this.sheep.isChild() ? 50 : 1000) != 0) if (this.sheep.getRNG().nextInt(this.sheep.isChild() ? 50 : 1000) != 0)
@ -36,11 +46,14 @@ public class EntityAIEatBOPGrass extends EntityAIEatGrass
} }
else else
{ {
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);
return IS_TALL_GRASS.apply(this.world.getBlockState(blockpos)) ? true : this.world.getBlockState(blockpos.down()).getBlock() instanceof BlockGrass; 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() public void startExecuting()
{ {
this.bopEatingGrassTimer = 40; this.bopEatingGrassTimer = 40;
@ -48,48 +61,67 @@ public class EntityAIEatBOPGrass extends EntityAIEatGrass
this.sheep.getNavigator().clearPathEntity(); this.sheep.getNavigator().clearPathEntity();
} }
@Override
public void resetTask() public void resetTask()
{ {
this.bopEatingGrassTimer = 0; this.bopEatingGrassTimer = 0;
} }
@Override
public boolean continueExecuting() public boolean continueExecuting()
{ {
return this.bopEatingGrassTimer > 0; return this.bopEatingGrassTimer > 0;
} }
@Override
public int getEatingGrassTimer() public int getEatingGrassTimer()
{ {
return this.bopEatingGrassTimer; return this.bopEatingGrassTimer;
} }
@Override
public void updateTask() public void updateTask()
{ {
this.bopEatingGrassTimer = Math.max(0, this.bopEatingGrassTimer - 1); this.bopEatingGrassTimer = Math.max(0, this.bopEatingGrassTimer - 1);
if (this.bopEatingGrassTimer == 4) 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")) if (this.world.getGameRules().getBoolean("mobGriefing"))
{ {
this.world.destroyBlock(blockpos, false); this.world.destroyBlock(pos, false);
} }
this.sheep.eatGrassBonus(); this.sheep.eatGrassBonus();
} }
else else {
{ BlockPos posDown = pos.down();
BlockPos blockpos1 = blockpos.down(); IBlockState stateDown = world.getBlockState(posDown);
if (this.world.getBlockState(blockpos1).getBlock() == Blocks.GRASS) if (stateDown.getBlock() instanceof BlockBOPGrass)
{
BlockBOPGrass grass = (BlockBOPGrass) stateDown.getBlock();
Block dirtBlock = grass.getDirtBlockState(stateDown).getBlock();
if (dirtBlock instanceof BlockBOPDirt)
{ {
if (this.world.getGameRules().getBoolean("mobGriefing")) if (this.world.getGameRules().getBoolean("mobGriefing"))
{ {
this.world.playEvent(2001, blockpos1, Block.getIdFromBlock(Blocks.GRASS)); this.world.playEvent(2001, posDown, Block.getIdFromBlock(BOPBlocks.grass));
this.world.setBlockState(blockpos1, Blocks.DIRT.getDefaultState(), 2); 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(); 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; package biomesoplenty.common.handler;
import biomesoplenty.common.entities.ai.EntityAIEatBOPGrass; import biomesoplenty.common.entities.ai.EntityAIEatBOPGrass;
import net.minecraft.entity.ai.EntityAIBase;
import net.minecraft.entity.passive.EntitySheep; import net.minecraft.entity.passive.EntitySheep;
import net.minecraftforge.event.entity.EntityJoinWorldEvent; import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.relauncher.ReflectionHelper;
import java.lang.reflect.Field;
public class SheepEventHandler public class SheepEventHandler
{ {
@SubscribeEvent @SubscribeEvent
public void onEntityConstructEvent(EntityJoinWorldEvent event) public void onEntityJoin(EntityJoinWorldEvent event)
{ {
if (event.getEntity() instanceof EntitySheep) if (event.getEntity() instanceof EntitySheep)
{ {
EntitySheep sheep = (EntitySheep)event.getEntity(); EntitySheep sheep = (EntitySheep) event.getEntity();
Field eatGrass = ReflectionHelper.findField(EntitySheep.class, new String[]{"entityAIEatGrass", "field_146087_bs"});
try sheep.tasks.addTask(5, new EntityAIEatBOPGrass(sheep));
{
eatGrass.set(sheep, new EntityAIEatBOPGrass(sheep));
}
catch (IllegalArgumentException e)
{
e.printStackTrace();
}
catch (IllegalAccessException e)
{
e.printStackTrace();
}
} }
} }
} }