Skip to content

Commit

Permalink
Support 1.20's block interaction reach attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
2008Choco committed Aug 27, 2024
1 parent 5b61abf commit 56f220b
Showing 1 changed file with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package wtf.choco.veinminer.player;

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

import java.util.ArrayList;
Expand All @@ -8,10 +9,14 @@
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;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.RayTraceResult;
import org.jetbrains.annotations.ApiStatus.Internal;
Expand Down Expand Up @@ -49,6 +54,10 @@
@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 @@ -175,7 +184,8 @@ public void handleToggleVeinMiner(@NotNull ServerboundToggleVeinMiner message) {

@Override
public void handleRequestVeinMine(@NotNull ServerboundRequestVeinMine message) {
ItemStack itemStack = player.getPlayer().getInventory().getItemInMainHand();
Player bukkitPlayer = player.getPlayer();
ItemStack itemStack = bukkitPlayer.getInventory().getItemInMainHand();
VeinMinerPlugin plugin = VeinMinerPlugin.getInstance();
VeinMinerToolCategory category = plugin.getToolCategoryRegistry().get(itemStack);

Expand All @@ -184,7 +194,8 @@ public void handleRequestVeinMine(@NotNull ServerboundRequestVeinMine message) {
return;
}

RayTraceResult rayTraceResult = player.getPlayer().rayTraceBlocks(6);
double reachDistance = getReachDistance(bukkitPlayer);
RayTraceResult rayTraceResult = bukkitPlayer.rayTraceBlocks(reachDistance);
if (rayTraceResult == null) {
this.player.sendMessage(new ClientboundVeinMineResults());
return;
Expand All @@ -200,7 +211,7 @@ public void handleRequestVeinMine(@NotNull ServerboundRequestVeinMine message) {

// Validate the client's target block against the server's client block. It should be within 2 blocks of the client's target
BlockPosition clientTargetBlock = message.getPosition();
if (clientTargetBlock.distanceSquared(targetBlock.getX(), targetBlock.getY(), targetBlock.getZ()) >= 4) { // TODO: Reach attribute
if (clientTargetBlock.distanceSquared(targetBlock.getX(), targetBlock.getY(), targetBlock.getZ()) >= 4) {
this.player.sendMessage(new ClientboundVeinMineResults());
return;
}
Expand Down Expand Up @@ -244,4 +255,15 @@ 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;
}

}

0 comments on commit 56f220b

Please sign in to comment.