From df11b72b112f3ef87c8b873de7b8153cbabb2fdf Mon Sep 17 00:00:00 2001 From: xDec0de_ Date: Wed, 30 Oct 2024 02:48:04 +0100 Subject: [PATCH] YamlFile: Fix method to get jar resource --- .../storage/files/yaml/YamlConfig.java | 9 +++- .../mcutils/storage/files/yaml/YamlFile.java | 50 ++++++++++++------- .../storage/files/yaml/YamlMessages.java | 9 +++- 3 files changed, 45 insertions(+), 23 deletions(-) diff --git a/shared/src/main/java/net/codersky/mcutils/storage/files/yaml/YamlConfig.java b/shared/src/main/java/net/codersky/mcutils/storage/files/yaml/YamlConfig.java index f8e8fa7..fd3d48b 100644 --- a/shared/src/main/java/net/codersky/mcutils/storage/files/yaml/YamlConfig.java +++ b/shared/src/main/java/net/codersky/mcutils/storage/files/yaml/YamlConfig.java @@ -1,5 +1,6 @@ package net.codersky.mcutils.storage.files.yaml; +import net.codersky.mcutils.MCUtils; import net.codersky.mcutils.storage.Config; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -8,7 +9,11 @@ public class YamlConfig extends YamlFile implements Config { - public YamlConfig(@Nullable File parent, @NotNull String path) { - super(parent, path); + public YamlConfig(@NotNull MCUtils utils, @Nullable File parent, @NotNull String path) { + super(utils, parent, path); + } + + public YamlConfig(@NotNull MCUtils utils, @NotNull String path) { + super(utils, path); } } diff --git a/shared/src/main/java/net/codersky/mcutils/storage/files/yaml/YamlFile.java b/shared/src/main/java/net/codersky/mcutils/storage/files/yaml/YamlFile.java index 881d7e0..711e371 100644 --- a/shared/src/main/java/net/codersky/mcutils/storage/files/yaml/YamlFile.java +++ b/shared/src/main/java/net/codersky/mcutils/storage/files/yaml/YamlFile.java @@ -1,5 +1,6 @@ package net.codersky.mcutils.storage.files.yaml; +import net.codersky.mcutils.MCUtils; import net.codersky.mcutils.Reloadable; import net.codersky.mcutils.java.MCFiles; import net.codersky.mcutils.storage.DataHandler; @@ -26,17 +27,22 @@ public class YamlFile implements DataHandler, Reloadable, UpdatableFile { protected final HashMap keys = new HashMap<>(); + protected final ClassLoader loader; + protected final Yaml yaml; + protected final File file; + protected final String resourcePath; - private final Yaml yaml; - private final File file; - private final String resourcePath; - - YamlFile(@Nullable File parent, @NotNull String path) { + public YamlFile(@NotNull MCUtils utils, @Nullable File parent, @NotNull String path) { + this.loader = utils.getPlugin().getClass().getClassLoader(); this.yaml = getNewYaml(); this.file = new File(parent, path); this.resourcePath = path; } + public YamlFile(@NotNull MCUtils utils, @NotNull String path) { + this(utils, utils.getDataFolder(), path); + } + /* * DataHandler implementation */ @@ -76,7 +82,7 @@ public boolean setup() { } public boolean save() { - if (!setup()) + if (!exists() && !MCFiles.create(file)) return false; try { Writer writer = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8); @@ -106,10 +112,20 @@ public boolean reload() { * UpdatableFile implementation */ + private boolean isIgnored(String path, @Nullable List ignored) { + if (ignored == null) + return false; + for (String ignoredPath : ignored) + if (path.startsWith(ignoredPath)) + return true; + return false; + } + /** * Gets the updated {@link InputStream} of this {@link YamlFile}. - * By default, this will be {@link Class#getResourceAsStream(String)} - * with the {@code name} being {@link #asFile asFile().}{@link File#getName() getName()}. + * By default, this method uses the {@link ClassLoader} of the + * {@link MCUtils} instance that was provided on the constructor, using + * only the {@code path} without the parent {@link File} to get the resource. * The returned value of this method directly affects {@link #update(List)}, * as this is the {@link InputStream} that said method will compare against, if * {@code null}, the file won't update. @@ -120,16 +136,7 @@ public boolean reload() { */ @Nullable protected InputStream getUpdatedStream() { - return getClass().getResourceAsStream(resourcePath); - } - - private boolean isIgnored(String path, @Nullable List ignored) { - if (ignored == null) - return false; - for (String ignoredPath : ignored) - if (path.startsWith(ignoredPath)) - return true; - return false; + return loader.getResourceAsStream(resourcePath); } public boolean update(@Nullable List ignored) { @@ -140,7 +147,12 @@ public boolean update(@Nullable List ignored) { for (Map.Entry entry : updMap.entrySet()) if (!keys.containsKey(entry.getKey()) && !isIgnored(entry.getKey(), ignored)) keys.put(entry.getKey(), entry.getValue()); - return true; + try { + updated.close(); + return true; + } catch (IOException e) { + return false; + } } /* diff --git a/shared/src/main/java/net/codersky/mcutils/storage/files/yaml/YamlMessages.java b/shared/src/main/java/net/codersky/mcutils/storage/files/yaml/YamlMessages.java index ca7f421..9c6ae7e 100644 --- a/shared/src/main/java/net/codersky/mcutils/storage/files/yaml/YamlMessages.java +++ b/shared/src/main/java/net/codersky/mcutils/storage/files/yaml/YamlMessages.java @@ -1,5 +1,6 @@ package net.codersky.mcutils.storage.files.yaml; +import net.codersky.mcutils.MCUtils; import net.codersky.mcutils.storage.files.MessagesFile; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -8,8 +9,12 @@ public class YamlMessages extends YamlFile implements MessagesFile { - public YamlMessages(@Nullable File parent, @NotNull String path) { - super(parent, path); + public YamlMessages(@NotNull MCUtils utils, @Nullable File parent, @NotNull String path) { + super(utils, parent, path); + } + + public YamlMessages(@NotNull MCUtils utils, @NotNull String path) { + super(utils, path); } @Nullable