Skip to content

Commit 1499333

Browse files
committed
add %player_absorption% (closes #35)
1 parent 9b2c4b9 commit 1499333

2 files changed

Lines changed: 14 additions & 23 deletions

File tree

src/main/java/com/extendedclip/papi/expansion/player/PlayerExpansion.java

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ public String onRequest(OfflinePlayer player, String identifier) {
159159

160160
Player p = player.getPlayer();
161161

162+
// to get rid of IDE warnings
163+
if (p == null) {
164+
return "";
165+
}
166+
162167
if (identifier.startsWith("has_permission_")) {
163168
if (identifier.split("has_permission_").length > 1) {
164169
String perm = identifier.split("has_permission_")[1];
@@ -218,6 +223,13 @@ public String onRequest(OfflinePlayer player, String identifier) {
218223
}
219224

220225
switch (identifier) {
226+
case "absorption": {
227+
if (VersionHelper.HAS_ABSORPTION_METHODS) {
228+
return Integer.toString((int) p.getAbsorptionAmount());
229+
} else {
230+
return "-1";
231+
}
232+
}
221233
case "has_empty_slot":
222234
return bool(p.getInventory().firstEmpty() > -1);
223235
case "empty_slots":
@@ -462,24 +474,4 @@ private String retrievePing(final Player player, final boolean colored) {
462474
return ChatColor.translateAlternateColorCodes('&', ping > highValue ? high : ping > mediumValue ? medium : low) + ping;
463475
}
464476

465-
/**
466-
* Helper method to return the major version that the server is running.
467-
*
468-
* This is needed because in 1.17, NMS is no longer versioned.
469-
*
470-
* @return the major version of Minecraft the server is running
471-
*/
472-
public static int minecraftVersion() {
473-
try {
474-
final Matcher matcher = Pattern.compile("\\(MC: (\\d)\\.(\\d+)\\.?(\\d+?)?\\)").matcher(Bukkit.getVersion());
475-
if (matcher.find()) {
476-
return Integer.parseInt(matcher.toMatchResult().group(2), 10);
477-
} else {
478-
throw new IllegalArgumentException(String.format("No match found in '%s'", Bukkit.getVersion()));
479-
}
480-
} catch (final IllegalArgumentException ex) {
481-
throw new RuntimeException("Failed to determine Minecraft version", ex);
482-
}
483-
}
484-
485477
}

src/main/java/com/extendedclip/papi/expansion/player/PlayerUtil.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ public final class PlayerUtil {
4545
private static final SimpleDateFormat twelve = new SimpleDateFormat("h:mm aa", Locale.ENGLISH);
4646
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 };
4747

48-
private PlayerUtil() {
49-
}
48+
private PlayerUtil() { }
5049

5150
private static final Function<Player, Integer> PLAYER_GET_PING = new Function<Player, Integer>() {
5251

@@ -78,7 +77,7 @@ private void cacheReflection(final Player player) throws NoSuchFieldException, N
7877

7978
final Object entityPlayer = getHandle.invoke(player);
8079

81-
ping = entityPlayer.getClass().getDeclaredField(PlayerExpansion.minecraftVersion() >= 17 ? "e" : "ping");
80+
ping = entityPlayer.getClass().getDeclaredField(VersionHelper.IS_1_17_OR_NEWER ? "e" : "ping");
8281
ping.setAccessible(true);
8382
}
8483
};

0 commit comments

Comments
 (0)