mirror of
https://github.com/LuckPerms/LuckPerms.git
synced 2024-11-28 05:35:26 +01:00
Cache & fix (and maybe break) Vault Chat implementation
This commit is contained in:
parent
a32ab4bf7f
commit
1fc0d77971
@ -24,6 +24,8 @@ package me.lucko.luckperms.api.vault;
|
||||
|
||||
import lombok.NonNull;
|
||||
import me.lucko.luckperms.api.Node;
|
||||
import me.lucko.luckperms.api.vault.cache.ChatData;
|
||||
import me.lucko.luckperms.api.vault.cache.VaultUserCache;
|
||||
import me.lucko.luckperms.contexts.Contexts;
|
||||
import me.lucko.luckperms.core.PermissionHolder;
|
||||
import me.lucko.luckperms.exceptions.ObjectAlreadyHasException;
|
||||
@ -31,6 +33,7 @@ import me.lucko.luckperms.groups.Group;
|
||||
import me.lucko.luckperms.users.User;
|
||||
import net.milkbowl.vault.chat.Chat;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
@ -98,12 +101,70 @@ public class VaultChatHook extends Chat {
|
||||
perms.objectSave(holder);
|
||||
}
|
||||
|
||||
private String getMeta(PermissionHolder holder, String world, String node, String defaultValue) {
|
||||
if (holder == null) return defaultValue;
|
||||
private void setChatMeta(boolean prefix, PermissionHolder holder, String value, String world) {
|
||||
if (holder == null) return;
|
||||
if (value.equals("")) return;
|
||||
|
||||
Node.Builder node = new me.lucko.luckperms.core.Node.Builder(prefix ? "prefix" : "suffix" + ".1000." + escapeCharacters(value));
|
||||
node.setValue(true);
|
||||
if (!perms.getServer().equalsIgnoreCase("global")) {
|
||||
node.setServer(perms.getServer());
|
||||
}
|
||||
|
||||
if (world != null && !world.equals("")) {
|
||||
node.setServer(perms.getServer()).setWorld(world);
|
||||
}
|
||||
|
||||
try {
|
||||
holder.setPermission(node.build());
|
||||
} catch (ObjectAlreadyHasException ignored) {}
|
||||
|
||||
perms.objectSave(holder);
|
||||
}
|
||||
|
||||
private String getUserMeta(User user, String world, String node, String defaultValue) {
|
||||
if (user == null) return defaultValue;
|
||||
node = escapeCharacters(node);
|
||||
|
||||
if (!perms.getVaultUserManager().containsUser(user.getUuid())) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
VaultUserCache vaultUser = perms.getVaultUserManager().getUser(user.getUuid());
|
||||
Map<String, String> context = new HashMap<>();
|
||||
context.put("server", perms.getServer());
|
||||
if (world != null) {
|
||||
context.put("world", world);
|
||||
}
|
||||
|
||||
ChatData cd = vaultUser.processChatData(context);
|
||||
return unescapeCharacters(cd.getMeta().getOrDefault(node, defaultValue));
|
||||
}
|
||||
|
||||
private String getUserChatMeta(boolean prefix, User user, String world) {
|
||||
if (user == null) return "";
|
||||
|
||||
if (!perms.getVaultUserManager().containsUser(user.getUuid())) {
|
||||
return "";
|
||||
}
|
||||
|
||||
VaultUserCache vaultUser = perms.getVaultUserManager().getUser(user.getUuid());
|
||||
Map<String, String> context = new HashMap<>();
|
||||
context.put("server", perms.getServer());
|
||||
if (world != null) {
|
||||
context.put("world", world);
|
||||
}
|
||||
|
||||
ChatData cd = vaultUser.processChatData(context);
|
||||
return unescapeCharacters(prefix ? (cd.getPrefix() == null ? "" : cd.getPrefix()) : (cd.getSuffix() == null ? "" : cd.getSuffix()));
|
||||
}
|
||||
|
||||
private String getGroupMeta(Group group, String world, String node, String defaultValue) {
|
||||
if (group == null) return defaultValue;
|
||||
if (node.equals("")) return defaultValue;
|
||||
node = escapeCharacters(node);
|
||||
|
||||
for (Node n : holder.getPermissions(true)) {
|
||||
for (Node n : group.getPermissions(true)) {
|
||||
if (!n.getValue()) {
|
||||
continue;
|
||||
}
|
||||
@ -129,34 +190,19 @@ public class VaultChatHook extends Chat {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
private void setChatMeta(boolean prefix, PermissionHolder holder, String value, String world) {
|
||||
if (holder == null) return;
|
||||
if (value.equals("")) return;
|
||||
|
||||
Node.Builder node = new me.lucko.luckperms.core.Node.Builder(prefix ? "prefix" : "suffix" + ".1000." + escapeCharacters(value));
|
||||
node.setValue(true);
|
||||
if (!perms.getServer().equalsIgnoreCase("global")) {
|
||||
node.setServer(perms.getServer());
|
||||
}
|
||||
|
||||
if (world != null && !world.equals("")) {
|
||||
node.setServer(perms.getServer()).setWorld(world);
|
||||
}
|
||||
|
||||
try {
|
||||
holder.setPermission(node.build());
|
||||
} catch (ObjectAlreadyHasException ignored) {}
|
||||
|
||||
perms.objectSave(holder);
|
||||
}
|
||||
|
||||
private String getChatMeta(boolean prefix, PermissionHolder holder, String world) {
|
||||
if (holder == null) return "";
|
||||
private String getGroupChatMeta(boolean prefix, Group group, String world) {
|
||||
if (group == null) return "";
|
||||
|
||||
int priority = Integer.MIN_VALUE;
|
||||
String meta = null;
|
||||
|
||||
for (Node n : holder.getAllNodes(null, Contexts.allowAll())) {
|
||||
Map<String, String> context = new HashMap<>();
|
||||
context.put("server", perms.getServer());
|
||||
if (world != null) {
|
||||
context.put("world", world);
|
||||
}
|
||||
|
||||
for (Node n : group.getAllNodes(null, new Contexts(context, perms.isIncludeGlobal(), true, true, true, true))) {
|
||||
if (!n.getValue()) {
|
||||
continue;
|
||||
}
|
||||
@ -185,7 +231,7 @@ public class VaultChatHook extends Chat {
|
||||
|
||||
public String getPlayerPrefix(String world, @NonNull String player) {
|
||||
final User user = perms.getPlugin().getUserManager().get(player);
|
||||
return getChatMeta(true, user, world);
|
||||
return getUserChatMeta(true, user, world);
|
||||
}
|
||||
|
||||
public void setPlayerPrefix(String world, @NonNull String player, @NonNull String prefix) {
|
||||
@ -195,7 +241,7 @@ public class VaultChatHook extends Chat {
|
||||
|
||||
public String getPlayerSuffix(String world, @NonNull String player) {
|
||||
final User user = perms.getPlugin().getUserManager().get(player);
|
||||
return getChatMeta(false, user, world);
|
||||
return getUserChatMeta(false, user, world);
|
||||
}
|
||||
|
||||
public void setPlayerSuffix(String world, @NonNull String player, @NonNull String suffix) {
|
||||
@ -205,7 +251,7 @@ public class VaultChatHook extends Chat {
|
||||
|
||||
public String getGroupPrefix(String world, @NonNull String group) {
|
||||
final Group g = perms.getPlugin().getGroupManager().get(group);
|
||||
return getChatMeta(false, g, world);
|
||||
return getGroupChatMeta(false, g, world);
|
||||
}
|
||||
|
||||
public void setGroupPrefix(String world, @NonNull String group, @NonNull String prefix) {
|
||||
@ -215,7 +261,7 @@ public class VaultChatHook extends Chat {
|
||||
|
||||
public String getGroupSuffix(String world, @NonNull String group) {
|
||||
final Group g = perms.getPlugin().getGroupManager().get(group);
|
||||
return getChatMeta(false, g, world);
|
||||
return getGroupChatMeta(false, g, world);
|
||||
}
|
||||
|
||||
public void setGroupSuffix(String world, @NonNull String group, @NonNull String suffix) {
|
||||
@ -226,7 +272,7 @@ public class VaultChatHook extends Chat {
|
||||
public int getPlayerInfoInteger(String world, @NonNull String player, @NonNull String node, int defaultValue) {
|
||||
final User user = perms.getPlugin().getUserManager().get(player);
|
||||
try {
|
||||
return Integer.parseInt(getMeta(user, world, node, String.valueOf(defaultValue)));
|
||||
return Integer.parseInt(getUserMeta(user, world, node, String.valueOf(defaultValue)));
|
||||
} catch (NumberFormatException e) {
|
||||
return defaultValue;
|
||||
}
|
||||
@ -240,7 +286,7 @@ public class VaultChatHook extends Chat {
|
||||
public int getGroupInfoInteger(String world, @NonNull String group, @NonNull String node, int defaultValue) {
|
||||
final Group g = perms.getPlugin().getGroupManager().get(group);
|
||||
try {
|
||||
return Integer.parseInt(getMeta(g, world, node, String.valueOf(defaultValue)));
|
||||
return Integer.parseInt(getGroupMeta(g, world, node, String.valueOf(defaultValue)));
|
||||
} catch (NumberFormatException e) {
|
||||
return defaultValue;
|
||||
}
|
||||
@ -254,7 +300,7 @@ public class VaultChatHook extends Chat {
|
||||
public double getPlayerInfoDouble(String world, @NonNull String player, @NonNull String node, double defaultValue) {
|
||||
final User user = perms.getPlugin().getUserManager().get(player);
|
||||
try {
|
||||
return Double.parseDouble(getMeta(user, world, node, String.valueOf(defaultValue)));
|
||||
return Double.parseDouble(getUserMeta(user, world, node, String.valueOf(defaultValue)));
|
||||
} catch (NumberFormatException e) {
|
||||
return defaultValue;
|
||||
}
|
||||
@ -268,7 +314,7 @@ public class VaultChatHook extends Chat {
|
||||
public double getGroupInfoDouble(String world, @NonNull String group, @NonNull String node, double defaultValue) {
|
||||
final Group g = perms.getPlugin().getGroupManager().get(group);
|
||||
try {
|
||||
return Double.parseDouble(getMeta(g, world, node, String.valueOf(defaultValue)));
|
||||
return Double.parseDouble(getGroupMeta(g, world, node, String.valueOf(defaultValue)));
|
||||
} catch (NumberFormatException e) {
|
||||
return defaultValue;
|
||||
}
|
||||
@ -281,7 +327,7 @@ public class VaultChatHook extends Chat {
|
||||
|
||||
public boolean getPlayerInfoBoolean(String world, @NonNull String player, @NonNull String node, boolean defaultValue) {
|
||||
final User user = perms.getPlugin().getUserManager().get(player);
|
||||
String s = getMeta(user, world, node, String.valueOf(defaultValue));
|
||||
String s = getUserMeta(user, world, node, String.valueOf(defaultValue));
|
||||
if (!s.equalsIgnoreCase("true") && !s.equalsIgnoreCase("false")) {
|
||||
return defaultValue;
|
||||
}
|
||||
@ -295,7 +341,7 @@ public class VaultChatHook extends Chat {
|
||||
|
||||
public boolean getGroupInfoBoolean(String world, @NonNull String group, @NonNull String node, boolean defaultValue) {
|
||||
final Group g = perms.getPlugin().getGroupManager().get(group);
|
||||
String s = getMeta(g, world, node, String.valueOf(defaultValue));
|
||||
String s = getGroupMeta(g, world, node, String.valueOf(defaultValue));
|
||||
if (!s.equalsIgnoreCase("true") && !s.equalsIgnoreCase("false")) {
|
||||
return defaultValue;
|
||||
}
|
||||
@ -309,7 +355,7 @@ public class VaultChatHook extends Chat {
|
||||
|
||||
public String getPlayerInfoString(String world, @NonNull String player, @NonNull String node, String defaultValue) {
|
||||
final User user = perms.getPlugin().getUserManager().get(player);
|
||||
return getMeta(user, world, node, defaultValue);
|
||||
return getUserMeta(user, world, node, defaultValue);
|
||||
}
|
||||
|
||||
public void setPlayerInfoString(String world, @NonNull String player, @NonNull String node, String value) {
|
||||
@ -319,7 +365,7 @@ public class VaultChatHook extends Chat {
|
||||
|
||||
public String getGroupInfoString(String world, @NonNull String group, @NonNull String node, String defaultValue) {
|
||||
final Group g = perms.getPlugin().getGroupManager().get(group);
|
||||
return getMeta(g, world, node, defaultValue);
|
||||
return getGroupMeta(g, world, node, defaultValue);
|
||||
}
|
||||
|
||||
public void setGroupInfoString(String world, @NonNull String group, @NonNull String node, String value) {
|
||||
|
53
bukkit/src/main/java/me/lucko/luckperms/api/vault/cache/ChatData.java
vendored
Normal file
53
bukkit/src/main/java/me/lucko/luckperms/api/vault/cache/ChatData.java
vendored
Normal file
@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright (c) 2016 Lucko (Luck) <luck@lucko.me>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package me.lucko.luckperms.api.vault.cache;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@Getter
|
||||
@RequiredArgsConstructor
|
||||
public class ChatData {
|
||||
|
||||
@Getter
|
||||
private final Map<String, String> context;
|
||||
|
||||
@Setter
|
||||
private String prefix = null;
|
||||
|
||||
@Setter
|
||||
private String suffix = null;
|
||||
|
||||
private Map<String, String> meta = new ConcurrentHashMap<>();
|
||||
|
||||
public void invalidateCache() {
|
||||
prefix = null;
|
||||
suffix = null;
|
||||
meta.clear();
|
||||
}
|
||||
|
||||
}
|
@ -25,11 +25,13 @@ package me.lucko.luckperms.api.vault.cache;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.lucko.luckperms.LPBukkitPlugin;
|
||||
import me.lucko.luckperms.api.Node;
|
||||
import me.lucko.luckperms.api.vault.VaultPermissionHook;
|
||||
import me.lucko.luckperms.contexts.Contexts;
|
||||
import me.lucko.luckperms.users.User;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@ -44,11 +46,18 @@ public class VaultUserCache {
|
||||
@Getter
|
||||
private final Map<Map<String, String>, ContextData> contextData = new ConcurrentHashMap<>();
|
||||
|
||||
@Getter
|
||||
private final Map<Map<String, String>, ChatData> chatData = new ConcurrentHashMap<>();
|
||||
|
||||
public boolean hasPermission(Map<String, String> context, String permission) {
|
||||
ContextData cd = contextData.computeIfAbsent(context, map -> calculatePermissions(map, false));
|
||||
return cd.getPermissionValue(permission).asBoolean();
|
||||
}
|
||||
|
||||
public ChatData processChatData(Map<String, String> context) {
|
||||
return chatData.computeIfAbsent(context, map -> calculateChat(map, false));
|
||||
}
|
||||
|
||||
public ContextData calculatePermissions(Map<String, String> context, boolean apply) {
|
||||
Map<String, Boolean> toApply = user.exportNodes(
|
||||
new Contexts(context, vault.isIncludeGlobal(), true, true, true, true),
|
||||
@ -84,4 +93,89 @@ public class VaultUserCache {
|
||||
existing.getPermissionCache().putAll(toApply);
|
||||
return existing;
|
||||
}
|
||||
|
||||
public ChatData calculateChat(Map<String, String> context, boolean apply) {
|
||||
ChatData existing = chatData.get(context);
|
||||
if (existing == null) {
|
||||
existing = new ChatData(context);
|
||||
if (apply) {
|
||||
chatData.put(context, existing);
|
||||
}
|
||||
}
|
||||
|
||||
Map<String, String> contexts = new HashMap<>(context);
|
||||
String server = contexts.get("server");
|
||||
String world = contexts.get("world");
|
||||
contexts.remove("server");
|
||||
contexts.remove("world");
|
||||
|
||||
existing.invalidateCache();
|
||||
|
||||
// Load meta
|
||||
for (Node n : user.getPermissions(true)) {
|
||||
if (!n.getValue()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!n.isMeta()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!n.shouldApplyOnServer(server, vault.isIncludeGlobal(), false)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!n.shouldApplyOnWorld(world, true, false)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!n.shouldApplyWithContext(contexts, false)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Map.Entry<String, String> meta = n.getMeta();
|
||||
existing.getMeta().put(meta.getKey(), meta.getValue());
|
||||
}
|
||||
|
||||
// Load prefixes & suffixes
|
||||
|
||||
int prefixPriority = Integer.MIN_VALUE;
|
||||
int suffixPriority = Integer.MIN_VALUE;
|
||||
|
||||
for (Node n : user.getAllNodes(null, new Contexts(context, vault.isIncludeGlobal(), true, true, true, true))) {
|
||||
if (!n.getValue()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!n.isPrefix() && !n.isSuffix()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!n.shouldApplyOnServer(server, vault.isIncludeGlobal(), false)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!n.shouldApplyOnWorld(world, true, false)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!n.shouldApplyWithContext(contexts, false)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (n.isPrefix()) {
|
||||
Map.Entry<Integer, String> value = n.getPrefix();
|
||||
if (value.getKey() > prefixPriority) {
|
||||
existing.setPrefix(value.getValue());
|
||||
}
|
||||
} else {
|
||||
Map.Entry<Integer, String> value = n.getSuffix();
|
||||
if (value.getKey() > suffixPriority) {
|
||||
existing.setSuffix(value.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return existing;
|
||||
}
|
||||
}
|
||||
|
@ -43,11 +43,13 @@ public class VaultUserManager {
|
||||
public void setupUser(User user) {
|
||||
VaultUserCache vaultUser = userCache.computeIfAbsent(user.getUuid(), uuid -> new VaultUserCache(plugin, vault, user));
|
||||
vaultUser.calculatePermissions(Collections.singletonMap("server", vault.getServer()), true);
|
||||
vaultUser.calculateChat(Collections.singletonMap("server", vault.getServer()), true);
|
||||
for (World world : plugin.getServer().getWorlds()) {
|
||||
Map<String, String> context = new HashMap<>();
|
||||
context.put("server", vault.getServer());
|
||||
context.put("world", world.getName());
|
||||
vaultUser.calculatePermissions(context, true);
|
||||
vaultUser.calculateChat(context, true);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user