function) {
+ Pattern pattern = Pattern.compile(match);
+ return modify(s -> {
+ Matcher matcher = pattern.matcher(s);
+ StringBuilder sb = new StringBuilder();
+ while (matcher.find()) {
+ matcher.appendReplacement(sb, function.apply(matcher.group(1)));
+ }
+ matcher.appendTail(sb);
+ return sb.toString();
+ });
+ }
+
+ /**
+ * Will match all occurrences of the given regex and replace them with the
+ * result of the function by using a wildcard.
+ *
+ * Example:
+ * matchReplace("%flag@%", "@", s -> s); //Will set all flag placeholders as
+ * //whatever the flag is.
+ *
+ *
+ * @param match The regex to match
+ * @param wildcard The wildcard to use
+ * @param function The function to use
+ * @return The modified translatable
+ */
+ public BlobTranslatableModder matchReplace(String match, String wildcard, Function function) {
+ String regex = match.replace(wildcard, "(.*?)");
+ return regexReplace(regex, function);
+ }
+
+ /**
+ * Will match all occurrences of the given regex and replace them with the
+ * result of the function.
+ * Will use '@' as the wildcard.
+ *
+ * @param match The regex to match
+ * @param function The function to use
+ * @return The modified translatable
+ */
+ public BlobTranslatableModder matchReplace(String match, Function function) {
+ return matchReplace(match, "@", function);
+ }
+
/**
* @return The translatable in lower case
*/
diff --git a/src/main/java/us/mytheria/bloblib/exception/KeySharingException.java b/src/main/java/us/mytheria/bloblib/exception/KeySharingException.java
new file mode 100644
index 0000000..862e12b
--- /dev/null
+++ b/src/main/java/us/mytheria/bloblib/exception/KeySharingException.java
@@ -0,0 +1,16 @@
+package us.mytheria.bloblib.exception;
+
+import org.jetbrains.annotations.NotNull;
+
+import java.util.Objects;
+
+public class KeySharingException extends RuntimeException {
+ public static KeySharingException DEFAULT(@NotNull String key) {
+ Objects.requireNonNull(key);
+ return new KeySharingException("The key " + key + " is already in use");
+ }
+
+ public KeySharingException(String message) {
+ super(message);
+ }
+}
diff --git a/src/main/java/us/mytheria/bloblib/managers/BlobPlugin.java b/src/main/java/us/mytheria/bloblib/managers/BlobPlugin.java
index 901f7e5..ce4d904 100644
--- a/src/main/java/us/mytheria/bloblib/managers/BlobPlugin.java
+++ b/src/main/java/us/mytheria/bloblib/managers/BlobPlugin.java
@@ -41,7 +41,7 @@ protected void blobLibReload() {
PluginManager.unloadAssets(this);
//Loads assets
PluginManager.loadAssets(this);
- getManagerDirector().reload();
+ getManagerDirector().reloadAll();
}
/**
diff --git a/src/main/java/us/mytheria/bloblib/managers/IManagerDirector.java b/src/main/java/us/mytheria/bloblib/managers/IManagerDirector.java
index 0a20fc9..20f8977 100644
--- a/src/main/java/us/mytheria/bloblib/managers/IManagerDirector.java
+++ b/src/main/java/us/mytheria/bloblib/managers/IManagerDirector.java
@@ -16,7 +16,7 @@ public interface IManagerDirector {
/**
* Will do reload logic (asynchronously).
*/
- void reload();
+ void reloadAll();
/**
* Will get the file manager that's required by BlobLib.
diff --git a/src/main/java/us/mytheria/bloblib/managers/ManagerDirector.java b/src/main/java/us/mytheria/bloblib/managers/ManagerDirector.java
index 9bd8de4..fd95015 100644
--- a/src/main/java/us/mytheria/bloblib/managers/ManagerDirector.java
+++ b/src/main/java/us/mytheria/bloblib/managers/ManagerDirector.java
@@ -15,6 +15,7 @@
import us.mytheria.bloblib.entities.currency.WalletOwner;
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.ResourceUtil;
import java.io.File;
@@ -41,9 +42,8 @@ public abstract class ManagerDirector implements IManagerDirector {
*/
public ManagerDirector(BlobPlugin plugin) {
this.namespacedKeys = new HashMap<>();
- namespacedKeys.put("tangibleCurrencyKey", new NamespacedKey(plugin, "tangibleCurrencyKey"));
- namespacedKeys.put("tangibleCurrencyDenomination", new NamespacedKey(plugin, "tangibleCurrencyDenomination"));
this.plugin = plugin;
+ reloadNamespacedKeys();
this.pluginOperator = () -> plugin;
this.blobFileManager = new BlobFileManager(this,
"plugins/" + plugin.getName(),
@@ -75,9 +75,8 @@ public ManagerDirector(BlobPlugin plugin) {
@Deprecated
public ManagerDirector(BlobPlugin plugin, String fileManagerPathname) {
this.namespacedKeys = new HashMap<>();
- namespacedKeys.put("tangibleCurrencyKey", new NamespacedKey(plugin, "tangibleCurrencyKey"));
- namespacedKeys.put("tangibleCurrencyDenomination", new NamespacedKey(plugin, "tangibleCurrencyDenomination"));
this.plugin = plugin;
+ reloadNamespacedKeys();
this.pluginOperator = () -> plugin;
this.blobFileManager = new BlobFileManager(this,
fileManagerPathname, plugin);
@@ -98,9 +97,8 @@ public ManagerDirector(BlobPlugin plugin, String fileManagerPathname) {
*/
public ManagerDirector(BlobPlugin plugin, BlobFileManager fileManager) {
this.namespacedKeys = new HashMap<>();
- namespacedKeys.put("tangibleCurrencyKey", new NamespacedKey(plugin, "tangibleCurrencyKey"));
- namespacedKeys.put("tangibleCurrencyDenomination", new NamespacedKey(plugin, "tangibleCurrencyDenomination"));
this.plugin = plugin;
+ reloadNamespacedKeys();
this.pluginOperator = () -> plugin;
this.blobFileManager = Objects.requireNonNull(fileManager, "BlobFileManager cannot be null!");
this.proxiedFileManager = BlobProxifier.PROXY(blobFileManager);
@@ -387,6 +385,14 @@ public void addDirector(String objectName,
public void reload() {
}
+ /**
+ * Will reload ManagerDirector's defaults and do reload logic.
+ */
+ public void reloadAll() {
+ reloadNamespacedKeys();
+ reload();
+ }
+
/**
* Will retrieve a manager by providing a String 'key'.
*
@@ -751,6 +757,29 @@ public ManagerDirector registerTranslatableSnippet(String... fileNames) {
return registerTranslatableSnippet(false, fileNames);
}
+ /**
+ * Creates a new NamespacedKey.
+ *
+ * @param key The key
+ * @return The NamespacedKey
+ */
+ public NamespacedKey createNamespacedKey(String key) {
+ if (namespacedKeys.containsKey(key))
+ throw KeySharingException.DEFAULT(key);
+ NamespacedKey namespacedKey = new NamespacedKey(plugin, key);
+ namespacedKeys.put(key, namespacedKey);
+ return namespacedKey;
+ }
+
+ /**
+ * Reloads all namespaced keys.
+ */
+ public void reloadNamespacedKeys() {
+ namespacedKeys.clear();
+ createNamespacedKey("tangibleCurrencyKey");
+ createNamespacedKey("tangibleCurrencyDenomination");
+ }
+
protected Set> getManagerEntry() {
return managers.entrySet();
}