Skip to content

Commit

Permalink
Adds HandyDirectory
Browse files Browse the repository at this point in the history
Removes deprecated ManagerDirector constructor
Adds ManagerDirector#registerTranslatableItem
Adds ManagerDirector#registerTagSet
Adds ManagerDirector#loadBlobLibExpansion
  • Loading branch information
anjoismysign committed Apr 18, 2024
1 parent b2411cf commit a7f2829
Show file tree
Hide file tree
Showing 9 changed files with 392 additions and 67 deletions.
2 changes: 1 addition & 1 deletion local-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>us.mytheria</groupId>
<artifactId>BlobLib</artifactId>
<version>1.698.12</version>
<version>1.698.13</version>
<relativePath>pom.xml</relativePath>
</parent>
<artifactId>bloblib</artifactId>
Expand Down
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>us.mytheria</groupId>
<artifactId>BlobLib</artifactId>
<version>1.698.12</version>
<version>1.698.13</version>
<packaging>pom</packaging>

<properties>
Expand Down Expand Up @@ -133,6 +133,11 @@
<artifactId>skeramidcommands</artifactId>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId>net.lingala.zip4j</groupId>
<artifactId>zip4j</artifactId>
<version>2.11.5</version>
</dependency>
</dependencies>
<distributionManagement>
<repository>
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/us/mytheria/bloblib/entities/DataAssetType.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ public enum DataAssetType {
TRANSLATABLE_SNIPPET,
TRANSLATABLE_ITEM,
TAG_SET;

public String getObjectName() {
return name().replace("_", "");
}
}
38 changes: 22 additions & 16 deletions src/main/java/us/mytheria/bloblib/entities/ObjectDirector.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -62,30 +62,36 @@ public void loadFiles(File path, CompletableFuture<Void> mainFuture) {
if (!path.exists())
path.mkdir();
Bukkit.getScheduler().runTaskAsynchronously(getPlugin(), () -> {
String[] extensions = {"yml"};
Collection<File> files = FileUtils.listFiles(path, extensions, true);
HandyDirectory handyDirectory = HandyDirectory.of(path);
Collection<File> files = handyDirectory.listRecursively("yml");
List<CompletableFuture<Void>> futures = new ArrayList<>();
files.forEach(file -> {
CompletableFuture<Void> 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<Throwable> 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();
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/us/mytheria/bloblib/entities/ObjectManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -68,6 +69,14 @@ public void addObjectFile(String key, File file) {
*/
public abstract void loadFiles(File path, CompletableFuture<Void> 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<Throwable> ifFail);

/**
* Adds an object to the manager and
* tracks the file it was loaded from.
Expand Down
43 changes: 35 additions & 8 deletions src/main/java/us/mytheria/bloblib/managers/DataAssetManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 -> {
Expand All @@ -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();
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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 -> {
Expand All @@ -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();
}
});
}

Expand Down
Loading

0 comments on commit a7f2829

Please sign in to comment.