fix sugarcane acting as logs with mechanical saws

- prevent trees adjacent to sugar cane or similar blocks from being cut by the mechanical saw unintentionally
This commit is contained in:
zelophed 2024-03-17 00:22:01 +01:00
parent c4098b31b2
commit 92975231d7
3 changed files with 24 additions and 13 deletions

View file

@ -259,7 +259,7 @@ public class SawBlockEntity extends BlockBreakingKineticBlockEntity {
super.invalidate();
invProvider.invalidate();
}
@Override
public void destroy() {
super.destroy();
@ -351,8 +351,8 @@ public class SawBlockEntity extends BlockBreakingKineticBlockEntity {
ItemHelper.addToList(stack, list);
}
}
for (int slot = 0; slot < list.size() && slot + 1 < inventory.getSlots(); slot++)
for (int slot = 0; slot < list.size() && slot + 1 < inventory.getSlots(); slot++)
inventory.setStackInSlot(slot + 1, list.get(slot));
award(AllAdvancements.SAW_PROCESSING);
@ -461,7 +461,7 @@ public class SawBlockEntity extends BlockBreakingKineticBlockEntity {
}
super.onBlockBroken(stateToBreak);
TreeCutter.findTree(level, breakingPos)
TreeCutter.findTree(level, breakingPos, stateToBreak)
.destroyBlocks(level, null, this::dropItemFromCutTree);
}

View file

@ -72,7 +72,7 @@ public class SawMovementBehaviour extends BlockBreakingMovementBehaviour {
return;
}
TreeCutter.findTree(context.world, pos)
TreeCutter.findTree(context.world, pos, brokenState)
.destroyBlocks(context.world, null, (stack, dropPos) -> dropItemFromCutTree(context, stack, dropPos));
}

View file

@ -55,15 +55,20 @@ public class TreeCutter {
return Optional.empty();
}
@Deprecated(forRemoval = true)
public static Tree findTree(@Nullable BlockGetter reader, BlockPos pos) {
return findTree(reader, pos, Blocks.AIR.defaultBlockState());
}
/**
* Finds a tree at the given pos. Block at the position should be air
*
* @param reader
* @param pos
* @return null if not found or not fully cut
* @param reader the level that will be searched for a tree
* @param pos position that the saw cut at
* @param brokenState block state what was broken by the saw
*/
@Nonnull
public static Tree findTree(@Nullable BlockGetter reader, BlockPos pos) {
public static Tree findTree(@Nullable BlockGetter reader, BlockPos pos, BlockState brokenState) {
if (reader == null)
return NO_TREE;
@ -72,11 +77,14 @@ public class TreeCutter {
Set<BlockPos> visited = new HashSet<>();
List<BlockPos> frontier = new LinkedList<>();
// Bamboo, Sugar Cane, Cactus
BlockState stateAbove = reader.getBlockState(pos.above());
if (isVerticalPlant(stateAbove)) {
// Bamboo, Sugar Cane, Cactus
if (isVerticalPlant(brokenState)) {
if (!isVerticalPlant(stateAbove))
return NO_TREE;
logs.add(pos.above());
for (int i = 1; i < 256; i++) {
for (int i = 1; i < reader.getHeight(); i++) {
BlockPos current = pos.above(i);
if (!isVerticalPlant(reader.getBlockState(current)))
break;
@ -87,7 +95,10 @@ public class TreeCutter {
}
// Chorus
if (isChorus(stateAbove)) {
if (isChorus(brokenState)) {
if (!isChorus(stateAbove))
return NO_TREE;
frontier.add(pos.above());
while (!frontier.isEmpty()) {
BlockPos current = frontier.remove(0);