Merge pull request #62 from PlaceholderAPI/001-gaby-add-absorption-placeholder

add %player_absorption%
This commit is contained in:
Gabriel Dumitru authored and GitHub committed 2021-10-01 12:41:41 +03:00
commit ab4bc1fa2f
3 files changed
+69 -23

No files matched your search

@@ -159,6 +159,11 @@ public final class PlayerExpansion extends PlaceholderExpansion implements Confi
Player p = player.getPlayer();
// to get rid of IDE warnings
if (p == null) {
return "";
}
if (identifier.startsWith("has_permission_")) {
if (identifier.split("has_permission_").length > 1) {
String perm = identifier.split("has_permission_")[1];
@@ -218,6 +223,13 @@ public final class PlayerExpansion extends PlaceholderExpansion implements Confi
}
switch (identifier) {
case "absorption": {
if (VersionHelper.HAS_ABSORPTION_METHODS) {
return Integer.toString((int) p.getAbsorptionAmount());
} else {
return "-1";
}
}
case "has_empty_slot":
return bool(p.getInventory().firstEmpty() > -1);
case "empty_slots":
@@ -462,24 +474,4 @@ public final class PlayerExpansion extends PlaceholderExpansion implements Confi
return ChatColor.translateAlternateColorCodes('&', ping > highValue ? high : ping > mediumValue ? medium : low) + ping;
}
/**
* Helper method to return the major version that the server is running.
*
* This is needed because in 1.17, NMS is no longer versioned.
*
* @return the major version of Minecraft the server is running
*/
public static int minecraftVersion() {
try {
final Matcher matcher = Pattern.compile("\\(MC: (\\d)\\.(\\d+)\\.?(\\d+?)?\\)").matcher(Bukkit.getVersion());
if (matcher.find()) {
return Integer.parseInt(matcher.toMatchResult().group(2), 10);
} else {
throw new IllegalArgumentException(String.format("No match found in '%s'", Bukkit.getVersion()));
}
} catch (final IllegalArgumentException ex) {
throw new RuntimeException("Failed to determine Minecraft version", ex);
}
}
}
@@ -45,8 +45,7 @@ public final class PlayerUtil {
private static final SimpleDateFormat twelve = new SimpleDateFormat("h:mm aa", Locale.ENGLISH);
private static final BlockFace[] radial = { BlockFace.NORTH, BlockFace.NORTH_EAST, BlockFace.EAST, BlockFace.SOUTH_EAST, BlockFace.SOUTH, BlockFace.SOUTH_WEST, BlockFace.WEST, BlockFace.NORTH_WEST };
private PlayerUtil() {
}
private PlayerUtil() { }
private static final Function<Player, Integer> PLAYER_GET_PING = new Function<Player, Integer>() {
@@ -78,7 +77,7 @@ public final class PlayerUtil {
final Object entityPlayer = getHandle.invoke(player);
ping = entityPlayer.getClass().getDeclaredField(PlayerExpansion.minecraftVersion() >= 17 ? "e" : "ping");
ping = entityPlayer.getClass().getDeclaredField(VersionHelper.IS_1_17_OR_NEWER ? "e" : "ping");
ping.setAccessible(true);
}
};
@@ -0,0 +1,55 @@
package com.extendedclip.papi.expansion.player;
import com.google.common.primitives.Ints;
import org.bukkit.Bukkit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author Matt (<a href="https://github.com/ipsk">@ipsk</a>)
*/
public final class VersionHelper {
private static final int VERSION = getCurrentVersion();
/**
* <a href="https://docs.docdex.helpch.at/1.15/org/bukkit/entity/Damageable.html#getAbsorptionAmount--">
* Damageable#getAbsorptionAmount
* </a>
*/
public static final boolean HAS_ABSORPTION_METHODS = VERSION >= 1_15_0;
public static final boolean IS_1_17_OR_NEWER = VERSION >= 1_17_0;
private VersionHelper() { }
/**
* Gets the current server version
*
* @return A protocol like number representing the version, for example 1.16.5 - 1165
*/
private static int getCurrentVersion() {
// No need to cache since will only run once
final Matcher matcher = Pattern.compile("(?<version>\\d+\\.\\d+)(?<patch>\\.\\d+)?").matcher(Bukkit.getBukkitVersion());
final StringBuilder stringBuilder = new StringBuilder();
if (matcher.find()) {
final String patch = matcher.group("patch");
stringBuilder
.append(matcher.group("version").replace(".", ""))
.append((patch == null) ? "0" : patch.replace(".", ""));
}
final Integer version = Ints.tryParse(stringBuilder.toString());
// Should never fail
if (version == null) {
throw new IllegalArgumentException("Could not retrieve server version!");
}
return version;
}
}