diff --git a/bukkit/src/main/java/me/lucko/luckperms/LPBukkitPlugin.java b/bukkit/src/main/java/me/lucko/luckperms/LPBukkitPlugin.java index 6c9bc3884..f64fa834c 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/LPBukkitPlugin.java +++ b/bukkit/src/main/java/me/lucko/luckperms/LPBukkitPlugin.java @@ -28,6 +28,7 @@ import me.lucko.luckperms.api.LuckPermsApi; import me.lucko.luckperms.api.PlatformType; import me.lucko.luckperms.api.implementation.ApiProvider; import me.lucko.luckperms.api.vault.VaultHook; +import me.lucko.luckperms.calculators.DefaultsProvider; import me.lucko.luckperms.commands.ConsecutiveExecutor; import me.lucko.luckperms.commands.Sender; import me.lucko.luckperms.config.LPConfiguration; @@ -38,7 +39,6 @@ import me.lucko.luckperms.contexts.WorldCalculator; import me.lucko.luckperms.core.UuidCache; import me.lucko.luckperms.data.Importer; import me.lucko.luckperms.groups.GroupManager; -import me.lucko.luckperms.inject.DefaultsProvider; import me.lucko.luckperms.runnables.ExpireTemporaryTask; import me.lucko.luckperms.runnables.UpdateTask; import me.lucko.luckperms.storage.Datastore; diff --git a/bukkit/src/main/java/me/lucko/luckperms/api/vault/VaultChatHook.java b/bukkit/src/main/java/me/lucko/luckperms/api/vault/VaultChatHook.java index 59519229c..f0faf3b08 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/api/vault/VaultChatHook.java +++ b/bukkit/src/main/java/me/lucko/luckperms/api/vault/VaultChatHook.java @@ -24,8 +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.api.vault.cache.ChatCache; +import me.lucko.luckperms.api.vault.cache.VaultUser; import me.lucko.luckperms.contexts.Contexts; import me.lucko.luckperms.core.PermissionHolder; import me.lucko.luckperms.exceptions.ObjectAlreadyHasException; @@ -139,14 +139,14 @@ public class VaultChatHook extends Chat { return defaultValue; } - VaultUserCache vaultUser = perms.getVaultUserManager().getUser(user.getUuid()); + VaultUser vaultUser = perms.getVaultUserManager().getUser(user.getUuid()); Map context = new HashMap<>(); context.put("server", perms.getServer()); if (world != null) { context.put("world", world); } - ChatData cd = vaultUser.processChatData(context); + ChatCache cd = vaultUser.processChatData(context); return unescapeCharacters(cd.getMeta().getOrDefault(node, defaultValue)); } @@ -157,14 +157,14 @@ public class VaultChatHook extends Chat { return ""; } - VaultUserCache vaultUser = perms.getVaultUserManager().getUser(user.getUuid()); + VaultUser vaultUser = perms.getVaultUserManager().getUser(user.getUuid()); Map context = new HashMap<>(); context.put("server", perms.getServer()); if (world != null) { context.put("world", world); } - ChatData cd = vaultUser.processChatData(context); + ChatCache cd = vaultUser.processChatData(context); return unescapeCharacters(prefix ? (cd.getPrefix() == null ? "" : cd.getPrefix()) : (cd.getSuffix() == null ? "" : cd.getSuffix())); } diff --git a/bukkit/src/main/java/me/lucko/luckperms/api/vault/VaultPermissionHook.java b/bukkit/src/main/java/me/lucko/luckperms/api/vault/VaultPermissionHook.java index 5fff61282..f822d7a40 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/api/vault/VaultPermissionHook.java +++ b/bukkit/src/main/java/me/lucko/luckperms/api/vault/VaultPermissionHook.java @@ -27,7 +27,7 @@ import lombok.NonNull; import lombok.Setter; import me.lucko.luckperms.LPBukkitPlugin; import me.lucko.luckperms.api.Node; -import me.lucko.luckperms.api.vault.cache.VaultUserCache; +import me.lucko.luckperms.api.vault.cache.VaultUser; import me.lucko.luckperms.api.vault.cache.VaultUserManager; import me.lucko.luckperms.contexts.Contexts; import me.lucko.luckperms.core.PermissionHolder; @@ -39,6 +39,10 @@ import net.milkbowl.vault.permission.Permission; import java.util.*; +/** + * The LuckPerms Vault Permission implementation + * Most lookups are cached. + */ public class VaultPermissionHook extends Permission implements Runnable { private final List tasks = new ArrayList<>(); @@ -57,21 +61,6 @@ public class VaultPermissionHook extends Permission implements Runnable { @Setter private boolean includeGlobal = true; - @Override - public String getName() { - return "LuckPerms"; - } - - @Override - public boolean isEnabled() { - return plugin.getDatastore().isAcceptingLogins(); - } - - @Override - public boolean hasSuperPermsCompat() { - return true; - } - public void setup() { vaultUserManager = new VaultUserManager(plugin, this); plugin.getServer().getScheduler().runTaskTimerAsynchronously(plugin, this, 1L, 1L); @@ -100,6 +89,21 @@ public class VaultPermissionHook extends Permission implements Runnable { toRun.forEach(Runnable::run); } + @Override + public String getName() { + return "LuckPerms"; + } + + @Override + public boolean isEnabled() { + return plugin.getDatastore().isAcceptingLogins(); + } + + @Override + public boolean hasSuperPermsCompat() { + return true; + } + private boolean objectHas(String world, Group group, String permission) { if (group == null) return false; @@ -168,7 +172,7 @@ public class VaultPermissionHook extends Permission implements Runnable { return false; } - VaultUserCache vaultUser = vaultUserManager.getUser(user.getUuid()); + VaultUser vaultUser = vaultUserManager.getUser(user.getUuid()); Map context = new HashMap<>(); context.put("server", server); if (world != null) { diff --git a/bukkit/src/main/java/me/lucko/luckperms/api/vault/cache/ChatData.java b/bukkit/src/main/java/me/lucko/luckperms/api/vault/cache/ChatCache.java similarity index 97% rename from bukkit/src/main/java/me/lucko/luckperms/api/vault/cache/ChatData.java rename to bukkit/src/main/java/me/lucko/luckperms/api/vault/cache/ChatCache.java index c1fc757e4..031bda09c 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/api/vault/cache/ChatData.java +++ b/bukkit/src/main/java/me/lucko/luckperms/api/vault/cache/ChatCache.java @@ -31,9 +31,7 @@ import java.util.concurrent.ConcurrentHashMap; @Getter @RequiredArgsConstructor -public class ChatData { - - @Getter +public class ChatCache { private final Map context; @Setter diff --git a/bukkit/src/main/java/me/lucko/luckperms/api/vault/cache/ContextData.java b/bukkit/src/main/java/me/lucko/luckperms/api/vault/cache/ContextCache.java similarity index 75% rename from bukkit/src/main/java/me/lucko/luckperms/api/vault/cache/ContextData.java rename to bukkit/src/main/java/me/lucko/luckperms/api/vault/cache/ContextCache.java index 3a1e6f292..c93d97e6f 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/api/vault/cache/ContextData.java +++ b/bukkit/src/main/java/me/lucko/luckperms/api/vault/cache/ContextCache.java @@ -26,10 +26,7 @@ import lombok.Getter; import lombok.NonNull; import me.lucko.luckperms.LuckPermsPlugin; import me.lucko.luckperms.api.Tristate; -import me.lucko.luckperms.calculators.PermissionCalculator; -import me.lucko.luckperms.calculators.PermissionProcessor; -import me.lucko.luckperms.inject.DefaultsProvider; -import me.lucko.luckperms.inject.LPPermissible; +import me.lucko.luckperms.calculators.*; import me.lucko.luckperms.users.BukkitUser; import me.lucko.luckperms.users.User; @@ -38,7 +35,7 @@ import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -public class ContextData { +public class ContextCache { @Getter private final Map context; @@ -48,19 +45,19 @@ public class ContextData { private final PermissionCalculator calculator; - public ContextData(User user, Map context, LuckPermsPlugin plugin, DefaultsProvider defaultsProvider) { + public ContextCache(User user, Map context, LuckPermsPlugin plugin, DefaultsProvider defaultsProvider) { this.context = context; List processors = new ArrayList<>(5); - processors.add(new PermissionCalculator.MapProcessor(permissionCache)); + processors.add(new MapProcessor(permissionCache)); if (plugin.getConfiguration().isApplyingWildcards()) { - processors.add(new PermissionCalculator.WildcardProcessor(permissionCache)); + processors.add(new WildcardProcessor(permissionCache)); } if (plugin.getConfiguration().isApplyingRegex()) { - processors.add(new PermissionCalculator.RegexProcessor(permissionCache)); + processors.add(new RegexProcessor(permissionCache)); } - processors.add(new LPPermissible.BukkitDefaultsProcessor(() -> ((BukkitUser) user).getLpPermissible().isOp(), defaultsProvider)); + processors.add(new DefaultsProcessor(() -> ((BukkitUser) user).getLpPermissible().isOp(), defaultsProvider)); calculator = new PermissionCalculator(plugin, user.getName(), plugin.getConfiguration().isDebugPermissionChecks(), processors); } diff --git a/bukkit/src/main/java/me/lucko/luckperms/api/vault/cache/VaultUserCache.java b/bukkit/src/main/java/me/lucko/luckperms/api/vault/cache/VaultUser.java similarity index 86% rename from bukkit/src/main/java/me/lucko/luckperms/api/vault/cache/VaultUserCache.java rename to bukkit/src/main/java/me/lucko/luckperms/api/vault/cache/VaultUser.java index dfa524884..c55311003 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/api/vault/cache/VaultUserCache.java +++ b/bukkit/src/main/java/me/lucko/luckperms/api/vault/cache/VaultUser.java @@ -36,7 +36,7 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @RequiredArgsConstructor -public class VaultUserCache { +public class VaultUser { private final LPBukkitPlugin plugin; private final VaultPermissionHook vault; @@ -44,30 +44,30 @@ public class VaultUserCache { private final User user; @Getter - private final Map, ContextData> contextData = new ConcurrentHashMap<>(); + private final Map, ContextCache> contextData = new ConcurrentHashMap<>(); @Getter - private final Map, ChatData> chatData = new ConcurrentHashMap<>(); + private final Map, ChatCache> chatData = new ConcurrentHashMap<>(); public boolean hasPermission(Map context, String permission) { - ContextData cd = contextData.computeIfAbsent(context, map -> calculatePermissions(map, false)); + ContextCache cd = contextData.computeIfAbsent(context, map -> calculatePermissions(map, false)); return cd.getPermissionValue(permission).asBoolean(); } - public ChatData processChatData(Map context) { + public ChatCache processChatData(Map context) { return chatData.computeIfAbsent(context, map -> calculateChat(map, false)); } - public ContextData calculatePermissions(Map context, boolean apply) { + public ContextCache calculatePermissions(Map context, boolean apply) { Map toApply = user.exportNodes( new Contexts(context, vault.isIncludeGlobal(), true, true, true, true), Collections.emptyList(), true ); - ContextData existing = contextData.get(context); + ContextCache existing = contextData.get(context); if (existing == null) { - existing = new ContextData(user, context, plugin, plugin.getDefaultsProvider()); + existing = new ContextCache(user, context, plugin, plugin.getDefaultsProvider()); if (apply) { contextData.put(context, existing); } @@ -94,10 +94,10 @@ public class VaultUserCache { return existing; } - public ChatData calculateChat(Map context, boolean apply) { - ChatData existing = chatData.get(context); + public ChatCache calculateChat(Map context, boolean apply) { + ChatCache existing = chatData.get(context); if (existing == null) { - existing = new ChatData(context); + existing = new ChatCache(context); if (apply) { chatData.put(context, existing); } diff --git a/bukkit/src/main/java/me/lucko/luckperms/api/vault/cache/VaultUserManager.java b/bukkit/src/main/java/me/lucko/luckperms/api/vault/cache/VaultUserManager.java index b56c21163..b43c1b807 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/api/vault/cache/VaultUserManager.java +++ b/bukkit/src/main/java/me/lucko/luckperms/api/vault/cache/VaultUserManager.java @@ -38,10 +38,10 @@ import java.util.concurrent.ConcurrentHashMap; public class VaultUserManager { private final LPBukkitPlugin plugin; private final VaultPermissionHook vault; - private final Map userCache = new ConcurrentHashMap<>(); + private final Map userCache = new ConcurrentHashMap<>(); public void setupUser(User user) { - VaultUserCache vaultUser = userCache.computeIfAbsent(user.getUuid(), uuid -> new VaultUserCache(plugin, vault, user)); + VaultUser vaultUser = userCache.computeIfAbsent(user.getUuid(), uuid -> new VaultUser(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()) { @@ -61,7 +61,7 @@ public class VaultUserManager { return userCache.containsKey(uuid); } - public VaultUserCache getUser(UUID uuid) { + public VaultUser getUser(UUID uuid) { return userCache.get(uuid); } diff --git a/bukkit/src/main/java/me/lucko/luckperms/calculators/AttachmentProcessor.java b/bukkit/src/main/java/me/lucko/luckperms/calculators/AttachmentProcessor.java new file mode 100644 index 000000000..093f463c6 --- /dev/null +++ b/bukkit/src/main/java/me/lucko/luckperms/calculators/AttachmentProcessor.java @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2016 Lucko (Luck) + * + * 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.calculators; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import me.lucko.luckperms.api.Tristate; +import org.bukkit.permissions.PermissionAttachmentInfo; + +import java.util.Map; + +@AllArgsConstructor +public class AttachmentProcessor implements PermissionProcessor { + + @Getter + private final Map map; + + @Override + public Tristate hasPermission(String permission) { + if (map.containsKey(permission)) { + return Tristate.fromBoolean(map.get(permission).getValue()); + } + + return Tristate.UNDEFINED; + } + +} diff --git a/bukkit/src/main/java/me/lucko/luckperms/calculators/DefaultsProcessor.java b/bukkit/src/main/java/me/lucko/luckperms/calculators/DefaultsProcessor.java new file mode 100644 index 000000000..f6414ff73 --- /dev/null +++ b/bukkit/src/main/java/me/lucko/luckperms/calculators/DefaultsProcessor.java @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2016 Lucko (Luck) + * + * 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.calculators; + +import lombok.AllArgsConstructor; +import me.lucko.luckperms.api.Tristate; +import org.bukkit.Bukkit; +import org.bukkit.permissions.Permission; + +import java.util.function.Supplier; + +@AllArgsConstructor +public class DefaultsProcessor implements PermissionProcessor { + private final Supplier isOp; + private final DefaultsProvider defaultsProvider; + + @Override + public Tristate hasPermission(String permission) { + Tristate t = defaultsProvider.hasDefault(permission, isOp.get()); + if (t != Tristate.UNDEFINED) { + return t; + } + + Permission defPerm = Bukkit.getServer().getPluginManager().getPermission(permission); + if (defPerm != null) { + return Tristate.fromBoolean(defPerm.getDefault().getValue(isOp.get())); + } else { + return Tristate.UNDEFINED; + } + } +} diff --git a/bukkit/src/main/java/me/lucko/luckperms/inject/DefaultsProvider.java b/bukkit/src/main/java/me/lucko/luckperms/calculators/DefaultsProvider.java similarity index 99% rename from bukkit/src/main/java/me/lucko/luckperms/inject/DefaultsProvider.java rename to bukkit/src/main/java/me/lucko/luckperms/calculators/DefaultsProvider.java index 41d829a8f..5eb49e37b 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/inject/DefaultsProvider.java +++ b/bukkit/src/main/java/me/lucko/luckperms/calculators/DefaultsProvider.java @@ -20,7 +20,7 @@ * SOFTWARE. */ -package me.lucko.luckperms.inject; +package me.lucko.luckperms.calculators; import lombok.AllArgsConstructor; import me.lucko.luckperms.api.Tristate; diff --git a/bukkit/src/main/java/me/lucko/luckperms/inject/LPPermissible.java b/bukkit/src/main/java/me/lucko/luckperms/inject/LPPermissible.java index e7236be9c..0e8f3501a 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/inject/LPPermissible.java +++ b/bukkit/src/main/java/me/lucko/luckperms/inject/LPPermissible.java @@ -22,13 +22,11 @@ package me.lucko.luckperms.inject; -import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NonNull; import me.lucko.luckperms.LuckPermsPlugin; import me.lucko.luckperms.api.Tristate; -import me.lucko.luckperms.calculators.PermissionCalculator; -import me.lucko.luckperms.calculators.PermissionProcessor; +import me.lucko.luckperms.calculators.*; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; import org.bukkit.permissions.*; @@ -36,7 +34,6 @@ import org.bukkit.plugin.Plugin; import java.util.*; import java.util.concurrent.ConcurrentHashMap; -import java.util.function.Supplier; import java.util.logging.Level; import java.util.stream.Collectors; @@ -60,15 +57,15 @@ public class LPPermissible extends PermissibleBase { this.parent = sender; List processors = new ArrayList<>(5); - processors.add(new PermissionCalculator.MapProcessor(luckPermsPermissions)); + processors.add(new MapProcessor(luckPermsPermissions)); processors.add(new AttachmentProcessor(attachmentPermissions)); if (plugin.getConfiguration().isApplyingWildcards()) { - processors.add(new PermissionCalculator.WildcardProcessor(luckPermsPermissions)); + processors.add(new WildcardProcessor(luckPermsPermissions)); } if (plugin.getConfiguration().isApplyingRegex()) { - processors.add(new PermissionCalculator.RegexProcessor(luckPermsPermissions)); + processors.add(new RegexProcessor(luckPermsPermissions)); } - processors.add(new BukkitDefaultsProcessor(parent::isOp, defaultsProvider)); + processors.add(new DefaultsProcessor(parent::isOp, defaultsProvider)); calculator = new PermissionCalculator(plugin, parent.getName(), plugin.getConfiguration().isDebugPermissionChecks(), processors); @@ -260,42 +257,4 @@ public class LPPermissible extends PermissibleBase { attachment.remove(); } } - - @AllArgsConstructor - private static class AttachmentProcessor implements PermissionProcessor { - - @Getter - private final Map map; - - @Override - public Tristate hasPermission(String permission) { - if (map.containsKey(permission)) { - return Tristate.fromBoolean(map.get(permission).getValue()); - } - - return Tristate.UNDEFINED; - } - - } - - @AllArgsConstructor - public static class BukkitDefaultsProcessor implements PermissionProcessor { - private final Supplier isOp; - private final DefaultsProvider defaultsProvider; - - @Override - public Tristate hasPermission(String permission) { - Tristate t = defaultsProvider.hasDefault(permission, isOp.get()); - if (t != Tristate.UNDEFINED) { - return t; - } - - Permission defPerm = Bukkit.getServer().getPluginManager().getPermission(permission); - if (defPerm != null) { - return Tristate.fromBoolean(defPerm.getDefault().getValue(isOp.get())); - } else { - return Tristate.UNDEFINED; - } - } - } } diff --git a/bungee/src/main/java/me/lucko/luckperms/BungeePlayerCache.java b/bungee/src/main/java/me/lucko/luckperms/BungeePlayerCache.java index 6c4d0a161..d83154f9b 100644 --- a/bungee/src/main/java/me/lucko/luckperms/BungeePlayerCache.java +++ b/bungee/src/main/java/me/lucko/luckperms/BungeePlayerCache.java @@ -23,8 +23,7 @@ package me.lucko.luckperms; import lombok.Getter; -import me.lucko.luckperms.calculators.PermissionCalculator; -import me.lucko.luckperms.calculators.PermissionProcessor; +import me.lucko.luckperms.calculators.*; import java.util.ArrayList; import java.util.List; @@ -40,12 +39,12 @@ public class BungeePlayerCache { public BungeePlayerCache(LuckPermsPlugin plugin, String name) { List processors = new ArrayList<>(3); - processors.add(new PermissionCalculator.MapProcessor(permissions)); + processors.add(new MapProcessor(permissions)); if (plugin.getConfiguration().isApplyingWildcards()) { - processors.add(new PermissionCalculator.WildcardProcessor(permissions)); + processors.add(new WildcardProcessor(permissions)); } if (plugin.getConfiguration().isApplyingRegex()) { - processors.add(new PermissionCalculator.RegexProcessor(permissions)); + processors.add(new RegexProcessor(permissions)); } calculator = new PermissionCalculator(plugin, name, plugin.getConfiguration().isDebugPermissionChecks(), processors); diff --git a/common/src/main/java/me/lucko/luckperms/calculators/MapProcessor.java b/common/src/main/java/me/lucko/luckperms/calculators/MapProcessor.java new file mode 100644 index 000000000..00357ef1e --- /dev/null +++ b/common/src/main/java/me/lucko/luckperms/calculators/MapProcessor.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2016 Lucko (Luck) + * + * 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.calculators; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import me.lucko.luckperms.api.Tristate; + +import java.util.Map; + +@AllArgsConstructor +public class MapProcessor implements PermissionProcessor { + + @Getter + private final Map map; + + @Override + public Tristate hasPermission(String permission) { + if (map.containsKey(permission)) { + return Tristate.fromBoolean(map.get(permission)); + } + + return Tristate.UNDEFINED; + } +} diff --git a/common/src/main/java/me/lucko/luckperms/calculators/PermissionCalculator.java b/common/src/main/java/me/lucko/luckperms/calculators/PermissionCalculator.java index 8f508219e..b29aa009c 100644 --- a/common/src/main/java/me/lucko/luckperms/calculators/PermissionCalculator.java +++ b/common/src/main/java/me/lucko/luckperms/calculators/PermissionCalculator.java @@ -22,17 +22,13 @@ package me.lucko.luckperms.calculators; -import lombok.AllArgsConstructor; -import lombok.Getter; import lombok.RequiredArgsConstructor; import me.lucko.luckperms.LuckPermsPlugin; import me.lucko.luckperms.api.Tristate; -import me.lucko.luckperms.constants.Patterns; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -import java.util.regex.Pattern; /** * Calculates and caches permissions @@ -72,96 +68,4 @@ public class PermissionCalculator { return Tristate.UNDEFINED; } - - @AllArgsConstructor - public static class MapProcessor implements PermissionProcessor { - - @Getter - private final Map map; - - @Override - public Tristate hasPermission(String permission) { - if (map.containsKey(permission)) { - return Tristate.fromBoolean(map.get(permission)); - } - - return Tristate.UNDEFINED; - } - } - - @AllArgsConstructor - public static class RegexProcessor implements PermissionProcessor { - - @Getter - private final Map map; - - @Override - public Tristate hasPermission(String permission) { - for (Map.Entry e : map.entrySet()) { - if (e.getKey().toLowerCase().startsWith("r=")) { - Pattern p = Patterns.compile(e.getKey().substring(2)); - if (p == null) { - continue; - } - - if (p.matcher(permission).matches()) { - return Tristate.fromBoolean(e.getValue()); - } - } - } - - return Tristate.UNDEFINED; - } - } - - @AllArgsConstructor - public static class WildcardProcessor implements PermissionProcessor { - - @Getter - private final Map map; - - @Override - public Tristate hasPermission(String permission) { - String node = permission; - - while (node.contains(".")) { - int endIndex = node.lastIndexOf('.'); - if (endIndex == -1) { - break; - } - - node = node.substring(0, endIndex); - if (!isEmpty(node)) { - if (map.containsKey(node + ".*")) { - return Tristate.fromBoolean(map.get(node + ".*")); - } - } - } - - if (map.containsKey("'*'")) { - return Tristate.fromBoolean(map.get("'*'")); - } - - if (map.containsKey("*")) { - return Tristate.fromBoolean(map.get("*")); - } - - return Tristate.UNDEFINED; - } - - private static boolean isEmpty(String s) { - if (s.equals("")) { - return true; - } - - char[] chars = s.toCharArray(); - for (char c : chars) { - if (c != '.') { - return false; - } - } - - return true; - } - } } diff --git a/common/src/main/java/me/lucko/luckperms/calculators/RegexProcessor.java b/common/src/main/java/me/lucko/luckperms/calculators/RegexProcessor.java new file mode 100644 index 000000000..72f8a9f75 --- /dev/null +++ b/common/src/main/java/me/lucko/luckperms/calculators/RegexProcessor.java @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2016 Lucko (Luck) + * + * 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.calculators; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import me.lucko.luckperms.api.Tristate; +import me.lucko.luckperms.constants.Patterns; + +import java.util.Map; +import java.util.regex.Pattern; + +@AllArgsConstructor +public class RegexProcessor implements PermissionProcessor { + + @Getter + private final Map map; + + @Override + public Tristate hasPermission(String permission) { + for (Map.Entry e : map.entrySet()) { + if (e.getKey().toLowerCase().startsWith("r=")) { + Pattern p = Patterns.compile(e.getKey().substring(2)); + if (p == null) { + continue; + } + + if (p.matcher(permission).matches()) { + return Tristate.fromBoolean(e.getValue()); + } + } + } + + return Tristate.UNDEFINED; + } +} diff --git a/common/src/main/java/me/lucko/luckperms/calculators/WildcardProcessor.java b/common/src/main/java/me/lucko/luckperms/calculators/WildcardProcessor.java new file mode 100644 index 000000000..ca23b5864 --- /dev/null +++ b/common/src/main/java/me/lucko/luckperms/calculators/WildcardProcessor.java @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2016 Lucko (Luck) + * + * 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.calculators; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import me.lucko.luckperms.api.Tristate; + +import java.util.Map; + +@AllArgsConstructor +public class WildcardProcessor implements PermissionProcessor { + + @Getter + private final Map map; + + @Override + public Tristate hasPermission(String permission) { + String node = permission; + + while (node.contains(".")) { + int endIndex = node.lastIndexOf('.'); + if (endIndex == -1) { + break; + } + + node = node.substring(0, endIndex); + if (!isEmpty(node)) { + if (map.containsKey(node + ".*")) { + return Tristate.fromBoolean(map.get(node + ".*")); + } + } + } + + if (map.containsKey("'*'")) { + return Tristate.fromBoolean(map.get("'*'")); + } + + if (map.containsKey("*")) { + return Tristate.fromBoolean(map.get("*")); + } + + return Tristate.UNDEFINED; + } + + private static boolean isEmpty(String s) { + if (s.equals("")) { + return true; + } + + char[] chars = s.toCharArray(); + for (char c : chars) { + if (c != '.') { + return false; + } + } + + return true; + } +} diff --git a/sponge/src/main/java/me/lucko/luckperms/api/sponge/ContextData.java b/sponge/src/main/java/me/lucko/luckperms/api/sponge/ContextData.java index 13551a908..b6030a435 100644 --- a/sponge/src/main/java/me/lucko/luckperms/api/sponge/ContextData.java +++ b/sponge/src/main/java/me/lucko/luckperms/api/sponge/ContextData.java @@ -22,12 +22,10 @@ package me.lucko.luckperms.api.sponge; -import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NonNull; import me.lucko.luckperms.api.Tristate; -import me.lucko.luckperms.calculators.PermissionCalculator; -import me.lucko.luckperms.calculators.PermissionProcessor; +import me.lucko.luckperms.calculators.*; import org.spongepowered.api.service.context.Context; import java.util.ArrayList; @@ -54,15 +52,15 @@ public class ContextData { Set contexts = context.entrySet().stream().map(e -> new Context(e.getKey(), e.getValue())).collect(Collectors.toSet()); List processors = new ArrayList<>(5); - processors.add(new PermissionCalculator.MapProcessor(permissionCache)); + processors.add(new MapProcessor(permissionCache)); if (service.getPlugin().getConfiguration().isApplyingWildcards()) { processors.add(new SpongeWildcardProcessor(permissionCache)); - processors.add(new PermissionCalculator.WildcardProcessor(permissionCache)); + processors.add(new WildcardProcessor(permissionCache)); } if (service.getPlugin().getConfiguration().isApplyingRegex()) { - processors.add(new PermissionCalculator.RegexProcessor(permissionCache)); + processors.add(new RegexProcessor(permissionCache)); } - processors.add(new SpongeDefaultsProcessor(service, contexts)); + processors.add(new DefaultsProcessor(service, contexts)); calculator = new PermissionCalculator(service.getPlugin(), parent.getUser().getName(), service.getPlugin().getConfiguration().isDebugPermissionChecks(), processors); } @@ -79,72 +77,4 @@ public class ContextData { return Tristate.UNDEFINED; } } - - @AllArgsConstructor - private static class SpongeWildcardProcessor implements PermissionProcessor { - - @Getter - private final Map map; - - @Override - public me.lucko.luckperms.api.Tristate hasPermission(String permission) { - String node = permission; - - while (node.contains(".")) { - int endIndex = node.lastIndexOf('.'); - if (endIndex == -1) { - break; - } - - node = node.substring(0, endIndex); - if (!isEmpty(node)) { - if (map.containsKey(node)) { - return me.lucko.luckperms.api.Tristate.fromBoolean(map.get(node)); - } - } - } - - if (map.containsKey("'*'")) { - return me.lucko.luckperms.api.Tristate.fromBoolean(map.get("'*'")); - } - - if (map.containsKey("*")) { - return me.lucko.luckperms.api.Tristate.fromBoolean(map.get("*")); - } - - return me.lucko.luckperms.api.Tristate.UNDEFINED; - } - - private static boolean isEmpty(String s) { - if (s.equals("")) { - return true; - } - - char[] chars = s.toCharArray(); - for (char c : chars) { - if (c != '.') { - return false; - } - } - - return true; - } - } - - @AllArgsConstructor - private static class SpongeDefaultsProcessor implements PermissionProcessor { - private final LuckPermsService service; - private final Set contexts; - - @Override - public me.lucko.luckperms.api.Tristate hasPermission(String permission) { - org.spongepowered.api.util.Tristate t = service.getDefaults().getPermissionValue(contexts, permission); - if (t != org.spongepowered.api.util.Tristate.UNDEFINED) { - return me.lucko.luckperms.api.Tristate.fromBoolean(t.asBoolean()); - } else { - return me.lucko.luckperms.api.Tristate.UNDEFINED; - } - } - } - } diff --git a/sponge/src/main/java/me/lucko/luckperms/calculators/DefaultsProcessor.java b/sponge/src/main/java/me/lucko/luckperms/calculators/DefaultsProcessor.java new file mode 100644 index 000000000..7a5c2d4d1 --- /dev/null +++ b/sponge/src/main/java/me/lucko/luckperms/calculators/DefaultsProcessor.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2016 Lucko (Luck) + * + * 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.calculators; + +import lombok.AllArgsConstructor; +import me.lucko.luckperms.api.sponge.LuckPermsService; +import org.spongepowered.api.service.context.Context; + +import java.util.Set; + +@AllArgsConstructor +public class DefaultsProcessor implements PermissionProcessor { + private final LuckPermsService service; + private final Set contexts; + + @Override + public me.lucko.luckperms.api.Tristate hasPermission(String permission) { + org.spongepowered.api.util.Tristate t = service.getDefaults().getPermissionValue(contexts, permission); + if (t != org.spongepowered.api.util.Tristate.UNDEFINED) { + return me.lucko.luckperms.api.Tristate.fromBoolean(t.asBoolean()); + } else { + return me.lucko.luckperms.api.Tristate.UNDEFINED; + } + } +} diff --git a/sponge/src/main/java/me/lucko/luckperms/calculators/SpongeWildcardProcessor.java b/sponge/src/main/java/me/lucko/luckperms/calculators/SpongeWildcardProcessor.java new file mode 100644 index 000000000..c07753c1e --- /dev/null +++ b/sponge/src/main/java/me/lucko/luckperms/calculators/SpongeWildcardProcessor.java @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2016 Lucko (Luck) + * + * 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.calculators; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +import java.util.Map; + +@AllArgsConstructor +public class SpongeWildcardProcessor implements PermissionProcessor { + + @Getter + private final Map map; + + @Override + public me.lucko.luckperms.api.Tristate hasPermission(String permission) { + String node = permission; + + while (node.contains(".")) { + int endIndex = node.lastIndexOf('.'); + if (endIndex == -1) { + break; + } + + node = node.substring(0, endIndex); + if (!isEmpty(node)) { + if (map.containsKey(node)) { + return me.lucko.luckperms.api.Tristate.fromBoolean(map.get(node)); + } + } + } + + if (map.containsKey("'*'")) { + return me.lucko.luckperms.api.Tristate.fromBoolean(map.get("'*'")); + } + + if (map.containsKey("*")) { + return me.lucko.luckperms.api.Tristate.fromBoolean(map.get("*")); + } + + return me.lucko.luckperms.api.Tristate.UNDEFINED; + } + + private static boolean isEmpty(String s) { + if (s.equals("")) { + return true; + } + + char[] chars = s.toCharArray(); + for (char c : chars) { + if (c != '.') { + return false; + } + } + + return true; + } +}