From 99b2342a1c7596dc45e5b845f256cbe876eb82b0 Mon Sep 17 00:00:00 2001 From: drcrazy Date: Wed, 25 Dec 2019 03:39:46 +0300 Subject: [PATCH] Make JackHammer great again. --- .../tool/advanced/AdvancedJackhammerItem.java | 121 ++++++++++++++++++ src/main/java/techreborn/utils/ToolsUtil.java | 11 +- 2 files changed, 130 insertions(+), 2 deletions(-) diff --git a/src/main/java/techreborn/items/tool/advanced/AdvancedJackhammerItem.java b/src/main/java/techreborn/items/tool/advanced/AdvancedJackhammerItem.java index bab10c63c..bb6614d5c 100644 --- a/src/main/java/techreborn/items/tool/advanced/AdvancedJackhammerItem.java +++ b/src/main/java/techreborn/items/tool/advanced/AdvancedJackhammerItem.java @@ -26,14 +26,38 @@ package techreborn.items.tool.advanced; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; +import net.minecraft.block.*; +import net.minecraft.client.item.TooltipContext; +import net.minecraft.entity.LivingEntity; +import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ItemGroup; import net.minecraft.item.ItemStack; +import net.minecraft.item.Items; import net.minecraft.item.ToolMaterials; +import net.minecraft.text.Text; +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 techreborn.config.TechRebornConfig; import techreborn.init.TRContent; import techreborn.items.tool.JackhammerItem; import techreborn.utils.InitUtils; +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 { @@ -44,6 +68,103 @@ public class AdvancedJackhammerItem extends JackhammerItem { this.transferLimit = 1000; } + 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; + } + BlockState blockState = worldIn.getBlockState(pos); + if (blockState.getMaterial() == Material.AIR) { + return false; + } + if (blockState.getMaterial().isLiquid()) { + return false; + } + if (blockState.getBlock() instanceof OreBlock){ + return false; + } + return (Items.IRON_PICKAXE.isEffectiveOn(blockState)); + } + + // 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)) { + ToolsUtil.breakBlock(stack, worldIn, additionalPos, entityLiving, cost); + } + } + return super.postMine(stack, worldIn, stateIn, pos, entityLiving); + } + + // Item + @Override + public TypedActionResult use(final World world, final PlayerEntity player, final Hand hand) { + final ItemStack stack = player.getStackInHand(hand); + if (player.isSneaking()) { + ItemUtils.switchActive(stack, cost, world.isClient, MessageIDs.poweredToolID); + return new TypedActionResult<>(ActionResult.SUCCESS, stack); + } + return new TypedActionResult<>(ActionResult.PASS, stack); + } + + @Override + public void usageTick(World world, LivingEntity entity, ItemStack stack, int i) { + ItemUtils.checkActive(stack, cost, entity.world.isClient, MessageIDs.poweredToolID); + } + + @Environment(EnvType.CLIENT) + @Override + public void appendTooltip(ItemStack stack, @Nullable World worldIn, List tooltip, TooltipContext flagIn) { + ItemUtils.buildActiveTooltip(stack, tooltip); + } + @Environment(EnvType.CLIENT) @Override public void appendStacks(ItemGroup par2ItemGroup, DefaultedList itemList) { diff --git a/src/main/java/techreborn/utils/ToolsUtil.java b/src/main/java/techreborn/utils/ToolsUtil.java index 3ae9bf289..acd0ba522 100644 --- a/src/main/java/techreborn/utils/ToolsUtil.java +++ b/src/main/java/techreborn/utils/ToolsUtil.java @@ -26,6 +26,8 @@ package techreborn.utils; import net.minecraft.block.BlockState; import net.minecraft.block.Blocks; +import net.minecraft.enchantment.EnchantmentHelper; +import net.minecraft.enchantment.Enchantments; import net.minecraft.entity.LivingEntity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ItemStack; @@ -33,6 +35,8 @@ import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import team.reborn.energy.Energy; +import java.util.Random; + /** * @author drcrazy * @@ -43,8 +47,11 @@ public class ToolsUtil { if (!(entityLiving instanceof PlayerEntity)) { return; } - if (!Energy.of(tool).use(cost)) { - return; + Random rand = new Random(); + if (rand.nextInt(EnchantmentHelper.getLevel(Enchantments.UNBREAKING, tool) + 1) == 0) { + if (!Energy.of(tool).use(cost)) { + return; + } } BlockState blockState = world.getBlockState(pos); if (blockState.getHardness(world, pos) == -1.0F) {