diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/BukkitCalculatorFactory.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/BukkitCalculatorFactory.java index 4ec3623e..dc1f4469 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/BukkitCalculatorFactory.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/BukkitCalculatorFactory.java @@ -22,6 +22,7 @@ package me.lucko.luckperms.bukkit; +import com.google.common.collect.ImmutableList; import lombok.AllArgsConstructor; import me.lucko.luckperms.api.Contexts; import me.lucko.luckperms.bukkit.calculators.AttachmentProcessor; @@ -31,9 +32,6 @@ import me.lucko.luckperms.bukkit.inject.LPPermissible; import me.lucko.luckperms.common.calculators.*; import me.lucko.luckperms.common.users.User; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; import java.util.UUID; @AllArgsConstructor @@ -41,23 +39,23 @@ public class BukkitCalculatorFactory implements CalculatorFactory { private final LPBukkitPlugin plugin; @Override - public PermissionCalculator build(Contexts contexts, User user, Map map) { + public PermissionCalculator build(Contexts contexts, User user) { UUID uuid = plugin.getUuidCache().getExternalUUID(user.getUuid()); - List processors = new ArrayList<>(5); - processors.add(new MapProcessor(map)); + ImmutableList.Builder processors = ImmutableList.builder(); + processors.add(new MapProcessor()); processors.add(new AttachmentProcessor(() -> { LPPermissible permissible = Injector.getPermissible(uuid); return permissible == null ? null : permissible.getAttachmentPermissions(); })); if (plugin.getConfiguration().isApplyingWildcards()) { - processors.add(new WildcardProcessor(map)); + processors.add(new WildcardProcessor()); } if (plugin.getConfiguration().isApplyingRegex()) { - processors.add(new RegexProcessor(map)); + processors.add(new RegexProcessor()); } processors.add(new DefaultsProcessor(contexts.isOp(), plugin.getDefaultsProvider())); - return new PermissionCalculator(plugin, user.getName(), plugin.getConfiguration().isDebugPermissionChecks(), processors); + return new PermissionCalculator(plugin, user.getName(), plugin.getConfiguration().isDebugPermissionChecks(), processors.build()); } } diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/calculators/AttachmentProcessor.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/calculators/AttachmentProcessor.java index 4ef1b760..636821b5 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/calculators/AttachmentProcessor.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/calculators/AttachmentProcessor.java @@ -48,4 +48,9 @@ public class AttachmentProcessor implements PermissionProcessor { return pai == null ? Tristate.UNDEFINED : Tristate.fromBoolean(pai.getValue()); } + @Override + public void updateBacking(Map map) { + // Do nothing, this doesn't use the backing + } + } diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/calculators/DefaultsProcessor.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/calculators/DefaultsProcessor.java index 46923733..18bc9353 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/calculators/DefaultsProcessor.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/calculators/DefaultsProcessor.java @@ -28,6 +28,8 @@ import me.lucko.luckperms.common.calculators.PermissionProcessor; import org.bukkit.Bukkit; import org.bukkit.permissions.Permission; +import java.util.Map; + @AllArgsConstructor public class DefaultsProcessor implements PermissionProcessor { private final boolean isOp; @@ -43,4 +45,9 @@ public class DefaultsProcessor implements PermissionProcessor { Permission defPerm = Bukkit.getServer().getPluginManager().getPermission(permission); return defPerm == null ? Tristate.UNDEFINED : Tristate.fromBoolean(defPerm.getDefault().getValue(isOp)); } + + @Override + public void updateBacking(Map map) { + // Do nothing, this doesn't use the backing + } } diff --git a/bungee/src/main/java/me/lucko/luckperms/bungee/BungeeCalculatorFactory.java b/bungee/src/main/java/me/lucko/luckperms/bungee/BungeeCalculatorFactory.java index 2c6b9fd6..1d9c18fe 100644 --- a/bungee/src/main/java/me/lucko/luckperms/bungee/BungeeCalculatorFactory.java +++ b/bungee/src/main/java/me/lucko/luckperms/bungee/BungeeCalculatorFactory.java @@ -22,30 +22,27 @@ package me.lucko.luckperms.bungee; +import com.google.common.collect.ImmutableList; import lombok.AllArgsConstructor; import me.lucko.luckperms.api.Contexts; import me.lucko.luckperms.common.calculators.*; import me.lucko.luckperms.common.users.User; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - @AllArgsConstructor public class BungeeCalculatorFactory implements CalculatorFactory { private final LPBungeePlugin plugin; @Override - public PermissionCalculator build(Contexts contexts, User user, Map map) { - List processors = new ArrayList<>(3); - processors.add(new MapProcessor(map)); + public PermissionCalculator build(Contexts contexts, User user) { + ImmutableList.Builder processors = ImmutableList.builder(); + processors.add(new MapProcessor()); if (plugin.getConfiguration().isApplyingWildcards()) { - processors.add(new WildcardProcessor(map)); + processors.add(new WildcardProcessor()); } if (plugin.getConfiguration().isApplyingRegex()) { - processors.add(new RegexProcessor(map)); + processors.add(new RegexProcessor()); } - return new PermissionCalculator(plugin, user.getName(), plugin.getConfiguration().isDebugPermissionChecks(), processors); + return new PermissionCalculator(plugin, user.getName(), plugin.getConfiguration().isDebugPermissionChecks(), processors.build()); } } diff --git a/common/src/main/java/me/lucko/luckperms/common/caching/PermissionCache.java b/common/src/main/java/me/lucko/luckperms/common/caching/PermissionCache.java index c757d08a..c55725ab 100644 --- a/common/src/main/java/me/lucko/luckperms/common/caching/PermissionCache.java +++ b/common/src/main/java/me/lucko/luckperms/common/caching/PermissionCache.java @@ -53,7 +53,8 @@ public class PermissionCache implements PermissionData { public PermissionCache(Contexts contexts, User user, CalculatorFactory calculatorFactory) { permissions = new ConcurrentHashMap<>(); - calculator = calculatorFactory.build(contexts, user, permissions); + calculator = calculatorFactory.build(contexts, user); + calculator.updateBacking(permissions); // Initial setup. } @Override @@ -64,6 +65,7 @@ public class PermissionCache implements PermissionData { public void setPermissions(Map permissions) { this.permissions.clear(); this.permissions.putAll(permissions); + calculator.updateBacking(this.permissions); invalidateCache(); } diff --git a/common/src/main/java/me/lucko/luckperms/common/calculators/CalculatorFactory.java b/common/src/main/java/me/lucko/luckperms/common/calculators/CalculatorFactory.java index 5bd48a57..a3c29304 100644 --- a/common/src/main/java/me/lucko/luckperms/common/calculators/CalculatorFactory.java +++ b/common/src/main/java/me/lucko/luckperms/common/calculators/CalculatorFactory.java @@ -25,13 +25,11 @@ package me.lucko.luckperms.common.calculators; import me.lucko.luckperms.api.Contexts; import me.lucko.luckperms.common.users.User; -import java.util.Map; - /** * Creates a calculator instance given a set of contexts */ public interface CalculatorFactory { - PermissionCalculator build(Contexts contexts, User user, Map map); + PermissionCalculator build(Contexts contexts, User user); } diff --git a/common/src/main/java/me/lucko/luckperms/common/calculators/MapProcessor.java b/common/src/main/java/me/lucko/luckperms/common/calculators/MapProcessor.java index 085cd172..140539b4 100644 --- a/common/src/main/java/me/lucko/luckperms/common/calculators/MapProcessor.java +++ b/common/src/main/java/me/lucko/luckperms/common/calculators/MapProcessor.java @@ -22,21 +22,23 @@ package me.lucko.luckperms.common.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; + private Map map = null; @Override public Tristate hasPermission(String permission) { Boolean b = map.get(permission); return b == null ? Tristate.UNDEFINED : Tristate.fromBoolean(b); } + + @Override + public void updateBacking(Map map) { + if (this.map == null) { + this.map = map; + } + } } diff --git a/common/src/main/java/me/lucko/luckperms/common/calculators/PermissionCalculator.java b/common/src/main/java/me/lucko/luckperms/common/calculators/PermissionCalculator.java index 24a0d378..d2b21a25 100644 --- a/common/src/main/java/me/lucko/luckperms/common/calculators/PermissionCalculator.java +++ b/common/src/main/java/me/lucko/luckperms/common/calculators/PermissionCalculator.java @@ -30,6 +30,7 @@ import me.lucko.luckperms.api.Tristate; import me.lucko.luckperms.common.LuckPermsPlugin; import java.util.List; +import java.util.Map; /** * Calculates and caches permissions @@ -76,4 +77,10 @@ public class PermissionCalculator { return Tristate.UNDEFINED; } + + public synchronized void updateBacking(Map map) { + for (PermissionProcessor processor : processors) { + processor.updateBacking(map); + } + } } diff --git a/common/src/main/java/me/lucko/luckperms/common/calculators/PermissionProcessor.java b/common/src/main/java/me/lucko/luckperms/common/calculators/PermissionProcessor.java index d90886be..f6bc4865 100644 --- a/common/src/main/java/me/lucko/luckperms/common/calculators/PermissionProcessor.java +++ b/common/src/main/java/me/lucko/luckperms/common/calculators/PermissionProcessor.java @@ -24,8 +24,11 @@ package me.lucko.luckperms.common.calculators; import me.lucko.luckperms.api.Tristate; +import java.util.Map; + public interface PermissionProcessor { Tristate hasPermission(String permission); + void updateBacking(Map map); } diff --git a/common/src/main/java/me/lucko/luckperms/common/calculators/RegexProcessor.java b/common/src/main/java/me/lucko/luckperms/common/calculators/RegexProcessor.java index aa21c5f7..f70c2a19 100644 --- a/common/src/main/java/me/lucko/luckperms/common/calculators/RegexProcessor.java +++ b/common/src/main/java/me/lucko/luckperms/common/calculators/RegexProcessor.java @@ -22,28 +22,20 @@ package me.lucko.luckperms.common.calculators; -import lombok.AllArgsConstructor; -import lombok.Getter; import me.lucko.luckperms.api.Tristate; import me.lucko.luckperms.common.constants.Patterns; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import java.util.regex.Pattern; -@AllArgsConstructor public class RegexProcessor implements PermissionProcessor { - - @Getter - private final Map map; + private Map regexPermissions = new ConcurrentHashMap<>(); @Override public Tristate hasPermission(String permission) { - for (Map.Entry e : map.entrySet()) { - if (!e.getKey().startsWith("r=") && !e.getKey().startsWith("R=")) { - continue; - } - - Pattern p = Patterns.compile(e.getKey().substring(2)); + for (Map.Entry e : regexPermissions.entrySet()) { + Pattern p = Patterns.compile(e.getKey()); if (p == null) { continue; } @@ -55,4 +47,16 @@ public class RegexProcessor implements PermissionProcessor { return Tristate.UNDEFINED; } + + @Override + public void updateBacking(Map map) { + regexPermissions.clear(); + for (Map.Entry e : map.entrySet()) { + if (!e.getKey().startsWith("r=") && !e.getKey().startsWith("R=")) { + continue; + } + + regexPermissions.put(e.getKey().substring(2), e.getValue()); + } + } } diff --git a/common/src/main/java/me/lucko/luckperms/common/calculators/WildcardProcessor.java b/common/src/main/java/me/lucko/luckperms/common/calculators/WildcardProcessor.java index 12736ba1..ed99ba6b 100644 --- a/common/src/main/java/me/lucko/luckperms/common/calculators/WildcardProcessor.java +++ b/common/src/main/java/me/lucko/luckperms/common/calculators/WildcardProcessor.java @@ -22,17 +22,12 @@ package me.lucko.luckperms.common.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; + private Map map = null; @Override public Tristate hasPermission(String permission) { @@ -65,4 +60,11 @@ public class WildcardProcessor implements PermissionProcessor { return Tristate.UNDEFINED; } + + @Override + public void updateBacking(Map map) { + if (this.map == null) { + this.map = map; + } + } } diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeCalculatorFactory.java b/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeCalculatorFactory.java index ea6b3687..f77276d8 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeCalculatorFactory.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/SpongeCalculatorFactory.java @@ -22,6 +22,7 @@ package me.lucko.luckperms.sponge; +import com.google.common.collect.ImmutableList; import lombok.AllArgsConstructor; import me.lucko.luckperms.api.Contexts; import me.lucko.luckperms.common.calculators.*; @@ -30,27 +31,23 @@ import me.lucko.luckperms.sponge.calculators.DefaultsProcessor; import me.lucko.luckperms.sponge.calculators.SpongeWildcardProcessor; import me.lucko.luckperms.sponge.service.LuckPermsService; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - @AllArgsConstructor public class SpongeCalculatorFactory implements CalculatorFactory { private final LPSpongePlugin plugin; @Override - public PermissionCalculator build(Contexts contexts, User user, Map map) { - List processors = new ArrayList<>(5); - processors.add(new MapProcessor(map)); + public PermissionCalculator build(Contexts contexts, User user) { + ImmutableList.Builder processors = ImmutableList.builder(); + processors.add(new MapProcessor()); if (plugin.getConfiguration().isApplyingWildcards()) { - processors.add(new SpongeWildcardProcessor(map)); - processors.add(new WildcardProcessor(map)); + processors.add(new SpongeWildcardProcessor()); + processors.add(new WildcardProcessor()); } if (plugin.getConfiguration().isApplyingRegex()) { - processors.add(new RegexProcessor(map)); + processors.add(new RegexProcessor()); } processors.add(new DefaultsProcessor(plugin.getService(), LuckPermsService.convertContexts(contexts.getContexts()))); - return new PermissionCalculator(plugin, user.getName(), plugin.getConfiguration().isDebugPermissionChecks(), processors); + return new PermissionCalculator(plugin, user.getName(), plugin.getConfiguration().isDebugPermissionChecks(), processors.build()); } } diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/calculators/DefaultsProcessor.java b/sponge/src/main/java/me/lucko/luckperms/sponge/calculators/DefaultsProcessor.java index deb60ce9..205941bf 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/calculators/DefaultsProcessor.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/calculators/DefaultsProcessor.java @@ -28,6 +28,7 @@ import me.lucko.luckperms.sponge.service.LuckPermsService; import org.spongepowered.api.service.context.Context; import org.spongepowered.api.util.Tristate; +import java.util.Map; import java.util.Set; import static me.lucko.luckperms.sponge.service.LuckPermsService.convertTristate; @@ -51,4 +52,9 @@ public class DefaultsProcessor implements PermissionProcessor { return me.lucko.luckperms.api.Tristate.UNDEFINED; } + + @Override + public void updateBacking(Map map) { + // Do nothing, this doesn't use the backing + } } diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/calculators/SpongeWildcardProcessor.java b/sponge/src/main/java/me/lucko/luckperms/sponge/calculators/SpongeWildcardProcessor.java index 5213b3f0..4ebe9c22 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/calculators/SpongeWildcardProcessor.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/calculators/SpongeWildcardProcessor.java @@ -22,18 +22,13 @@ package me.lucko.luckperms.sponge.calculators; -import lombok.AllArgsConstructor; -import lombok.Getter; import me.lucko.luckperms.api.Tristate; import me.lucko.luckperms.common.calculators.PermissionProcessor; import java.util.Map; -@AllArgsConstructor public class SpongeWildcardProcessor implements PermissionProcessor { - - @Getter - private final Map map; + private Map map = null; @Override public Tristate hasPermission(String permission) { @@ -56,4 +51,11 @@ public class SpongeWildcardProcessor implements PermissionProcessor { return Tristate.UNDEFINED; } + + @Override + public void updateBacking(Map map) { + if (this.map == null) { + this.map = map; + } + } }