From 71b33a296cf68f1a459c5809c477d7498333c6f8 Mon Sep 17 00:00:00 2001 From: Luck Date: Thu, 29 Sep 2016 20:38:03 +0100 Subject: [PATCH] Calculate Sponge defaults using context --- .../luckperms/api/sponge/ContextData.java | 76 ++++++++++++++++++- .../api/sponge/LuckPermsUserSubject.java | 68 ----------------- 2 files changed, 74 insertions(+), 70 deletions(-) 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 4ba47865..13551a90 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,16 +22,20 @@ 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 org.spongepowered.api.service.context.Context; import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.concurrent.ConcurrentHashMap; +import java.util.stream.Collectors; public class ContextData { private final LuckPermsUserSubject parent; @@ -48,16 +52,17 @@ public class ContextData { this.parent = parent; this.context = context; + 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)); if (service.getPlugin().getConfiguration().isApplyingWildcards()) { - processors.add(new LuckPermsUserSubject.SpongeWildcardProcessor(permissionCache)); + processors.add(new SpongeWildcardProcessor(permissionCache)); processors.add(new PermissionCalculator.WildcardProcessor(permissionCache)); } if (service.getPlugin().getConfiguration().isApplyingRegex()) { processors.add(new PermissionCalculator.RegexProcessor(permissionCache)); } - processors.add(new LuckPermsUserSubject.SpongeDefaultsProcessor(service)); + processors.add(new SpongeDefaultsProcessor(service, contexts)); calculator = new PermissionCalculator(service.getPlugin(), parent.getUser().getName(), service.getPlugin().getConfiguration().isDebugPermissionChecks(), processors); } @@ -75,4 +80,71 @@ public class ContextData { } } + @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/api/sponge/LuckPermsUserSubject.java b/sponge/src/main/java/me/lucko/luckperms/api/sponge/LuckPermsUserSubject.java index 1b904293..dd0bc371 100644 --- a/sponge/src/main/java/me/lucko/luckperms/api/sponge/LuckPermsUserSubject.java +++ b/sponge/src/main/java/me/lucko/luckperms/api/sponge/LuckPermsUserSubject.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.event.events.UserPermissionRefreshEvent; import me.lucko.luckperms.api.implementation.internal.UserLink; -import me.lucko.luckperms.calculators.PermissionProcessor; import me.lucko.luckperms.contexts.Contexts; import me.lucko.luckperms.users.User; import org.spongepowered.api.Sponge; @@ -156,70 +154,4 @@ public class LuckPermsUserSubject extends LuckPermsSubject { service.getPlugin().getContextManager().giveApplicableContext(player.get(), context); return context.entrySet().stream().map(e -> new Context(e.getKey(), e.getValue())).collect(Collectors.toSet()); } - - @AllArgsConstructor - 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 - static class SpongeDefaultsProcessor implements PermissionProcessor { - private final LuckPermsService service; - - @Override - public me.lucko.luckperms.api.Tristate hasPermission(String permission) { - Tristate t = service.getDefaults().getPermissionValue(Collections.emptySet(), permission); - if (t != Tristate.UNDEFINED) { - return me.lucko.luckperms.api.Tristate.fromBoolean(t.asBoolean()); - } else { - return me.lucko.luckperms.api.Tristate.UNDEFINED; - } - } - } }