Skip to content

Commit

Permalink
Account for reach distance in block break listener as well
Browse files Browse the repository at this point in the history
  • Loading branch information
2008Choco committed Aug 27, 2024
1 parent 56f220b commit dd85bab
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import wtf.choco.veinminer.player.VeinMinerPlayer;
import wtf.choco.veinminer.tool.VeinMinerToolCategory;
import wtf.choco.veinminer.tool.VeinMinerToolCategoryHand;
import wtf.choco.veinminer.util.AttributeUtil;
import wtf.choco.veinminer.util.VMConstants;
import wtf.choco.veinminer.util.VMEventFactory;

Expand Down Expand Up @@ -110,9 +111,9 @@ private void onBlockBreak(BlockBreakEvent event) {
}

// Fetch the target block face
double reachDistance = AttributeUtil.getReachDistance(player);
BlockFace targetBlockFace;
RayTraceResult rayTraceResult = player.rayTraceBlocks(6, FluidCollisionMode.NEVER);

RayTraceResult rayTraceResult = player.rayTraceBlocks(reachDistance, FluidCollisionMode.NEVER);
if (rayTraceResult == null || (targetBlockFace = rayTraceResult.getHitBlockFace()) == null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package wtf.choco.veinminer.player;

import com.google.common.base.Enums;
import com.google.common.base.Preconditions;

import java.util.ArrayList;
Expand All @@ -9,10 +8,7 @@
import java.util.concurrent.ConcurrentLinkedQueue;

import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.NamespacedKey;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeInstance;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.data.BlockData;
Expand Down Expand Up @@ -41,6 +37,7 @@
import wtf.choco.veinminer.pattern.PatternRegistry;
import wtf.choco.veinminer.pattern.VeinMiningPattern;
import wtf.choco.veinminer.tool.VeinMinerToolCategory;
import wtf.choco.veinminer.util.AttributeUtil;
import wtf.choco.veinminer.util.BlockPosition;
import wtf.choco.veinminer.util.VMEventFactory;

Expand All @@ -54,10 +51,6 @@
@Internal
public final class PlayerNetworkListener implements VeinMinerServerboundMessageListener {

private static final Attribute ATTRIBUTE_BLOCK_INTERACTION_RANGE = Enums.getIfPresent(Attribute.class, "PLAYER_BLOCK_INTERACTION_RANGE")
.or(Enums.getIfPresent(Attribute.class, "BLOCK_INTERACTION_RANGE")) // Renamed in 1.21.2
.orNull();

private boolean clientReady = false;
private boolean usingClientMod = false;
private boolean clientKeyPressed = false;
Expand Down Expand Up @@ -194,7 +187,7 @@ public void handleRequestVeinMine(@NotNull ServerboundRequestVeinMine message) {
return;
}

double reachDistance = getReachDistance(bukkitPlayer);
double reachDistance = AttributeUtil.getReachDistance(bukkitPlayer);
RayTraceResult rayTraceResult = bukkitPlayer.rayTraceBlocks(reachDistance);
if (rayTraceResult == null) {
this.player.sendMessage(new ClientboundVeinMineResults());
Expand Down Expand Up @@ -255,15 +248,4 @@ public void handleSelectPattern(@NotNull ServerboundSelectPattern message) {
this.player.setVeinMiningPattern(event.getNewPattern());
}

private double getReachDistance(Player player) {
if (ATTRIBUTE_BLOCK_INTERACTION_RANGE != null) {
AttributeInstance attribute = player.getAttribute(ATTRIBUTE_BLOCK_INTERACTION_RANGE);
if (attribute != null) {
return attribute.getValue();
}
}

return player.getGameMode() == GameMode.CREATIVE ? 4.5 : 4;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package wtf.choco.veinminer.util;

import com.google.common.base.Enums;

import org.bukkit.GameMode;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeInstance;
import org.bukkit.entity.Player;

public final class AttributeUtil {

private static final Attribute ATTRIBUTE_BLOCK_INTERACTION_RANGE = Enums.getIfPresent(Attribute.class, "PLAYER_BLOCK_INTERACTION_RANGE")
.or(Enums.getIfPresent(Attribute.class, "BLOCK_INTERACTION_RANGE")) // Renamed in 1.21.2
.orNull();

private AttributeUtil() { }

public static double getReachDistance(Player player) {
if (ATTRIBUTE_BLOCK_INTERACTION_RANGE != null) {
AttributeInstance attribute = player.getAttribute(ATTRIBUTE_BLOCK_INTERACTION_RANGE);
if (attribute != null) {
return attribute.getValue();
}
}

return player.getGameMode() == GameMode.CREATIVE ? 4.5 : 4;
}

}

0 comments on commit dd85bab

Please sign in to comment.