From 3c20ceb740162d4a880e21048341dd91bd93caf0 Mon Sep 17 00:00:00 2001 From: DevDrizzy Date: Sat, 18 Feb 2023 19:48:23 +0500 Subject: [PATCH] Fix null pointers Signed-off-by: DevDrizzy --- StorageAPI.iml | 1 - pom.xml | 9 ++++++++- .../api/storage/impl/ParentYamlStorage.java | 15 ++++++++++++++- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/StorageAPI.iml b/StorageAPI.iml index 574de5b..88c4fa0 100644 --- a/StorageAPI.iml +++ b/StorageAPI.iml @@ -12,7 +12,6 @@ - \ No newline at end of file diff --git a/pom.xml b/pom.xml index f9be95a..37853c4 100644 --- a/pom.xml +++ b/pom.xml @@ -15,7 +15,7 @@ xyz.refinedev.api StorageAPI - 1.5 + 1.6 8 @@ -122,6 +122,13 @@ me.carleslc.Simple-YAML Simple-Yaml 1.8.3 + compile + + + me.carleslc.Simple-YAML + Simple-Configuration + + diff --git a/src/main/java/xyz/refinedev/api/storage/impl/ParentYamlStorage.java b/src/main/java/xyz/refinedev/api/storage/impl/ParentYamlStorage.java index 755e436..deaafc7 100644 --- a/src/main/java/xyz/refinedev/api/storage/impl/ParentYamlStorage.java +++ b/src/main/java/xyz/refinedev/api/storage/impl/ParentYamlStorage.java @@ -1,6 +1,9 @@ package xyz.refinedev.api.storage.impl; +import com.google.common.base.Preconditions; + import org.bukkit.plugin.java.JavaPlugin; + import xyz.refinedev.api.storage.YamlStorage; import xyz.refinedev.api.storage.annotations.ConfigValue; @@ -13,7 +16,7 @@ public abstract class ParentYamlStorage extends YamlStorage { * Set based cache for child storages of this Parent Storage * We keep it concurrent to allow asynchronous file I/O */ - private final Set childStorages = Collections.synchronizedSet(new HashSet<>()); + private Set childStorages; /** * Initiation method for a config file @@ -31,6 +34,13 @@ public ParentYamlStorage(JavaPlugin plugin, String name) { * @param storage {@link ChildYamlStorage storage} */ public void addChildStorage(ChildYamlStorage storage) { + Preconditions.checkNotNull(storage, "[StorageAPI] Child Storage can not be null!"); + + // Bypass for constructor being called before default variable initialization + if (childStorages == null) { + this.childStorages = Collections.synchronizedSet(new HashSet<>()); + } + this.childStorages.add(storage); } @@ -46,6 +56,9 @@ public List getConfigFields() { // Add child fields this.childStorages.stream().map(ChildYamlStorage::getConfigFields).forEach(annotatedFields::addAll); + Preconditions.checkArgument(annotatedFields.stream().allMatch(field -> field.isAnnotationPresent(ConfigValue.class)), + "[Storage-API] One of your field is missing annotation!"); + // Sort according to priority annotatedFields.sort(Comparator.comparingInt(field -> field.getAnnotation(ConfigValue.class).priority()));