Skip to content

Commit ab4bc1f

Browse files
authored
Merge pull request #62 from PlaceholderAPI/001-gaby-add-absorption-placeholder
add %player_absorption%
2 parents 642c6fd + 1499333 commit ab4bc1f

3 files changed

Lines changed: 69 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
};
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.extendedclip.papi.expansion.player;
2+
3+
import com.google.common.primitives.Ints;
4+
import org.bukkit.Bukkit;
5+
6+
import java.util.regex.Matcher;
7+
import java.util.regex.Pattern;
8+
9+
/**
10+
* @author Matt (<a href="https://github.com/ipsk">@ipsk</a>)
11+
*/
12+
public final class VersionHelper {
13+
14+
private static final int VERSION = getCurrentVersion();
15+
16+
/**
17+
* <a href="https://docs.docdex.helpch.at/1.15/org/bukkit/entity/Damageable.html#getAbsorptionAmount--">
18+
* Damageable#getAbsorptionAmount
19+
* </a>
20+
*/
21+
public static final boolean HAS_ABSORPTION_METHODS = VERSION >= 1_15_0;
22+
23+
public static final boolean IS_1_17_OR_NEWER = VERSION >= 1_17_0;
24+
25+
private VersionHelper() { }
26+
27+
/**
28+
* Gets the current server version
29+
*
30+
* @return A protocol like number representing the version, for example 1.16.5 - 1165
31+
*/
32+
private static int getCurrentVersion() {
33+
// No need to cache since will only run once
34+
final Matcher matcher = Pattern.compile("(?<version>\\d+\\.\\d+)(?<patch>\\.\\d+)?").matcher(Bukkit.getBukkitVersion());
35+
final StringBuilder stringBuilder = new StringBuilder();
36+
37+
if (matcher.find()) {
38+
final String patch = matcher.group("patch");
39+
40+
stringBuilder
41+
.append(matcher.group("version").replace(".", ""))
42+
.append((patch == null) ? "0" : patch.replace(".", ""));
43+
}
44+
45+
final Integer version = Ints.tryParse(stringBuilder.toString());
46+
47+
// Should never fail
48+
if (version == null) {
49+
throw new IllegalArgumentException("Could not retrieve server version!");
50+
}
51+
52+
return version;
53+
}
54+
55+
}

0 commit comments

Comments
 (0)