Fixed the chainsaw bug (#2943)

* Fixed the chainsaw bug

* Fixed bugged solution, works now, tested :)
This commit is contained in:
Ayutac 2022-05-03 01:55:37 +02:00 committed by GitHub
parent e4f56abdaa
commit 71b438acca
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -50,15 +50,30 @@ import techreborn.utils.ToolsUtil;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional;
public class IndustrialChainsawItem extends ChainsawItem { public class IndustrialChainsawItem extends ChainsawItem {
private static final Direction[] SEARCH_ORDER = new Direction[]{Direction.NORTH, Direction.SOUTH, Direction.EAST, Direction.WEST, Direction.UP}; private static final Direction[] SEARCH_ORDER = new Direction[]{Direction.NORTH, Direction.SOUTH, Direction.EAST, Direction.WEST, Direction.UP};
private BlockState lastCheckedBlockState;
public IndustrialChainsawItem() { public IndustrialChainsawItem() {
super(TRToolMaterials.INDUSTRIAL_CHAINSAW, TechRebornConfig.industrialChainsawCharge, RcEnergyTier.EXTREME, TechRebornConfig.industrialChainsawCost, 20F, 1.0F, Items.DIAMOND_AXE); super(TRToolMaterials.INDUSTRIAL_CHAINSAW, TechRebornConfig.industrialChainsawCharge, RcEnergyTier.EXTREME, TechRebornConfig.industrialChainsawCost, 20F, 1.0F, Items.DIAMOND_AXE);
} }
private boolean isValidLog(BlockState state) {
return state.isIn(BlockTags.LOGS);
}
private boolean isValidLeaves(BlockState state) {
return state.isIn(BlockTags.LEAVES) || state.isIn(BlockTags.WART_BLOCKS) || state.isOf(Blocks.SHROOMLIGHT);
}
private boolean isValidStartBlock(BlockState state) {
return isValidLog(state) || isValidLeaves(state);
}
private void findWood(World world, BlockPos pos, List<BlockPos> wood, List<BlockPos> leaves) { private void findWood(World world, BlockPos pos, List<BlockPos> wood, List<BlockPos> leaves) {
//Limit the amount of wood to be broken to 64 blocks. //Limit the amount of wood to be broken to 64 blocks.
if (wood.size() >= 64) { if (wood.size() >= 64) {
@ -73,10 +88,10 @@ public class IndustrialChainsawItem extends ChainsawItem {
if (!wood.contains(checkPos) && !leaves.contains(checkPos)) { if (!wood.contains(checkPos) && !leaves.contains(checkPos)) {
BlockState state = world.getBlockState(checkPos); BlockState state = world.getBlockState(checkPos);
if (state.isIn(BlockTags.LOGS)) { if (isValidLog(state)) {
wood.add(checkPos); wood.add(checkPos);
findWood(world, checkPos, wood, leaves); findWood(world, checkPos, wood, leaves);
} else if (state.isIn(BlockTags.LEAVES) || state.isIn(BlockTags.WART_BLOCKS) || state.isOf(Blocks.SHROOMLIGHT)) { } else if (isValidLeaves(state)) {
leaves.add(checkPos); leaves.add(checkPos);
findWood(world, checkPos, wood, leaves); findWood(world, checkPos, wood, leaves);
} }
@ -84,12 +99,18 @@ public class IndustrialChainsawItem extends ChainsawItem {
} }
} }
@Override
public boolean canMine(BlockState state, World world, BlockPos pos, PlayerEntity miner) {
lastCheckedBlockState = state;
return super.canMine(state, world, pos, miner);
}
//ChainsawItem //ChainsawItem
@Override @Override
public boolean postMine(ItemStack stack, World worldIn, BlockState blockIn, BlockPos pos, LivingEntity entityLiving) { public boolean postMine(ItemStack stack, World worldIn, BlockState blockIn, BlockPos pos, LivingEntity entityLiving) {
List<BlockPos> wood = new ArrayList<>(); List<BlockPos> wood = new ArrayList<>();
List<BlockPos> leaves = new ArrayList<>(); List<BlockPos> leaves = new ArrayList<>();
if (ItemUtils.isActive(stack)) { if (ItemUtils.isActive(stack) && (lastCheckedBlockState == null || isValidStartBlock(lastCheckedBlockState))) {
findWood(worldIn, pos, wood, leaves); findWood(worldIn, pos, wood, leaves);
wood.remove(pos); wood.remove(pos);
wood.stream() wood.stream()