From 8f95dcc9c3bf28008893a7dcfd475c5b03873c27 Mon Sep 17 00:00:00 2001 From: GirafiStudios Date: Tue, 23 Aug 2016 21:12:50 +0200 Subject: [PATCH] Fixed that sheep won't eat BoP grass - Sheep now also eat BoPs variant of tall grass --- .../entities/ai/EntityAIEatBOPGrass.java | 69 ++++++++++++++----- .../common/handler/SheepEventHandler.java | 25 ++----- 2 files changed, 58 insertions(+), 36 deletions(-) diff --git a/src/main/java/biomesoplenty/common/entities/ai/EntityAIEatBOPGrass.java b/src/main/java/biomesoplenty/common/entities/ai/EntityAIEatBOPGrass.java index 33aec456c..a47633117 100644 --- a/src/main/java/biomesoplenty/common/entities/ai/EntityAIEatBOPGrass.java +++ b/src/main/java/biomesoplenty/common/entities/ai/EntityAIEatBOPGrass.java @@ -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 IS_TALL_GRASS = BlockStateMatcher.forBlock(Blocks.TALLGRASS).where(BlockTallGrass.TYPE, Predicates.equalTo(BlockTallGrass.EnumType.GRASS)); + private static final Predicate IS_SHORT_GRASS = forBoPPlant(BOPPlants.SHORTGRASS); + private static final Predicate IS_MEDIUM_GRASS = forBoPPlant(BOPPlants.MEDIUMGRASS); + private static final Predicate IS_WHEAT_GRASS = forBoPPlant(BOPPlants.WHEATGRASS); + private static final Predicate 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)); + } } \ No newline at end of file diff --git a/src/main/java/biomesoplenty/common/handler/SheepEventHandler.java b/src/main/java/biomesoplenty/common/handler/SheepEventHandler.java index b1e2db3d8..e07133ecd 100644 --- a/src/main/java/biomesoplenty/common/handler/SheepEventHandler.java +++ b/src/main/java/biomesoplenty/common/handler/SheepEventHandler.java @@ -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)); } } -} +} \ No newline at end of file