Skip to content

Commit

Permalink
Fixed a bug causing server to crash
Browse files Browse the repository at this point in the history
  • Loading branch information
Taskeren committed Jan 31, 2023
1 parent f366bef commit fa8ac09
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/main/java/cn/elytra/mod/mining/item/MiningHammerBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,16 @@ public boolean onBlockStartBreak(ItemStack stack, int x, int y, int z, EntityPla
// Removed the CoFHCore logic for looping blocks.
// Making it more flexible to modify the range of mining blocks.

BlockPos center = BlockPos.fromMovingObjectPosition(pos);
if(center == null) {
// fixes NPE
// when player destroyed a block and the server lags, the MovingObjectPosition can be null as player moved
// its target block to air (unreachable).
return false;
}

ImmutableList<BlockPos> breakingBlocks =
getMiningBlocks(stack, BlockPos.fromMovingObjectPosition(pos), HitSide.fromSideHit(pos.sideHit), player);
getMiningBlocks(stack, center, HitSide.fromSideHit(pos.sideHit), player);

for(BlockPos bp : breakingBlocks) {
x2 = bp.getX();
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/cn/elytra/mod/mining/util/BlockPos.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.collect.ImmutableList;
import net.minecraft.util.MovingObjectPosition;

import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -59,8 +60,9 @@ public static BlockPos relativeTo(BlockPos reference, int offsetX, int offsetY,
return new BlockPos(reference.x + offsetX, reference.y + offsetY, reference.z + offsetZ);
}

public static BlockPos fromMovingObjectPosition(MovingObjectPosition mop) {
return new BlockPos(mop.blockX, mop.blockY, mop.blockZ);
@Nullable
public static BlockPos fromMovingObjectPosition(@Nullable MovingObjectPosition mop) {
return mop == null ? null : new BlockPos(mop.blockX, mop.blockY, mop.blockZ);
}

public static ImmutableList<BlockPos> range(BlockPos p1, BlockPos p2) {
Expand Down

0 comments on commit fa8ac09

Please sign in to comment.