From fdc68ca5bbe1bfba3fd5f9565dd171c79d1accc6 Mon Sep 17 00:00:00 2001 From: GirafiStudios Date: Thu, 18 Aug 2016 22:54:33 +0200 Subject: [PATCH 1/3] Fixed jar issues. --- .../common/item/ItemJarEmpty.java | 84 +++++++++++-------- .../common/item/ItemJarFilled.java | 40 +++++---- 2 files changed, 73 insertions(+), 51 deletions(-) diff --git a/src/main/java/biomesoplenty/common/item/ItemJarEmpty.java b/src/main/java/biomesoplenty/common/item/ItemJarEmpty.java index 75677954f..d0f18c450 100644 --- a/src/main/java/biomesoplenty/common/item/ItemJarEmpty.java +++ b/src/main/java/biomesoplenty/common/item/ItemJarEmpty.java @@ -18,6 +18,7 @@ import net.minecraft.entity.item.EntityItem; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; +import net.minecraft.stats.StatList; import net.minecraft.util.ActionResult; import net.minecraft.util.EnumActionResult; import net.minecraft.util.EnumHand; @@ -39,44 +40,61 @@ public class ItemJarEmpty extends Item @Override public ActionResult onItemRightClick(ItemStack stack, World world, EntityPlayer player, EnumHand hand) { - RayTraceResult hit = this.rayTrace(world, player, true); - if (hit == null) {new ActionResult(EnumActionResult.FAIL, stack);} - if (hit.typeOfHit != RayTraceResult.Type.BLOCK) {new ActionResult(EnumActionResult.FAIL, stack);} - BlockPos pos = hit.getBlockPos(); - if (!world.isBlockModifiable(player, pos)) {new ActionResult(EnumActionResult.FAIL, stack);} - if (!player.canPlayerEdit(pos, hit.sideHit, stack)) {new ActionResult(EnumActionResult.FAIL, stack);} - - // determine if the block is one of our BOP fluids - // note - no need to check level - you don't get a hit unless it's full - IBlockState state = world.getBlockState(pos); - ItemJarFilled.JarContents jarContents = null; - if (state.getBlock() == BOPBlocks.honey) - { - jarContents = ItemJarFilled.JarContents.HONEY; - } - - // if it was honey or poison, return the corresponding filled jar - if (jarContents != null) - { - world.setBlockToAir(pos); - --stack.stackSize; - //TODO: 1.9 player.triggerAchievement(StatList.objectUseStats[Item.getIdFromItem(this)]); - - ItemStack honeyJar = new ItemStack(BOPItems.jar_filled, 1, jarContents.ordinal()); - // if there was only one empty jar in the stack, replace it, otherwise add the filledJar elsewhere in the inventory - if (stack.stackSize <= 0) + + if (hit == null) { + return new ActionResult(EnumActionResult.PASS, stack); + } else { + if (hit.typeOfHit == RayTraceResult.Type.BLOCK) { - return new ActionResult(EnumActionResult.FAIL, honeyJar); - } - else if (!player.inventory.addItemStackToInventory(honeyJar)) - { - // no room in inventory, so just drop it on the floor - player.dropItem(honeyJar, false); + BlockPos pos = hit.getBlockPos(); + + if (!world.isBlockModifiable(player, pos) || !player.canPlayerEdit(pos.offset(hit.sideHit), hit.sideHit, stack)) + { + return new ActionResult(EnumActionResult.PASS, stack); + } + + // determine if the block is one of our BOP fluids + // note - no need to check level - you don't get a hit unless it's full + IBlockState state = world.getBlockState(pos); + ItemJarFilled.JarContents jarContents = null; + if (state.getBlock() == BOPBlocks.honey) + { + jarContents = ItemJarFilled.JarContents.HONEY; + } + + // if it was honey, return the corresponding filled jar + if (jarContents != null) + { + world.setBlockToAir(pos); + + return new ActionResult(EnumActionResult.SUCCESS, this.fillJar(stack, player, new ItemStack(BOPItems.jar_filled, 1, jarContents.ordinal()))); + } } } - return new ActionResult(EnumActionResult.SUCCESS, stack); + player.setActiveHand(hand); + return new ActionResult(EnumActionResult.PASS, stack); + } + + private ItemStack fillJar(ItemStack stack, EntityPlayer player, ItemStack jarStack) + { + --stack.stackSize; + player.addStat(StatList.getObjectUseStats(this)); + + // if there was only one empty jar in the stack, replace it, otherwise add the filledJar elsewhere in the inventory + if (stack.stackSize <= 0) + { + return jarStack; + } else { + if (!player.inventory.addItemStackToInventory(jarStack)) + { + // no room in inventory, so just drop it on the floor + player.dropItem(jarStack, false); + } + + return stack; + } } @Override diff --git a/src/main/java/biomesoplenty/common/item/ItemJarFilled.java b/src/main/java/biomesoplenty/common/item/ItemJarFilled.java index e87d9a80f..3da50ed02 100644 --- a/src/main/java/biomesoplenty/common/item/ItemJarFilled.java +++ b/src/main/java/biomesoplenty/common/item/ItemJarFilled.java @@ -8,19 +8,15 @@ package biomesoplenty.common.item; -import java.util.List; - +import biomesoplenty.api.item.BOPItems; import biomesoplenty.common.entities.EntityButterfly; import biomesoplenty.common.entities.EntityPixie; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; -import net.minecraft.util.ActionResult; -import net.minecraft.util.EnumActionResult; -import net.minecraft.util.EnumFacing; -import net.minecraft.util.EnumHand; -import net.minecraft.util.IStringSerializable; +import net.minecraft.stats.StatList; +import net.minecraft.util.*; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.MathHelper; import net.minecraft.util.math.RayTraceResult; @@ -30,6 +26,8 @@ import net.minecraft.world.World; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; +import java.util.List; + public class ItemJarFilled extends Item { @@ -130,7 +128,6 @@ public class ItemJarFilled extends Item world.spawnEntityInWorld(pixie); pixie.playLivingSound(); if (stack.hasDisplayName()) {pixie.setCustomNameTag(stack.getDisplayName());} - if (!player.capabilities.isCreativeMode) {--stack.stackSize;} return true; } else { player.addChatComponentMessage(new TextComponentString("\u00a75Pixies cannot survive in this environment!")); @@ -147,7 +144,6 @@ public class ItemJarFilled extends Item world.spawnEntityInWorld(butterfly); butterfly.playLivingSound(); if (stack.hasDisplayName()) {butterfly.setCustomNameTag(stack.getDisplayName());} - if (!player.capabilities.isCreativeMode) {--stack.stackSize;} return true; } else { player.addChatComponentMessage(new TextComponentString("\u00a75Butterflies cannot survive in this environment!")); @@ -168,22 +164,35 @@ public class ItemJarFilled extends Item // release pixie into the air in front of the player (target distance 0.8, but will be closer if there's blocks in the way) Vec3d releasePoint = this.getAirPositionInFrontOfPlayer(world, player, 0.8D); this.releasePixie(stack, world, player, releasePoint); + this.emptyJar(stack, player, new ItemStack(BOPItems.jar_empty)); + return new ActionResult(EnumActionResult.SUCCESS, stack); } - return new ActionResult(EnumActionResult.SUCCESS, stack); - + case BUTTERFLY: if (this.getContentsType(stack) == JarContents.BUTTERFLY) { // release pixie into the air in front of the player (target distance 0.8, but will be closer if there's blocks in the way) Vec3d releasePoint = this.getAirPositionInFrontOfPlayer(world, player, 0.8D); this.releaseButterfly(stack, world, player, releasePoint); + this.emptyJar(stack, player, new ItemStack(BOPItems.jar_empty)); + return new ActionResult(EnumActionResult.SUCCESS, stack); } - return new ActionResult(EnumActionResult.SUCCESS, stack); - + case HONEY: default: return new ActionResult(EnumActionResult.FAIL, stack); } } + + protected ItemStack emptyJar(ItemStack stack, EntityPlayer player, ItemStack emptyJarStack) + { + if (!player.capabilities.isCreativeMode) { --stack.stackSize; } + player.addStat(StatList.getObjectUseStats(this)); + + if (!player.inventory.addItemStackToInventory(emptyJarStack)) { + player.dropItem(emptyJarStack, false); + } + return stack; + } @Override @@ -214,10 +223,5 @@ public class ItemJarFilled extends Item return EnumActionResult.SUCCESS; } } - - - - - } \ No newline at end of file From eee1a0358ac8b050dca083be72003d31c496ebb5 Mon Sep 17 00:00:00 2001 From: GirafiStudios Date: Fri, 19 Aug 2016 00:57:29 +0200 Subject: [PATCH 2/3] Fixed silk touching on BoP blocks --- .../common/handler/SilkTouchEventHandler.java | 21 +++++++++++++++++++ .../common/init/ModHandlers.java | 1 + 2 files changed, 22 insertions(+) create mode 100644 src/main/java/biomesoplenty/common/handler/SilkTouchEventHandler.java diff --git a/src/main/java/biomesoplenty/common/handler/SilkTouchEventHandler.java b/src/main/java/biomesoplenty/common/handler/SilkTouchEventHandler.java new file mode 100644 index 000000000..d8e3a7085 --- /dev/null +++ b/src/main/java/biomesoplenty/common/handler/SilkTouchEventHandler.java @@ -0,0 +1,21 @@ +package biomesoplenty.common.handler; + +import biomesoplenty.common.block.IBOPBlock; +import net.minecraft.block.state.IBlockState; +import net.minecraftforge.event.world.BlockEvent; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; + +public class SilkTouchEventHandler { + + @SubscribeEvent + public void onSilkTouched(BlockEvent.HarvestDropsEvent event) + { + IBlockState state = event.getState(); + + if (state.getBlock() instanceof IBOPBlock && event.isSilkTouching()) + { + event.getDrops().clear(); + event.getDrops().add(state.getBlock().getPickBlock(state, null, event.getWorld(), event.getPos(), event.getHarvester())); + } + } +} \ No newline at end of file diff --git a/src/main/java/biomesoplenty/common/init/ModHandlers.java b/src/main/java/biomesoplenty/common/init/ModHandlers.java index f6ac2a589..3481717a2 100644 --- a/src/main/java/biomesoplenty/common/init/ModHandlers.java +++ b/src/main/java/biomesoplenty/common/init/ModHandlers.java @@ -39,6 +39,7 @@ public class ModHandlers MinecraftForge.EVENT_BUS.register(new AchievementEventHandler()); MinecraftForge.EVENT_BUS.register(new GrassPathEventHandler()); MinecraftForge.EVENT_BUS.register(new SheepEventHandler()); + MinecraftForge.EVENT_BUS.register(new SilkTouchEventHandler()); MinecraftForge.EVENT_BUS.register(new LootTableEventHandler()); if (FMLCommonHandler.instance().getSide() == Side.CLIENT) From 8f95dcc9c3bf28008893a7dcfd475c5b03873c27 Mon Sep 17 00:00:00 2001 From: GirafiStudios Date: Tue, 23 Aug 2016 21:12:50 +0200 Subject: [PATCH 3/3] 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