Skip to content

Commit

Permalink
Fix ClassCastException in 1.21.3 due to Attribute becoming an interface
Browse files Browse the repository at this point in the history
  • Loading branch information
2008Choco committed Nov 9, 2024
1 parent 2932ac8 commit 9cf7344
Showing 1 changed file with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package wtf.choco.veinminer.util;

import com.google.common.base.Enums;

import org.bukkit.GameMode;
import org.bukkit.NamespacedKey;
import org.bukkit.Registry;
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 static final Attribute ATTRIBUTE_BLOCK_INTERACTION_RANGE = getAttribute(
NamespacedKey.minecraft("player.block_interaction_range"),
NamespacedKey.minecraft("block_interaction_range") // "player." prefix removed in 1.21.2
);

private AttributeUtil() { }

Expand All @@ -31,4 +32,15 @@ public static double getReachDistance(Player player) {
return reachDistance;
}

private static Attribute getAttribute(NamespacedKey... possibleKeys) {
for (NamespacedKey key : possibleKeys) {
Attribute attribute = Registry.ATTRIBUTE.get(key);
if (attribute != null) {
return attribute;
}
}

return null;
}

}

0 comments on commit 9cf7344

Please sign in to comment.