Update placeholders to use the latest API version

This commit is contained in:
Luck 2016-09-11 15:36:19 +01:00
parent 116094ea96
commit 22426bab33
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
2 changed files with 14 additions and 79 deletions

View File

@ -30,8 +30,8 @@
<dependency> <dependency>
<groupId>me.lucko.luckperms</groupId> <groupId>me.lucko.luckperms</groupId>
<artifactId>luckperms-api</artifactId> <artifactId>luckperms-api</artifactId>
<version>2.5</version> <version>2.8</version>
<scope>compile</scope> <scope>provided</scope>
</dependency> </dependency>
<!-- BukkitAPI --> <!-- BukkitAPI -->
<dependency> <dependency>

View File

@ -25,15 +25,11 @@ package me.lucko.luckperms.api.placeholders;
import me.clip.placeholderapi.PlaceholderAPIPlugin; import me.clip.placeholderapi.PlaceholderAPIPlugin;
import me.clip.placeholderapi.expansion.PlaceholderExpansion; import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import me.clip.placeholderapi.util.TimeUtil; import me.clip.placeholderapi.util.TimeUtil;
import me.lucko.luckperms.api.LuckPermsApi; import me.lucko.luckperms.api.*;
import me.lucko.luckperms.api.Track;
import me.lucko.luckperms.api.User;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.regex.Pattern;
/* /*
* PlaceholderAPI Expansion for LuckPerms, implemented using the LuckPerms API. * PlaceholderAPI Expansion for LuckPerms, implemented using the LuckPerms API.
@ -75,7 +71,7 @@ public class LuckPermsPlaceholderExpansion extends PlaceholderExpansion {
@Override @Override
public String getVersion() { public String getVersion() {
return "2.5"; return "2.8";
} }
@Override @Override
@ -127,17 +123,11 @@ public class LuckPermsPlaceholderExpansion extends PlaceholderExpansion {
if (identifier.startsWith("expiry_time_") && identifier.length() > "expiry_time_".length()) { if (identifier.startsWith("expiry_time_") && identifier.length() > "expiry_time_".length()) {
String node = identifier.substring("expiry_time_".length()); String node = identifier.substring("expiry_time_".length());
long currentTime = System.currentTimeMillis() / 1000L; long currentTime = System.currentTimeMillis() / 1000L;
Map<Map.Entry<String, Boolean>, Long> temps = user.getTemporaryNodes(); for (Node n : user.getTemporaryPermissionNodes()) {
if (n.getPermission().equalsIgnoreCase(node)) {
for (Map.Entry<Map.Entry<String, Boolean>, Long> e : temps.entrySet()) { return TimeUtil.getTime((int) (n.getExpiryUnixTime() - currentTime));
if (!e.getKey().getKey().equalsIgnoreCase(node)) {
continue;
} }
return TimeUtil.getTime((int) (e.getValue() - currentTime));
} }
return ""; return "";
@ -145,42 +135,27 @@ public class LuckPermsPlaceholderExpansion extends PlaceholderExpansion {
if (identifier.startsWith("group_expiry_time_") && identifier.length() > "group_expiry_time_".length()) { if (identifier.startsWith("group_expiry_time_") && identifier.length() > "group_expiry_time_".length()) {
String node = "group." + identifier.substring("group_expiry_time_".length()); String node = "group." + identifier.substring("group_expiry_time_".length());
long currentTime = System.currentTimeMillis() / 1000L; long currentTime = System.currentTimeMillis() / 1000L;
Map<Map.Entry<String, Boolean>, Long> temps = user.getTemporaryNodes(); for (Node n : user.getTemporaryPermissionNodes()) {
if (n.getPermission().equalsIgnoreCase(node)) {
for (Map.Entry<Map.Entry<String, Boolean>, Long> e : temps.entrySet()) { return TimeUtil.getTime((int) (n.getExpiryUnixTime() - currentTime));
if (!e.getKey().getKey().equalsIgnoreCase(node)) {
continue;
} }
return TimeUtil.getTime((int) (e.getValue() - currentTime));
} }
return ""; return "";
} }
if (identifier.equalsIgnoreCase("prefix")) { if (identifier.equalsIgnoreCase("prefix")) {
return getChatMeta(PREFIX_PATTERN, user); return MetaUtils.getPrefix(user, null, null, true);
} }
if (identifier.equalsIgnoreCase("suffix")) { if (identifier.equalsIgnoreCase("suffix")) {
return getChatMeta(SUFFIX_PATTERN, user); return MetaUtils.getSuffix(user, null, null, true);
} }
if (identifier.startsWith("meta_") && identifier.length() > "meta_".length()) { if (identifier.startsWith("meta_") && identifier.length() > "meta_".length()) {
String node = "meta." + escapeCharacters(identifier.substring("meta_".length())) + "."; String node = identifier.substring("meta_".length());
Map<String, Boolean> nodes = user.getNodes(); return MetaUtils.getMeta(user, null, null, node, "", true);
for (Map.Entry<String, Boolean> e : nodes.entrySet()) {
if (!e.getValue()) continue;
if (!e.getKey().toLowerCase().startsWith(node)) continue;
String meta = e.getKey().substring(node.length());
return unescapeCharacters(meta);
}
return "";
} }
return null; return null;
@ -204,44 +179,4 @@ public class LuckPermsPlaceholderExpansion extends PlaceholderExpansion {
private static String formatBoolean(boolean b) { private static String formatBoolean(boolean b) {
return b ? PlaceholderAPIPlugin.booleanTrue() : PlaceholderAPIPlugin.booleanFalse(); return b ? PlaceholderAPIPlugin.booleanTrue() : PlaceholderAPIPlugin.booleanFalse();
} }
private static String escapeCharacters(String s) {
s = s.replace(".", "{SEP}");
s = s.replace("/", "{FSEP}");
s = s.replace("$", "{DSEP}");
return s;
}
private static String unescapeCharacters(String s) {
s = s.replace("{SEP}", ".");
s = s.replace("{FSEP}", "/");
s = s.replace("{DSEP}", "$");
return s;
}
private static final Pattern PREFIX_PATTERN = Pattern.compile("(?i)prefix\\.\\d+\\..*");
private static final Pattern SUFFIX_PATTERN = Pattern.compile("(?i)suffix\\.\\d+\\..*");
private static final Pattern DOT_PATTERN = Pattern.compile("\\.");
private static String getChatMeta(Pattern pattern, User user) {
int priority = 0;
String meta = null;
for (Map.Entry<String, Boolean> e : user.getLocalPermissions(null, null).entrySet()) {
if (!e.getValue()) continue;
if (pattern.matcher(e.getKey()).matches()) {
String[] parts = DOT_PATTERN.split(e.getKey(), 3);
int p = Integer.parseInt(parts[1]);
if (meta == null || p > priority) {
meta = parts[2];
priority = p;
}
}
}
return meta == null ? "" : unescapeCharacters(meta);
}
} }