Fix industrial jackhammer AOE mining. Closes #1975

This commit is contained in:
drcrazy 2020-01-14 02:22:06 +03:00
parent 2aa2f6d7bb
commit a543a562b2
4 changed files with 129 additions and 219 deletions

View file

@ -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<BlockPos> getTargetBlocks(World worldIn, BlockPos pos, @Nullable LivingEntity entityLiving) {
Set<BlockPos> 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);
}

View file

@ -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<BlockPos> getTargetBlocks(World worldIn, BlockPos pos, @Nullable LivingEntity entityLiving) {
if (!(entityLiving instanceof PlayerEntity)) {
return ImmutableSet.of();
}
Set<BlockPos> 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);
}

View file

@ -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<BlockPos> getTargetBlocks(World worldIn, BlockPos pos, @Nullable LivingEntity entityLiving) {
Set<BlockPos> 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<Text> 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));

View file

@ -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<BlockPos> getAOEMiningBlocks(World worldIn, BlockPos pos, @Nullable LivingEntity entityLiving, int radius) {
if (!(entityLiving instanceof PlayerEntity)) {
return ImmutableSet.of();
}
Set<BlockPos> 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;
}
}