From 11a3ecbba0ae308a9c7b35ab1f965c83539939b6 Mon Sep 17 00:00:00 2001 From: Luck Date: Thu, 20 Sep 2018 10:51:00 +0100 Subject: [PATCH] Fix NPE when loading subject data on Sponge (#1222) --- .../persisted/SubjectDataContainer.java | 19 ++++++++++++------- .../service/persisted/SubjectStorage.java | 2 ++ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/service/persisted/SubjectDataContainer.java b/sponge/src/main/java/me/lucko/luckperms/sponge/service/persisted/SubjectDataContainer.java index 339d770a8..dfd67774a 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/service/persisted/SubjectDataContainer.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/service/persisted/SubjectDataContainer.java @@ -93,15 +93,20 @@ public class SubjectDataContainer { } this.parents = parentsBuilder.build(); } + + private static JsonArray getArray(JsonObject root, String key) { + JsonElement element = root.get(key); + if (element == null) { + return new JsonArray(); + } + Preconditions.checkArgument(element instanceof JsonArray); + return ((JsonArray) element); + } private SubjectDataContainer(LPPermissionService service, JsonObject root) { - Preconditions.checkArgument(root.get("permissions").isJsonArray()); - Preconditions.checkArgument(root.get("options").isJsonArray()); - Preconditions.checkArgument(root.get("parents").isJsonArray()); - - JsonArray permissions = root.get("permissions").getAsJsonArray(); - JsonArray options = root.get("options").getAsJsonArray(); - JsonArray parents = root.get("parents").getAsJsonArray(); + JsonArray permissions = getArray(root, "permissions"); + JsonArray options = getArray(root, "options"); + JsonArray parents = getArray(root, "parents"); ImmutableMap.Builder> permissionsBuilder = ImmutableMap.builder(); for (JsonElement e : permissions) { diff --git a/sponge/src/main/java/me/lucko/luckperms/sponge/service/persisted/SubjectStorage.java b/sponge/src/main/java/me/lucko/luckperms/sponge/service/persisted/SubjectStorage.java index ac0fdbb26..8c939b1f9 100644 --- a/sponge/src/main/java/me/lucko/luckperms/sponge/service/persisted/SubjectStorage.java +++ b/sponge/src/main/java/me/lucko/luckperms/sponge/service/persisted/SubjectStorage.java @@ -204,6 +204,8 @@ public class SubjectStorage { JsonObject data = this.gson.fromJson(reader, JsonObject.class); SubjectDataContainer model = SubjectDataContainer.deserialize(this.service, data); return new LoadedSubject(subjectName, model); + } catch (Exception e) { + throw new IOException("Exception occurred whilst loading from " + file.toString(), e); } }