From a0f8ba9afb0f56813ee2a4e04a09d217ead2054c Mon Sep 17 00:00:00 2001 From: Luck Date: Thu, 5 Jan 2017 18:27:58 +0000 Subject: [PATCH] Fix NPE in LuckPermsSubjectData - closes #115 --- .../sponge/service/LuckPermsSubjectData.java | 58 ++++++++----------- 1 file changed, 25 insertions(+), 33 deletions(-) diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsSubjectData.java b/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsSubjectData.java index 1f6c6afc..ca619f70 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsSubjectData.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/service/LuckPermsSubjectData.java @@ -86,7 +86,7 @@ public class LuckPermsSubjectData implements LPSubjectData { @Override public Map> getPermissions() { try (Timing ignored = service.getPlugin().getTimings().time(LPTiming.LP_SUBJECT_GET_PERMISSIONS)) { - Map> perms = new HashMap<>(); + Map> perms = new HashMap<>(); for (Node n : enduring ? holder.getNodes() : holder.getTransientNodes()) { MutableContextSet contexts = MutableContextSet.fromSet(n.getContexts()); @@ -99,16 +99,12 @@ public class LuckPermsSubjectData implements LPSubjectData { contexts.add(new Context(Context.WORLD_KEY, n.getWorld().get())); } - if (!perms.containsKey(contexts)) { - perms.put(contexts.makeImmutable(), new HashMap<>()); - } - - perms.get(contexts).put(n.getPermission(), n.getValue()); + perms.computeIfAbsent(contexts.makeImmutable(), cs -> new HashMap<>()).put(n.getPermission(), n.getValue()); } ImmutableMap.Builder> map = ImmutableMap.builder(); - for (Map.Entry> e : perms.entrySet()) { - map.put(e.getKey().makeImmutable(), ImmutableMap.copyOf(e.getValue())); + for (Map.Entry> e : perms.entrySet()) { + map.put(e.getKey(), ImmutableMap.copyOf(e.getValue())); } return map.build(); } @@ -233,11 +229,7 @@ public class LuckPermsSubjectData implements LPSubjectData { contexts.add(new Context(Context.WORLD_KEY, n.getWorld().get())); } - if (!parents.containsKey(contexts)) { - parents.put(contexts.makeImmutable(), new HashSet<>()); - } - - parents.get(contexts).add(service.getGroupSubjects().get(n.getGroupName()).toReference()); + parents.computeIfAbsent(contexts.makeImmutable(), cs -> new HashSet<>()).add(service.getGroupSubjects().get(n.getGroupName()).toReference()); } ImmutableMap.Builder> map = ImmutableMap.builder(); @@ -374,9 +366,9 @@ public class LuckPermsSubjectData implements LPSubjectData { @Override public Map> getOptions() { try (Timing ignored = service.getPlugin().getTimings().time(LPTiming.LP_SUBJECT_GET_OPTIONS)) { - Map> options = new HashMap<>(); - Map minPrefixPriority = new HashMap<>(); - Map minSuffixPriority = new HashMap<>(); + Map> options = new HashMap<>(); + Map minPrefixPriority = new HashMap<>(); + Map minSuffixPriority = new HashMap<>(); for (Node n : enduring ? holder.getNodes() : holder.getTransientNodes()) { if (!n.getValue()) { @@ -397,39 +389,41 @@ public class LuckPermsSubjectData implements LPSubjectData { contexts.add(new Context(Context.WORLD_KEY, n.getWorld().get())); } - if (!options.containsKey(contexts)) { - options.put(contexts, new HashMap<>()); - minPrefixPriority.put(contexts, Integer.MIN_VALUE); - minSuffixPriority.put(contexts, Integer.MIN_VALUE); + ImmutableContextSet immutableContexts = contexts.makeImmutable(); + + if (!options.containsKey(immutableContexts)) { + options.put(immutableContexts, new HashMap<>()); + minPrefixPriority.put(immutableContexts, Integer.MIN_VALUE); + minSuffixPriority.put(immutableContexts, Integer.MIN_VALUE); } if (n.isPrefix()) { Map.Entry value = n.getPrefix(); - if (value.getKey() > minPrefixPriority.get(contexts)) { - options.get(contexts).put("prefix", value.getValue()); - minPrefixPriority.put(contexts, value.getKey()); + if (value.getKey() > minPrefixPriority.get(immutableContexts)) { + options.get(immutableContexts).put("prefix", value.getValue()); + minPrefixPriority.put(immutableContexts, value.getKey()); } continue; } if (n.isSuffix()) { Map.Entry value = n.getSuffix(); - if (value.getKey() > minSuffixPriority.get(contexts)) { - options.get(contexts).put("suffix", value.getValue()); - minSuffixPriority.put(contexts, value.getKey()); + if (value.getKey() > minSuffixPriority.get(immutableContexts)) { + options.get(immutableContexts).put("suffix", value.getValue()); + minSuffixPriority.put(immutableContexts, value.getKey()); } continue; } if (n.isMeta()) { Map.Entry meta = n.getMeta(); - options.get(contexts).put(meta.getKey(), meta.getValue()); + options.get(immutableContexts).put(meta.getKey(), meta.getValue()); } } ImmutableMap.Builder> map = ImmutableMap.builder(); - for (Map.Entry> e : options.entrySet()) { - map.put(e.getKey().makeImmutable(), ImmutableMap.copyOf(e.getValue())); + for (Map.Entry> e : options.entrySet()) { + map.put(e.getKey(), ImmutableMap.copyOf(e.getValue())); } return map.build(); } @@ -455,8 +449,7 @@ public class LuckPermsSubjectData implements LPSubjectData { } else { holder.setTransientPermission(NodeFactory.makeMetaNode(key, value).withExtraContext(context).build()); } - } catch (ObjectAlreadyHasException ignored) { - } + } catch (ObjectAlreadyHasException ignored) {} objectSave(holder); return true; } @@ -512,8 +505,7 @@ public class LuckPermsSubjectData implements LPSubjectData { } else { holder.unsetTransientPermission(n); } - } catch (ObjectLacksException ignored) { - } + } catch (ObjectLacksException ignored) {} }); objectSave(holder);