From 694763e6a26b1bad3f7fdb8a708208251cee1916 Mon Sep 17 00:00:00 2001 From: drcrazy Date: Fri, 30 Mar 2018 14:56:00 +0300 Subject: [PATCH] Refactored AOE break checks --- .../items/tools/ItemAdvancedChainsaw.java | 1 - .../items/tools/ItemAdvancedDrill.java | 71 ++++++++++--------- 2 files changed, 38 insertions(+), 34 deletions(-) diff --git a/src/main/java/techreborn/items/tools/ItemAdvancedChainsaw.java b/src/main/java/techreborn/items/tools/ItemAdvancedChainsaw.java index 6824c7f5e..6b9298191 100644 --- a/src/main/java/techreborn/items/tools/ItemAdvancedChainsaw.java +++ b/src/main/java/techreborn/items/tools/ItemAdvancedChainsaw.java @@ -79,7 +79,6 @@ public class ItemAdvancedChainsaw extends ItemChainsaw { return Items.DIAMOND_AXE.canHarvestBlock(blockIn); } - @SuppressWarnings("deprecation") public void breakBlock(BlockPos pos, ItemStack stack, World world, EntityLivingBase entityLiving, BlockPos oldPos) { if (oldPos == pos) { return; diff --git a/src/main/java/techreborn/items/tools/ItemAdvancedDrill.java b/src/main/java/techreborn/items/tools/ItemAdvancedDrill.java index 5ed9f4915..0611a3cbe 100644 --- a/src/main/java/techreborn/items/tools/ItemAdvancedDrill.java +++ b/src/main/java/techreborn/items/tools/ItemAdvancedDrill.java @@ -29,7 +29,6 @@ import net.minecraft.block.state.IBlockState; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.init.Blocks; import net.minecraft.init.Items; import net.minecraft.item.ItemStack; import net.minecraft.util.EnumFacing; @@ -46,6 +45,8 @@ import techreborn.init.ModItems; import java.util.HashSet; import java.util.Set; +import javax.annotation.Nullable; + public class ItemAdvancedDrill extends ItemDrill { public ItemAdvancedDrill() { @@ -53,28 +54,27 @@ public class ItemAdvancedDrill extends ItemDrill { this.cost = 250; } - public Set getTargetBlocks(World worldIn, BlockPos pos, EntityLivingBase entityLiving) { + public Set getTargetBlocks(World worldIn, BlockPos pos, @Nullable EntityPlayer playerIn) { Set targetBlocks = new HashSet(); - if (!(entityLiving instanceof EntityPlayer)) { + if (!(playerIn instanceof EntityPlayer)) { return new HashSet(); } - RayTraceResult raytrace = rayTrace(worldIn, (EntityPlayer) entityLiving, false); + RayTraceResult raytrace = rayTrace(worldIn, playerIn, false); EnumFacing enumfacing = raytrace.sideHit; if (enumfacing == EnumFacing.SOUTH || enumfacing == EnumFacing.NORTH) { for (int i = -1; i < 2; i++) { for (int j = -1; j < 2; j++) { BlockPos newPos = pos.add(i, j, 0); - if (worldIn.getBlockState(pos).getBlock() != Blocks.AIR) { + if (shouldBreak(playerIn, worldIn, pos, newPos)) { targetBlocks.add(newPos); } - } } } else if (enumfacing == EnumFacing.EAST || enumfacing == EnumFacing.WEST) { for (int i = -1; i < 2; i++) { for (int j = -1; j < 2; j++) { BlockPos newPos = pos.add(0, j, i); - if (worldIn.getBlockState(pos).getBlock() != Blocks.AIR) { + if (shouldBreak(playerIn, worldIn, pos, newPos)) { targetBlocks.add(newPos); } } @@ -83,7 +83,7 @@ public class ItemAdvancedDrill extends ItemDrill { for (int i = -1; i < 2; i++) { for (int j = -1; j < 2; j++) { BlockPos newPos = pos.add(j, 0, i); - if (worldIn.getBlockState(pos).getBlock() != Blocks.AIR) { + if (shouldBreak(playerIn, worldIn, pos, newPos)) { targetBlocks.add(newPos); } } @@ -92,42 +92,47 @@ public class ItemAdvancedDrill extends ItemDrill { return targetBlocks; } - public void breakBlock(BlockPos pos, World world, EntityPlayer playerIn, BlockPos originalPos, float originalHardness, ItemStack drill) { - if (originalPos == pos) { - return; - } + public void breakBlock(BlockPos pos, World world, EntityPlayer playerIn, ItemStack drill) { IBlockState blockState = world.getBlockState(pos); - float blockHardness = world.getBlockState(pos).getPlayerRelativeBlockHardness(playerIn, world, pos); - if (blockHardness == -1.0F) { - return; - } - if(blockState.getMaterial().isLiquid()){ - return; - } - if ((originalHardness / blockHardness) > 10.0F) { - return; - } - if(blockState.getMaterial() == Material.AIR){ - return; - } - if(!PoweredItem.canUseEnergy(cost, drill)){ - return; - } PoweredItem.useEnergy(cost, drill); blockState.getBlock().harvestBlock(world, playerIn, pos, blockState, world.getTileEntity(pos), drill); world.setBlockToAir(pos); world.removeTileEntity(pos); } + + private boolean shouldBreak(EntityPlayer playerIn, World worldIn, BlockPos originalPos, BlockPos pos) { + if (originalPos.equals(pos)) { + return false; + } + IBlockState blockState = worldIn.getBlockState(pos); + if (blockState.getMaterial() == Material.AIR) { + return false; + } + if (blockState.getMaterial().isLiquid()) { + return false; + } + float blockHardness = blockState.getPlayerRelativeBlockHardness(playerIn, worldIn, pos); + if (blockHardness == -1.0F) { + return false; + } + float originalHardness = worldIn.getBlockState(originalPos).getPlayerRelativeBlockHardness(playerIn, worldIn, originalPos); + if ((originalHardness / blockHardness) > 10.0F) { + return false; + } + + return true; + } // ItemDrill @Override public boolean onBlockDestroyed(ItemStack stack, World worldIn, IBlockState blockIn, BlockPos pos, EntityLivingBase entityLiving) { - EntityPlayer playerIn = (EntityPlayer) entityLiving; - float originalHardness = blockIn.getPlayerRelativeBlockHardness(playerIn, worldIn, pos); - for (BlockPos additionalPos : getTargetBlocks(worldIn, pos, entityLiving)) { - breakBlock(additionalPos, worldIn, playerIn, pos, originalHardness, stack); + EntityPlayer playerIn = null; + if ((entityLiving instanceof EntityPlayer)) { + playerIn = (EntityPlayer) entityLiving; + } + for (BlockPos additionalPos : getTargetBlocks(worldIn, pos, playerIn)) { + breakBlock(additionalPos, worldIn, playerIn, stack); } - // Use energy only once no matter how many blocks were broken, e.g. energy used per application of a drill return super.onBlockDestroyed(stack, worldIn, blockIn, pos, entityLiving); }