From a7f2829d911608b3e27a3c554aa9da2518daf7c6 Mon Sep 17 00:00:00 2001 From: lbenav8095 Date: Thu, 18 Apr 2024 14:04:31 -0600 Subject: [PATCH] Adds HandyDirectory Removes deprecated ManagerDirector constructor Adds ManagerDirector#registerTranslatableItem Adds ManagerDirector#registerTagSet Adds ManagerDirector#loadBlobLibExpansion --- local-pom.xml | 2 +- pom.xml | 7 +- .../bloblib/entities/DataAssetType.java | 4 + .../bloblib/entities/ObjectDirector.java | 38 +-- .../bloblib/entities/ObjectManager.java | 9 + .../bloblib/managers/DataAssetManager.java | 43 ++- .../managers/LocalizableDataAssetManager.java | 44 ++- .../bloblib/managers/ManagerDirector.java | 253 +++++++++++++++--- .../bloblib/utilities/HandyDirectory.java | 59 ++++ 9 files changed, 392 insertions(+), 67 deletions(-) create mode 100644 src/main/java/us/mytheria/bloblib/utilities/HandyDirectory.java diff --git a/local-pom.xml b/local-pom.xml index bce42d0..3af471b 100644 --- a/local-pom.xml +++ b/local-pom.xml @@ -5,7 +5,7 @@ us.mytheria BlobLib - 1.698.12 + 1.698.13 pom.xml bloblib diff --git a/pom.xml b/pom.xml index 64168a7..96646b7 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 us.mytheria BlobLib - 1.698.12 + 1.698.13 pom @@ -133,6 +133,11 @@ skeramidcommands 1.0.5 + + net.lingala.zip4j + zip4j + 2.11.5 + diff --git a/src/main/java/us/mytheria/bloblib/entities/DataAssetType.java b/src/main/java/us/mytheria/bloblib/entities/DataAssetType.java index 0036bc0..0debc2a 100644 --- a/src/main/java/us/mytheria/bloblib/entities/DataAssetType.java +++ b/src/main/java/us/mytheria/bloblib/entities/DataAssetType.java @@ -10,4 +10,8 @@ public enum DataAssetType { TRANSLATABLE_SNIPPET, TRANSLATABLE_ITEM, TAG_SET; + + public String getObjectName() { + return name().replace("_", ""); + } } diff --git a/src/main/java/us/mytheria/bloblib/entities/ObjectDirector.java b/src/main/java/us/mytheria/bloblib/entities/ObjectDirector.java index e3d0851..22e0810 100644 --- a/src/main/java/us/mytheria/bloblib/entities/ObjectDirector.java +++ b/src/main/java/us/mytheria/bloblib/entities/ObjectDirector.java @@ -1,7 +1,6 @@ package us.mytheria.bloblib.entities; import me.anjoismysign.anjo.entities.Result; -import org.apache.commons.io.FileUtils; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.entity.Player; @@ -14,6 +13,7 @@ import us.mytheria.bloblib.itemstack.ItemStackBuilder; import us.mytheria.bloblib.managers.Manager; import us.mytheria.bloblib.managers.ManagerDirector; +import us.mytheria.bloblib.utilities.HandyDirectory; import us.mytheria.bloblib.utilities.ItemStackUtil; import java.io.File; @@ -62,30 +62,36 @@ public void loadFiles(File path, CompletableFuture mainFuture) { if (!path.exists()) path.mkdir(); Bukkit.getScheduler().runTaskAsynchronously(getPlugin(), () -> { - String[] extensions = {"yml"}; - Collection files = FileUtils.listFiles(path, extensions, true); + HandyDirectory handyDirectory = HandyDirectory.of(path); + Collection files = handyDirectory.listRecursively("yml"); List> futures = new ArrayList<>(); files.forEach(file -> { CompletableFuture fileFuture = CompletableFuture.runAsync(() -> { - T blobObject; - try { - blobObject = readFunction.apply(file); - if (blobObject != null) { - if (blobObject.edit() != null) - objectIsEditable = true; - this.addObject(blobObject.getKey(), blobObject, file); - } - } catch (Throwable throwable) { - Bukkit.getLogger().log(Level.SEVERE, throwable.getMessage() + " \n " + - "At: " + file.getPath(), throwable); - mainFuture.completeExceptionally(throwable); - } + loadFile(file, mainFuture::completeExceptionally); }); futures.add(fileFuture); }); CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).thenAccept(v -> mainFuture.complete(null)); }); } + + @Override + public void loadFile(@NotNull File file, + Consumer ifFail) { + T blobObject; + try { + blobObject = readFunction.apply(file); + if (blobObject != null) { + if (blobObject.edit() != null) + objectIsEditable = true; + this.addObject(blobObject.getKey(), blobObject, file); + } + } catch (Throwable throwable) { + Bukkit.getLogger().log(Level.SEVERE, throwable.getMessage() + " \n " + + "At: " + file.getPath(), throwable); + ifFail.accept(throwable); + } + } }; Bukkit.getPluginManager().registerEvents(this, managerDirector.getPlugin()); objectName = objectDirectorData.objectName(); diff --git a/src/main/java/us/mytheria/bloblib/entities/ObjectManager.java b/src/main/java/us/mytheria/bloblib/entities/ObjectManager.java index 60c7b06..35001f8 100644 --- a/src/main/java/us/mytheria/bloblib/entities/ObjectManager.java +++ b/src/main/java/us/mytheria/bloblib/entities/ObjectManager.java @@ -3,6 +3,7 @@ import me.anjoismysign.anjo.entities.Result; import me.anjoismysign.skeramidcommands.command.CommandTarget; import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import us.mytheria.bloblib.entities.logger.BlobPluginLogger; import us.mytheria.bloblib.managers.Manager; @@ -68,6 +69,14 @@ public void addObjectFile(String key, File file) { */ public abstract void loadFiles(File path, CompletableFuture mainFuture); + /** + * Loads a file into the ObjectManager. + * If fails, prints the error to the console. + * + * @param file The file to load + */ + public abstract void loadFile(@NotNull File file, Consumer ifFail); + /** * Adds an object to the manager and * tracks the file it was loaded from. diff --git a/src/main/java/us/mytheria/bloblib/managers/DataAssetManager.java b/src/main/java/us/mytheria/bloblib/managers/DataAssetManager.java index 6af53fc..47e8039 100644 --- a/src/main/java/us/mytheria/bloblib/managers/DataAssetManager.java +++ b/src/main/java/us/mytheria/bloblib/managers/DataAssetManager.java @@ -119,8 +119,13 @@ private void loadYamlConfiguration(File file) { String fileName = FilenameUtils.removeExtension(file.getName()); YamlConfiguration yamlConfiguration = YamlConfiguration.loadConfiguration(file); if (filter.test(yamlConfiguration)) { - T asset = readFunction.apply(yamlConfiguration, fileName); - addOrCreate(asset, fileName); + try { + T asset = readFunction.apply(yamlConfiguration, fileName); + addOrCreate(asset, fileName); + } catch (Throwable throwable) { + BlobLib.getInstance().getLogger().severe("At: " + file.getPath()); + throwable.printStackTrace(); + } return; } yamlConfiguration.getKeys(true).forEach(reference -> { @@ -129,22 +134,44 @@ private void loadYamlConfiguration(File file) { ConfigurationSection section = yamlConfiguration.getConfigurationSection(reference); if (!filter.test(section)) return; - T asset = readFunction.apply(section, reference); - addOrCreate(asset, reference); + try { + T asset = readFunction.apply(section, reference); + addOrCreate(asset, reference); + } catch (Throwable throwable) { + BlobLib.getInstance().getLogger().severe("At: " + file.getPath()); + throwable.printStackTrace(); + } }); } private void loadYamlConfiguration(File file, BlobPlugin plugin) { + String fileName = FilenameUtils.removeExtension(file.getName()); YamlConfiguration yamlConfiguration = YamlConfiguration.loadConfiguration(file); + if (filter.test(yamlConfiguration)) { + try { + T asset = readFunction.apply(yamlConfiguration, fileName); + addOrCreate(asset, fileName); + pluginAssets.get(plugin.getName()).add(fileName); + } catch (Throwable throwable) { + BlobLib.getInstance().getLogger().severe("At: " + file.getPath()); + throwable.printStackTrace(); + } + return; + } yamlConfiguration.getKeys(true).forEach(reference -> { if (!yamlConfiguration.isConfigurationSection(reference)) return; ConfigurationSection section = yamlConfiguration.getConfigurationSection(reference); - if (filter.test(section)) - return; - if (!addOrCreate(readFunction.apply(section, reference), reference)) + if (!filter.test(section)) return; - pluginAssets.get(plugin.getName()).add(reference); + try { + T asset = readFunction.apply(section, reference); + addOrCreate(asset, reference); + pluginAssets.get(plugin.getName()).add(reference); + } catch (Throwable throwable) { + BlobLib.getInstance().getLogger().severe("At: " + file.getPath()); + throwable.printStackTrace(); + } }); } diff --git a/src/main/java/us/mytheria/bloblib/managers/LocalizableDataAssetManager.java b/src/main/java/us/mytheria/bloblib/managers/LocalizableDataAssetManager.java index 6c49c27..886b721 100644 --- a/src/main/java/us/mytheria/bloblib/managers/LocalizableDataAssetManager.java +++ b/src/main/java/us/mytheria/bloblib/managers/LocalizableDataAssetManager.java @@ -110,6 +110,7 @@ private void loadFiles(File path) { try { loadYamlConfiguration(file); } catch (Throwable throwable) { + BlobLib.getInstance().getLogger().severe("At: " + file.getPath()); throwable.printStackTrace(); continue; } @@ -124,8 +125,13 @@ private void loadYamlConfiguration(File file) { YamlConfiguration yamlConfiguration = YamlConfiguration.loadConfiguration(file); String locale = yamlConfiguration.getString("Locale", "en_us"); if (filter.test(yamlConfiguration)) { - T asset = readFunction.apply(yamlConfiguration, locale, fileName); - addOrCreateLocale(asset, fileName); + try { + T asset = readFunction.apply(yamlConfiguration, locale, fileName); + addOrCreateLocale(asset, fileName); + } catch (Throwable throwable) { + BlobLib.getInstance().getLogger().severe("At: " + file.getPath()); + throwable.printStackTrace(); + } return; } yamlConfiguration.getKeys(true).forEach(reference -> { @@ -134,23 +140,45 @@ private void loadYamlConfiguration(File file) { ConfigurationSection section = yamlConfiguration.getConfigurationSection(reference); if (!filter.test(section)) return; - T asset = readFunction.apply(section, locale, reference); - addOrCreateLocale(asset, reference); + try { + T asset = readFunction.apply(section, locale, reference); + addOrCreateLocale(asset, reference); + } catch (Throwable throwable) { + BlobLib.getInstance().getLogger().severe("At: " + file.getPath()); + throwable.printStackTrace(); + } }); } private void loadYamlConfiguration(File file, BlobPlugin plugin) { + String fileName = FilenameUtils.removeExtension(file.getName()); YamlConfiguration yamlConfiguration = YamlConfiguration.loadConfiguration(file); String locale = yamlConfiguration.getString("Locale", "en_us"); + if (filter.test(yamlConfiguration)) { + try { + T asset = readFunction.apply(yamlConfiguration, locale, fileName); + addOrCreateLocale(asset, fileName); + assets.get(plugin.getName()).add(fileName); + } catch (Throwable throwable) { + BlobLib.getInstance().getLogger().severe("At: " + file.getPath()); + throwable.printStackTrace(); + } + return; + } yamlConfiguration.getKeys(true).forEach(reference -> { if (!yamlConfiguration.isConfigurationSection(reference)) return; ConfigurationSection section = yamlConfiguration.getConfigurationSection(reference); - if (filter.test(section)) - return; - if (!addOrCreateLocale(readFunction.apply(section, locale, reference), reference)) + if (!filter.test(section)) return; - assets.get(plugin.getName()).add(reference); + try { + T asset = readFunction.apply(section, locale, reference); + addOrCreateLocale(asset, reference); + assets.get(plugin.getName()).add(reference); + } catch (Throwable throwable) { + BlobLib.getInstance().getLogger().severe("At: " + file.getPath()); + throwable.printStackTrace(); + } }); } diff --git a/src/main/java/us/mytheria/bloblib/managers/ManagerDirector.java b/src/main/java/us/mytheria/bloblib/managers/ManagerDirector.java index fd95015..8e0aa16 100644 --- a/src/main/java/us/mytheria/bloblib/managers/ManagerDirector.java +++ b/src/main/java/us/mytheria/bloblib/managers/ManagerDirector.java @@ -1,6 +1,8 @@ package us.mytheria.bloblib.managers; import me.anjoismysign.anjo.logger.Logger; +import net.lingala.zip4j.ZipFile; +import org.apache.commons.io.FileUtils; import org.apache.commons.io.FilenameUtils; import org.bukkit.Bukkit; import org.bukkit.NamespacedKey; @@ -16,9 +18,11 @@ import us.mytheria.bloblib.entities.currency.WalletOwnerManager; import us.mytheria.bloblib.entities.proxy.BlobProxifier; import us.mytheria.bloblib.exception.KeySharingException; +import us.mytheria.bloblib.utilities.HandyDirectory; import us.mytheria.bloblib.utilities.ResourceUtil; import java.io.File; +import java.io.IOException; import java.util.*; import java.util.function.Consumer; import java.util.function.Function; @@ -57,43 +61,11 @@ public ManagerDirector(BlobPlugin plugin) { plugin.registerToBlobLib(this); } - /** - * Constructs a new ManagerDirector. - * Here an example if BlobPlugin inside plugin.yml is named 'MyHome': - *
-     *     ManagerDirector director = new ManagerDirector(plugin, "plugins/MyHome");
-     *     
- * - * @param plugin The plugin - * @param fileManagerPathname The path to the file manager directory. - * Needs to include 'plugins/' since it starts - * from the server's root directory. - * @deprecated Use {@link #ManagerDirector(BlobPlugin)} instead - * since it completely automates the process of creating a file manager. - * Might be removed in the future unless proven useful ??? - */ - @Deprecated - public ManagerDirector(BlobPlugin plugin, String fileManagerPathname) { - this.namespacedKeys = new HashMap<>(); - this.plugin = plugin; - reloadNamespacedKeys(); - this.pluginOperator = () -> plugin; - this.blobFileManager = new BlobFileManager(this, - fileManagerPathname, plugin); - this.proxiedFileManager = BlobProxifier.PROXY(blobFileManager); - chatListenerManager = BlobLib.getInstance().getChatManager(); - selectorListenerManager = BlobLib.getInstance().getSelectorManager(); - positionListenerManager = BlobLib.getInstance().getPositionManager(); - dropListenerManager = BlobLib.getInstance().getDropListenerManager(); - managers = new HashMap<>(); - plugin.registerToBlobLib(this); - } - /** * Constructs a new manager director by providing a specific BlobFileManager. * * @param plugin The plugin - * @param fileManager The file manager + * @param fileManager The fileManager */ public ManagerDirector(BlobPlugin plugin, BlobFileManager fileManager) { this.namespacedKeys = new HashMap<>(); @@ -757,6 +729,58 @@ public ManagerDirector registerTranslatableSnippet(String... fileNames) { return registerTranslatableSnippet(false, fileNames); } + /** + * Will detach all TranslatableItems provided. + * + * @param debug Whether to print debug messages + * @param fileNames The names of the files to detach. Needs to include the extension. + * @return The ManagerDirector instance for method chaining + */ + public ManagerDirector registerTranslatableItem(boolean debug, String... fileNames) { + String[] yaml = addYml(fileNames); + File[] freshFiles = freshFiles(debug, getRealFileManager().getDirectory(DataAssetType.TRANSLATABLE_ITEM), yaml); + BlobLib.getInstance().getTranslatableItemManager().continueLoadingAssets(plugin, true, freshFiles); + if (debug) + getPlugin().getAnjoLogger().debug(" translatable item asset " + Arrays.toString(fileNames) + " successfully registered"); + return this; + } + + /** + * Will detach all TranslatableItems provided. + * + * @param fileNames The names of the files to detach. Needs to include the extension. + * @return The ManagerDirector instance for method chaining + */ + public ManagerDirector registerTranslatableItem(String... fileNames) { + return registerTranslatableItem(false, fileNames); + } + + /** + * Will detach all TagSets provided. + * + * @param debug Whether to print debug messages + * @param fileNames The names of the files to detach. Needs to include the extension. + * @return The ManagerDirector instance for method chaining + */ + public ManagerDirector registerTagSet(boolean debug, String... fileNames) { + String[] yaml = addYml(fileNames); + File[] freshFiles = freshFiles(debug, getRealFileManager().getDirectory(DataAssetType.TAG_SET), yaml); + BlobLib.getInstance().getTagSetManager().continueLoadingAssets(plugin, true, freshFiles); + if (debug) + getPlugin().getAnjoLogger().debug(" tag set asset " + Arrays.toString(fileNames) + " successfully registered"); + return this; + } + + /** + * Will detach all TagSets provided. + * + * @param fileNames The names of the files to detach. Needs to include the extension. + * @return The ManagerDirector instance for method chaining + */ + public ManagerDirector registerTagSet(String... fileNames) { + return registerTagSet(false, fileNames); + } + /** * Creates a new NamespacedKey. * @@ -780,6 +804,169 @@ public void reloadNamespacedKeys() { createNamespacedKey("tangibleCurrencyDenomination"); } + /** + * Loads a BlobLib expansion. + * It will unzip it and load all assets. + * All assets that cannot be loaded (because it's either an Action or not a BlobLib asset), + * will stay in the output directory. + * + * @param expansion The expansion file + * @return Whether the expansion was successfully loaded + */ + public boolean loadBlobLibExpansion(@NotNull File expansion) { + Objects.requireNonNull(expansion); + File expansionDirectory = new File(plugin.getDataFolder(), "expansion"); + if (!expansion.getPath().startsWith(expansionDirectory.getPath())) + throw new IllegalArgumentException("Expansion must be in '" + expansionDirectory.getPath() + "' directory," + + " but was in '" + expansion.getPath() + "'."); + File outputFile = new File(expansionDirectory, "output"); + if (!outputFile.exists()) + outputFile.mkdirs(); + try (ZipFile zipFile = new ZipFile(expansion)) { + zipFile.extractAll(outputFile.getAbsolutePath()); + } catch (Exception e) { + e.printStackTrace(); + return false; + } + HandyDirectory handyDirectory = HandyDirectory.of(outputFile); + Map> assets = new HashMap<>(); + Map assetsDirectory = new HashMap<>(); + for (File directory : handyDirectory.listDirectories()) { + if (directory.getName().equalsIgnoreCase(DataAssetType.BLOB_SOUND.getObjectName())) { + HandyDirectory subDirectory = HandyDirectory.of(directory); + List list = subDirectory.listRecursively("yml").stream().toList(); + assets.put(DataAssetType.BLOB_SOUND, list); + assetsDirectory.put(DataAssetType.BLOB_SOUND, directory); + continue; + } + if (directory.getName().equalsIgnoreCase(DataAssetType.BLOB_MESSAGE.getObjectName())) { + HandyDirectory subDirectory = HandyDirectory.of(directory); + List list = subDirectory.listRecursively("yml").stream().toList(); + assets.put(DataAssetType.BLOB_MESSAGE, list); + assetsDirectory.put(DataAssetType.BLOB_MESSAGE, directory); + continue; + } + if (directory.getName().equalsIgnoreCase(DataAssetType.BLOB_INVENTORY.getObjectName())) { + HandyDirectory subDirectory = HandyDirectory.of(directory); + List list = subDirectory.listRecursively("yml").stream().toList(); + assets.put(DataAssetType.BLOB_INVENTORY, list); + assetsDirectory.put(DataAssetType.BLOB_INVENTORY, directory); + continue; + } + if (directory.getName().equalsIgnoreCase(DataAssetType.META_BLOB_INVENTORY.getObjectName())) { + HandyDirectory subDirectory = HandyDirectory.of(directory); + List list = subDirectory.listRecursively("yml").stream().toList(); + assets.put(DataAssetType.META_BLOB_INVENTORY, list); + assetsDirectory.put(DataAssetType.META_BLOB_INVENTORY, directory); + continue; + } + if (directory.getName().equalsIgnoreCase(DataAssetType.TRANSLATABLE_BLOCK.getObjectName())) { + HandyDirectory subDirectory = HandyDirectory.of(directory); + List list = subDirectory.listRecursively("yml").stream().toList(); + assets.put(DataAssetType.TRANSLATABLE_BLOCK, list); + assetsDirectory.put(DataAssetType.TRANSLATABLE_BLOCK, directory); + continue; + } + if (directory.getName().equalsIgnoreCase(DataAssetType.TRANSLATABLE_SNIPPET.getObjectName())) { + HandyDirectory subDirectory = HandyDirectory.of(directory); + List list = subDirectory.listRecursively("yml").stream().toList(); + assets.put(DataAssetType.TRANSLATABLE_SNIPPET, list); + assetsDirectory.put(DataAssetType.TRANSLATABLE_SNIPPET, directory); + continue; + } + if (directory.getName().equalsIgnoreCase(DataAssetType.TRANSLATABLE_ITEM.getObjectName())) { + HandyDirectory subDirectory = HandyDirectory.of(directory); + List list = subDirectory.listRecursively("yml").stream().toList(); + assets.put(DataAssetType.TRANSLATABLE_ITEM, list); + assetsDirectory.put(DataAssetType.TRANSLATABLE_ITEM, directory); + continue; + } + if (directory.getName().equalsIgnoreCase(DataAssetType.TAG_SET.getObjectName())) { + HandyDirectory subDirectory = HandyDirectory.of(directory); + List list = subDirectory.listRecursively("yml").stream().toList(); + assets.put(DataAssetType.TAG_SET, list); + assetsDirectory.put(DataAssetType.TAG_SET, directory); + } + } + if (assets.isEmpty()) + return true; + BlobLib blobLib = BlobLib.getInstance(); + List blobSound = assets.get(DataAssetType.BLOB_SOUND); + if (blobSound != null && !blobSound.isEmpty()) { + SoundManager.continueLoadingSounds(plugin, true, blobSound.toArray(new File[0])); + try { + FileUtils.deleteDirectory(assetsDirectory.get(DataAssetType.BLOB_SOUND)); + } catch (IOException e) { + e.printStackTrace(); + } + } + List blobMessage = assets.get(DataAssetType.BLOB_MESSAGE); + if (blobMessage != null && !blobMessage.isEmpty()) { + MessageManager.continueLoadingMessages(plugin, true, blobMessage.toArray(new File[0])); + try { + FileUtils.deleteDirectory(assetsDirectory.get(DataAssetType.BLOB_MESSAGE)); + } catch (IOException e) { + e.printStackTrace(); + } + } + List blobInventory = assets.get(DataAssetType.BLOB_INVENTORY); + if (blobInventory != null && !blobInventory.isEmpty()) { + InventoryManager.continueLoadingBlobInventories(plugin, blobInventory.toArray(new File[0])); + try { + FileUtils.deleteDirectory(assetsDirectory.get(DataAssetType.BLOB_INVENTORY)); + } catch (IOException e) { + e.printStackTrace(); + } + } + List metaBlobInventory = assets.get(DataAssetType.META_BLOB_INVENTORY); + if (metaBlobInventory != null && !metaBlobInventory.isEmpty()) { + InventoryManager.continueLoadingMetaInventories(plugin, metaBlobInventory.toArray(new File[0])); + try { + FileUtils.deleteDirectory(assetsDirectory.get(DataAssetType.META_BLOB_INVENTORY)); + } catch (IOException e) { + e.printStackTrace(); + } + } + List translatableBlock = assets.get(DataAssetType.TRANSLATABLE_BLOCK); + if (translatableBlock != null + && !translatableBlock.isEmpty()) { + TranslatableManager.continueLoadingBlocks(plugin, true, translatableBlock.toArray(new File[0])); + try { + FileUtils.deleteDirectory(assetsDirectory.get(DataAssetType.TRANSLATABLE_BLOCK)); + } catch (IOException e) { + e.printStackTrace(); + } + } + List translatableSnippet = assets.get(DataAssetType.TRANSLATABLE_SNIPPET); + if (translatableSnippet != null && !translatableSnippet.isEmpty()) { + TranslatableManager.continueLoadingSnippets(plugin, true, translatableSnippet.toArray(new File[0])); + try { + FileUtils.deleteDirectory(assetsDirectory.get(DataAssetType.TRANSLATABLE_SNIPPET)); + } catch (IOException e) { + e.printStackTrace(); + } + } + List translatableItem = assets.get(DataAssetType.TRANSLATABLE_ITEM); + if (translatableItem != null && !translatableItem.isEmpty()) { + blobLib.getTranslatableItemManager().continueLoadingAssets(plugin, true, translatableItem.toArray(new File[0])); + try { + FileUtils.deleteDirectory(assetsDirectory.get(DataAssetType.TRANSLATABLE_ITEM)); + } catch (IOException e) { + e.printStackTrace(); + } + } + List tagSet = assets.get(DataAssetType.TAG_SET); + if (tagSet != null && !tagSet.isEmpty()) { + blobLib.getTagSetManager().continueLoadingAssets(plugin, true, tagSet.toArray(new File[0])); + try { + FileUtils.deleteDirectory(assetsDirectory.get(DataAssetType.TAG_SET)); + } catch (IOException e) { + e.printStackTrace(); + } + } + return true; + } + protected Set> getManagerEntry() { return managers.entrySet(); } diff --git a/src/main/java/us/mytheria/bloblib/utilities/HandyDirectory.java b/src/main/java/us/mytheria/bloblib/utilities/HandyDirectory.java new file mode 100644 index 0000000..cd0d9d1 --- /dev/null +++ b/src/main/java/us/mytheria/bloblib/utilities/HandyDirectory.java @@ -0,0 +1,59 @@ +package us.mytheria.bloblib.utilities; + +import org.apache.commons.io.FileUtils; +import org.jetbrains.annotations.NotNull; + +import java.io.File; +import java.util.Collection; +import java.util.Objects; + +public record HandyDirectory(@NotNull File directory) { + + public static HandyDirectory of(@NotNull File file) { + Objects.requireNonNull(file, "File cannot be null"); + if (!file.exists()) + throw new IllegalArgumentException("File does not exist"); + if (!file.isDirectory()) + throw new IllegalArgumentException("File is not a directory"); + return new HandyDirectory(file); + } + + /** + * Lists all directories in the directory. + * + * @return An array of directories. + */ + public File[] listDirectories() { + return directory.listFiles(File::isDirectory); + } + + /** + * Lists all files in the directory. + * + * @return An array of files. + */ + public File[] listFiles() { + return directory.listFiles(File::isFile); + } + + /** + * Lists all files in the directory with the specified extension. + * + * @param extension The extension to filter by. + * @return An array of files. + */ + public File[] listFiles(@NotNull String extension) { + Objects.requireNonNull(extension, "Extension cannot be null"); + return directory.listFiles((dir, name) -> name.endsWith(extension)); + } + + /** + * Lists all files in the directory with the specified extensions. + * + * @param extensions The extensions to filter by. + * @return An array of files. + */ + public Collection listRecursively(@NotNull String... extensions) { + return FileUtils.listFiles(directory, extensions, true); + } +}