From a543a562b2d60821a4aa1c73486a573601ccc0f8 Mon Sep 17 00:00:00 2001 From: drcrazy Date: Tue, 14 Jan 2020 02:22:06 +0300 Subject: [PATCH] Fix industrial jackhammer AOE mining. Closes #1975 --- .../tool/advanced/AdvancedJackhammerItem.java | 67 ++--------- .../tool/industrial/IndustrialDrillItem.java | 68 ++--------- .../industrial/IndustrialJackhammerItem.java | 101 ++-------------- src/main/java/techreborn/utils/ToolsUtil.java | 112 ++++++++++++++++-- 4 files changed, 129 insertions(+), 219 deletions(-) diff --git a/src/main/java/techreborn/items/tool/advanced/AdvancedJackhammerItem.java b/src/main/java/techreborn/items/tool/advanced/AdvancedJackhammerItem.java index d49e1a027..7f20c9816 100644 --- a/src/main/java/techreborn/items/tool/advanced/AdvancedJackhammerItem.java +++ b/src/main/java/techreborn/items/tool/advanced/AdvancedJackhammerItem.java @@ -26,7 +26,10 @@ package techreborn.items.tool.advanced; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; -import net.minecraft.block.*; +import net.minecraft.block.BlockState; +import net.minecraft.block.Material; +import net.minecraft.block.OreBlock; +import net.minecraft.block.RedstoneOreBlock; import net.minecraft.client.item.TooltipContext; import net.minecraft.entity.LivingEntity; import net.minecraft.entity.player.PlayerEntity; @@ -37,11 +40,7 @@ import net.minecraft.text.Text; import net.minecraft.util.ActionResult; import net.minecraft.util.Hand; import net.minecraft.util.TypedActionResult; -import net.minecraft.util.hit.BlockHitResult; -import net.minecraft.util.hit.HitResult; import net.minecraft.util.math.BlockPos; -import net.minecraft.util.math.Direction; -import net.minecraft.world.RayTraceContext; import net.minecraft.world.World; import reborncore.common.util.ItemUtils; import team.reborn.energy.EnergyTier; @@ -51,10 +50,7 @@ import techreborn.utils.MessageIDs; import techreborn.utils.ToolsUtil; import javax.annotation.Nullable; -import java.util.Collections; -import java.util.HashSet; import java.util.List; -import java.util.Set; public class AdvancedJackhammerItem extends JackhammerItem { @@ -62,53 +58,6 @@ public class AdvancedJackhammerItem extends JackhammerItem { super(ToolMaterials.DIAMOND, TechRebornConfig.advancedJackhammerCharge, EnergyTier.EXTREME, TechRebornConfig.advancedJackhammerCost); } - private Set getTargetBlocks(World worldIn, BlockPos pos, @Nullable LivingEntity entityLiving) { - Set targetBlocks = new HashSet<>(); - if (!(entityLiving instanceof PlayerEntity)) { - return new HashSet<>(); - } - PlayerEntity playerIn = (PlayerEntity) entityLiving; - - //Put a dirt block down to raytrace with to stop it raytracing past the intended block - worldIn.setBlockState(pos, Blocks.DIRT.getDefaultState()); - HitResult hitResult = rayTrace(worldIn, playerIn, RayTraceContext.FluidHandling.NONE); - worldIn.setBlockState(pos, Blocks.AIR.getDefaultState()); - - if (!(hitResult instanceof BlockHitResult)) { - return Collections.emptySet(); - } - Direction enumfacing = ((BlockHitResult) hitResult).getSide(); - if (enumfacing == Direction.SOUTH || enumfacing == Direction.NORTH) { - for (int i = -1; i < 2; i++) { - for (int j = -1; j < 2; j++) { - BlockPos newPos = pos.add(i, j, 0); - if (shouldBreak(worldIn, pos, newPos)) { - targetBlocks.add(newPos); - } - } - } - } else if (enumfacing == Direction.EAST || enumfacing == Direction.WEST) { - for (int i = -1; i < 2; i++) { - for (int j = -1; j < 2; j++) { - BlockPos newPos = pos.add(0, j, i); - if (shouldBreak(worldIn, pos, newPos)) { - targetBlocks.add(newPos); - } - } - } - } else if (enumfacing == Direction.DOWN || enumfacing == Direction.UP) { - for (int i = -1; i < 2; i++) { - for (int j = -1; j < 2; j++) { - BlockPos newPos = pos.add(j, 0, i); - if (shouldBreak(worldIn, pos, newPos)) { - targetBlocks.add(newPos); - } - } - } - } - return targetBlocks; - } - private boolean shouldBreak(World worldIn, BlockPos originalPos, BlockPos pos) { if (originalPos.equals(pos)) { return false; @@ -132,11 +81,15 @@ public class AdvancedJackhammerItem extends JackhammerItem { // JackhammerItem @Override public boolean postMine(ItemStack stack, World worldIn, BlockState stateIn, BlockPos pos, LivingEntity entityLiving) { - if (ItemUtils.isActive(stack)) { - for (BlockPos additionalPos : getTargetBlocks(worldIn, pos, entityLiving)) { + if (!ItemUtils.isActive(stack)) { + return super.postMine(stack, worldIn, stateIn, pos, entityLiving); + } + for (BlockPos additionalPos : ToolsUtil.getAOEMiningBlocks(worldIn, pos, entityLiving, 1)) { + if (shouldBreak(worldIn, pos, additionalPos)) { ToolsUtil.breakBlock(stack, worldIn, additionalPos, entityLiving, cost); } } + return super.postMine(stack, worldIn, stateIn, pos, entityLiving); } diff --git a/src/main/java/techreborn/items/tool/industrial/IndustrialDrillItem.java b/src/main/java/techreborn/items/tool/industrial/IndustrialDrillItem.java index e719f4fe5..bdf422d36 100644 --- a/src/main/java/techreborn/items/tool/industrial/IndustrialDrillItem.java +++ b/src/main/java/techreborn/items/tool/industrial/IndustrialDrillItem.java @@ -24,11 +24,9 @@ package techreborn.items.tool.industrial; -import com.google.common.collect.ImmutableSet; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.block.BlockState; -import net.minecraft.block.Blocks; import net.minecraft.block.Material; import net.minecraft.client.item.TooltipContext; import net.minecraft.entity.LivingEntity; @@ -42,11 +40,7 @@ import net.minecraft.util.ActionResult; import net.minecraft.util.DefaultedList; import net.minecraft.util.Hand; import net.minecraft.util.TypedActionResult; -import net.minecraft.util.hit.BlockHitResult; -import net.minecraft.util.hit.HitResult; import net.minecraft.util.math.BlockPos; -import net.minecraft.util.math.Direction; -import net.minecraft.world.RayTraceContext; import net.minecraft.world.World; import reborncore.common.util.ItemUtils; import team.reborn.energy.EnergyTier; @@ -58,10 +52,7 @@ import techreborn.utils.MessageIDs; import techreborn.utils.ToolsUtil; import javax.annotation.Nullable; -import java.util.Collections; -import java.util.HashSet; import java.util.List; -import java.util.Set; public class IndustrialDrillItem extends DrillItem { @@ -69,53 +60,6 @@ public class IndustrialDrillItem extends DrillItem { super(ToolMaterials.DIAMOND, TechRebornConfig.industrialDrillCharge, EnergyTier.INSANE, TechRebornConfig.industrialDrillCost, 15.0F, 2.0F, Items.DIAMOND_PICKAXE, Items.DIAMOND_SHOVEL); } - private Set getTargetBlocks(World worldIn, BlockPos pos, @Nullable LivingEntity entityLiving) { - if (!(entityLiving instanceof PlayerEntity)) { - return ImmutableSet.of(); - } - Set targetBlocks = new HashSet<>(); - PlayerEntity playerIn = (PlayerEntity) entityLiving; - - //Put a dirt block down to raytrace with to stop it raytracing past the intended block - worldIn.setBlockState(pos, Blocks.DIRT.getDefaultState()); - HitResult hitResult = rayTrace(worldIn, playerIn, RayTraceContext.FluidHandling.NONE); - worldIn.setBlockState(pos, Blocks.AIR.getDefaultState()); - - if (!(hitResult instanceof BlockHitResult)) { - return Collections.emptySet(); - } - Direction enumfacing = ((BlockHitResult) hitResult).getSide(); - if (enumfacing == Direction.SOUTH || enumfacing == Direction.NORTH) { - for (int i = -1; i < 2; i++) { - for (int j = -1; j < 2; j++) { - BlockPos newPos = pos.add(i, j, 0); - if (shouldBreak(playerIn, worldIn, pos, newPos)) { - targetBlocks.add(newPos); - } - } - } - } else if (enumfacing == Direction.EAST || enumfacing == Direction.WEST) { - for (int i = -1; i < 2; i++) { - for (int j = -1; j < 2; j++) { - BlockPos newPos = pos.add(0, j, i); - if (shouldBreak(playerIn, worldIn, pos, newPos)) { - targetBlocks.add(newPos); - } - } - } - } else if (enumfacing == Direction.DOWN || enumfacing == Direction.UP) { - for (int i = -1; i < 2; i++) { - for (int j = -1; j < 2; j++) { - BlockPos newPos = pos.add(j, 0, i); - if (shouldBreak(playerIn, worldIn, pos, newPos)) { - targetBlocks.add(newPos); - } - } - } - } - return targetBlocks; - } - private boolean shouldBreak(PlayerEntity playerIn, World worldIn, BlockPos originalPos, BlockPos pos) { if (originalPos.equals(pos)) { return false; @@ -138,11 +82,19 @@ public class IndustrialDrillItem extends DrillItem { // DrillItem @Override public boolean postMine(ItemStack stack, World worldIn, BlockState stateIn, BlockPos pos, LivingEntity entityLiving) { - if (ItemUtils.isActive(stack)) { - for (BlockPos additionalPos : getTargetBlocks(worldIn, pos, entityLiving)) { + if (!ItemUtils.isActive(stack)) { + return super.postMine(stack, worldIn, stateIn, pos, entityLiving); + } + if (!(entityLiving instanceof PlayerEntity)) { + return super.postMine(stack, worldIn, stateIn, pos, entityLiving); + } + PlayerEntity playerIn = (PlayerEntity) entityLiving; + for (BlockPos additionalPos : ToolsUtil.getAOEMiningBlocks(worldIn, pos, entityLiving, 1)) { + if (shouldBreak(playerIn, worldIn, pos, additionalPos)) { ToolsUtil.breakBlock(stack, worldIn, additionalPos, entityLiving, cost); } } + return super.postMine(stack, worldIn, stateIn, pos, entityLiving); } diff --git a/src/main/java/techreborn/items/tool/industrial/IndustrialJackhammerItem.java b/src/main/java/techreborn/items/tool/industrial/IndustrialJackhammerItem.java index 38dff69b4..a38fec13b 100644 --- a/src/main/java/techreborn/items/tool/industrial/IndustrialJackhammerItem.java +++ b/src/main/java/techreborn/items/tool/industrial/IndustrialJackhammerItem.java @@ -39,11 +39,7 @@ import net.minecraft.util.ActionResult; import net.minecraft.util.Formatting; import net.minecraft.util.Hand; import net.minecraft.util.TypedActionResult; -import net.minecraft.util.hit.BlockHitResult; -import net.minecraft.util.hit.HitResult; import net.minecraft.util.math.BlockPos; -import net.minecraft.util.math.Direction; -import net.minecraft.world.RayTraceContext; import net.minecraft.world.World; import reborncore.common.util.ChatUtils; import reborncore.common.util.ItemUtils; @@ -56,10 +52,7 @@ import techreborn.utils.MessageIDs; import techreborn.utils.ToolsUtil; import javax.annotation.Nullable; -import java.util.Collections; -import java.util.HashSet; import java.util.List; -import java.util.Set; public class IndustrialJackhammerItem extends JackhammerItem { @@ -77,7 +70,7 @@ public class IndustrialJackhammerItem extends JackhammerItem { ChatUtils.sendNoSpamMessages(messageId, new LiteralText(Formatting.GRAY + StringUtils.t("techreborn.message.setTo") + " " + Formatting.GOLD + "3*3")); } } else { - if (isAOE(stack)) { + if (isAOE5(stack)) { ItemUtils.switchActive(stack, cost, isClient, messageId); stack.getOrCreateTag().putBoolean("AOE5", false); } else { @@ -89,85 +82,6 @@ public class IndustrialJackhammerItem extends JackhammerItem { } } - private Set getTargetBlocks(World worldIn, BlockPos pos, @Nullable LivingEntity entityLiving) { - Set targetBlocks = new HashSet<>(); - if (!(entityLiving instanceof PlayerEntity)) { - return new HashSet<>(); - } - PlayerEntity playerIn = (PlayerEntity) entityLiving; - - //Put a dirt block down to raytrace with to stop it raytracing past the intended block - worldIn.setBlockState(pos, Blocks.DIRT.getDefaultState()); - HitResult hitResult = rayTrace(worldIn, playerIn, RayTraceContext.FluidHandling.NONE); - worldIn.setBlockState(pos, Blocks.AIR.getDefaultState()); - - if (!(hitResult instanceof BlockHitResult)) { - return Collections.emptySet(); - } - Direction enumfacing = ((BlockHitResult) hitResult).getSide(); - int add = isAOE(entityLiving.getMainHandStack()) ? 2 : 0; - if (enumfacing == Direction.SOUTH || enumfacing == Direction.NORTH) { - for (int i = -1 - add / 2; i < 2 + add / 2; i++) { - for (int j = -1; j < 2 + add; j++) { - BlockPos newPos = pos.add(i, j, 0); - if (shouldBreak(worldIn, pos, newPos)) { - targetBlocks.add(newPos); - } - } - } - } else if (enumfacing == Direction.EAST || enumfacing == Direction.WEST) { - for (int i = -1 - add / 2; i < 2 + add / 2; i++) { - for (int j = -1; j < 2 + add; j++) { - BlockPos newPos = pos.add(0, j, i); - if (shouldBreak(worldIn, pos, newPos)) { - targetBlocks.add(newPos); - } - } - } - } else if (enumfacing == Direction.DOWN || enumfacing == Direction.UP) { - Direction playerFacing = playerIn.getHorizontalFacing(); - int minX = 0; - int maxX = 0; - int minZ = 0; - int maxZ = 0; - switch (playerFacing) { - case SOUTH: - minZ = -1; - maxZ = 3; - minX = -2; - maxX = 2; - break; - case NORTH: - minZ = -3; - maxZ = 1; - minX = -2; - maxX = 2; - break; - case WEST: - minZ = -2; - maxZ = 2; - minX = -3; - maxX = 1; - break; - case EAST: - minZ = -2; - maxZ = 2; - minX = -1; - maxX = 3; - break; - } - for (int x = minX; x <= maxX; x++) { - for (int z = minZ; z <= maxZ; z++) { - BlockPos newPos = pos.add(x, 0, z); - if (shouldBreak(worldIn, pos, newPos)) { - targetBlocks.add(newPos); - } - } - } - } - return targetBlocks; - } - private boolean shouldBreak(World worldIn, BlockPos originalPos, BlockPos pos) { if (originalPos.equals(pos)) { return false; @@ -188,18 +102,23 @@ public class IndustrialJackhammerItem extends JackhammerItem { return (Items.IRON_PICKAXE.isEffectiveOn(blockState)); } - private boolean isAOE(ItemStack stack) { + private boolean isAOE5(ItemStack stack) { return !stack.isEmpty() && stack.getTag() != null && stack.getTag().getBoolean("AOE5"); } // JackhammerItem @Override public boolean postMine(ItemStack stack, World worldIn, BlockState stateIn, BlockPos pos, LivingEntity entityLiving) { - if (ItemUtils.isActive(stack)) { - for (BlockPos additionalPos : getTargetBlocks(worldIn, pos, entityLiving)) { + if (!ItemUtils.isActive(stack)) { + return super.postMine(stack, worldIn, stateIn, pos, entityLiving); + } + int radius = isAOE5(stack) ? 2 : 1; + for (BlockPos additionalPos : ToolsUtil.getAOEMiningBlocks(worldIn, pos, entityLiving, radius)) { + if (shouldBreak(worldIn, pos, additionalPos)) { ToolsUtil.breakBlock(stack, worldIn, additionalPos, entityLiving, cost); } } + return super.postMine(stack, worldIn, stateIn, pos, entityLiving); } @@ -235,7 +154,7 @@ public class IndustrialJackhammerItem extends JackhammerItem { public void appendTooltip(ItemStack stack, @Nullable World worldIn, List tooltip, TooltipContext flagIn) { ItemUtils.buildActiveTooltip(stack, tooltip); if (ItemUtils.isActive(stack)) { - if (isAOE(stack)) { + if (isAOE5(stack)) { tooltip.add(new LiteralText("5*5").formatted(Formatting.RED)); } else { tooltip.add(new LiteralText("3*3").formatted(Formatting.RED)); diff --git a/src/main/java/techreborn/utils/ToolsUtil.java b/src/main/java/techreborn/utils/ToolsUtil.java index c9db42553..1ae04f5f4 100644 --- a/src/main/java/techreborn/utils/ToolsUtil.java +++ b/src/main/java/techreborn/utils/ToolsUtil.java @@ -24,6 +24,7 @@ package techreborn.utils; +import com.google.common.collect.ImmutableSet; import net.minecraft.block.BlockState; import net.minecraft.block.Blocks; import net.minecraft.enchantment.EnchantmentHelper; @@ -31,34 +32,119 @@ import net.minecraft.enchantment.Enchantments; import net.minecraft.entity.LivingEntity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ItemStack; +import net.minecraft.util.hit.BlockHitResult; +import net.minecraft.util.hit.HitResult; import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.Direction; import net.minecraft.world.World; import team.reborn.energy.Energy; +import javax.annotation.Nullable; +import java.util.Collections; +import java.util.HashSet; import java.util.Random; +import java.util.Set; /** * @author drcrazy - * */ public class ToolsUtil { - public static void breakBlock(ItemStack tool, World world, BlockPos pos, LivingEntity entityLiving, int cost) { - if (!(entityLiving instanceof PlayerEntity)) { - return; - } - BlockState blockState = world.getBlockState(pos); - if (blockState.getHardness(world, pos) == -1.0F) { - return; - } + public static void breakBlock(ItemStack tool, World world, BlockPos pos, LivingEntity entityLiving, int cost) { + if (!(entityLiving instanceof PlayerEntity)) { + return; + } + BlockState blockState = world.getBlockState(pos); + if (blockState.getHardness(world, pos) == -1.0F) { + return; + } Random rand = new Random(); if (rand.nextInt(EnchantmentHelper.getLevel(Enchantments.UNBREAKING, tool) + 1) == 0) { if (!Energy.of(tool).use(cost)) { return; } } - blockState.getBlock().afterBreak(world, (PlayerEntity) entityLiving, pos, blockState, world.getBlockEntity(pos), tool); - world.setBlockState(pos, Blocks.AIR.getDefaultState()); - world.removeBlockEntity(pos); - } + blockState.getBlock().afterBreak(world, (PlayerEntity) entityLiving, pos, blockState, world.getBlockEntity(pos), tool); + world.setBlockState(pos, Blocks.AIR.getDefaultState()); + world.removeBlockEntity(pos); + } + + /** + * Fills in set of BlockPos which should be broken by AOE mining + * @param worldIn World reference + * @param pos BlockPos Position of originally broken block + * @param entityLiving LivingEntity Player who broke block + * @param radius int Radius of additional blocks to include. E.g. for 3x3 mining radius will be 1 + * @return Set of BlockPos to process by tool block break logic + */ + public static Set getAOEMiningBlocks(World worldIn, BlockPos pos, @Nullable LivingEntity entityLiving, int radius) { + if (!(entityLiving instanceof PlayerEntity)) { + return ImmutableSet.of(); + } + Set targetBlocks = new HashSet<>(); + PlayerEntity playerIn = (PlayerEntity) entityLiving; + + //Put a dirt block down to raytrace with to stop it raytracing past the intended block + worldIn.setBlockState(pos, Blocks.DIRT.getDefaultState()); + HitResult hitResult = playerIn.rayTrace(20D, 0F, false); + worldIn.setBlockState(pos, Blocks.AIR.getDefaultState()); + + if (!(hitResult instanceof BlockHitResult)) { + return Collections.emptySet(); + } + Direction direction = ((BlockHitResult) hitResult).getSide(); + + if (direction == Direction.SOUTH || direction == Direction.NORTH) { + for (int x = -radius; x <= radius; x++) { + for (int y = -1; y <= 1 + (radius - 1) * 2; y++) { + targetBlocks.add(pos.add(x, y, 0)); + } + } + } else if (direction == Direction.EAST || direction == Direction.WEST) { + for (int z = -radius; z <= radius; z++) { + for (int y = -1; y <= 1 + (radius - 1) * 2; y++) { + targetBlocks.add(pos.add(0, y, z)); + } + } + } else if (direction == Direction.DOWN || direction == Direction.UP) { + Direction playerDirection = playerIn.getHorizontalFacing(); + int minX = 0; + int maxX = 0; + int minZ = 0; + int maxZ = 0; + + switch (playerDirection) { + case SOUTH: + minZ = -1; + maxZ = 1 + (radius - 1) * 2; + minX = -radius; + maxX = radius; + break; + case NORTH: + minZ = -1 - (radius - 1) * 2; + maxZ = 1; + minX = -radius; + maxX = radius; + break; + case WEST: + minZ = -radius; + maxZ = radius; + minX = -1 - (radius - 1) * 2; + maxX = 1; + break; + case EAST: + minZ = -radius; + maxZ = radius; + minX = -1; + maxX = 1 + (radius - 1) * 2; + break; + } + for (int x = minX; x <= maxX; x++) { + for (int z = minZ; z <= maxZ; z++) { + targetBlocks.add(pos.add(x, 0, z)); + } + } + } + return targetBlocks; + } }